aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question from Dan's topic mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question from Dan Watch "a question from Dan New topic
Author

a question from Dan's topic mock exam

duole
Greenhorn

Joined: Jul 01, 2002
Posts: 7
When I took this question my aswer is f, but the correct answer is e. Can anybody explain what exactly happens here? thank you very much.
class A {
public static void main (String[] args) {
int i = 0,j = 0,m = 0,n = 0,p;
label1:
try {
do {
m++;
try {
p = i + j + m + n;
if (p>=5) break label1;
n++;
} finally {i++;}
} while (m++ < 2);
} finally {j++;}
System.out.print(i + "," + j + "," + m + "," + n);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: 1, 1, 2, 3
b. Prints: 1, 1, 2, 2
c. Prints: 1, 1, 3, 1
d. Prints: 1, 0, 3, 1
e. Prints: 2, 1, 3, 1
f. Prints: 2, 1, 4, 1
g. Prints: 0, 0, 3, 1
h. Runtime Exception
i. Compiler Error
j. None of the Above
Kedar Joshi
Greenhorn

Joined: Aug 26, 2002
Posts: 12
it is correct, because in the second loop, p becomes 5 so it comes of the outer try loop. So ultimately m is incremented only thrice.


Sing, laugh, dance, and meditate
Kedar Joshi
Greenhorn

Joined: Aug 26, 2002
Posts: 12
According to me answer should be C.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you for trying my exam.
The comment associated with the answer is as follows.

A break statement with a label will transfer control outside of the enclosing statement with the matching label. Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not evaluated as control transfers out of the loop. However, if the break statement transfers control out of a try-catch-finally statement, then the finally clause is processed.

To get a better idea of the control flow try running the following.

The result is as follows.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi There:
I found the answer e correct.
Dan: Is this a typical question for the exam? It takes a while to properly indent the code and write down the intermediate results etc. Would we
have enough time, paper and pencil available?
Thanks
[ August 29, 2002: Message edited by: Barkat Mardhani ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Barkat Mardhani:
Hi There:
I found the answer e correct.
Dan: Is this a typical question for the exam? It takes a while to properly indent the code and write down the intermediate results etc. Would we
have enough time, paper and pencil available?
Thanks
[ August 29, 2002: Message edited by: Barkat Mardhani ]


The primary purpose of the real exam is to simply verify that you know the material. The real exam is not intended to be a learning experience. For that reason, the final result of most of the real exam questions will just be a single value such as the integer 42. The primary purpose of my exam is to demonstrate the basic concepts. For that reason, my questions print a lot of intermediate results that allow a person to more easily understand what the code is doing.
I think that my exams provide a very good learning experience and that is why I encourage people to use the single topic exams while working through an exam study guide chapter-by-chapter. I encourage people to use other mock exams available elsewhere on the Internet during the final phases of their preparation for the real exam. At that point, a person should already know the material and should simply be looking for practice with questions that more closely resemble what will be encountered on the real exam.
My exam offers its greatest value early in the study process. It is not intended to be a last minute preview of the real exam. A person that is scheduled to take the real exam in a week or two should be using mock exams that are known to closely resemble the real exam. A good example would be the Marcus Green Exams.
 
jQuery in Action, 2nd edition
 
subject: a question from Dan's topic mock exam