Menu
Topics Index
...
`

Test on if


If you need explanation Read this topic

If you need Answer Take test on this topic

Test on if
What will be the output of the following program?
public class AllTheBest {
    public static void main(String[] str) {
        boolean result;
        int ary[] = {2, 4, 8};
        result = groupSum(0, ary, 10);
        System.out.print(result + ", ");
        result = groupSum(0, ary, 9);
        System.out.print(result + ", ");
        result = groupSum(0, ary, 14);
        System.out.println(result);
    }
    public static boolean groupSum(int start, int[] nums, int target) {
        if (start >= nums.length)
            return (target == 0);
        if (groupSum(start + 1, nums, target - nums[start]))
            return true;
        if (groupSum(start + 1, nums, target))
            return true;
        return false;
    }
}

A. true, false, false
B. true, false, true
C. true, true, true
D. false, false, false
E. Compilation Error or Runtime Error
Topic: Learn Arrays And Loops

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.

b

Posted by Goutham Singarapu    2015-02-03 04:01:51


Ans is B

Posted by Gopinath Manchikanti    2015-02-03 05:59:38


ANS IS B

Here Recursion is used till the base condition is satisfied.

--true,true,false will be the output.

Posted by Mânïshå Mùlchåndânï    2015-02-03 18:59:23


ans b. initially we have 1d array assigned with 3 elements, so length of the array is 3.Boolean result assigned with one function which pass 3 parameters 0 ,array and 10 .here this function called recursively upto target reached 0, here initially 0 +1,arr,10-2=8,t hn 1+1,arr ,8-arr[1]=8-4=4.,nw we have groupsum (2+1,arr,4-8),so target value not reached 0, tryin nxt if condition returns true.thn 2 nd function has 0 ,arr,9 parameters, here also function recursively called but target not reached to 0.bcz in array we have even numbers. so result is false,3 rd function here 2 nd if condition returns true,1st pass start 1,target 12.2nd pass Start 2 target 8,3rd pass start 3 target 0.so start>=array lenth,target also 0 so returns true.

Posted by Maheshwari Natarajan    2015-02-03 19:39:34


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Maheshwari Natarajan for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-02-04 04:27:19


© meritcampus 2019

All Rights Reserved.

Open In App