Since you are preparing for the Java Programmer Certification Exam and have made it this far in the course,
I assume that you are familiar with the basics of Java's programming statements.
The following series of images will help you to review them and identify which statements you need to study further. Subsequent lessons in this module will cover the important points you should know in order to do well on the exam.
In this module, I use the term
statement block[1] to refer to either a single semicolon-terminated statement or a group of statements surrounded by braces.
What will the following program snippet print?
public class ProgramSnippet {
public static void main(String args[]) {
int i = 0, j = 11;
do {
if (i > j) {
break;
} // end - if
j--;
}// end - do
while (++i < 5);
System.out.println(i + " " + j);
} // end -main
}
Select 1 option:
- 5 5
- 5 6
- 6 6
- 6 5
- 4 5
Answer: b
Explanation:
++i < 5 means, increment the value of i and then compare with 5.
Now, try to work out the values of i and j at every iteration.
To start with, i=0 and j=11. At the time of evaluation of the while condition, i and j are as follows:
- j = 10 and i=1 (loop will continue because i <5) (Remember that comparison will happen AFTER increment i because it is ++i and not i++.
- j = 9 and i=2 (loop will continue because i<5).
- j = 8 and i=3 (loop will continue because i<5).
- j = 7 and i=4 (loop will continue because i<5).
- j = 6 and i=5 (loop will NOT continue because i not <5).
So it will print 5 6. (It is print i first and then j).