Chapter 4: Loops
What is the output, if n is 3? for (int i = 1; i <= n; ++i) { int factorial = 1; factorial = factorial * i; System.out.print(factorial + " "); } 1 1 1 1 2 3 1 2 6 Error: Cannot use the loop variable i inside the for loop
1 2 3
What is the output? int num = 10; while (num <= 15) { System.out.print(num + " "); if (num == 12) { break; } ++num; } System.out.print("Done"); 10 Done 10 11 Done 10 11 12 Done 10 11 12 13 14 15 Done
10 11 12 Done
What is the ending value of sum, if the input is 2 5 7 3? All variables are ints. x = scnr.nextInt(); sum = 0; for (i = 0; i < x; ++i) { currValue = scnr.nextInt(); sum += currValue; } 5 10 12 15
12
How many times will the loop iterate, if the input is 105 107 99 103? x = scnr.nextInt(); while (x > 100) { // Do somethingx = scnr.nextInt(); } 1 2 3 4
2
What is the output, if the input is 3 2 4 5? All variables are ints. num = scnr.nextInt(); for (i = 0; i < num; ++i) { curr = scnr.nextInt(); System.out.print(curr); } 24 245 324 3245
245
What is the output? public static void main(String[] args) { for (int i = 0; i < 3; ++i) { System.out.print(i); } System.out.print(i); } 012 0122 0123 Error: The variable i is not declared in the right scope
Error: The variable i is not declared in the right scope
Which input for char c causes "Done" to be output next? c = 'y'; while (c = 'y') { // Do something System.out.println("Enter y to continue, n to quit: "); // Get c from input} System.out.println("Done"); 'y' only 'n' only Any value other than 'y' No such value (error comparing c and 'y')
No such value (error comparing c and 'y')
For the given pseudocode, which XXX and YYY will output the number of times that 10 is input (stopping when 0 is input)? Choices are in the form XXX / YYY. count = 0 val = Get next input While val is not 0 If val == 10 XXX Else YYY val = Get next input Put count to output count = count + 1 / count = 0 count = count + 1 / (nothing) count = count + val / (nothing) count = count + val / count = 0
count = count + 1 / (nothing)
Which XXX causes every character in string inputWord to be output? for (XXX) { System.out.println(inputWord.charAt(i)); } i = 0; i < (inputWord.length() - 1); ++i i = 0; i < inputWord.length(); ++i i = 0; i < (inputWord.length() + 1); ++i i = 0; i <= inputWord.length(); ++i
i = 0; i < (inputWord.length()); ++i
Which YYY outputs the string in reverse? Ex: If the input is "Oh my", the output is "ym hO". int i; String str; str = scnr.nextLine(); for (YYY) { System.out.print(str.charAt(i)); } i = str.length(); i < 0; --i i = str.length(); i > 0; --i i = str.length() - 1; i >= 0; --i i = str.length() + 1; i > 0; --i
i = str.length() - 1; i >= 0; --i
A loop should sum all inputs, stopping when the input is 0. If the input is 2 4 6 0, sum should end with 12. What should XXX, YYY, and ZZZ be? Choices are in the form XXX / YYY / ZZZ. int sum; int currVal; XXX; currVal = scnr.nextInt(); while (YYY) { ZZZ; currVal = scnr.nextInt(); } sum = 0 / currVal != 0 / sum = sum + currVal sum = currVal / currVal == 0 / sum = currVal sum = 1 / currVal != 0 / sum = sum + 1 sum = scnr.nextInt() / currVal == 0 / sum = scnr.nextInt()
sum = 0 / currVal != 0 / sum = sum + currVal
For the given pseudocode, which XXX and YYY will output the sum of the input integers (stopping when -1 is input)? Choices are in the form XXX / YYY. val = Get next input XXX While val is not -1 YYY val = Get next input Put sum to output sum = val / sum = val sum = val / sum = sum + val sum = 0 / sum = sum + val sum = 0 / sum = val
sum = 0 / sum = sum + val
For the given pseudocode, which XXX and YYY will output the smallest non-negative input (stopping when a negative value is input)? Choices are in the form XXX / YYY. min = 0 val = Get next input min = val While val is not negative If XXX YYY val = Get next input Put min to output val > 0 / min = val min < val / val = min val > min / val = min val < min / min = val
val < min / min = val
Which XXX and YYY will loop as long as the input is an integer less than 100? Choices are in the form XXX / YYY. w = scnr.nextInt(); while (XXX) { // Do something YYY; } w < 100 / w = scnr.nextInt() w >= 100 / (nothing) w < 100 / (nothing) w >= 100 / w = scnr.nextInt()
w < 100 / w = scnr.nextInt()
How many times does the while loop execute for the given input values of -1 4 0 9? userNum = 3; while (userNum > 0) { // Do something // Get userNum from input } 0 1 2 3
1
Which input value causes "Goodbye" to be output next? int x; x = scnr.nextInt(); while (x >= 0) { // Do something x = scnr.nextInt(); } System.out.println("Goodbye"); -1 0 1 No such value
-1
What is the output? int n; for (n = 0; n < 10; n = n + 3) { System.out.print(n + " ");} 0 1 2 3 4 5 6 7 8 9 1 4 7 10 0 3 6 9 0 3 6 9 12
0 3 6 9
What is the output? int columns; int rows; for (rows = 0; rows < 2; ++rows) { for (columns = 0; columns < 3; ++columns) { System.out.print("x"); } System.out.println(); } xxx xxx xx xx xx xx xx xx xxx xxx
xxx xxx
How many x's will be output? Assume row and col are ints. for (row = 0; row < 2; ++row) { System.out.print("x"); for (col = 0; col < 3; ++col) { // Do something } } 1 2 3 6
2
What is the output, if userVal is 5?int x; x = 100; if (userVal != 0) { int tmpVal; tmpVal = x / userVal; System.out.print(tmpVal); } 0 5 20 Error: The compiler generates an error
20
Which input value causes the loop body to execute a 2nd time, thus outputting "In loop" again? String s = "Go"; while ((!s.equals("q")) && (!s.equals("Q"))) { System.out.println("In loop"); s = scnr.next(); } "q" only "Q" only Either "q" or "Q" "Quit"
"Quit"
What is the output? int x = 18; while (x > 0) { // Output x and a space x = x / 3; } 6 2 6 2 0 18 6 2 18 6 2 0
18 6 2
What is the output if count is 4? for (i = count; i > 0; --i) { // Output i } 4 321 4321 43210
4321
How many x's will be output? i = 1; while (i <= 3) { j = 1; while (j <= i) { System.out.print("x"); ++j; } System.out.println(); ++i; } 0 3 4 6
6
What is the output, if the input is 3 2 1 0? x = scnr.nextInt(); while (x > 0) {System.out.print(2 * x + " "); } 6 6 4 2 6 4 2 0 6 6 6 6 6 ... (infinite loop)
6 6 6 6 6 ... (infinite loop)
Which for loop will iterate 100 times? for (i = 0; i < 99; i++) for (i = 1; i < 99; i++) for (i = 0; i < 100; i++) for (i = 1; i < 100; i++)
for (i = 0; i < 100; i++)
The program should output even values between -10 and 10 (inclusive), so -10 -8 ... 8 10. What should XXX be? for (XXX) { System.out.print(i + " "); } i = -10; i < 10; i = i + 2 i = -10; i <= 10; i = i + 2 i = -10; (i * 2) < 10; ++i i = -10; (i * 2) <= 10, ++i
i = -10; i <= 10; i = i + 2
The program should determine the largest integer seen. What should XXX be if the input values are any integers (negative and non-negative)? All variables are ints and have unknown initial values. for (i = 0; i < 10; ++i) { currValue = scnr.nextInt(); if (i == 0) { // First iteration XXX; } else if (currValue > maxSoFar) { maxSoFar = currValue; } } maxSoFar = 0 maxSoFar = currValue currValue = 0 currValue = maxSoFar
maxSoFar = currValue
What is the output? char letter1; char letter2; letter1 = 'p'; while (letter1 <= 'q') { letter2 = 'x'; while (letter2 <= 'y') { System.out.print("" + letter1 + letter2 + " "); ++letter2; } ++letter1; } px px py qx qy px py qx qy
px py qx qy