Menu
Topics Index
...
`

Crating class with more methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Crating class with more methods
What is the output of the following program?
public class Output {
    public static void main(String s[]) {
        Output o = new Output();
        o.string();
    }
    void string() {
        Output1 t = new Output1();
        System.out.print(t.x);
        Output1 t2 = fix(t);
        System.out.println(" " + t.x + " " + t2.x);
    }
    Output1 fix(Output1 tt) {
        tt.x += 42;
        return tt;
    }
}
class Output1 {
    byte x = 42;
}

A. 42 42 42
B. 42 42 84
C. 42 84 42
D. 42 84 84
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.

D is the correct answer because first we are creating the object of Output1 and printing x so it prints 42 after that we are addind 42 to it so now both t and t2 are pointing to same object and prints 84 84

Posted by Ram Reddy Bitla    2013-08-19 03:42:10


B is answer we give t.x vals 2 times and then the modified tt.x in the end

Posted by Karteek Paruchuri    2013-08-19 03:42:19


OPTION B 42 42 84 we just pass the object t....but it will not be modified so ans is 42 42 84

Posted by Datta Sai Krishna    2013-08-19 03:44:15


answer:D,as first string method is invoked by the object o and it prints 42,later fix is invoked within string method and it prints 84 and later the method returns back and again executes fix

Posted by Krishna Chaitanya    2013-08-19 03:48:40


Congratulations Ram Reddy Bitla. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-08-20 03:53:04


got recharge thenks MC

Posted by Ram Reddy Bitla    2013-08-22 04:10:19


© meritcampus 2019

All Rights Reserved.

Open In App