Subex Placement: Sample Questions 8 - 9 of 19

Doorsteptutor material for competitive exams is prepared by world's top subject experts: get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 8

Edit

Write in Short

Short Answer▾

What will be the output of the following code?

  1. main()
  2. {
  3.     inti=3;
  4.     while(i--)
  5.     {
  6.         inti=100;
  7.         i--;
  8.         printf(“n%d”,i);
  9.     }
  10. }

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.

Question 9

Edit

Write in Short

Short Answer▾

What will be the output of the following code?

  1. main()
  2. {
  3.     intx;
  4.     printf("n%d",x=0,x=20,x=40);
  5. }

Explanation

Here, in main () function integer variable x is declared. In the printf () function it is initialized with three different values like 0,20, 40. Here, multiple times varialbe x is initialized. But output will be 0 because in the printf () function 0 is assigned first to the variable x. It does not consider the further initialization of variable x.

Developed by: