Menu
Topics Index
...
`

Multiple constructors for Bike class


If you need explanation Read this topic

If you need Answer Take test on this topic

Multiple constructors for Bike class
What will be the output of the following program.
class BikeTester
{
    public static void main(String s[])
    {
        Bike bike = new Bike("Hero Honda", "Shine", 125);
        bike.mileage = 72.5;
        bike.diskBrakes = false;

        System.out.println(bike.company + "'s " + bike.model + " has " + bike.cc + "cc power.");
    }
}

class Bike
{
    Bike(String companyParam, String modelParam, int ccParam)
    {
        company = companyParam;
        model = modelParam;
        cc = ccParam;
    }

    Bike()
    {
    }

    String company;
    String model;
    int cc;
    double mileage;
    boolean diskBrakes;
}

A. Compilation Error - since we can not have multiple constructors in a class.
B. Compilation Error - since we can not define a constructor with no parameters.
 Every constructor should have at least one parameter.
C. Hero Honda's Shine has 125cc power.
D. Compilation Error - since the default constructor has to be 
defined above the other constructors.
Topic: Class With Multiple Constructors 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..here we have the Bike class with the following properties company,model,cc,mileage,diskbreaks and has 2 constructors one is default arg and another is which takes 3 arg in this constructor we are assigning the received values to the instance variables..
Execution:
in the main() we create the instance for the   and passed the arg as "Hero Honda" and "Shine" 125 and the values is assigned to the company.model,cc next using the instance we are assigning values to the mileage 72.5 and diskBreaks as false and next we are printing the company and model and cc using the instance so output is  Hero Honda's Shine has 125cc power

Posted by Uday Kumar    2015-02-13 03:41:19


c

Posted by Shubham Bansal    2015-02-13 19:23:49


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. 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-02-16 04:44:07


© meritcampus 2019

All Rights Reserved.

Open In App