Menu
Topics Index
...
`

Phone Rings


If you need explanation Read this topic

If you need Answer Take test on this topic

Phone Rings
What will be the output of the following program?
public class PhoneRings {

    public static void main(String[] args) {
        System.out.print(call(53) + " " + call(25));
    }

    public static int call(int number) {
        System.out.print(number + ":");
        if (number % 7 == 0) {
            return 77;
        }
        if (number % 6 == 0) {
            return 66;
        }
        return call(number - 1);
    }
}

A. 53:52:51:50:49:25:24:77 66
B. 53:52:51:50:49:77 25:24:66
C. Some other output or Compilation Error
D. Throws StackOverflowError
Topic: Recursive In Java

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 (a) part...

Posted by Anuj Batham    2015-03-12 14:10:18


Answer is B....
call(53) is executed first
Prints 53 and checks for if conditions which checks if number is divisible by 7 or 6, if true returns 77 or 66.Else call the method recursively by subtracting the number by 1.
This flow repeats until the number is 49 which is divisible by 7 and therefore it returns 77 after printing 53:52:51:50:49:
call(53) in System.out.print prints 53:52:51:50:49:77
Similarly call(25) prints 25:24:66
So the final output is 53:52:51:50:49:77 25:24:66

Posted by Shashanka Mogaliraju    2015-03-12 14:54:59


ANS is B

Here the concept of recursion is used.

--call() method is defined in which we check the divisibility of number by 7 and 6 and return 77 and 66 respectively.

--First,53 is passed to call () the method as it is not divisible by 7 and 6 so again call method is called by subtracting 1 from number I.e. 52 and it continues till it is divisible by 7 or 6.

So,
53:52:51:50:49:77 as 49 is divisible by 7 so 77 is returned.

--Again,call() method is called by passing 25.

Again it is neither divisible by 7 or 6 so again call() method is called by subtracting 1 from number.

So,
25:24:66 as 24 is divisible by 6 so 66 is returned.

OUTPUT:
53:52:51:50:49:77 25:24:66

Posted by Mânïshå Mùlchåndânï    2015-03-12 17:32:23


This dose is now closed and the winners are Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is ???? ???? ???????. 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    2015-03-13 01:43:27


© meritcampus 2019

All Rights Reserved.

Open In App