Preview

the java

Better Essays
Open Document
Open Document
4105 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
the java
Java Software Solutions: Foundations of Program Design, 6e (Lewis/Loftus)
Chapter 5 Conditionals and Loops

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

You May Also Find These Documents Helpful

Related Topics