• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

MINDQ EXAM- Q29, Q38, Q50(Please answer)

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
Hi!
I've got some doughts regarding following Qs.from MINDQ EXAM.Can anyone clear them?
Q29)For the code
1. m =0;
2. while(m++ < 2)
3. System.out.println(m);
which of the following are printed to standard output?
a)0 b)1 c)2 d)3 e)Nothing and an exception is thrown.
I tried this on PC.Actually none of the option is correct.
It gives compilation error saying undefined variable m. So I changed the line 1 to int m = 0;But...
I was expecting o/p: b) i.e 1 only.
but on PC i m getting o/p:- b, c HOW?
since m is initially 0, due to m++ it becomes 1 which is < 2
so m = 1 is printed.then it becomes 2 which is !<2 so loop terminates.
O/P: option b)
II]I would also like to know in(m++ < 2), which one gets executed first i.e. m++ or first cond. is checked and then m++ takes place.Because in that 0 should get printed first.
As per me, m++ will take place first before checking for <2 because postfix has got high precedence than (<).Am I correct?
Q38)What line of code would begin execution of thread named myThread?
I gave answer: myThread.start(); WHICH I MISSED.I DON'T HOW? CAN ANYONE EXPLAIN & tell me correct answer.
Q50) I missed this.
Which of the following java.awt.Graphics methods would be used to draw outline of rectangle with single method call.
a)fillRect() b) drawRect() c) fillPolygon() d)drawPolygone()
e)drawLine()
Gave ans: b but now I think it should have been b)& d) IS IT SO?
I want to confirm.
THANX IN ADVANCE.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krutika
Step 1
intially
m=0
m=0 is checked for condition <2
True
m is increamented so m=1
m is printed so 1 is printed.
Step 2
m=1
m=1 is checked for condition <2
True
m is increamented so that m=2
m is printed so 2 is printed
Step 3
m=2
m=2 is checked for condition <2
False
comes out
Hope this makes clear.
Regards
Sandip
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q38)What line of code would begin execution of thread named myThread?
I gave answer: myThread.start(); WHICH I MISSED.I DON'T HOW? CAN ANYONE EXPLAIN & tell me correct answer.


What other options did they give you? Calling start doesn't actually cause the thread to begin execution, it will give the thread to the scheduler which is in charge of deciding which threads run and when. The thread won't actually begin execution until it's run method is executed.

Q50) I missed this.
Which of the following java.awt.Graphics methods would be used to draw outline of rectangle with single method call.
a)fillRect() b) drawRect() c) fillPolygon() d)drawPolygone()
e)drawLine()


Did they spell D the same as you have it in your post? if so they mispelled and B would have been the only correct answer. If they spelled it right then both B and D would be right.
hope this helps
Dave
 
krutika burse
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandip & Dave,
THANK U VERY MUCH.
Q 29)I got it! But I've still got a dought.As I mentioned earlier,postfix operator(++) has got higher precedence than (<)operator.Then how come it is checking for (<)cond. first and then incrementing m.Can u Please explain it?
As per as Q 38), he has given question this way only and not given any options for it.What I thought was myThread.start() will make the thread eligible for execution and whenever scheduler assigns it the turn it will start executing its run().Because normally we call start() & not run() directly.
Q 50)I got it! Actually d] option is drawPolygon only & not drawPolygone().I only mispelled it.So it means both options b] & d] are correct.

Regards
Krutika.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q 29)
m++ is read "get value of m to be used, then increment", so even if the ++ has higher priority, it is still the "old" value of m that is used in the comparison. Compare to ++m, where it is "increment, then get value of m".
Do you see the difference? Any clearer?
There is a great example that has popped up a few times here (and in some mock-exams):

What gets printed?
Nope, 6 is wrong. The correct answer is 4.
Try and compile it, and you'll probably be surprised, but if you look at what I wrote above, it might make sence.
Again, you take the value of the variable (i in this instance) to be used, increment it and then do the asignment, with the value you first took.
/Mike
 
krutika burse
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,
Thank you.

It cleared my dought very well.In fact because of this particular concept I missed many Qs. specially on loops with break, continue etc.Now I'll go thro' them once again.
Thank you.
Regards
krutika.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int i=4;
i=i++;
i=i++;
System.out.println(i);
[/CODE]
What gets printed?
Nope, 6 is wrong. The correct answer is 4.
Try and compile it, and you'll probably be surprised, but if you look at what I wrote above, it might make sence.
Again, you take the value of the variable (i in this instance) to be used, increment it and then do the asignment, with the value you first took.
/Mike[/B]


Mike, you are right. I even put another "i=i++;", and the printout is still 4. But i am confused theoretically.
int i=4;//1
i=i++;//2
i=i++;//3
My Q is regarding line 2:
//Only one i exists im memory, right? So after this statement (i is assigned 4 first) is excuted, i++ get excuted and 5 is produced. Where is i++(that is value 5), is it gone from memory. i think value 5 should overwrite 4.
THANKS IN ADVANCE.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am confused....
int i=4;
i=i++;
i=i++;
System.out.println(i);
please explain why this prints 4 and not 6....???
please explain in details....
please help any body
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic