Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

very tricky

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What WAS the output when you ran it?
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
answer is option one.....but i don't understand how???
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/code]
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic