Menu
Topics Index
...
`

Classes inside other class


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside other class
What will be the output of the following program?
class Desktop extends TSystem {
    public String getString() {
        Laptop l = new Laptop();
        str += l.getString();
        return str + "Hello";
    }
}

class Laptop extends TSystem {
    public String getString() {
        return "Welcome";
    }
}

public class TSystem {
    static String str = "";
    public String getString() {
        Desktop d = new Desktop();
        str += d.getString();
        return str + "Hai";
    }
    public static void main(String[] args) {
        TSystem ts = new TSystem();
        str += ts.getString();
        System.out.println(str);
    }
}

A. HaiHelloWelcome
B. WelcomeHelloHai
C. HelloHaiWelcome
D. HaiWelcomeHello
E. Some other output
F. Compilation Error or Runtime Error
Topic: Static Keyword 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.

B is the answer.hellowelcomehai.initially on object creation of ts internally str=hello inturns it calls laptop containing welcome and laptop inturns appends hai..strings are appended as str+; so B is answer

Posted by Shabaaz Shaikz    2013-11-19 12:39:16


answer is A ts.getString() method returns hai and calls d.getstring() wch returns hello+str and calls l.getstring() it adds welcome to str.

Posted by Shiva Goud    2013-11-19 12:40:46


Answer is B
Flow as follows TSystem.getString() to Desktop.getString() to Laptop.getString() which returns "Welcome" (in Laptop) and appended with "Hello" (in Desktop) then appended with "Hai" (in TSystem) as it is static variable any string operations are affected with "str" object would reflect in all inherited classes

Posted by Jayachandra Gupta    2013-11-19 12:44:24


answer is 'E' because in return statement we returned we concatinated str with hello and hai so the output would be WelcomeWelcomeHelloWelomeWelcomeHelloHai

Posted by Sai Teja    2013-11-19 12:55:24


Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-20 04:07:17


© meritcampus 2019

All Rights Reserved.

Open In App