• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

loops

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic