Menu
Topics Index
...
`

Static


If you need explanation Read this topic

If you need Answer Take test on this topic

Static
What will be the output of the following program?
public class Delta
{
    static boolean Oka(char c)
    {
        System.out.print(c + " ");
        return true;
    }

    public static void main(String[] argv)
    {
        int i = 0;

        for (Oka('A'); Oka('B') && (i < 2); Oka('C'))
        {
            i++;
            Oka('D');
        }
    }
}

A. A B C D A B C D
B. A B D C B D C B
C. A B D C B D C
D. Compilation Error
E. Runtime Error
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 concept of the function calls..here we have oka() which takes the char as arg than returns the boolean value..
Execution:

in the main() we have the local variable i=0 than we started for loop  in the initialization part we invoke the oka() by passing the 'A' than this method is invoked and prints 'A' and than returns true than it will enter into the condition stage there we are invoking the oka() by passing the 'B' than it will enters into the method and prints 'B' next expression in the && i<2 is true so it will enters into the loop body and increase the i than it will become 1 than we are calling oka() with 'D' it will print 'D' next control will enters into the increment /  decreament part and invokes the oka() by passing the 'C' and prints the 'C'..again it goes to the condition stage and invokes the oka() with the B and prints B and 1<2 it is true than it will enters into the body and increase the i than it will become 2 and invokes the oka() with 'D' it will print D and next it will goes  increment /  decreament  and prints 'C' and goes to condition and invokes the oka() with the 'B' and prints 'B' and returns true and next expression is false so condition is false..comes out of the loop

Posted by Uday Kumar    2015-02-12 09:23:53


b

Posted by Shubham Bansal    2015-02-13 03:15:30


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Ammad Islam. 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-13 03:23:43


© meritcampus 2019

All Rights Reserved.

Open In App