Hi Helen,
Originally posted by Helen
Q15
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);
}
}
a.1
b.2
c.3
d.4
Answer is a. why??
If you understand the second question then the first one is more easier than second one. How does Ternary Operator works:
a = x ? 1: 2;
if x is true then a=1. If x is false the a=2;
If this is the case ,in the first case the x is true and so
a = x ? 1: 2;,this part(true part) of if statement is executed. Apply ternary statement rules ie,x is true,so a=1.
In the second case x is false and so false part of "if" is executed and so the answer is a=4;
Hope this helps.
Regards
Nirmala