Intro. to Java Programming, Ninth Edition - Ch.4
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10); A. 8 B. 9 C. 10 D. 11 E. 0
C. 10
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; } A. 8 B. 9 C. 10 D. 11 E. 0
C. 10
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); } A. 8 B. 9 C. 10 D. 11 E. 0
C. 10
What is the value in count after the following loop is executed? int count = 0; do { __System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count); A. 8 B. 9 C. 10 D. 11 E. 0
C. 10 // int count = 0; do { __System.out.println("Welcome to Java"); } while (*count++* < 9); System.out.println(count); //this code prints the # of times count was hit. So: count = 0 is actually 1 hit total count = 1 is actually 2 hits total ... count = 8 is actually 9 hits total then it increments to: count = 9 which is actually 10 hits total then this is now false: while (count++ < 9); and the loop exits
Given the following four patterns, Pattern A 1 12 123 1234 12345 123456 Pattern B 123456 12345 1234 123 12 1 Pattern C __________1 ________12 ______123 ____1234 __12345 123456 Pattern D 123456 __12345 ____1234 ______123 ________12 __________1 Which of the pattern is produced by the following code? for (int i = 1; i <= 6; i++) { for (int j = 6; j >= 1; j--) System.out.print(j <= i ? j + " " : " " + " "); System.out.println(); } A. Pattern A B. Pattern B C. Pattern C D. Pattern D
C. Pattern C
What will be displayed when the following code is executed? int number = 6; while (number > 0) { number -= 3; System.out.print(number + " "); } A. 6 3 0 B. 6 3 C. 3 0 D. 3 0 -3 E. 0 -3
C. 3 0
Analyze the following code: import java.util.Scanner; public class Test { _public static void main(String[] args) { _int sum = 0; _for (int i = 0; i < 100000; i++) { ____Scanner input = new Scanner(System.in); ____sum += input.nextInt(); _} _} } *A.* The program does not compile because the Scanner input = new Scanner(System.in); statement is inside the loop. *B.* The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop. *C.* The program compiles and runs, but it is not effient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop. *D.* The program compiles, but does not run because there is not prompting message for entering the input. (efficient is not spelled correctly (effient) on the original which I am leaving so people can search for the question based on the original)
*C.* The program compiles and runs, but it is not effient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop. //The loop just needs the following: for (initial-action; loop-continuation-condition; action-after-each-iteration) { _____________// Loop body; _____________Statement(s); } //efficient is not spelled correctly (effient) on the original which I am leaving so people can search for the question based on the original
Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } } (choose more than one) *A.* The program has a compile error because of the semicolon (;) on the for loop line. *B.* The program compiles despite the semicolon (;) on the for loop line, and displays 4. *C.* The program compiles despite the semicolon (;) on the for loop line, and displays 14. *D.* The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);
*C.* The program compiles despite the semicolon (;) on the for loop line, and displays 14. *D.* The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);
How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.println(i * j); A. 100 B. 20 C. 10 D. 45
A. 100
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum >= 4) continue; } while (item < 5); A. 15 B. 16 C. 17 D. 18
A. 15
Is the following loop correct? for (; ; ); A. Yes B. No
A. Yes //This isn't relevant to C169 for WGU that I can find.
Will the following program terminate? int balance = 10; while (true) { __if (balance < 9) break; __balance = balance - 9; } A. Yes B. No
A. Yes //loop has to have a false to terminate or in this case a break. while (true) { __if (balance < 9) *break*; __balance = balance - 9; }
Do the following two statements in (I) and (II) result in the same value in sum? (I): for (int i = 0; i<10; ++i) { sum += i; } (II): for (int i = 0; i<10; i++) { sum += i; } A. Yes B. No
A. Yes //trick question - What matters is the *loop-continuation-condition* which when false stops the loop. Also, the increment in this example is performed after each iteration, see below: In general, the syntax of a for loop is: for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; Statement(s); }
To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0. B. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0.
A. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.
Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C (choose more than one) A. count < 100 is always true at Point A B. count < 100 is always true at Point B C. count < 100 is always false at Point B D. count < 100 is always true at Point C E. count < 100 is always false at Point C
A. count < 100 is always true at Point A E. count < 100 is always false at Point C
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5); A. 5 B. 6 C. 7 D. 8
B. 6
Will the following program terminate? int balance = 10; while (true) { _if (balance < 9) continue; _balance = balance - 9; } A. Yes B. No
B. No //There has to be a false somewhere in the loop for it to terminate. This loop has an explicit continue. while (true) { _if (balance < 9) *continue*; _balance = balance - 9; } //Also this is not an if loop. It is a while loop.
What will be displayed after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number; i++) { if (number % i == 0) { isPrime = false; break; } } System.out.println("i is " + i + " isPrime is " + isPrime); A. i is 5 isPrime is true B. i is 5 isPrime is false C. i is 6 isPrime is true D. i is 6 isPrime is false
B. i is 5 isPrime is false //normally once the loop finishes at i = 5 it exits the loop adding +1 to i making it i = 6 but the break; statement prevents the addition of +1. It stops everything at i = 5.
What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration } A. 2*n B. n C. n - 1 D. n + 1
B. n
Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { __d += 0.1; __sum += sum + d; } A. The program does not compile because sum and d are declared double, but assigned with integer value 0. B. The program never stops because d is always 0.1 inside the loop. C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. //The reason for this is because double isn't accurate as a precise measurement. It might get very close but there is no guarantee it will hit 10.0 exactly.
What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration } A. 2*n B. n C. n - 1 D. n + 1
C. n - 1
The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; } A. 1 2 3 4 5 6 7 8 9 B. 1 2 3 4 5 6 7 8 9 10 C. 1 2 3 4 5 D. 1 3 5 7 9 E. 2 4 6 8 10
D. 1 3 5 7 9
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10); A. 8 B. 9 C. 10 D. 11 E. 0
D. 11
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); A. 100 B. 20 C. 10 D. 45
D. 45
Analyze the following statement: double sum = 0; for (double d = 0; d < 10;) { __d += 0.1; __sum += sum + d; } A. The program has a compile error because the adjustment is missing in the for loop. B. The program has a compile error because the control variable in the for loop cannot be of the double type. C. The program runs in an infinite loop because d<10 would always be true. D. The program compiles and runs fine.
D. The program compiles and runs fine. //the iteration normally found in the initial line is found here: __d += 0.1;
Suppose the input for number is 9. What is the output from running the following program? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input.nextInt(); int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i); if (isPrime) System.out.println(number + " is prime"); else System.out.println(number + " is not prime"); } } A. i is 3 followed by 9 is prime B. i is 3 followed by 9 is not prime C. i is 4 followed by 9 is prime D. i is 4 followed by 9 is not prime
D. i is 4 followed by 9 is not prime
What will be displayed after the following loop terminates? int number = 25; int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) { isPrime = false; } } System.out.println("i is " + i + " isPrime is " + isPrime); A. i is 5 isPrime is true B. i is 5 isPrime is false C. i is 6 isPrime is true D. i is 6 isPrime is false
D. i is 6 isPrime is false //once the loop finishes at i = 5 it exits the loop adding +1 to i making it i = 6
What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); A. 10 B. 11 C. 12 D. 13 E. 45
E. 45
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"); } A. BD B. ABC C. AC D. BC E. AB
E. AB
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A: double sum = 0; for (int i = 1; i <= 99; i++) { sum = i / (i + 1); } System.out.println("Sum is " + sum); B: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1); } System.out.println("Sum is " + sum); C: double sum = 0; for (int i = 1; i <= 99; i++) { sum += 1.0 * i / (i + 1); } System.out.println("Sum is " + sum); D: double sum = 0; for (int i = 1; i <= 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum); E: double sum = 0; for (int i = 1; i < 99; i++) { sum += i / (i + 1.0); } System.out.println("Sum is " + sum); A. BCD B. ABCD C. B D. CDE E. CD
E. CD
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x); A. x is 0 B. x is 1 C. x is 2 D. x is 3 E. x is 4
E. x is 4
How many times will the following code print "Welcome to Java"? int count = 0; do { __System.out.println("Welcome to Java"); __count++; } while (count < 10); A. 8 B. 9 C. 10 D. 11 E. 0
How many times will the following code *print* "Welcome to Java"? C. 10 //keyword is print //count++ happens before while so it stops on count 9 but print 10
What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { __y += i; } A. 9 B. 10 C. 11 D. undefined
What is *i* *after* the following for loop? D. undefined //Explanation: The scope of i is inside the loop. After the loop, i is not defined. //p.148 Tip - The control variable must be declared inside the control structure of the loop or before the loop. If the loop control variable is used only in the loop, and not elsewhere, it is good programming practice to declare it in the initial-action of the for loop. *If the variable is declared inside the loop control structure, it cannot be referenced outside the loop.* In the preceding code, for example, you cannot reference i outside the for loop, because it is declared inside the for loop.