• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Chisolm mock exam query

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic