C++ Operators MCQ - MCQ Questions and Answers

Welcome to the blog post, "C++ Operators MCQ - MCQ Questions and Answers." This engaging and informative quiz is tailored for learners who are enthusiastic about mastering the use of operators in C++. Operators are the backbone of any programming language, and C++ offers a rich set of operators to perform various operations. 

This quiz comprises 20 multiple-choice questions that cover a wide range of operator types in C++, from basic arithmetic to more complex logical and bitwise operations. Whether you're a beginner aiming to solidify your fundamentals or an experienced coder looking to refresh your skills, these questions are designed to challenge and enhance your understanding. Get ready to test your knowledge and learn more about the intricacies of C++ operators!

1. What is the purpose of the assignment operator '=' in C++?

a) To compare two values
b) To assign a value to a variable
c) To perform addition
d) To decrement a value

Answer:

b) To assign a value to a variable

Explanation:

The assignment operator '=' in C++ is used to assign the value on its right to the variable on its left.

2. Which operator is used for addition in C++?

a) +
b) -
c) *
d) /

Answer:

a) +

Explanation:

The '+' operator is used for addition in C++.

3. What does the '++' operator do in C++?

a) Addition
b) Subtraction
c) Increment by 1
d) Increment by 2

Answer:

c) Increment by 1

Explanation:

The '++' operator increments the value of a variable by 1.

4. What is the output of the expression 5 + 3 * 2 in C++?

a) 16
b) 11
c) 13
d) 10

Answer:

b) 11

Explanation:

According to operator precedence in C++, multiplication is performed before addition. Hence, 3 * 2 is evaluated first, making the expression 5 + 6, which equals 11.

5. What does the '==' operator do in C++?

a) Assigns a value to a variable
b) Compares two values for equality
c) Adds two values
d) Subtracts one value from another

Answer:

b) Compares two values for equality

Explanation:

The '==' operator is used to compare two values to determine if they are equal.

6. What is the result of the expression 8 % 3 in C++?

a) 2.67
b) 2
c) 5
d) 3

Answer:

b) 2

Explanation:

The '%' operator returns the remainder of the division of 8 by 3, which is 2.

7. What is the purpose of the bitwise AND operator '&' in C++?

a) To compare two values
b) To add two values
c) To perform a logical AND operation
d) To perform a bitwise AND operation

Answer:

d) To perform a bitwise AND operation

Explanation:

The '&' operator performs a bitwise AND operation, comparing each bit of its first operand to the corresponding bit of its second operand.

8. Which operator is used for division in C++?

a) /
b) DIV
c) %
d) *

Answer:

a) /

Explanation:

The '/' operator is used for division in C++.

9. What is the result of the expression 5 << 2 in C++?

a) 10
b) 20
c) 25
d) 7

Answer:

b) 20

Explanation:

The '<<' operator is the left shift operator. It shifts the bits of 5 (which is 0101 in binary) 2 places to the left, resulting in 10100, which is 20 in decimal.

10. What is the precedence of the '+' and '*' operators in C++?

a) '+' has higher precedence than '*'
b) '*' has higher precedence than '+'
c) Both have the same precedence
d) Depends on the compiler

Answer:

b) '*' has higher precedence than '+'

Explanation:

In C++, the '*' (multiplication) operator has higher precedence than the '+' (addition) operator.

11. What does the 'sizeof' operator return in C++?

a) The size of a variable in bits
b) The size of a variable in bytes
c) The length of an array
d) The number of elements in an array

Answer:

b) The size of a variable in bytes

Explanation:

The 'sizeof' operator returns the size of a variable or data type in bytes.

12. What is the output of the following C++ code?

   int x = 9, y = 4;
   printf("%d", x & y);
a) 0
b) 1
c) 5
d) 13

Answer:

c) 5

Explanation:

The '&' operator performs a bitwise AND operation. In binary, 9 is 1001 and 4 is 0100. The AND operation of these gives 0001, which is 5 in decimal.

13. What is the result of the expression 5 | 3 in C++?

a) 8
b) 7
c) 6
d) 5

Answer:

b) 7

Explanation:

The '|' operator performs a bitwise OR operation. In binary, 5 is 0101 and 3 is 0011. The OR operation of these gives 0111, which is 7 in decimal.

14. What is the output of the following C++ code?

int a = 10;
   printf("%d", !a);
a) 0
b) 1
c) 10
d) -10

Answer:

a) 0

Explanation:

The '!' operator is the logical NOT operator. It inverts the truthiness of a, and since a is non-zero, !a is 0.

15. What does the '->' operator do in C++?

a) Accesses a member of a structure through a pointer
b) Accesses a member of an array
c) Performs a bitwise operation
d) Compares two pointers

Answer:

a) Accesses a member of a structure through a pointer

Explanation:

The '->' operator is used to access a member of a structure or a union through a pointer.

16. What is the result of the expression '5 == 5' in C++?

a) True
b) False
c) 5
d) Error

Answer:

a) True

Explanation:

The '==' operator compares two values for equality. Since 5 is equal to 5, the expression evaluates to true (1).

17. What is the output of the following C++ code?

int a = 5;
   int b = a++;
   printf("%d %d", a, b);
a) 5 6
b) 6 5
c) 6 6
d) 5 5

Answer:

b) 6 5

Explanation:

The post-increment operator 'a++' increments 'a' after its current value is assigned to 'b'. So, 'b' becomes 5, and 'a' becomes 6.

18. What does the ternary operator '?:' do in C++?

a) Executes three operations in order
b) Divides an expression into three parts
c) Evaluates a condition and returns one of two values
d) Creates a three-dimensional array

Answer:

c) Evaluates a condition and returns one of two values

Explanation:

The ternary operator '?:' in C++ is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false.

19. Which operator is used to dynamically allocate memory for an array in C++?

a) new
b) malloc
c) alloc
d) create

Answer:

a) new

Explanation:

The 'new' operator is used in C++ for dynamic memory allocation, including allocating memory for arrays.

20. What is the function of the comma operator ',' in C++?

a) To separate function arguments
b) To separate variables in declarations
c) To execute multiple expressions in sequence
d) To compare two values

Answer:

c) To execute multiple expressions in sequence

Explanation:

The comma operator in C++ allows executing multiple expressions in sequence, where each expression is evaluated and the result of the last expression is returned.

Comments