Menu
Topics Index
...
`

Write code to print required output


If you need explanation Read this topic

If you need Answer Take test on this topic

Write code to print required output
Which option will be placed in 'Write code here' then, produce the output 'Hello World'?
public class Computer {
    class HardDisk {

        //Write code here.

    }
    public static void main(String args[]) {
        new Computer().display();
    }
    void display() {
        new HardDisk();
    }
    void memory() {
        System.out.println("Hello World");
    }
}

A. Computer.memory();
B. this.memory();
C. HardDisk()
{
    Computer.memory();
}
D. HardDisk()
{
    Computer.this.memory();
}
E. None of the above
Topic: Calling A Class From Another Class In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D is the correct answer because while creating the obj constructor is executed and c is wrong because not static memb cant be called with class name

Posted by Ram Reddy Bitla    2013-07-29 04:09:13


c

Posted by Prasanth Hero    2013-07-29 06:28:34


C is teh answer because control starts from main method where Computer,display() is called where in Harddisk constructor is called.

Posted by Sravanthi Kappuram    2013-07-29 12:00:35


So in harddisk ,constructor code is written. where the memory () of Computer shoud be called.
we are present ly in the HArddisk class so to name teh function of teh outer class we cal it wid teh name of the Computer class.

Hence ,Computer.memory(); shud be in teh constructor

Posted by Sravanthi Kappuram    2013-07-29 12:02:57


Congratulations Ram Reddy Bitla. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-07-29 13:34:26


Thanks to merit campus I got recharge

Posted by Ram Reddy Bitla    2013-07-30 05:07:06


© meritcampus 2019

All Rights Reserved.

Open In App