Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chisolm mock exam query

 
ketki kalkar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Thankyou chandra for helping me with the study process and other queries as well.I have one question regarding the chisolm mock exam.See the code below:
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

The answer is Prints: 012
Can you please explain?
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}}


i,j,k will be post increment so your are getting that output.


Regards
NIK
SCKP 1.5
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find helpful, if you debug this code using some IDE (netbeans, eclipse), so you can trace every line and see how each variable is changing.
 
rajesh baba
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test
{
public static void main (String[] args)
{
int i = 0, j = 0, k = 0;
do
{
while (i++ < 3 )
{
System.out.print(k++);
}
} while (j++ < 3);
}
}

its a while loop inside do while loop so the code inside do while will be executed once so inner while loop prints 012 and do while runs three times but the value of i became 3 in so inner while condition fails so even though outer do while executes three time inner while loop executes only once so
output is:012
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic