Topics Index


If you need explanation Read this topic

If you need Answer Take test on this topic

What will be the output of the following program?
public class iProgram {

    public static void main(String[] iArgs) {
        iPad iPad = new iPad();
        iPad.iOS = 6;
        iPhone iPhone = new iPhone();
        System.out.print(iPad.iOS + "->");
        iPhone.iPad = iPad;
        System.out.print(iPad.iOS + "->");
        iPhone.iPad = new iPad();
        System.out.print(iPad.iOS + "->" + iPhone.iPad.iOS);

class iPad {

    int iOS = 0;

class iPhone {

    iPad iPad;

    void release() {

A. 6->7->1->1
B. 6->7->7->1
C. 6->6->6->6
D. Some other output or Compilation Error or Runtime Error
Topic: Advanced Java Programming Concepts

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: D In Class Iphone  "ipad.ios++"obj for iPad is not we cannot refer the ios++ variable.

Posted by Indrasena Reddy    2014-05-27 08:40:04

Ans is B. First time iOS value of iPhone object is printed as 6 and by invoking release() method the value of iOS will change to 7. Next iOS value of iPad object created in main method will remain same and value of iOS in iPad object in iPhone class will change i.e., become 1 because of release() method.

Posted by Nikhil Sulibhavi    2014-05-27 08:55:19

compilation error. option d is correct.. as we cannot use variables of a clas without initializing that clas or creating clas variable

Posted by Vijay Kumar    2014-05-27 08:57:54

Answer : B
> we get 6 as we are defining the ios variable after creating an object for the class iPad
> we get 7 becoz of the method release() in the class iPhone
>Then we create an object again for the iPad class which initiates the ios=0 then we access the release() in iPhone clas which makes the ios value 1

Posted by Deepak Kumar Yadav    2014-05-27 09:40:54

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

Posted by Merit Campus    2014-05-28 14:59:21

© meritcampus 2019

All Rights Reserved.

Open In App