Topics Index

Truth Always Triumphs

If you need explanation Read this topic

If you need Answer Take test on this topic

Truth Always Triumphs
What will be the output of the following program?
public class TruthAlwaysTriumphs {

    public static void main(String[] args) {

    public static void print(int i) {
        boolean result = true;
        switch (i) {
            case 0 :
                result &= (5 > 7);
            case 1 :
                result = result || 7 > i++;
            case 2 :
                result = result ^ result;
            default :
                result = false;
        System.out.print(result ? "(F)" : "(T)" + i);

A. (T)1(T)1(T)2(T)-1(T)3
B. (T)0(T)1(T)2(T)-1(T)3
C. (F)0(F)1(F)2(F)-1(F)3
D. Some other output or Compilation Error
Topic: Java Methods - Parameter Passing And Scope

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. for the first print() call, result is initially true and i is 0. after first case in switch i.e., case 0 result wil become false (since true and false is false (5 > 7 gives false)). Since there is break statement it will continue with second case where i is incremented to 1 and result is true. after that for next case the result is false so (T) is printed first time and so on.

Posted by Nikhil Sulibhavi    2014-05-27 12:49:33

Answer is : option A,using switchcases and break we print the numbers present in the main function except print(0) because it fails  print(1),print(1),print(2),print(-1),print(3).

Posted by Sivani Reddy Kakuru    2014-05-27 12:52:33

Answer : A
> for print(0) the Case 0 , Case 1 , Case 2 are executed and due to which the result variable will  store false , true , and ultimately false anr print T , 1 coz of the incerment in Case 1
> for print(1) case 1 , Case 2 are executed and result will store true then false and print T , 1
>for print(2) case 2 is executed and result will have false and hence will print T 2
>for print(-1),print(3) the default case is executed and the result will hvae false value and will print T-1 and T3 respectively

Posted by Deepak Kumar Yadav    2014-05-27 13:02:32

ans A. in the 1st function 0 is passd to the switch case case 0 fails ,case 1 result is true so 1 and i incremntd,case 2 result false bcz of xor operation- (T)1.
2nd functn case 1 true,case 2 result false,hre i is not incremntd bcz of using or opertn (if result false thn i incrmnt )-(T)1.
3rd functn case 2 result false  (T)2.
4 th func -1 goes default case so ans is (T)-1.
5th functn 3 default case result false (T)3.

Posted by Maheshwari Natarajan    2014-05-27 13:21:13

Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-28 15:00:20

© meritcampus 2019

All Rights Reserved.

Open In App