Menu
Topics Index
...
`

Objects and References188


If you need explanation Read this topic

If you need Answer Take test on this topic

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

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

        Bike pulsar = new Bike();
        pulsar.company = "Bajaj";
        pulsar.model = "Pulsar";
        pulsar.mileage = 55.2;
        pulsar.diskBrakes = true;

        System.out.println(pulsar.company + "'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 all bike objects should be created at one place.
B. Hero Honda's Shine gives a mileage of 72.5kmpl.
Bajaj's Pulsar gives a mileage of 55.2kmpl.
C. Compilation Error - since multiple bike objects can not be created.
D. Compilation Error - since diskBrakes member variable on shine bike is not 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.

i'll go with option b .. Program Execution Starts from main() .. instantiation of shine is correct and initialization of shine's member varialbles is also correct and at sysout option B's 1st line will be printed  (according to initalization) and instantiation of pulsar is also correct and initialization of pulsar's member varialbles is also correct   and at sysout option B's 2nd line will be printed  (according to initalization) .

Posted by ?????????? ?????    2015-01-09 05:52:27


Option B.....        when execution start it will go to main method there we have created object for Bike class then we initialized the member variables of the class Bike and then we are printing those member variables, and again we are creating object for Bike class that means it will create new object for the class Bike and again we are reinitializing member variables of class Bike  after we are printing that object's member variable.     we can create 'n' number of objects for a class , so Option B is correct

Posted by Bhagi Bhagyasri    2015-01-09 10:01:14


ANS IS B

Here concept of classes and objects is used.

--In above Example BikeTester class,Object of class "Bike" is used.Whenever Object is created,Memory is allocated for the attributes defined in that class whose object is created.

--"Shine" object of class "Bike" is created.Value is initianlized for three attributes:
1.company is initialized with hero honda
2.model is initialized with shine
3.mileage is initialized with 72.5

--Attributes value are printed with the object created whose name is shine.i.e "Hero Honda's shine gives mileage of 72.5kmpl."

--"pulsar" object of class "Bike" is created.Value is initianlized for four attributes:
1.company is initialized with bajaj
2.model is initialized with pulsar
3.mileage is initialized with 55.2
4.diskbrakes in initialized to "true"

--Attributes value are printed with the object created whose name is pulsar.i.e "Bajaj's Pulsar gives a mileage of 55.2 kmpl."

Posted by Mânïshå Mùlchåndânï    2015-01-09 15:36:26


b.

Posted by Shubham Bansal    2015-01-09 19:32:57


This dose is now closed and the winners are ?????????? ?????,  for 'First Correct Comment', Bhagi Bhagyasri, Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-12 05:05:17


© meritcampus 2019

All Rights Reserved.

Open In App