Menu
Topics Index
...
`

Mobile Test Output


If you need explanation Read this topic

If you need Answer Take test on this topic

Mobile Test Output
What will be the output of the following program?
class Mobile
{
    String model, maker;
    int numberOfSims;
    double memory;

    Mobile(String model, String maker, int numberOfSims, double memory) {
        this.model = model;
        this.maker = maker;
        this.numberOfSims = numberOfSims;
        this.memory = memory;
    }

    public static void main(String[] args)
    {
        Mobile m = new Mobile("Samsumg", "Wave 525", 1, 2048.0);
        Mobile n = new Mobile("Nokia", "Lumia", 2, 25000.0);
        m.print(m);
        n.print(n);
        Mobile t = n;
        n = m;
        m = t;
        m.print(n);
        n.print(n);
    }

    public void print(Mobile m)
    {
        System.out.println(model + " " + maker + " " + numberOfSims + " " + memory);
    }
}

A. Samsumg Wave 525 1 2048.0
Nokia Lumia 2 25000.0
Samsumg Wave 525 1 2048.0
Samsumg Wave 525 1 2048.0
B. Samsumg Wave 525 1 2048.0
Nokia Lumia 2 25000.0
Nokia Lumia 2 25000.0
Nokia Lumia 2 25000.0
C. Samsumg Wave 525 1 2048.0
Nokia Lumia 2 25000.0
Nokia Lumia 2 25000.0
Samsumg Wave 525 1 2048.0
D. Compilation Error
E. Runtime Error
Topic: Java Multiple Methods In One Class

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.

Answer is C . 1st and 2nd print function will print the values of the variables corresponding to  objects m and n .next references are swapped so values printed using reference 'm' will be of values for object 'n'  and vice versa

Posted by Shashanka Mogaliraju    2014-10-14 08:37:47


Ans is C..
in class Mobile we have parameterized constructor which taking 4 arguments
Constructor is an special method which wil automatically invoked wen u object for the class,mainly constructor wil used for the Initilization purpose

coming to the main()
here we create Object for the mobile as m by passing the arguments "samsung" "wave", 1, 2048.0
than it wil be like this
m---> model="samsung";
    maker=wave525;
    numberOfsims=1
    memory=2048;
we are creating another Object
n---> model="nokia";
    maker=lumia;
    numberOfsims=s
    memory=25000;
in constructor we are assiging the values to instance varible..here this is used to differentiate between the instance varibles and local varibles...this refers current Object
this.model means varible model in calling obejct
next we are creating reference for the Mobile class t
t=n means the object which n pointing is pointed by t also
t---> model="nokia";
    maker=lumia;
    numberOfsims=s
    memory=25000;
n=m..n wil point the object which m is pointg
n---> model="samsung";
    maker=wave525;
    numberOfsims=1
    memory=2048;
m=t means m wil point the whcih t is pointing
m---> model="nokia";
    maker=lumia;
    numberOfsims=s
    memory=25000;

by callig print method we are printing the details

Posted by Uday Kumar    2014-10-14 08:41:33


C is ans, as refrences gets swapped.

Posted by Shabaaz Shaikz    2014-10-14 10:44:05


c.

Posted by Shubham Bansal    2014-10-14 11:11:14


c

Posted by Jayakrishna Nani    2014-10-14 12:54:45


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Guran Cosmin-costel. 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    2014-10-15 03:41:08


© meritcampus 2019

All Rights Reserved.

Open In App