Skip to content
Related Articles

Related Articles

Improve Article

GATE | GATE 2017 MOCK II | Question 13

  • Last Updated : 28 Jun, 2021
Geek Week

What will the output of the following C code ?

#include <stdio.h>
int main()
   int i=2, j=2;
   while (i+1 ? --i : j++)
   return 0;

(A) 1
(B) 2
(C) 3
(D) 4

Answer: (A)

Explanation: Consider the while loop condition => i + 1 ? –i : j++

In first iteration :

i + 1 = 3 (true)
So ternary operator will return –i i.e. 1, condition part is 1 means true so while condition is true.
Hence printf statement will print 1

In second iteration :
i + 1 =2 (true)
So ternary operator will return –i i.e 0, condition part is 0 means false so while condition is false.
Hence program control will come out of the while loop.

Quiz of this Question

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :