C Programming Multiple Choice Questions On Operators

Practice Your Knowledge With MCQ on Operators In C

1. What is the output of this code?
#include <stdio.h>

void main()
{

    int x = 5.4 % 2;

    printf("Value of x is %d", x);

}
a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time error


Answer:d

2. What is the output of this code?
#include <stdio.h>

int main()
{
    int i = 1, 2, 3;
    
    printf("%d", i);
    
    return 0;
}
a) 1
b) 3
c) Garbage value
d) Compile time error


Answer:d
Explanation: Comma acts as a separator to create more variables. The compiler creates an integer variable i and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier. See rules for defining identifiers.

3. What is the output?
#include <stdio.h>

int main()
{

    int x = 1;

    short int i = 2;

    float f = 3;

    if (sizeof((x == 2) ? f : i) == sizeof(float))

        printf("float\n");

    else if (sizeof((x == 2) ? f : i) == sizeof(short int))

        printf("short int\n");

return 0;
}
a) float
b) short int
c) Undefined behaviour
d) Compiler time error


Answer:a
Explanation: An expression doesn't get evaluated in sizeof() operator. The conditional operator works as normal. x==2 will result in integer means true so result of conditional expression is f which is float so it returns size of float.

4. What is the output?
#include <stdio.h>

int main()
{
    int i = (1, 2, 3);
    
    printf("%d", i);
    
    return 0;
}
a) 1
b) 3
c) Compiler Error
d) Garbage Value


Answer:b
Explanation: The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the result of the last expression which gets assigned.

5. What is the output in a 32 bit system?
#include <stdio.h>

int main()
{

    short int i = 95;

    char c = 97;

    printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));

    return 0;
}
a) 2,1,2
b) 2,1,3
c) 2,1,4
d) 2,2,8


Answer:c
Explanation: Expression doesn't get evaluated in sizeof() operator. It returns the sizeof(int) as the result of an expression will be an integer.

6. What is the output?
#include <stdio.h>
int main()
{
    int i = 5, j = 10, k = 15;
    printf("%d ", sizeof(k /= i + j));
    printf("%d", k);
    return 0;
}
a) 4
b) 15
c) 21
d) Compiler time error


Answer:a
Explanation: The main theme of the program lies here: sizeof(k /= i + j). An expression doesn't get evaluated inside sizeof operator. sizeof operator returns sizeof(int) because the result of expression will be an integer. As the expression is not evaluated, value of k will not be changed.

7. What is the output?
#include <stdio.h>
int main()
{
    //Assume size of character is 1 byte and size of integer is 4 bytes
    printf("%d", sizeof(printf("ComputerScience")));
    return 0;
}
a) ComputerScience4
b) 4ComputerScience
c) ComputerScience9
d) 4


Answer:d
Explanation: An expression doesn't get evaluated inside sizeof operator. Since printf() in C returns the actual character count which is 15 here, so sizeof(int) is 4.

8. What is the output?
#include <stdio.h>
 
int main()
{
   int a = 1;
   int b = 1;
   int c = a || --b;
   int d = a-- && --b;
   printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
   return 0;
}
a) a = 0, b = 1, c = 1, d = 0
b) a = 0, b = 0, c = 1, d = 0
c) a = 1, b = 1, c = 1, d = 1
d) a = 0, b = 0, c = 0, d = 0


Answer:b
Explanation: Let us understand the execution line by line. Initial values of a and b are 1.

// Since a is 1, the expression --b is not executed because
// of the short-circuit property of logical or operator
// So c becomes 1, a and b remain 1
int c = a || --b;
// The post decrement operator -- returns the old value 
in current expression 
// and then updates the value. So the value of expression --a is 1. 
Since the first operand of logical and is 1, shortcircuiting doesn't 
happen here.  
//So the expression --b is executed and --b returns 0 
because it is pre-increment.
// The values of a and b become 0, and the value of d 
also becomes 0. int d = a-- && --b;

9. What is the output of this code?
#include<stdio.h>
int f1() { printf ("Computer"); return 1;}
int f2() { printf ("Science"); return 1;}
 
int main()
{
  int p = f1() + f2();
  return 0;
}
a) ComputerScience
b) ScienceComputer
c) Compiler Dependent
d) Compiler Error


Answer:c
Explanation: The operator ‘+’ doesn't have a standard defined order of evaluation for its operands. Either f1() or f2() may be executed first. So a compiler may choose to output either “GeeksQuiz” or “QuizGeeks”.

10. What is the output?
#include <stdio.h>

void main()
{

    int x = 5 * 9 / 3 + 9;

}
a) 4
b) Depends on compiler
c) 24
d) 3


Answer:c
11. Are logical operators sequence points?
a) True
b) False
c) Depends on the standard
d) Depends on the compiler


Answer:a
12. Relational operators cannot be used with
a) long
b) float
c) strings
d) structure


Answer:d
13. What is the only ternary operator present in C programming language?
a)!=
b)>=
c)a=b=c
d)?:


Answer:d
14. What is the use of sizeof() operator?
a)To get the size of data types or variables in bytes
b)To get size of data types only
c)To get size of variables only
d)All of the above
[You may get confused please read the options carefully.]


Answer:a
15. What is the syntax of conditional operator in C?
a) expression1 : expression2 ? expression3
b) expression1 ? expression2 : expression
c) expression1 : expression2 expression3 ?
d) All of the above


Answer:b
Explanation: If the value of expression1 is true value of expression2 is assigned to left hand side of the whole expression otherwise value of expression3 is assigned.

For example, a = (5>6) ? (6+6) : (5+5)

After the execution of above expression a will hold value 10 because 5>6 is false so value of expression3 is assigned to LHS, here expression3 is 5+5.
16. Which operator is used to perform bitwise AND operator between two operands?
a) |
b) ~
c) &
d) <<


Answer:c
Explanation: Bitwise AND operator is denoted by & symbol.
17. What is the use of typedef keyword?
a) To create user defined data type
b) To create special functions
c) To change the meaning of built in data types
d) None of the above


Answer:a
18. What is the use of << Left shift operator?
a) To shift the bit pattern to the right by specified number of positions
b) To shift the bit pattern to the left by specified number of positions
c) Both a and b
d) None of the above


Answer:b
19. Which operator is used to invert each bit of operand in C programming language?
a) &
b) ~
c) ||
d) >>


Answer:b
Explanation: ~ is a bitwise negation operator which is used to invert each bit of the operand. It is a unary operator i.e. it requires only one operand for its operation.
20. If X = 0, which operator is used to increment the value of X by 1?
a) +
b) -
c) ++
d) Both a and c


Answer:d