How can we help?

You can also find more resources in our Help Center.

39 terms

Java Quiz 4 - Switch Statements, Loops

STUDY
PLAY
1. What would be the value of x after the following statements were executed?
int x = 10;
switch (x)
{
case 10:
x += 15;
break;
case 12:
x -= 5;
break;
default:
x *= 3;
}
a. 5 b. 20 c. 25 d. 30
ANS: C
2. What would be the value of x after the following statements were executed?
int x = 12;
switch (x)
{
case 10:
x += 15;
break;
case 12:
x -= 5;
break;
default:
x *= 3;
}
a. 5 b. 20 c. 7 d. 30
ANS: C
3. What would be the value of x after the following statements were executed?
int x = 15;
switch (x)
{
case 10:
x += 15; break;
case 12:
x -= 5; break;
default:
x *= 3;
}
a. 5 b. 20 c. 45 d. 30
ANS: C
4. What would be the value of x after the following statements were executed?
int x = 15;
switch (x)
{
case 10:
x += 15; break;
case 12:
x -= 5; break;
}
a. 5 b. 20 c. 15 d. 30
ANS: C
5. What would be the value of x after the following statements were executed?
int x = 10;
switch (x)
{
case 10:
x += 15;
case 12:
x -= 5; break;
}
a. 5 b. 20 c. 25 d. 30
ANS: B
6. What is the value of routeNumber after the following statement is executed ?
int zipCode = 93705, routeNumber;
switch (zipCode)
{
case 93705:
case 93706:
routeNumber = 1;
break;
case 93710:
case 93720:
routeNumber = 2;
break;
default:
routeNumber = 0;
break;
}
a. 0 c. 2
b. 1 d. undefined
ANS: B
7. What is the value of x after the following statements are executed?
int x = 5;
switch(x)
{
case 5:
x += 2;
case 6:
x++;
break;
default:
x *= 2;
break;
}
a. 6 d. 10
b. 7 e. 16
c. 8
ANS: C
8. 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
9. 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
10. 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
11. 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
12. 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
13. 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
14. 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
15. 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
16. 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
17. 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
18. 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
19. 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
20. 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
21. 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
22. 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
23. 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
24. 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
25. 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
26. 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
27. 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!)
28. 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
29. 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
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
31. 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
32. 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
33. 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,
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");
}

34. (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
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");
}
35. (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
36. 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
37. 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
38. 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
39. 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