Operators And Expressions(C Program ) Questions and Answers

Question 1. Which of the following operator takes only integer operands?
  1.    +
  2.    *
  3.    /
  4.    %
  5.    None of these
Explanation:-
Answer: Option D. -> %
Two integers are taken to be input

Question 2. Determine output:
void main()
{
int c = - -2;
printf("c=%d", c);
}
  1.    1
  2.    -2
  3.    2
  4.    Error
Explanation:-
Answer: Option C. -> 2
Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus = plus.
Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

Question 3. Determine output:
void main()
{
int i=10;
i = !i>14;
printf("i=%d", i);
}
  1.    10
  2.    14
  3.    0
  4.    1
  5.    None of these
Explanation:-
Answer: Option C. -> 0
In the expression !i>14 , NOT (!) operator has more precedence than ' >' symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

Question 4. Determine output:
void main()
{
int i=0, j=1, k=2, m;
m = i++ || j++ || k++;
printf("%d %d %d %d", m, i, j, k);
}
  1.    1 1 2 3
  2.    1 1 2 2
  3.    0 1 2 2
  4.    0 1 2 3
  5.    None of these
Explanation:-
Answer: Option B. -> 1 1 2 2
In an expression involving || operator, evaluation takes place from left to right and will be stopped if one of its components evaluates to true(a non zero value).
So in the given expression m = i++ || j++ || k++.
It will be stop at j and assign the current value of j in m.
therefore m = 1 , i = 1, j = 2 and k = 2 (since k++ will not encounter. so its value remain 2)

Question 5. In an expression involving || operator, evaluation
I.   Will be stopped if one of its components evaluates to false
II.  Will be stopped if one of its components evaluates to true
III. Takes place from right to left
IV.  Takes place from left to right
  1.    I and II
  2.    I and III
  3.    II and III
  4.    II and IV
  5.    III and IV
Explanation:-
Answer: Option D. -> II and IV

Question 6. Find the output of the following program.
#include
void main()
{
int y=10;
if(y++>9 && y++!=10 && y++>11)
printf("%d", y);
else
printf("%d", y);
}
  1.    11
  2.    12
  3.    13
  4.    14
  5.    Compilation error
Explanation:-
Answer: Option C. -> 13
All the three condition in if is true.
if(y++>9 && y++!=10 && y++>11) such as
1st condition : 10++ > 9 is true and value of y is increase by 1 i.e y =11
2nd condition : 11++ != 10 is also ture and value of y is increase by 1 i.e y =12
3rd condition : 12++ > 11 is also ture and value of y is increase by 1 i.e y =13
Therefore if is excuted and print the value of y = 13

Question 7. Choose the correct output for the following program.
#include
void main()
{
int a=10, b=11, c=13, d;
d = (a=c, b+=a, c=a+b+c);
printf("%d %d %d %d", d, a, b, c);
}
  1.    50, 13, 11, 13
  2.    50, 13, 24, 50
  3.    13, 10, 24, 50
  4.    50, 13, 24, 13
  5.    13, 13, 24, 13
Explanation:-
Answer: Option B. -> 50, 13, 24, 50
For any comma separated expression the outcome is the right most part.

Question 8. Determine output of the following program code.
#include
void main()
{
int a, b=7;
a = b1 : a;
printf("%d %d", a, b);
}
  1.    3 7
  2.    7 3
  3.    8 3
  4.    3 8
  5.    None of these
Explanation:-
Answer: Option D. -> 3 8

Question 9. Find the output of the following program.
#include
void main()
{
int y=10;
if(y++>9 && y++!=11 && y++>11)
printf("%d", y);
else
printf("%d", y);
}
  1.    11
  2.    12
  3.    13
  4.    14
  5.    Compilation error
Explanation:-
Answer: Option B. -> 12
Since the second condition is false so, further conditions will not be checked, it will be skipped.

Question 10. Consider the following program fragment, and choose the correct one
void main()
{
int a, b = 2, c;
a = 2 * (b++);
c = 2 * (++b);
}
  1.    a = 4, c = 8
  2.    a = 3, c = 8
  3.    b = 3, c = 6
  4.    a = 4, c = 6
  5.    b = 4, c = 6
Explanation:-
Answer: Option A. -> a = 4, c = 8