This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes very tricky Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "very tricky" Watch "very tricky" New topic
Author

very tricky

maggie joseph
Ranch Hand

Joined: Dec 29, 2009
Posts: 185
What is the output ?

public class Toast extends Boast
{
Integer eye=1;
public static void main(String... args)
{
Toast eye = new Toast();
new Devil(eye).honey(eye);
new Boast(eye).honey(eye);
new Toast().honey(eye);
}
public void honey(Toast eye)
{
eye.eye = new Integer(-2);
}

}

class Boast extends Devil
{
public Boast(){System.out.print("boast ");}
public Boast(Toast eye)
{
eye.eye = new Integer(5);
}

public void honey(Toast eye)
{
eye.eye = new Integer(2);
}
}
class Devil
{
public Devil(){System.out.print("devil ");}
public Devil(Toast eye)
{
eye.eye = new Integer(1);
}
public void honey(Toast eye)
{
eye.eye = new Integer(9);
}
}
Choose: 1

Options

1.) devil boast devil devil boast

2.) devil boast devil boast devil boast devil boast

3.) devil boast devil devil boast devil boast

4.) Runtime error

5.) None of the above
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9952
    
    6

What WAS the output when you ran it?


Never ascribe to malice that which can be adequately explained by stupidity.
maggie joseph
Ranch Hand

Joined: Dec 29, 2009
Posts: 185
answer is option one.....but i don't understand how???
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
curve karve wrote:answer is option one.....but i don't understand how???


What do you think the output should be, and why?

EDIT: Fixed a spelling error. No need to panic!


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Can you please use code tags and NOT quote tags when writing a java program.
Also the best way to learn about what the output should be is to run the program in "Debug" mode and follow the execution.


SCJP 1.6 96%
maggie joseph
Ranch Hand

Joined: Dec 29, 2009
Posts: 185
[/code]
maggie joseph
Ranch Hand

Joined: Dec 29, 2009
Posts: 185
i understood it...first when devil(eye is called)its goes to Devil no args constructor...then while coming down it goes to Boast no args constructor.....again on next line since boast with args constructor is called it will go to Devil no args constructor by default...and print devil again...again when toast is called it will call its superclass constructors in order of heirarchy...i hope i am right....
Lokesh Varma
Greenhorn

Joined: Dec 30, 2009
Posts: 3
Toast eye = new Toast();
new Devil(eye).honey(eye);
new Boast(eye).honey(eye);
new Toast().honey(eye);

Above three lines produces the output.

Toast eye = new Toast(); Toast default constructor calls Boast no-arg constructor. Boast constructor calls Devil's no-arg constructor. No parent class to Devil so its no-arg constructor prints "devil" then Boast no-arg constructor prints "boast".

same exlanation : new Devil(eye).honey(eye); doe not print any thing.

new Boast(eye).honey(eye); prints "devil"

new Toast().honey(eye); prints "devil", "boast".....hope this helps
Lotary Li
Greenhorn

Joined: Dec 21, 2009
Posts: 5
new Toast(); <- this will produce the output: devil boast

Toast will have a default constructor: public Toast(){} -> this will call the constructor of Boast() and constructor of Devil();

new Devil(eye).honey(eye); <- doesn't produce anything
new Boast(eye).honey(eye); <- Boast(Toast eye){} constructor will call Devil() and produce: devil
new Toast().honey(eye);<- this will produce the output: devil boast


SCJP 6
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: very tricky
 
Similar Threads
Problem form CertPal
overloading
Flow of the program in Constructors
Doubt on constructors
Constructor's call