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

KB Question

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Crivitch{
public static void main(String a[])
{
int x=0;
// insert code here
do{
}while(x++<y);
System.out.print(x);
}
}

Options are:

1. int y=10;
2 int y=11;
3.int y=12.

which of these should be enter to produce o/p 12?

ans -2

Can some one expalin pls..
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dinesh,

The execution flow is quite simple here. The while condition executes till x=11.
So when x=11 it does x++<11 and exits the loop. The increment is applied immediately in the next statement.

For such questions to check the execution flow, I always use a debug mode in ECLIPSE to know which statement has what effect. Do try it out, its really useful.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajat Asani:
... For such questions to check the execution flow, I always use a debug mode in ECLIPSE to know which statement has what effect...


IDEs like Eclipse do a lot of things for you. This is great when your goal is efficiency, but when you're learning details for the SCJP exam, it can work against you.

You might learn more by inserting your own println statements at various points in the code to display what's happening. This will force you to think about what to output when, and you'll get a much better understanding of the code and execution flow.
 
Rajat Asani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Marc...would definitely keep that in mind.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it really works..
 
reply
    Bookmark Topic Watch Topic
  • New Topic