Multiple Choice Questions 3
Analyze the following code:
if (x <100) && (x >10)
System.out.println("x is between 10 and 100");
The statement has syntax errors because (x<100) && (x > 10) must be enclosed inside parentheses.
The statement has syntax errors because (x<100) && (x > 10) must be enclosed inside parentheses and the println(…) statement must be put inside a block.
The statement compiles fine.
Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?
if (x > 0)
if (y > 0)
System.out.println("x > 0 and y > 0");
else if (z > 0)
System.out.println("x < 0 and z > 0");
x > 0 and y > 0;
x < 0 and z > 0;
x < 0 and z < 0;
none of the above
What is y after the following switch statement is executed?
x = 3;
switch (x+3)
{ case 6: y = 1;
default: y += 1; }
1
2
3
4
What is y after the following statement is executed?
x = 0;
0) ? 1 : -1;
-1
0
1
2
Analyze the following program fragment:
int x;
double d = 1.5;
switch (d)
{ case 1.0: x = 1;
case 1.5: x = 2; }
The required break statement is missing in the switch statement.
The required default case is missing in the switch statement.
The switch control variable cannot be double.
None of the above.
Analyze the following statement:
double sum = 0;
for (double d = 0; d<10; )
{ d += 0.1;
sum += sum + d; }
The program has a syntax error because the adjustment is missing in the for loop.
The program has a syntax error because the control variable in the for loop cannot be of the double type.
The program compiles but does not stop because d would never be less than 10.
The program compiles and runs fine.
Do the following two statements result in the same value in sum?
for (int i=0; i<10; ++i)
{ sum += i; }
for (int i=0; i<10; i++)
{ sum += i; }
Yes
No
What is y after the following for loop statement is executed?
int y = 0;
for (int i=0; i<10; ++i)
{ y += i; }
10
11
12
None of the above.
What is i after the following for loop?
for (int i=0; i<10; ++i)
{ y += i; }
9
10
11
undefined
Is the following loop correct?
for (; ; );
Yes
No
Will the following program terminate?
int balance = 10;
while (true)
{ if (balance < 9) continue;
balance = balance - 9; }
Yes
No
Will the following program terminate?
int balance = 10;
while (true)
{ if (balance < 9) break;
balance = balance - 9; }
Yes
No
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);
5
6
7
8
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);
5
6
7
8
Analyze the following statement:
double sum = 0;
double d = 0;
while (d == 10.0)
{ d += 0.1;
sum += sum + d; }
The program does not compile because sum and d are declared double, but assigned with integer value 0.
The program never stops because d is always 0.1 inside the loop.
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
None of the above.
Visited
times since 3 Feb 1999.