English

What gets displayed on the screen when the following program is compiled and run. Select the one correct answer. public class test { public static void main(String args[]) { boolean x = true;

Advertisements

Question

What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.

public class test 
{
public static void main(String args[]) 
   {
   boolean x = true;
   int a;
   if(x) a = x ? 1: 2;
   else a = x? 3: 4;
   System.out.println(a);
   }
}
Short Answer
Advertisements

Solution

Initially, x = true.

1. if(x)

Since x is true, the if condition is satisfied, and the if block is executed.

2. a = x ? 1 : 2;

The conditional operator (?:) checks the value of x.

Since x = true, the first value (1) is assigned to a.

a = 1

3. System.out.println(a);

The statement displays the value of a.

Output:

1
shaalaa.com
  Is there an error in this question or solution?
Chapter 6: Variables and Expressions - EXERCISE [Page 134]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 6 Variables and Expressions
EXERCISE | Q 58. | Page 134
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×