Q99. What will be the final value of a and c in the following statement
A=2
C=1
C=(C) ? A= 0:2;
(A) A=0,C=0;
(B) A=2,C=2
(C) A=1,C=2
(D) A=2,C=0
Answers
Answer:
For initialization a = 2, c = 1 the value of a and c after this code will be c = (c) ? a = 0 : 2;. A. a = 0, c = 0;; B. a = 2, c = 2;; C. a = 2, c = 2;; D. a = 1, c = 2;.
Answer:
The correct answer is (A) A = 0; C = 0
Explanation:
ternary operator: ?: is known as ternary operator.
Format of ternary operator:
condition ? statement1 : statement2
If condition is true, statement1 is executed otherwise statement2 is executed
Initiallly, a = 2, c = 1
c = (c) ? a=0 : 2
Now, condition is checked, The value of c is 1, for any non zero value of c condition will evaluate to true else false
c is non zero, so condition is true, a=0 will be executed
Now we know that the assignment statement returns the assigned value therefore a=0 will return 0
c will store value returned by a=0 i.e 0
a = 0 and c = 0
Therefore, value of 'a' and 'c:' (A) a = 0 and c = 0
#SPJ6