# the java

Multiple-Choice Questions

1) The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as A) boolean execution

B) conditional statements

C) try and catch

D) sequentiality

E) flow of control

Answer: E

Explanation: E) The "flow of control" describes the order of execution of instructions. It defaults to being linear (or sequential) but is altered by using control statements like conditionals and loops.

2) Of the following if statements, which one correctly executes three instructions if the condition is true? A) if (x < 0)

a = b * 2;

y = x;

z = a - y;

B) {

if (x < 0)

a = b * 2;

y = x;

z = a - y;

}

C) if { (x < 0)

a = b * 2;

y = x;

z = a - y ;

}

D) if (x < 0)

{

a = b * 2;

y = x;

z = a - y;

}

E) B, C and D are all correct, but not A

Answer: D

Explanation: D) In order to have three instructions execute in the if clause when the condition is true, the three statements must be placed in a block following the condition. This is the syntax used in D. In A, there is no block. In B, the block is placed around the entire if statement such that the if clause is only a = b * 2; and the other two statements are not part of the if statement, but follow it. The syntax in C is illegal resulting in a syntax error. Don't forget that the structure of your code (how it lines up) is immaterial to the compiler.

3) Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0? A) if (x > 0) x++;

else x--;

B) if (x > 0) x++;

else if (x < 0) x--;

C) if (x > 0) x++;

if (x < 0) x--;

else x = 0;

D) if (x == 0) x = 0;

else x++;

x--;

E) x++;

x--;

Answer: B

Explanation: B) if x is positive, x++ is performed else if x is negative x-- is performed and otherwise, nothing happens, or x is unaffected. In A, C, D and E, the logic is incorrect. In A, x-- is done if x is not positive, thus if x is 0, x becomes -1 which is the wrong answer. In C, if x is positive, then x++ is performed. In either case, the next statement is executed and if x is not negative, the else clause is performed setting x to 0. So if x is positive, it becomes 0 after this set of code. In D, x++ and x-- are both performed if x is not 0. And in E, this code does not attempt to determine if x is positive or negative, it just adds one and then subtracts 1 from x leaving x the same. Given the nested if-else structure below, answer questions below.

if (a > 0)

if (b < 0)

x = x + 5;

else

if (a > 5)

x = x + 4;

else

x = x + 3;

else

x = x + 2;

4) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? A) 0

B) 2

C) 3

D) 4

E) 5

Answer: C

Explanation: C) Since (a > 0) is true, the next condition is checked. Since (b < 0) is false, the else clause for this condition is executed. Since (a > 5) is false the else clause for this condition is executed, which is x = x + 3. Therefore, 3 is added to x, so it is now 3.

5) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed? A) 0

B) 2

C) 3

D) 4

E) 5

Answer: B

Explanation: B) Since (a > 0) is not true, the else clause for this condition is executed, which is x = x + 2, so x is now 2. 6) If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed? A) 0

B) 2

C) 3

D) 4

E) 5

Answer: E

Explanation: E) Since (a > 0) is true, the if clause is executed, which is another if statement. Its condition (b < 0) is true, so its if clause is executed, which is x = x + 5, so x is now 5.

7) Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test...

Please join StudyMode to read the full document