Aptitude [Sasken Placement]: Sample Questions 18 - 18 of 35

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 18

Aptitude

Question

MCQ▾

What will be the output of the following program?

  1. main ()
  2. {
  3.     intI =3;
  4.     while (I--)
  5.     {
  6.         intI =100;
  7.         I--;
  8.         printf (-%d, I);
  9.     }
  10. }

Choices

Choice (4)

a.

99 99 99

b.

100 99 98

c.

Error

d.

99 98 97

Answer

a.

Explanation

  • In the main () function one integer variable i initialized with 3. The while () loop gets executed till the condition is true. Here, i- is passed in the while loop. i- is the post decremet operator. So, first while (3) will be executed as 3 > 0.
  • Here, again i is initialized with new value 100. i-= 99. So, when control reaches to printf () function 99 will be printed on new line of the output screen. Now value of i is decremented by 1 and becomes 2. So, while (2) gets executed as 2 > 0.
  • Here, again i is initialized with new value 100. i-= 99. So, when control reaches to printf () function 99 will be printed on new line of the output screen. Now value of i is decremented by 1 and becomes 1. Still 1 > 0 so while (1) gets executed as 2 > 0.
  • Here, again i is initialized with new value 100. i-= 99. So, when control reaches to printf () function 99 will be printed on new line of the output screen.

Developed by: