Menu
Topics Index
...
`

Final Person Class


If you need explanation Read this topic

If you need Answer Take test on this topic

Final Person Class
What will be the output of the following program?
public final class FinalPersonClass {
    private final String name;
    private final int age;
    public FinalPersonClass(final String name, final int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public int getAge() {    return age;    }

    public String getName() {    return name;    }

    public static void main(String[] args) {
        FinalPersonClass fpc = new FinalPersonClass("Oleti", 24);
        FinalPersonClass fc = new FinalPersonClass("Kiran", 25);
        System.out.println(fpc.name + " - " + fpc.age);
        System.out.println(fc.getName() + " - " + fc.getAge());
    }
}

A. Kiran - 25
Kiran - 25
B. Oleti - 24
Oleti - 24
C. Oleti - 24
Kiran - 25
D. Some other output
E. Compilation Error or Runtime Error
Topic: final Keyword 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 C. as we are creating two different objects for the same class, the value for the corresponding objects will be different

Posted by Nikhil Sulibhavi    2014-06-02 08:45:12


Answer : C
->the first object has string name as oleti and age as 24
-> the second object has string name as kiran and age as 25 which is inturn returned by the getName() abd getAge()
->in both the cases we explicitly call the constructor and set these values

Posted by Deepak Kumar Yadav    2014-06-02 08:49:59


Ans:C First sop prints "Oleti-24" and second sop prints "kiran-25"

Posted by Indrasena Reddy    2014-06-02 08:50:23


Ans is C
the first object has string name as oleti and age as 24,the second object has string name as kiran and age as 25 which is inturn returned by the getName() abd getAge(),in both the cases we explicitly call the constructor and set these values

Posted by Naveen Bala Madhanu    2014-06-02 08:53:36


lol...@Madhanu Naveen Bala u copied my answer :p -_-

Posted by Deepak Kumar Yadav    2014-06-02 09:03:25


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

Posted by Merit Campus    2014-06-03 04:33:10


© meritcampus 2019

All Rights Reserved.

Open In App