Topics Index

Objects and References192

If you need explanation Read this topic

If you need Answer Take test on this topic

Objects and References192
What will be the output of the following program?
class BikeTester
    public static void main(String s[])
        Bike bike1 = new Bike(); = "Hero Honda";
        bike1.model = "Splendor";
        bike1.mileage = 54.8;

        Bike bike2 = new Bike(); = "Yamaha";
        bike2.model = "RX100";
        bike2.mileage = 65.0;

        bike2 = bike1;
        bike1 = bike2;

        System.out.println( + "'s " + bike1.model + " gives a mileage of " + bike1.mileage + "kmpl.");
        System.out.println( + "'s " + bike2.model + " gives a mileage of " + bike2.mileage + "kmpl.");

class Bike
    String company;
    String model;
    int cc;
    double mileage;
    boolean diskBrakes;

A. Hero Honda's Splendor gives a mileage of 54.8kmpl.
Yamaha's RX100 gives a mileage of 65.0kmpl.
B. Hero Honda's Splendor gives a mileage of 54.8kmpl.
Hero Honda's Splendor gives a mileage of 54.8kmpl.
C. Yamaha's RX100 gives a mileage of 65.0kmpl.
Yamaha's RX100 gives a mileage of 65.0kmpl.
D. Compilation Error - since the value of reference can not be changed 
after it is initialized.
Topic: Class References And Objects 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 B both referneces pointing the bike1 i.e herohonda object

Posted by Vicky Maduri    2014-09-01 12:43:28

ans is B ,, first bike2 points to bike 1 then both r pointing same location as bike 1

Posted by Raju VarshnEy    2014-09-01 12:56:58

ans:B becuse while assigning bike1 value to be assigned first to bike bike2 and bike 1 both will referred to bike1 so after then u assigned bike2(allready it is bike1) to

Posted by Sapparapu Pradeep Kumar    2014-09-01 13:48:57

answer is B;

Posted by Sapparapu Pradeep Kumar    2014-09-01 13:49:25

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

Posted by Merit Campus    2014-09-02 04:55:54

© meritcampus 2019

All Rights Reserved.

Open In App