Menu
Topics Index
...
`

Output of Method Calls


If you need explanation Read this topic

If you need Answer Take test on this topic

Output of Method Calls
What will be the output of the following program?
class MethodCalls
{
    public static void main(String[] args)
    {
        a1(8);
    }

    static int a1(int a1)
    {
        System.out.print(" a1 = " + a1);
        return a2(a1++);
    }

    static int a2(int a2)
    {
        if (a2 == 0)
        {
            return 10;
        }
        System.out.print(" a2 = " + a2);
        return a1(a2 / 2);
    }
}

A.  a1 = 8 a2 = 8 a1 = 4 a2 = 4 a1 = 2 a2 = 2 a1 = 1 a2 = 1 a1 = 0 a2 = 0
B.  a1 = 8 a2 = 8 a1 = 4 a2 = 4 a1 = 2 a2 = 2 a1 = 1 a2 = 1 a1 = 0
C.  a1 = 1 a2 = 1 a1 = 2 a2 = 2 a1 = 4 a2 = 4 a1 = 8 a2 = 8
D. Compilation Error
E. Goes into infinite loop
Topic: Static and Non Static Variables - Static and Non Static Methods

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 is B..here we have the 2 methods a1() and a2()..both are static methods so that we can call those 2 methods without creating the objects..
here we are calling the a1() by passing the 8 so 8 get printed in the and we are calling a2() by passing a1++ means here it will send 8 only Bcz we are using post increment..
in a2 we are checking if received value is 0 than we are returning the 10 else we are printing it so here alos 8 get printed and next we are calling a1() by passing a2/2=4..

there we are printing the 4 and sending 4 to a2() here we are printing 4 and sending 2 to a1() there we are printing 2 and passing to a2() here we are printing next 1 will send ..1 printed both functions and 0 will sent to the a1() these it will print 0 and pass 0 to a2() here condtion is true so function call will terminate..
execution:

a1(8)--print 8 and call a2(8)
a2() print 8 and call a1(8/2)
a1()print 4 and call a2(4)
a2() print 4 and call a1(4/2)
a1() print 2 and call a2(2)
a2() print 2 and call a1(1)
a1() print 1 and call a2(1)
a2() print 1 and call a1(0)
a1() print 0 and call a2(0)
if condition is true so returned

Posted by Uday Kumar    2014-12-10 12:45:31


B

Posted by Valli Prince    2014-12-10 12:54:34


Answer is B

Posted by Dereje Goshu    2014-12-10 12:59:31


ans b

Java is a Object Oriented Programming(OOP) language, which means we need an object to access any method or variable inside or outside the class. However there are some special cases where we don’t need any object (or instance).  in order to access static methods we don’t need any object.

It can be directly called in a program or by using class name.

A non-static method is always be called by using the object of class

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static .Otherwise, it should be non-static.

coming to this program here we a1() ,a2() static methods.
in main a1() called value 8 is passed as an argument.
here sop prints 8 ,and this method returns a1++=8 ,we passing 8 to a2(),

if condition checks a2=10 or not,conditin failed thn sop prints 8,thn return a1() called with 8/2=4 arg.

thn a1's sop prints 4, return a2(4) bcz a1++=4.
a2's sop prints 4.returns a1(4/2)=a1(2).

a1's sop prints 2
returns a2(2)

a2's sop prints 2
returns a1(1).
if condition  faild.

a1'sop prints 1.
returns a2(1)

a2's sop prints 1
returns a1(0)

nw a1's sop prints 0
returns a2(0)

here if condition passed method returns 10
but this value not get stored n printd.

Posted by Maheshwari Natarajan    2014-12-10 13:11:59


b

Posted by Shubham Bansal    2014-12-10 15:46:38


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. 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-12-11 05:42:09


© meritcampus 2019

All Rights Reserved.

Open In App