Menu
Topics Index
...
`

Classes911


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes911
What will be the output of the following program?
class OutPut
{
    int c;

    public static void main(String[] args)
    {
        PrintA a = new PrintA();
        PrintB c = new PrintB();

        System.out.println("printA = " + a.printA);
        System.out.println("printB = " + c.printB);

        System.out.println("c = " + new OutPut().c);
    }
}

class PrintA
{
    int printA;
}

class PrintB
{
    int printB = 5;
}

A. printA = 0
printB = 5
c = (some object reference value)
B. printA = 0
printB = 5
c = 0
C. Compilation Error
D. Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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.becoz default value of printA and c is printed which is zero and printB is given 5.so answer is b

Posted by Abhishek Shankhadhar    2014-12-25 09:06:40


ans b . in this program we have 3 classes, in main class we declared int c ,it is a non static member variable. thn objects for printA,ptintB classes get created.1st sop ,it access PrintA class variable and printd.here 0 get printd bcz default int value is 0,2 nd sop prints 5 ,in class ptintB we initialized prubtB with 5 .3rd sop access non static c value from output class so default int value get printed. bcz we are not initialized c variable.

Posted by Maheshwari Natarajan    2014-12-25 11:14:25


ANS IS A

Objects of two classes i.e. printA and printBare created.

printA holds one integer printA which is not initialized so when it is printed it's default value which is zero will get printed.
--PRINTA=0

printB holds one integer printB which is not initialized to 5,when it is printed 5 will get printed.
--PRINTB=5

Ananymous object of OUTPUT class is created whose variable c is printed which is not iniialized to any value so zero will get printed
--c=0.

Posted by Mânïshå Mùlchåndânï    2014-12-25 15:42:57


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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    2014-12-26 02:39:38


© meritcampus 2019

All Rights Reserved.

Open In App