APCSA Quiz 4 Loops

Ace your homework & exams now with Quizwiz!

What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x > 5); a. 10 b. 200 c. infinite loop d. compiler error because the initial value of x > 5

ANS: C

How many times will this loop be executed? int m = 1; while(m < 10) System.out.print(m); m++; a. 9 b. 10 c. 11 d. this is an infinite loop

ANS: D

What is the output of the following code? int i, j, k; for (i = 1; i <= 5; i++) { for (j = 1; j <= (5 - i); j++) { System.out.print("*"); } for (k = 1; k <= i; k++) { System.out.print(i); } System.out.println(); }

ANS: ****1 ***22 **333 *4444 55555

What is the output of the following code? for (int i = 1; i < 5; i += 2) { for (int j = 0; j < i; j++) { System.out.print(i+","+j+","); } }

ANS: 1,0,3,0,3,1,3,2,

What is printed to the console after this code is executed? int i, n = 0; for(i=0;i<10;i++) { if ( i == 5 ) break; if ( i == 2 || i == 5 ) continue; n++; } System.out.println( i + " " + n );

ANS: 5 4

30. What is the output of the following code? int power = 5; int number = 10; int result = number; int i=1; for (; i <= (power - 1); i++) result = result * number; System.out.print (result); a. 100000 b. 1000000 c. 10000 d. none of these

ANS: A

Code example 4-1 int limit = 0; for (int i = 10; i >= limit; i -= 2) { for (int j = i; j <= 1; j++) { System.out.println("In inner for loop"); } System.out.println("In outer for loop"); } (Refer to code example 4-1.) How many times does "In inner for loop" print to the console? a. 0 d. 5 b. 1 e. 7 c. 2

ANS: C

What is the output of the following code? int x; for( x = 1; x <= 5; x = x+2) { System.out.print(x + ","); } System.out.println (x); a. 1,3,5,6 b. 1,3,5 c. 1,3,5,7 d. none of these

ANS: C

How many times will this loop be executed? int x = 10; while (x < 100) ; { x += 10; } a. 9 b. 10 c. 11 d. this is an infinite loop

ANS: D

What is the output of the following code? int sum = 0 ; for( int i = 10; i <= 20; i+=5 ) { sum += i ; } System.out.print (sum); a. 20 b. 25 c. 30 d. 45

ANS: D

What is the output of the following code? int sum = 5 ; for(int i = 1; i<=5; i++) { sum += i; } System.out.print (sum); a. 5 b. 10 c. 15 d. 20

ANS: D

What will be the value of x after the following code is executed? int x = 10, y = 20; while (y < 100) { x += y; } a. 90 b. 110 c. 210 d. this is an infinite loop

ANS: D

What will be the value of y after the following code is executed? int x; int y=0; for (x = 1; x < 5; x += 1) y += 4; y+= 1; a. 20 b. 25 c. 30 d. none of these

ANS: D (the value will be 17; think why!)

Code example 4-1 int limit = 0; for (int i = 10; i >= limit; i -= 2) { for (int j = i; j <= 1; j++) { System.out.println("In inner for loop"); } System.out.println("In outer for loop"); } (Refer to code example 4-1.) How many times does "In outer for loop" print to the console? a. 6 b. 8 c. 10 d. 11

ANS: A

How many lines are printed on the console when the following for loop is executed? int i = 10; for ( i = 2; i < 10; i++) { System.out.println(i); } a. 8 c. 10 b. 9 d. 7

ANS: A

In a while loop, the Boolean expression is tested a. before the loop is executed b. after the loop is executed c. both before and after the loop is executed

ANS: A

What is printed to the console after this code is executed? int number = 20; for (int i = 3; i <= number; i += 2) { if (number % i == 0) { System.out.print(i); if (i == number) System.out.print("special"); break; } } a. 5 b. 5special c. 2 d. 20special e. 55special

ANS: A

What is the output of the following code? for(int i = 0; i <= 9; i += 3){ System.out.print(i + ","); } a. 0,3,6,9, b. 0,3,6,9,12, c. 0,3,6, d. none of these

ANS: A

What is the output of the following code? int power = 5, number = 10; int result = number; int i=1; for (; i <= (power - 1); ) { result = result * number; i++; } System.out.print (result); a. 100000 b. 1000000 c. 10000 d. none of these

ANS: A

What is the output of the following code? int power = 5; int number = 10, i, result; for (i = 1, result = number; i <= (power - 1); i++, result *=number); System.out.print (result); a. 100000 b. 1000000 c. 10000 d. none of these

ANS: A

What is the output of the following code? int x = 10; for( x = 1; x <= 5; x++) { System.out.print(x + ","); } System.out.println(x); a. 1,2,3,4,5,6 b. 1,2,3,4,5,6,7 c. 1,2,3,4,5 d. none of these

ANS: A

What will be the value of result after the following code is executed? int power = 5; int number = 10; int i, result; for (i = 1, result = number; i <= (power - 1); i++) result = result * number; a. 100000 b. 1000000 c. 10000 d. none of these

ANS: A

What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y; a. 40 b. 25 c. 30 d. invalid for statement

ANS: A

What will be the value of y after the following code is executed? int x; int y=0; for (x = 1; x < 5; x += 1) { y += 4; y+= 1; } a. 20 b. 25 c. 30 d. none of these

ANS: A

How many lines are printed on the console when the following for loop is executed? for (int j = 10; j < 40; j *= 2) { System.out.println(j); } a. 1 b. 2 c. 3 d. 4

ANS: B

How many lines are printed on the console when the following for loops are executed? for (int i = 1; i < 5; i += 2) { for (int j = 0; j < i; j++) { System.out.println(j); } } a. 2 b. 4 c. 5 d. 20

ANS: B

How many times will the loop be executed? int i = 1, months = 5; while (i < months) { futureValue = futureValue * (1 + monthlyInterestRate); i = i+1; } a. 0 b. 4 c. 5 d. 6

ANS: B

How many times will this loop be executed? int n; for(n = 0; n < 10; n++) System.out.print(n); a. 9 b. 10 c. 11 d. none of these

ANS: B

In a do-while loop, the Boolean expression is tested a. before the loop is executed b. after the loop is executed c. both before and after the loop is executed

ANS: B

What will be the value of x after the following code is executed? int x = 10; while (x < 100) { x += 10; } a. 90 b. 100 c. 110 d. this is an infinite loop

ANS: B

What will be the value of x after the following code is executed? int x = 11; do { x += 10; } while (x > 100); a. 11 b. 21 c. 101 d. none of these

ANS: B

Which of the values below will not be printed to the console when this code is executed? for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { if (i == j) continue; System.out.println("i = " + i + " j = " + j); } } a. i = 0 j = 1 d. i = 2 j = 1 b. i = 1 j = 0 e. i = 1 j = 1 c. i = 2 j = 0

ANS: E


Related study sets

Teaching Young Writers (Midterm-Dr. Ellis)

View Set

Introduction to Networks FINAL EXAM (D)

View Set

Chapter 13, Sections 1, 2, 3, And 4

View Set