CENG-104 QUIZ-2 B Name: Number: Q-1) In a group of nested loops, which loop is executed the most number of times? a) the outermost loop b) the innermost loop c) all loops are executed the same number of times d) cannot be determined without knowing the size of the loops Q-2) Which looping process checks the test condition at the end of the loop? a) for b) while c) do-while d) no looping process checks the test condition at the end Q-3) What will be printed out when this code is compiled and run: for (int i = 0; i <= 3;) { System.out.println("i = " + i); } a) i = 0 i=1 i=2 i=3 b) i = 0 infinitely c) The code does not run. d) The code does not compile. Q-4) What is the output of the following program fragment? for ( int j = 0; j < 5; j++ ) { System.out.print( j + " " ); } System.out.println( ); a) 0 1 2 3 4 5 b) 0 1 2 3 4 c) 0 1 2 3 4 5 d) j j j j j Q-5) What is the output of the following code fragment? for ( int j = 10; j > 5; j-- ) { System.out.print( j + " " ); } System.out.println( ); a) 10 11 12 13 14 15 c) 10 9 8 7 6 5 b) 9 8 7 6 5 4 3 2 1 0 d) 10 9 8 7 6 Q-6) What must the test be so that the following fragment prints out the integers 5 through and including 15? for ( int j = 5; ________ ; j++ ) { System.out.print( j + " " ); } System.out.println( ); a) j<15 b) j<=16 c) j<16 d) j==15 Q-7) What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10? for ( int j = 0; j <= 10; _______ System.out.print( j + " " ); System.out.println( ); a) j+2 b) j = j+2 ) c) j++++ d) ++j++ Q-8) What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ? for ( _______; j < 0; j++ ) System.out.print( j + " " ); System.out.println( ); a) int j = 0 b) int j < 0 c) int j = -3 d) int j = -4 Q-9) What is the output of the following code fragment? for ( int count = 0; count < 9; ) System.out.print( count + " " ); count++ ; System.out.println( ); a) 0 1 2 3 4 5 6 7 8 b) Nothing --- the program will not compile. c) count count count count count count count count count d) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... Q-10) What is the output of the following code fragment? int j; for ( j = 0; j < 5; ) { System.out.print( j + " " ); j++ ; } System.out.println( ); a) 0 1 2 3 4 b) Nothing --- the program will not compile. c) 1 2 3 4 5 d) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... Q-11) A continue statement causes execution to skip to a) the end of the program b) the first statement after the loop c) the statement following the continue statement d) the next iteration of the loop Q-12) Examine the following code: int count = 0; while ( count <= 6 ) { System.out.print( count + " " ); count = count + 2; } System.out.println( ); What does this code print on the monitor? a) 1 2 3 4 5 6 b) 0 2 4 6 8 c) 0 2 4 d) 0 2 4 6 Q-13) Examine the following code: int count = 7; while ( count >= 4 ) { System.out.print( count + " " ); count = count - 1; } System.out.println( ); What does this code print on the monitor? a) 1 2 3 4 5 6 7 b) 7 6 5 4 c) 6 5 4 3 d) 7 6 5 4 3 Q-14) Examine the following code: int count = -2 ; while ( count < 3 ) { System.out.print( count + " " ); count = count + 1; } System.out.println( ); What does this code print on the monitor? a) -2 -1 1 2 3 4 b) -2 -1 1 2 3 c) -3 -4 -5 -6 -7 d) -2 -1 0 1 2 Q-15) Examine the following code: int count = 1; while ( count < 5 ) { System.out.print( count + " " ); } System.out.println( ); What does this code print on the monitor? a) 1 2 3 4 b) 1 2 3 4 5 c) 2 3 4 d) 1 1 1 1 1 1 1 1 1 1 1 . . . . Q-16) Examine the following code: int count = 1; while ( ___________ ) { System.out.print( count + " " ); count = count + 1; } System.out.println( ); What condition should be used so that the code writes out: 1 2 3 4 5 6 7 8 a) count < 8 b) count < 9 c) count+1 <= 8 d) count != 8 Q-17) What three parts of a counting loop must be coordinated in order for the loop to work properly? a) initializing the counter, testing the counter, changing the counter b) initializing the condition, changing the condition, terminating the loop c) the while, the assignment, and the loop body d) the while statement, the if statement, and sequential execution. Q-18) What's wrong? while( (i < 10) && (i > 24)) a) the logical operator && cannot be used in a test condition b) the while loop is an exit-condition loop c) the test condition is always false d) the test condition is always true