• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a question from Dan's topic mock exam

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kedar Joshi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me answer should be C.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic