This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "loops" Watch "loops" New topic
Author

loops

kishore kovil
Greenhorn

Joined: Dec 21, 2006
Posts: 20
Hi,
I am sorry once again.
How can I ensure that there are no such images.
Please help me
I herewith specify the code examples again:
Question 16
class JMM110 {
public static void main (String[] args) {
int j = 0;
do for (int i = 0; i++ < 2 ;)
System.out.print(i);
while (j++ < 2);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above

--------------------------------------------------------------------------------

Question 17
class JMM111 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i < 2; i++) do
System.out.print(i);
while (j++ < 2);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above

--------------------------------------------------------------------------------

Question 18
class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2 ;) do
System.out.print(i);
while (j++ < 2);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above

--------------------------------------------------------------------------------

Question 19
class JMM113 {
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Run-time error
j. Compile-time error
k. None of the above

--------------------------------------------------------------------------------

Question 20
class JMM114 {
public static void main (String[] args) {
int i = 0, j = 0;
while (i++ < 3) do
System.out.print(j);
while (j++ < 3);
}}

What is the result of attempting to compile and run the program?

a. Prints: 0001
b. Prints: 001122
c. Prints: 012012
d. Prints: 012345
e. Prints: 1112
f. Prints: 111222
g. Prints: 121212
h. Run-time error
i. Compile-time error
j. None of the above

--------------------------------------------------------------------------------

Question 21
class JMM115 {
static int m1(String s, int i) {
System.out.print(s + i);
return i;
}
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (m1("i", ++i) < 2)
System.out.print("k" + ++k);
while (m1("j", ++j) < 2);
}}

What is the result of attempting to compile and run the program?

a. Prints: i1k1i2k2j1i3j2
b. Prints: i1k1i2k2j1i1k1i2k2j2
c. Prints: i1k1i2j1i3j2
d. Run-time error
e. Compile-time error
f. None of the above

--------------------------------------------------------------------------------

Question 22
class JMM116 {
static int m1(String s, int i) {
System.out.print(s + i);
return i;
}
public static void main (String[] args) {
int j = 0;
for (int i = m1("A",0); m1("B",i) < 2; m1("C",++i)) {
m1("J",++j);
}
}}

What is the result of attempting to compile and run the program?

a. Prints: A0B0C1J1B1C2J2B2
b. Prints: A0B0J1C1B1J2C2B2
c. Prints: A0B0J1C1A1B1J2C2A2B2
d. Run-time error
e. Compile-time error
f. None of the above

--------------------------------------------------------------------------------

Question 23
class JMM117 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
i++;
if (j-- < i++) {break;}
} while (i < 5);
System.out.print(i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 5,4
b. Prints: 6,3
c. Prints: 6,6
d. Prints: 7,2
e. Run-time error
f. Compile-time error
g. None of the above

--------------------------------------------------------------------------------

Question 24
class JMM118 {
public static void main (String[] args) {
int i = 0, j = 9;
while (i++ <= j--) {i++; if (j < 5) break;}
System.out.print(i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 7,2
d. Prints: 8,5
e. Prints: 9,4
f. Run-time error
g. Compile-time error
h. None of the above

--------------------------------------------------------------------------------

Question 25
class JMM119 {
public static void main (String[] args) {
int i = 0, j = 9;
do {
if (j < 4) {break;} else if (j-- < 7) {continue;}
i++;
} while (i++ < 7);
System.out.print(i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 6,5
d. Prints: 6,4
e. Prints: 7,5
f. Prints: 8,4
g. Run-time error
h. Compile-time error
i. None of the above

--------------------------------------------------------------------------------

Question 26
class JMM120 {
public static void main (String args[]) {
int i = 0, j = 0, k = 0;
label1:
for (; ;) { i++;
label2:
do {
k = i + j;
switch (k) {
case 0: continue label2;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: break label1;
default: break label1;
}
} while (++j<5);
}
System.out.println(i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 2,1
b. Prints: 2,2
c. Prints: 3,1
d. Prints: 3,2
e. Prints: 3,3
f. Run-time error
g. Compile-time error
h. None of the above

--------------------------------------------------------------------------------

Question 27
class JMM121 {
public static void main (String args[]) {
int h = 0, i = 0, j = 0, k = 0;
label1:
for (; ;) { h++;
label2:
do { i++; k = h + i + j;
switch (k) {
default: break label1;
case 1: continue label1;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
} while (++j < 5);
}
System.out.println(h + "," + i + "," + j);
}}

What is the result of attempting to compile and run the program?

a. Prints: 1,2,3
b. Prints: 1,3,2
c. Prints: 2,2,2
d. Prints: 2,4,1
e. Prints: 2,4,2
f. Run-time error
g. Compile-time error
h. None of the above

Thanks,
Kishore
[ July 15, 2007: Message edited by: Ulf Dittmer ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42371
    
  64
You can disable the smilies pictures by checking the "Disable smilies" checkbox when posting - it's below the pictures of the smilies. I have done this for you in your post.

Also, if you post questions like these, please QuoteYourSources.

Finally, I think it would make people more likely to help you if you indicated for each question what you think the answer is (and why). That way you can show that you have thought about these already. Writing down the thought process can also help clarify issues for yourself.
[ July 15, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Jared Folino
Greenhorn

Joined: Apr 27, 2007
Posts: 25
Hey Kishore,

Don't worry too much, I think a lot of people are in the same situation. Here's a bit of a starter. Look at the following code:



Notice how i is pre-incremented in the the boolean expression in 'Flow Control 1' but i is pre-incremented in the end expression in 'Flow Control 2'. What you need to ask yourself is if this makes any difference to the end result? PM if you have any doubts.
Jared Folino
Greenhorn

Joined: Apr 27, 2007
Posts: 25
Actually here is a less confusing example. But before you scream because I am using a string in the for loop, stop for a second and think about it. The first statement can be any initialisation, the second can be any statement that evaluates to a boolean and the third can be any statement:



The output is:

B
BAB
BABAB

1. Firstly the an empty string is created. This satement is only ever executed once. The only time it can be executed more than once is when it is nested in another loop.

2. Next the sting (which is "") is compared to the string "BABABA" which returns false and is then inverted to true with !.

3. Now the last statement is executed in the for loop which makes the string equal "BA".

Repeat this 3 times and the for loop is finished. The moral of this example: is that you must know the order in which the for statement parts are executed.



1. Basically the init statement is executed once at the start. That's it. Not twice or three times and always at the start.

2.1 The boolean expression is evaluated.
if (boolean is true) then execute the statements
else FINISH for loop

2.2 Execute statement 1. This is the confusing bit. Logically you would think that the statement in the for loop would execute first, but it doesn't.

2.3 Execute statement 2.

3 Continue with step 2.1 until the loop is FINISHED


Hope this helps
 
 
subject: loops