What will be the output of the following Java code? [code] class test { int a; int b; test(int i, int

What will be the output of the following Java code?
Code:
    class test 
    {
        int a;
        int b;
        test(int i, int j) 
        {
            a = i;
            b = j;
        }
        void meth(test o) 
        {
            o.a *= 2;
            O.b /= 2;
        }          
    }    
    class Output 
    {
        public static void main(String args[])
        {
            test obj = new test(10 , 20);
            obj.meth(obj);
            System.out.println(obj.a + " " + obj.b);        
        } 
    }
A. 10 20
B. 20 10
C. 20 40
D. 40 20
 
Answer: b
Explanation: Class objects are always passed by reference, therefore changes done are reflected back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20 & 10 respectively.
output:
Code:
$ javac Output.java
$ java Output
20 10
This section of our 1000+ Java MCQs focuses on packages of Java Programming Language.
 

Tìm kiếm

Back
Top