Ap comp sci - ch 6 (Loops and hand tracing)
How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); } a) Ten times. b) The code snippet does not run because of a compile error. c) Infinite loop. d) Only one time.
Answer: a) Ten times
What is the output of the following code snippet? int i = 1; while (i < 10) { System.out.print(i + " "); i = i + 2; if (i == 5) { i = 9; } } a) 1 3 5 b) 1 3 9 c) 1 3 5 7 9 d) 1 3 5 9
Answer: b) 1 3 9
A loop inside another loop is called: a) A sentinel loop b) A nested loop c) A parallel loop d) A do/while loop
Answer: b) A nested loop
What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (int i = 0; i < 10; i++) { System.out.println("Hello"); } token1 = false; } a) No output. b) Hello will be displayed 10 times. c) Hello will be displayed 9 times. d) Hello will be displayed infinite times.
Answer: b) Hello will be displayed 10 times.
The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } a) The code snippet will not compile. b) The code snippet will display the desired result. c) The code snippet will display an incorrect result. d) The code snippet will loop forever.
Answer: b) The code snippet will display the desired result.
What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 11) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); a) The value of sum is 65 b) The value of sum is 66 c) The value of sum is 55 d) The value of sum is 56
Answer: b) The value of sum is 66
Which of the following for loops is illegal? a) for (int i = 0; ; ) { } b) for (int i = 0) { } c) for (int i = 0, k = 1; ; i++) { } d) for ( ; ; )
Answer: b) for (int i = 0) { }
Select the statement that correctly completes the loop in this code snippet. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; } a) years++; b) years--; c) balance++; d) balance--;
Answer: b) years--;
How many times does the following code fragment display "Hi"? int i = 10; while (i >= 0) { System.out.println("Hi"); i--; } a) 9 times b) 10 times c) 11 times d) 12 times
Answer: c) 11 times
How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); } a) 11 b) 12 c) 13 d) 14
Answer: c) 13
How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 2 == 0) { j--; } } while (j >= 1); a) 0 times b) 1 times c) 2 times d) 4 times
Answer: c) 2 times
How many times is the text "Let's have fun with Java." printed when this code snippet is run? int i = 0; do { System.out.println("Let's have fun with Java."); i++; if (i % 2 == 0) { i = 10; } } while (i <= 10); a) 1 b) 2 c) 3 d) 10
Answer: c) 3
How many times will the output line be printed in the following code snippet? for (int num2 = 1; num2 <= 3; num2++) { for (int num1 = 0; num1 <= 2; num1++) { System.out.println("" + num2 + " " + num1); } } a) 3 times b) 6 times c) 9 times d) 12 times
Answer: c) 9 times
Which loop does not check a condition at the beginning of the loop? I. The do loop II. The while loop III. The for loop a) I and II b) I and III c) I only d) III only
Answer: c) I only
Which of the loop(s) test the condition after the loop body executes? I. for loop II. while loop III. do loop a) I only b) II only c) III only d) II and III
Answer: c) III only
What is the output of this loop? int i = 0; boolean found; while (i < 20 && !found) { int sum = i 2 + i 3; System.out.print(sum + " "); if (sum > 50) { found = true; } i++; } a) 0 5 10 15 20 25 30 35 40 45 50 55 b) 0 c) No output, compilation error d) 0 5 10
Answer: c) No output, compilation error
1) Which of the following loops executes the statements inside the loop before checking the condition? a) for b) while c) do d) do-for
Answer: c) do
What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(" " + i); i = i + 2; } a) No output b) 0 2 4 6 8 c) 10 12 14 16 18 .... (infinite loop) d) 0 2 4 6 8 10 12 14 .... (infinite loop)
Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)
int i = 0; while (i != 9) { System.out.println("" + i); i = i + 2; } a) No output b) 0 2 4 6 8 c) 10 12 14 16 18 .... (infinite loop) d) 0 2 4 6 8 10 12 14 .... (infinite loop)
Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)
2) How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; } a) 0 b) 8 c) 9 d) 10
Answer: d) 10
What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5? double total = 0; boolean hasValidNumber = true; Scanner in = new Scanner(System.in); while (in.hasNextDouble() && hasValidNumber) { double input = in.nextDouble(); if (input < 0) { hasValidNumber = false; } else { total = total + input; } } System.out.println(total); a) 15.0 b) 14.0 c) 12.0 d) 10.0
Answer: d) 10.0
What will be the output of the following code snippet? int i; int j; for (i = 0; i < 6; i++) { for (j = 7; j > i; j--) { System.out.print("*"); } System.out.println(""); } a) A rectangle with six rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. b) A right triangle with six rows and seven columns of asterisks. The number of columns increments by one on completion of one iteration of the inner loop. c) A rectangle with seven rows and six columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.
Answer: d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.
Which of the following statements expresses why the following code is considered bad form? for (rate = 5; years-- > 0; System.out.println(balance)) . . . I. Unrelated expressions in loop header II. Doesn't match expected for loop idiom III. Loop iteration is not clear a) II and III only b) I and II only c) I and III only d) I, II, and III
Answer: d) I, II, and III
What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; } a) No output b) abcd c) abcde d) bcde
Answer: d) bcde
What is the output of the code snippet given below? String s = "abcde"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5); a) No output b) No output (infinite loop) c) abcde d) bcde
answer: b) No output (infinite loop)