Table of Content

Variable [MCQ] : Find Output of Program (Multiple Choice Questions)


Question 1
Guess the output of the following code snippet -
#include<stdio.h>
int main()
{
    int i = 10 ,j;
    j = ++i;
    printf("%d, %d", i, j);
    return 0;
}
A
11 10
B
11 11
C
10 11
D
10 10
Question 1 Explanation: 
In the following expression -
 j = ++i;
Value of i gets incremented first and then it is assigned to j.
Thus 11 is assigned to i and value of i also becomes 11.
Question 2
Guess the output of the following example -
#include<stdio.h>
int main()
{
    int i = 10, j = 10 ,k;
    k = ++i + j++;
    printf("%d, %d, %d", i, j, k);
    return 0;
}
A
10, 11, 21
B
11, 11, 22
C
11, 11, 21
D
11, 10, 21
Question 2 Explanation: 
Step 1 : Initial Values
int i = 10, j = 10 ,k;
k = ++i + j++;
Step 2 : Evaluating Expression
k = ++i + j++;
  = (Pre Increment i) + (Post Incr. j)
  = (11) + (10) ....[Post Incr Pending] 
  =  21
Step 3 : Execute Pending Post Increment
j = j++
  = 11
Question 3
In which order do the following gets evaluated in C Programming -
  1. Relational
  2. Arithmetic
  3. Logical
  4. Assignment
A
1234
B
2143
C
3214
D
2134
Question 4
Guess the output of the following program -
#include<stdio.h>
int main()
{
    int i = 3 ,j;
    j = i++;
    printf("%d, %d", i, j);
    return 0;
}
A
3, 4
B
4, 4
C
3, 3
D
4, 3
Question 4 Explanation: 
Value of i is assigned to the 'j' first then value of 'i' gets incremented because post increment is applied in the expression,
Question 5
Guess the output of the following expression ?
#include<stdio.h>
int main()
{
    int i = -30, j = 20, k = 1, m;
    m = ++i && ++j && --k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
A
-29, 21, 0, 0
B
-29, 21, 1, 1
C
-30, 21, 0, 1
D
-30, 21, 1, 1
Question 5 Explanation: 
Step 1 : Initial Values
int i = -30, j = 20, k = 1, m;
    m = ++i && ++j && --k;
Step 2 : Evaluating Expression
m = ++i && ++j && --k;
  = -29 && 21  && 0;
  = True && 0
  = True && False
  = False
  = 0
and In the pre increment the value of i,j,k will be -
i = -29
j = 21
k = 0
There are 5 questions to complete.