PROG2 - MIDTERM EXAMS

Is this your test? Login to manage it. If not, you can make a test just like it.

This is a non-interactive preview of the quiz content.

1.
1 point
int x, y;

if (x < 4)
y = 2;
else if (x > 4)
{
if (x > 7)
y = 4;
else
y = 6;
}
else
y = 8;

Based on the code above, what is the value of y if x = 9?
2.
1 point
int x, y;

if (x < 4)
y = 2;
else if (x > 4)
{
if (x > 7)
y = 4;
else
y = 6;
}
else
y = 8;


Based on the code above, what is the value of y if x = 1?
3.
1 point
Suppose x = 10 and y = 20. The value of the expression ((x >= 10) && (y <= 20)) is true.
4.
1 point
All switch structures include default cases.
5.
1 point
Suppose sum and num are int variables, and the input is

20 25 10 18 -1

What is the output of the following code? (Assume that console is a Scanner object initialized to the standard input device.)

sum = 0;
num = console.nextInt();
while (num != -1)
{
if (num >= 20)
sum = sum + num;
else
sum = sum - num;
num = console.nextInt();
}
System.out.println(sum);
6.
1 point
What is the output of the following Java code?

int num = 15;
while (num > 0)
num = num - 3;
System.out.println(num);
7.
1 point
int x, y;

if (x < 4)
y = 2;
else if (x > 4)
{
if (x > 7)
y = 4;
else
y = 6;
}
else
y = 8;

Based on the code above, what is the value of y if x = 5?
8.
1 point
In Java, case and switch are reserved words, but break is not a reserved word.
9.
1 point
What is the value of counter after the following statements execute?

counter = 1;
while (counter < 30)
counter = 2 * counter;
10.
1 point
The do...while loop has an exit condition but no entry condition.
11.
1 point
A loop is a control structure that causes certain statements to be executed over and over until certain conditions are met.
12.
1 point
switch (lastInitial)
{
case 'A':
System.out.println("section 1");
break;
case 'B':
System.out.println("section 2");
break;
case 'C':
System.out.println("section 3");
break;
case 'D':
System.out.println("section 4");
break;
default:
System.out.println("section 5");
}


Based on the code above, what is the output if lastInitial = 'E'?
13.
1 point
Suppose str1 and str2 are String variables. The expression (str1 == str2) determines whether str1 and str2 point to the same String object.
14.
1 point
What is the output of the following code?

int count;
int num = 2;

for (count = 1; count < 2; count++)
{
num = num + 3;
System.out.print(num + " ");
}
System.out.println();
15.
1 point
Suppose that the input is 6 and console is a Scanner object initialized to the standard input device. Consider the following code.

int alpha = 7;
int beta = console.nextInt();

switch (beta)
{
case 5:
alpha = alpha + 1;
case 6:
alpha = alpha + 2;
case 7:
alpha = alpha + 3;
default:
alpha = alpha + 5;
}

System.out.print(alpha);


-----> The output of this code is 9.
16.
1 point
Assume that all variables in the following code are properly declared and that the input is 3 7 4 –1. The output is 13.
num = console.nextInt();
sum = num;
while (num != -1)
{
num = console.nextInt();
sum = sum + num;
}

System.out.println(sum);
17.
1 point
int i;

for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");


Which of the following is the initial expression in the for loop above?
18.
1 point
Suppose that you have the following code.

int num = 10;

if (num > 10)
System.out.println(num);
else
System.out.println(num + 5);

The output of this code is 5.
19.
1 point
int i;

for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");


Which of the following is the logical expression in the for loop above?
20.
1 point
The output of the Java code, assuming that all variables are properly declared, is 32.

num = 10;
while (num <= 32)
num = num + 5;
System.out.println(num);
21.
1 point
The following for loop executes 21 times. (Assume all variables are properly declared.)

for (i = 1; i <= 20; i = i + 1)
System.out.println(i);
22.
1 point
int x, y;

if (x < 4)
y = 2;
else if (x > 4)
{
if (x > 7)
y = 4;
else
y = 6;
}
else
y = 8;

Based on the code above, what is the value of y if x = 4?
23.
1 point
int x = 27;
int y = 10;

do
x = x / 3;
while (x >= y);

What is the final value of x in the code above?
24.
1 point
Two-way selection in Java is implemented using ____.
25.
1 point
What is value of x after the following code executes?

int x = 0;
int i;

for (i = 0; i < 5; i++)
x = 3 * x + i;
26.
1 point
In Java, !, &&, and || are called logical operators.
27.
1 point
=! is a relational operator.
28.
1 point
The output of the following Java code is: Stoor.

int count = 5;
System.out.print("Sto");

do
{
System.out.print('o');
count--;
}
while (count >= 5);

System.out.println('r');
29.
1 point
The output of the Java code:

int alpha = 5;
int beta = 4;

switch (beta)
{
case 2:
alpha = alpha + 2;
case 4:
alpha = alpha + 4;
break;
case 6:
alpha = alpha + 6;
default:
alpha = alpha + 10;
}
System.out.print(alpha);

is: 9
30.
1 point
If the while expression becomes false in the middle of the while loop body, the loop terminates immediately.
31.
1 point
What is the output of the following Java code?

int x = 0;
if (x > 0)
System.out.println("positive ");
System.out.println("zero ");
System.out.println("negative");
32.
1 point
The expression !(x <= 0) is true only if x is a positive number.
33.
1 point
int i;

for (i = 0; i <= 10; i++)
System.out.println("*");
System.out.println("!");



Which of the following is the update expression (increment/decrement) in the for loop above?
34.
1 point
int x = 27;
int y = 10;

do
x = x / 3;
while (x >= y);


How many times does the statement above execute?
35.
1 point
Suppose P and Q are logical expressions. The logical expression P && Q is false if both P and Q are false.
36.
1 point
What is the output of the following Java code?

int x = 57;
int y = 3;

switch (x % 9)
{
case 0:
case 1:
y++;
case 2:
y = y - 2;
break;
case 3:
y = y + 2;
case 4:
break;
case 5:
case 6:
y = y + 3;
}

System.out.println(y);
37.
1 point
Suppose that you have the following code:

int sum = 0;
int num = 8;

if (num < 0)
sum = sum + num;
else if (num > 5)
sum = num + 15;

After this code executes, what is the value of sum?
38.
1 point
The symbol >= is a logical operator.
39.
1 point
switch (lastInitial)
{
case 'A':
System.out.println("section 1");
break;
case 'B':
System.out.println("section 2");
break;
case 'C':
System.out.println("section 3");
break;
case 'D':
System.out.println("section 4");
break;
default:
System.out.println("section 5");
}

Based on the code above, what is the output if lastInitial = 'C'?
40.
1 point
A computer program will recognize both = and == as the equality operator.