Topics Index

Accessing member variables2807

If you need explanation Read this topic

If you need Answer Take test on this topic

Accessing member variables2807
What will be the output of the following program?
class A {
    public static void main(String arg[]) {
        B a = new B();
        B b = new B();
        B c = new B();
        B d = new B();
        System.out.println("i = " + a.i);
        System.out.println("d = " + b.d);
        System.out.println("c = " + c.c);
        System.out.println("s = " + d.s);
class B {
    int i;
    double d;
    char c;
    String s;

A. i = 0
d = 0.0
c = 
s = null
B. Some other output
C. Output can not determined
D. Compilation Error or 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.

Ans is we have the concept of the instance we have the class b with the i,d,c,s as the properties in it..when we create the instance for the classB than the memory is allocated for these instance variables..and if we assign any values in the constructor than the values get filled if we did nt write any code than the JVM will assign the default values to the instance variables for int 0 double 0.0 for char it is empty and for string it is null..
in the main() we create the 4 instance for the class B and we print the it will print default values.. as i said

Posted by Uday Kumar    2015-02-23 03:43:05


Here concept of default values of data types is used.

--class B is defined with int, double, character or string variables.

--In main() method, following four objects of class B are created.

--After that these objects are used to display the variables of class B.

As the variables of class B are not defined, so their default values will be used.

Integer default value --> 0
Double default value --> 0.0
Character default value --> blank space
String default value --> null



Posted by Mânïshå Mùlchåndânï    2015-02-23 11:26:24

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Mânïshå Mùlchåndânï,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Goutham Singarapu. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-24 03:52:47

© meritcampus 2019

All Rights Reserved.

Open In App