File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes for loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "for loop problem" Watch "for loop problem" New topic

for loop problem

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
John Hunt M.E Q38
public class Test{
public void add(int a) {

loop: for (int i = 1; i< 3; i++){
for (int j = 1; j < 3; j++){
if (a == 5) {
break loop;

A. Generates a Runtime Error
B. Generates a Compilation Error
C. Complains that class has not been instantaited
D. Produces no out put

Answer is D and I verified it. But should it not be B or C?
Also, are the label "loop:" and "break loop" correct to use?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Labeling loops is correct. You can put a label just before an while or for loop and then break or continue from that loop later.
Seems like there is part of the code missing. What is the value passed into the add() method. The code for the add() method is fine, so I would say the answer is D based on the other answers. But you are missing main and the call to add here, so if you tried running this class with java Test you would get a runtime error as it wouldn't be able to find main.
Maja Grabovac

Joined: Feb 27, 2001
Posts: 16
I've seen this question somewhere and the value being passed for a is 5. That is the reason it breaks out of the loop immediately without printing out anything.
I agree. Here's the link:
subject: for loop problem
It's not a secret anymore!