Topics Index

Objects and References190

If you need explanation Read this topic

If you need Answer Take test on this topic

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

        Bike pulsar = shine; = "Bajaj";
        pulsar.model = "Pulsar";
        pulsar.mileage = 55.2;
        pulsar.diskBrakes = true;
        System.out.println( + "'s " + shine.model + " gives a mileage of " + shine.mileage + "kmpl.");
        System.out.println( + "'s " + pulsar.model + " gives a mileage of " + pulsar.mileage + "kmpl.");

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

A. Compilation Error - since shine bike object can not be assigned to pulsar. 
B. Hero Honda's Shine gives a mileage of 72.5kmpl.
Bajaj's Pulsar gives a mileage of 55.2kmpl.
C. Compilation Error - since diskBrakes is not initialized for shine bike object. 
D. Bajaj's Pulsar gives a mileage of 55.2kmpl.
Bajaj's Pulsar gives a mileage of 55.2kmpl.
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.

a.compilation eror.since shine object is created n its val r assigned n des oobj cant b assgnd to pulsr..hnc it wil throu compile eror

Posted by Asma Mujtaba Khan    2014-08-11 04:43:02

Ans is D.
since shine bike object is assigned to pulsar. so both are pointing to same object. Hence the details will be updated in object and output will be "Bajaj's pulsar gives a mileage of 55.2kmpl" for both shine and pulsar referrences.

Posted by Gopinath Manchikanti    2014-08-11 04:46:25

ans b....shine obj is crtd fr bike clas n values rr assigned fr its frst sop stmtmt these assignd values r prntd as  hero hobda shine gvs a mileage of 72.5...n fr the nxt sop the values of bike pulsar r prntd as bajaj pulsar gvs a milege of 74..... hnce fr both shine n pulsar vlues r prntd

Posted by Wajahat Khan    2014-08-11 04:50:51

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

Posted by Merit Campus    2014-08-12 04:11:15

© meritcampus 2019

All Rights Reserved.

Open In App