Java Ch. 6
What is the output of the following loop? int s = 1; int n = 1; do { s = s + n; n++; } while (s < 10 * n); System.out.println(s); A) 211 B) 210 C) 120 D) 123
A) 211
What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j); A) i = 1951, j = 0 B) i = 1951, j = 45 C) i = 65, j = 1 D) i = 65, j = 45
A) i = 1951, j = 0
) What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6) { System.out.print(i + ","); } A) 20, 14, 8, 2 B) 20, 14, 8, 2, -4 C) 20, 14, 8 D) 14, 8, 2
B) 20, 14, 8, 2, -4
) 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) // better is while (j * j <= number) { 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. // incorrect if number is 1 D) The code snippet will loop forever.
B) The code snippet will display the desired result.
What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i = " + i + ", j = " + j); A) i = 45, j = 1 B) i = 351, j = 0 C) i = 351, j = 2 D) i = 1311, j = 35
B) i = 351, j = 0
What is the output of the following code snippet? int i = 1; while (i != 9) { System.out.print(i + " "); i++; if (i == 9) { System.out.println("End"); } } A) 1 End B) 1 End (infinite loop) C) 1 2 3 4 5 6 7 8 End D) 1 2 3 4 5 6 7 8 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
) 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
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
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
C) 2 times
How many times does the code snippet given below display "Loop Execution"? int i = 1; while (i != 10) { System.out.println ("Loop Execution"); i++; } A) Infinite times B) 8 times C) 9 times D) 10 times
C) 9 times
What is the output of the code snippet given below? String s = "abcdefghijkl"; int i = 1; do { if (i > 2) { System.out.print(s.substring (1, i)); } i++; } while (i < 5); A) abcd B) bcde C) bcbcd D) cdef
C) bcbcd
What is the output of the code snippet given below? 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)
D) 0 2 4 6 8 10 12 14 .... (infinite loop)
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
D) 10
What is the output of the code snippet given below? int i; int j = 0; for (i = 0; i < 5; i++) { if (i % 2 == 0) { i = i + 2; j++; } else { i++; j = j + 2; } j++; } System.out.println("i=" + i + ", j=" + j); A) i=7, j =7 B) i =7, j =6 C) i =6, j =7 D) i =5, j =5
D) i =5, j =5
What is the output of the code fragment given below? int i = 0; int j = 0; while (i < 27) { i = i + 2; j++; } System.out.println("j=" + j); A) j=27 B) j=12 C) j=13 D) j=14
D) j=14