Chapter 5 Check Point Questions
Section 5.2
▼5.2.1
Analyze the following code.
Is count < 100 always true, always false, or sometimes true or sometimes false at Point A, Point B, and Point C?
int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C
▼5.2.2
How many times are the following loop bodies repeated? What is the output of each loop?
(a) int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i); (b) int i = 1; while (i < 10) if (i % 2 == 0) System.out.println(i++); (c) int i = 1; while (i < 10) if ((i++) % 2 == 0) System.out.println(i);
▼5.2.3
What is the output of the following code? Explain the reason.
int x = 80000000; while (x > 0) x++; System.out.println("x is " + x);
Section 5.3
▼5.3.1
What is wrong if guess is initialized to 0 in line 11 in Listing 5.3?
Section 5.4
▼5.4.1
Revise the code to increase the count only when the user gets a correct answer.
Section 5.5
▼5.5.1
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, max; number = input.nextInt(); max = number; while (number != 0) { number = input.nextInt(); if (number > max) max = number; } System.out.println("max is " + max); System.out.println("number " + number); } }
Section 5.6
▼5.6.1
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, max; number = input.nextInt(); max = number; do { number = input.nextInt(); if (number > max) max = number; } while (number != 0); System.out.println("max is " + max); System.out.println("number " + number); } }
▼5.6.2
What are the differences between a while loop and a do-while loop? Convert the following while loop into a do-while loop.
Scanner input = new Scanner(System.in); int sum = 0; System.out.println("Enter an integer " + "(the input ends if it is 0)"); int number = input.nextInt(); while (number != 0) { sum += number; System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); }
Section 5.7
▼5.7.1
Do the following two loops result in the same value in sum?
(a) for (int i = 0; i < 10; ++i) { sum += i; } (b) for (int i = 0; i < 10; i++) { sum += i; }
▼5.7.2
What are the three parts of a for loop control? Write a for loop that prints the numbers from 1 to 100.
▼5.7.3
Suppose the input is 2 3 4 5 0. What is the output of the following code?
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number, sum = 0, count; for (count = 0; count < 5; count++) { number = input.nextInt(); sum += number; } System.out.println("sum is " + sum); System.out.println("count is " + count); } }
▼5.7.4
What does the following statement do?
for ( ; ; ) { // Do something }
▼5.7.5
If a variable is declared in a for loop control, can it be used after the loop exits?
▼5.7.6
Convert the following for loop statement to a while loop and to a do-while loop:
long sum = 0; for (int i = 0; i <= 1000; i++) sum = sum + i;
▼5.7.7
Count the number of iterations in the following loops.
(a) int count = 0; while (count < n) { count++; } (b) for (int count = 0; count <= n; count++) { } (c) int count = 5; while (count < n) { count++; } (d) int count = 5; while (count < n) { count = count + 3; }
Section 5.8
▼5.8.1
Can you convert a for loop to a while loop? List the advantages of using for loops.
▼5.8.2
Can you always convert a while loop into a for loop? Convert the following while loop into a for loop.
int i = 1; int sum = 0; while (sum < 10000) { sum = sum + i; i++; }
▼5.8.3
Identify and fix the errors in the following code:
1 public class Test { 2 public void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 sum += i; 5 6 if (i < j); 7 System.out.println(i) 8 else 9 System.out.println(j); 10 11 while (j < 10); 12 { 13 j++; 14 } 15 16 do { 17 j++; 18 } while (j < 10) 19 } 20 }
▼5.8.4
What is wrong with the following programs?
(a) 1 public class ShowErrors { 2 public static void main(String[] args) { 3 int i = 0; 4 do { 5 System.out.println(i + 4); 6 i++; 7 } 8 while (i < 10) 8 } 9 } (b) 1 public class ShowErrors { 2 public static void main(String[] args) { 3 for (int i = 0; i < 10; i++); 4 System.out.println(i + 4); 5 } 6 }
Section 5.9
▼5.9.1
How many times is the println statement executed?
for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j)
▼5.9.2
Show the output of the following programs. (Hint: Draw a table and list the variables in the columns to trace these programs.)
(a) public class Test { public static void main(String[] args) { for (int i = 1; i < 5; i++) { int j = 0; while (j < i) { System.out.print(j + " "); j++; } } } } (b) public class Test { public static void main(String[] args) { int i = 0; while (i < 5) { for (int j = i; j > 1; j--) System.out.print(j + " "); System.out.println("****"); i++; } } } (c) public class Test { public static void main(String[] args) { int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "xxx"); num *= 2; } System.out.println(); i--; } } } (d) public class Test { public static void main(String[] args) { int i = 1; do { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "G"); num += 2; } System.out.println(); i++; } while (i <= 5); } }
Section 5.11
▼5.11.1
Will the program work if n1 and n2 are replaced by n1 / 2 and n2 / 2 in line 17 in Listing 5.9?
▼5.11.2
In Listing 5.11, why is it wrong if you change the code (char)(hexValue + '0') to hexValue + '0' in line 21?
▼5.11.3
In Listing 5.11, how many times the loop body is executed for a decimal number 245 and how many times the loop body is executed for a decimal number 3245?
▼5.11.4
What is the hex number after E? What is the hex number after F?
▼5.11.5
What will be the output if the input is 0? Revise line 27 in
Listing 5.11 so that the program displays hex number 0 if the
input decimal is 0.
Section 5.12
▼5.12.1
What is the keyword break for? What is the keyword continue for? Will the following programs terminate? If so, give the output.
(a) int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; } System.out.println("Balance is " + balance); (b) int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; } System.out.println("Balance is " + balance);
▼5.12.2
The for loop in (a) is converted into the while loop in (b). What is wrong? Correct it.
(a) int sum = 0; for (int i = 0; i < 4; i++) { if (i % 3 == 0) continue; sum += i; } (b) int i = 0, sum = 0; while (i < 4) { if (i % 3 == 0) continue; sum += i; i++; }
▼5.12.3
Rewrite the programs TestBreak and TestContinue in Listings 5.12 and 5.13 without using break and continue.
▼5.12.4
After the break statement in (a) is executed in the following loop, which statement is executed? Show the output. After the continue statement in (b) is executed in the following loop, which statement is executed? Show the output.
(a) for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { if (i * j > 2) break; System.out.println(i * j); } System.out.println(i); } (b) for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { if (i * j > 2) continue; System.out.println(i * j); } System.out.println(i); }
Section 5.13
▼5.13.1
What happens to the program if (low < high) in line 20 is
changed to (low <= high)?
Section 5.14
▼5.14.1
Simplify the code in lines 27-32 using a conditional operator.