Topics Index

Motors And Prices2247

If you need explanation Read this topic

If you need Answer Take test on this topic

Motors And Prices2247
What will be the output of the following program?
public class MotorsAndPrices {
    public static void main(String[] args) {
        Motor m = new Motor();
        WaterMotor w = new WaterMotor();
        m.manufacturer = "Kirslokar";
        m.price = 15_234_0;
        w.manufacturer = "Godavari";
        w.price = 1_52_340;
        p(m.manufacturer, m.price);
        p(w.manufacturer, w.price);
        m = w;
        p(m.manufacturer, m.price);
        m.manufacturer = "General Motors";
        p(w.manufacturer, w.price);
    private static void p(String manufacturer, double price) {
        System.out.print(manufacturer + "#" + price + "#");
class Motor {
    String manufacturer;
    double price;
class WaterMotor extends Motor {}

A. Kirslokar#152340.0#Godavari#152340.0#Godavari#152340.0#General Motors#152340.0#
B. Kirslokar#152340.0#Godavari#152340.0#null#0.0#Godavari#152340.0#
C. Kirslokar#1523.4#Godavari#1523.4#null#0.0#Godavari#1523.4#
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Class Inheritance

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:A it takes double 15_234_10  prints "152340.0"  4 times p(-,-) method will be called

Posted by Indrasena Reddy    2014-03-31 09:32:32

Ans is A..

1st time p() ni call chesinapudu "kirslokar" n 152340 n print ithundhi
2nd tim chesinapudu "Godavari" n 152340 velthndhi...n print ithundhi.
copying refrnces Parent class can Child clas Obj

now "Godavari" n 152340 velthayi..

w.manufacrure loki "Genaral motors" set chesthnam so nxt tim this one n 152340 wl go n print

Posted by Cherry Teegala    2014-03-31 09:35:51

Answer is A. From Java SE 7 onwards, we can have underscore characters anywhere between digits in a numeric literal. This improves readability of code by separating groups of digits in numeric literals. First two calls to p() output corresponding values of data members of objects Motor and WaterMotor. When we do m = w, we are copying object references. So the variables m and w both point to the same object. Third call to p() outputs corresponding values of WaterMotor object (we are accessing them using parent class reference). Then the instance variable manufacturer has been modified using parent class reference. So the next call to p() outputs the modified value of manufacturer and the previous price value of WaterMotor object.

Posted by Shaileshwar Sharma    2014-03-31 12:58:08

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

Posted by Merit Campus    2014-04-01 05:23:53

© meritcampus 2019

All Rights Reserved.

Open In App