cop quiz 7
8
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 8) { System.out.println("Welcome to Java"); count++; }//end of while loop
True
T/F: In the case of an infinite while loop, the while expression (that is, the loop condition) is always true.
False (executes 20 times)
T/F: The following for loop executes 21 times. (Assume all variables are properly declared.) for (i = 1; i <= 20; i = i + 1) System.out.println(i);
False (prints 35)
T/F: The output of the Java code, assuming that all variables are properly declared, is 32. num = 10; while (num <= 32) num = num + 5; System.out.println(num);
False (2 is not greater than or equal to 6, therefore it doesn't go into the loop)
T/F: The output of the Java code, assuming that all variables are properly declared, is: 2 3 4 5 6. n = 2; while (n >= 6) { System.out.print(n + " "); n++; } System.out.println();
True
T/F: The output of the following Java code is: Stoor. int count = 5; System.out.print("Sto"); do { System.out.print('o'); count--; } while (count >= 5); System.out.println('r');
False
T/F: Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }
1 3 5 7 9
What does the following loop display? for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }
0
What is the output of the following Java code? int num = 15; while (num > 0) num = num - 3; System.out.println(num);
5
What is the output of the following code? int count; int num = 2; for (count = 1; count < 2; count++) { num = num + 3; System.out.print(num + " "); } System.out.println();
False
When nesting loops, the variable in the outer loop changes more frequently.
b. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); }
Which is an infinite loop? a. loopCount = 4; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; } b. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); } c. loopCount = 1; while(loopCount < 3); { System.out.println("Hello"); loopCount = loopCount + 1; } d. loopCount = 5; while(loopCount > 3); { System.out.println("Hello"); loopCount = loopCount - 1; }
The body of the loop may not execute at all
Which of the following is true about a while loop? The body of the loop is executed at least once. The logical expression controlling the loop is evaluated before the loop is entered and after the loop exists. The body of the loop may not execute at all. It is a post-test loop
AB
Which of the following loops prints "Welcome to Java" 10 times? A: for (int count = 1; count <= 10; count++) { System.out.println("Welcome to Java"); } B: for (int count = 0; count < 10; count++) { System.out.println("Welcome to Java"); } C: for (int count = 1; count < 10; count++) { System.out.println("Welcome to Java"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); }