Topics Index

Objects and References

If you need explanation Read this topic

If you need Answer Take test on this topic

Objects and References
What will be the output of the following program.
class BikeTester
    public static void main(String s[])
        Bike bike; = "Hero Honda";
        bike.model = "Shine"; = 125;
        bike.mileage = 72.5;
        bike.diskBrakes = false;

        System.out.println( + "'s " + bike.model + " gives a mileage of " + bike.mileage + "kmpl.");
class Bike
    String company;
    String model;
    int cc;
    double mileage;
    boolean diskBrakes;

A. Hero Honda's Shine gives a mileage of 72.5kmpl.
B. Compilation error - since Bike class has two Strings - company and model.
C. Runtime Error - NullPointerException since the bike object is not created.
D. Compilation error - since Bike class does not have a constructor.
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.

Ans c..we didnt create any Obj to Bike class..jst we create obly refernc to it

Posted by Uday Kumar    2014-04-08 08:51:06

Ans is D as object is not crated to bike cls in BikeTester class

Posted by Bharath Yelchuri    2014-04-08 09:34:23

Compilation does not take place because at least one public class is required in main file..if BikeTester class is declared as public then it will be a compilation error of not creating the object of Bike class...

Posted by Sneha Rapool    2014-04-08 11:23:16

At line 5, we have only declared a variable (bike) of type Bike. But declaring a variable does not create an object. The new operator instantiates a new object by allocating memory for it. The instance variables and methods of the object could then be accessed through the reference. In this case, there is no object and hence no instance variables to refer to. So the compiler throws an error when we try to access instance variables (starting at line 6) of Bike class indicating that the variable bike has not been initialized.

Posted by Shaileshwar Sharma    2014-04-08 12:26:21

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

Posted by Merit Campus    2014-04-09 03:59:09

© meritcampus 2019

All Rights Reserved.

Open In App