Menu
Topics Index
...
`

Classes887


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes887
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 = " + c.c);
    }
}

class PrintA
{
    int printA;
}

class PrintB
{
    int printB = 5;
    int c;
}

A. printA = 0
printB = 5
c = 0
B. printA = 0
printB = 5
c =(some object reference value)
C. Compilation Error
D. Runtime Error
Topic: Member Variable 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.

option A

Posted by Yashasvi Bandi    2014-04-14 08:47:58


ans is A,since default of value of int variable(instance) is 0

Posted by Raviteja Daggupati    2014-04-14 08:49:04


A is answer. as only B is assigned with value 5.the rest gets 0.

Posted by Shabaaz Shaikz    2014-04-14 08:51:03


Ans is A.. first object is created and called the variable of the class.. but default of local varuable is zero..

Posted by Om Prakash Om    2014-04-14 08:56:46


Option is A
a is refernce of object of class PrintA
c refers to object of class PrintB

PrintA class has printA as instance varibale
so default values of printA=0;

PrintB class has printB and c as instance variables
c=0 by default
printB=5 as user specified it.

so a.printA will print the instance variable printA of class PrintA printing in "printA=0"

c.printB will print the instance variable printB of class PrintB printing in "printB=5"

c.c will print the instance variable c of class PrintB printing in c=0;

Posted by Kittu Manoj    2014-04-14 08:57:36


and is c bcz variables should be initialzed only inside constructor

Posted by Pooja Singh    2014-04-14 10:35:16


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

Posted by Merit Campus    2014-04-15 03:56:28


© meritcampus 2019

All Rights Reserved.

Open In App