Menu
Topics Index
...
`

Classes inside other class3004


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside other class3004
What will be the output of the following program?
public class GuessIt {
    public static void main(String[] args) {
        Guess g = new Guess();
        g.G();
        Guess.CantGuess.ICanGuess.Hai hai = null;
        hai.print();
    }
}
class Guess {
    public void G() {
        CantGuess cg = new CantGuess();
    }
    class CantGuess {
        public CantGuess() {
            System.out.print("CAB");
        }
        ICanGuess icg = new ICanGuess();
        GetICanGuess gicg = new GetICanGuess();
        class ICanGuess {
            public ICanGuess() {
                System.out.print("BAC");
            }
            Hai h = new Hai();
            public class Hai {
                public Hai() {
                    System.out.print("CBA");
                }
                public void print() {
                    System.out.print("print");
                }
            }
        }
        class GetICanGuess {
            public GetICanGuess() {
                System.out.print("ABC");
            }
        }
    }
}

A. CBABACABCCAB
B. CABCBABACABC
C. ABCBACCABCBA
D. Some other output
E. Compilation Error or Runtime Error
Topic: Nested And Inner Classes

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.

I'll go with Option E ... here in main() we have "hai" value initialized as null .. in the next step we have called "print()" method Using it("hai").. which throws Null Pointer Exception...

Posted by ?????????? ?????    2015-02-04 08:27:17


Ans is Option D... Execution starts from main() method after execution of first stmt it will create object of Guess class after that we are calling method G() inside that class  here inside G() method we are creating object of class CantGuess while creating object of class first it will initializes the member variable and then call constructor and of the particular class here the member variables of Class CantGuess are creating objects of classes ICanGuess and GetICanGuess first it will go to ICanGuess class there the member variable is creating Object of Class Hai so compiler will go to class Hai inside that no member variables then it will call constructor of class Hai inside this  we are printing String "CBA" then compiler will return to class ICanGuess and it will call GetICanGuess class inside this class no member variables and it will call constructor of class GetICanGuess inside this we are printing value "ABC" after that compiler calls the constructor of Class ICanGuess inside this we are printing "BAC" value then compiler will cal CantGuess Class inside this we are printing the value  CAB  after that compiler will go to main() method of Class GuessIt there we are creating reference variable of Class Hai and initializing with null value after that we are calling print() method here we will get RUNTIME Error which is nullpoint error bcz we are trying to call print() method with null value so the output is CBABACABCCAB
Exception in thread "main" java.lang.NullPointerException

Posted by Bhagi Bhagyasri    2015-02-04 09:35:24


e

Posted by Shubham Bansal    2015-02-05 04:07:59


This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-05 07:10:02


© meritcampus 2019

All Rights Reserved.

Open In App