हिंदी

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

प्रश्न

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);
   }
}
लघु उत्तरीय
Advertisements

उत्तर

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
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 6: Variables and Expressions - EXERCISE [पृष्ठ १३४]

APPEARS IN

धीरज मेहरोत्रा Computer Science [English] Class 12
अध्याय 6 Variables and Expressions
EXERCISE | Q 58. | पृष्ठ १३४
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×