Menu
Topics Index
...
`

Creating object for class1759


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating object for class1759
What will be the output of the following program?
class Note {
    String n1 = "n1";
    String n2 = "n2";
}
public class Book extends Note {
    String n1 = "n3";
    public static void main(String args[]) {
        Book n = new Book();
        Note b = n;
        System.out.println(n.n1 + n.n2 + b.n1 + b.n2);
    }
}

A. n1n2n1n2
B. n3n2n3n2
C. n3n2n1n2
D. Some other output
E. Compilation Error or 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.

C is the answer.because n.n1 gives n3 and b.n1 gives n1 since b is assigned to n.

Posted by Rahul Roy    2013-09-12 03:43:47


E. Compilation Error or Runtime Error

Posted by Sagar Kamble    2013-09-12 03:46:47


E bcoz...there is no public class inside ' main'....

Posted by Bhavya Rishitha    2013-09-12 04:42:17


Answer is C..n.n1 = ne3, n.n2 = n2 and as b is reference of Note class assigned to n, b.n1=n1 and b.n2=n2.

Posted by Sindhu Madishetty    2013-09-12 06:44:34


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

Posted by Merit Campus    2013-09-13 09:24:34


© meritcampus 2019

All Rights Reserved.

Open In App