Menu
Topics Index
...
`

Classes830


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes830
What will be the output of the following program?
class ReferencesAndObjects
{
    public static void main(String s[])
    {
        Student st1 = new Student();
        Student st2 = new Student();
        st1 = st2;

        st1.name = "Rajesh";
        st2.marks = 87;
        st1.section = 'C';
        
        System.out.println("Print using st1 : " + st1.name + " " + st1.marks + " " + st1.section);

        System.out.println("Print using st2 : " + st2.name + " " + st2.marks + " " + st2.section);

    }
}

class Student
{
    String name;
    int marks;
    char section;
}

A. Print using st1 : Rajesh 0 C
Print using st2 : null 87
B. Print using st1 : Rajesh 87 C
Print using st2 : Rajesh 87 C
C. Print using st1 : null 0 
Print using st2 : Rajesh 87 C
D. Compilation Error
E. 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.

answer is B , because st1 and st2 both are pointing to st2.

Posted by Ashok Kumar Paritala    2014-08-28 04:20:52


Ans B, the  second constructor reference is accessed by first constructor reference st1, so the marks of st2 is initiated to st1 marks , and as well as st2 is acess the st1properties..

Posted by Damu Surya    2014-08-28 04:26:27


b.few values of st1 n few of st 2 r initialised.wich r not initialsd dey r left

Posted by Shamee Shamee    2014-08-28 04:31:49


B ,,, references of st1 and st2 are same,,,

Posted by Raju VarshnEy    2014-08-28 04:46:16


b.

Posted by Shubham Bansal    2014-08-28 07:41:33


Congratulations Ashok Kumar Paritala. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-09-01 06:00:28


© meritcampus 2019

All Rights Reserved.

Open In App