C++ Operators Interview Questions
C++ Operators - All Types
What are the main categories of operators in C++?
C++ operators are categorized into:
- Arithmetic Operators
- Relational/Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment/Decrement Operators
- Conditional/Ternary Operator
- Special Operators
What are the basic arithmetic operators in C++?
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
What is the difference between prefix and postfix increment operators?
++i (prefix): increments first, then returns the incremented value.
i++ (postfix): returns the original value first, then increments.
Example:
i++ (postfix): returns the original value first, then increments.
Example:
int i = 5;
int a = ++i; // a = 6, i = 6
int b = i++; // b = 6, i = 7
int a = ++i; // a = 6, i = 6
int b = i++; // b = 6, i = 7
What are relational operators in C++?
Relational operators compare two values and return a boolean result:
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
What are logical operators in C++?
Logical operators combine boolean expressions:
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | (a > 0) && (b > 0) |
| || | Logical OR | (a > 0) || (b > 0) |
| ! | Logical NOT | !(a > 0) |
What is short-circuit evaluation in logical operators?
Short-circuit evaluation means the second operand is evaluated only if necessary:
- For &&: If first operand is false, second is not evaluated
- For ||: If first operand is true, second is not evaluated
What are bitwise operators in C++?
Bitwise operators work on individual bits:
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise NOT | ~a |
| << | Left shift | a << n |
| >> | Right shift | a >> n |
What is the ternary/conditional operator in C++?
The ternary operator ? : is a shorthand for if-else:
// Syntax: condition ? expression1 : expression2
int max = (a > b) ? a : b;
// Equivalent to:
// if (a > b)
// max = a;
// else
// max = b;
int max = (a > b) ? a : b;
// Equivalent to:
// if (a > b)
// max = a;
// else
// max = b;
What are assignment operators in C++?
| Operator | Description | Equivalent to |
|---|---|---|
| = | Simple assignment | a = b |
| += | Add and assign | a = a + b |
| -= | Subtract and assign | a = a - b |
| *= | Multiply and assign | a = a * b |
| /= | Divide and assign | a = a / b |
| %= | Modulus and assign | a = a % b |
| &= | Bitwise AND and assign | a = a & b |
| |= | Bitwise OR and assign | a = a | b |
| ^= | Bitwise XOR and assign | a = a ^ b |
| <<= | Left shift and assign | a = a << b |
| >>= | Right shift and assign | a = a >> b |
What is operator precedence in C++?
Operator precedence determines the order in which operators are evaluated. Highest to lowest precedence:
- Scope resolution (::), Function call, Subscript, Member access
- Unary operators (++, --, !, ~, +, -, *, &)
- Multiplicative (*, /, %)
- Additive (+, -)
- Shift (<<, >>)
- Relational (<, <=, >, >=)
- Equality (==, !=)
- Bitwise AND (&)
- Bitwise XOR (^)
- Bitwise OR (|)
- Logical AND (&&)
- Logical OR (||)
- Conditional (?:)
- Assignment (=, +=, -=, etc.)
- Comma (,)
What is the comma operator in C++?
The comma operator , evaluates multiple expressions and returns the value of the last expression. Lowest precedence.
int a = (x = 5, x + 3); // a = 8, x = 5
// In for loops:
for(int i = 0, j = 10; i < j; i++, j--) { ... }
// In for loops:
for(int i = 0, j = 10; i < j; i++, j--) { ... }
What are the scope resolution operator (::) uses?
The scope resolution operator :: is used to:
- Access global variables when hidden by local variables
- Define member functions outside class
- Access static members
- Access namespace members
What are member access operators in C++?
| Operator | Description | Usage |
|---|---|---|
| . | Direct member access | object.member |
| -> | Indirect member access | pointer->member |
| .* | Pointer to member (object) | object.*ptrToMember |
| ->* | Pointer to member (pointer) | pointer->*ptrToMember |
What is the sizeof operator in C++?
sizeof returns the size in bytes of a type or variable. Compile-time operator.
int x;
cout << sizeof(int); // Size of int type
cout << sizeof(x); // Size of variable x
cout << sizeof x; // Parentheses optional for variables
cout << sizeof(int); // Size of int type
cout << sizeof(x); // Size of variable x
cout << sizeof x; // Parentheses optional for variables
What are the address-of and dereference operators?
& (address-of): Returns memory address of a variable.
* (dereference): Accesses value at a memory address.
* (dereference): Accesses value at a memory address.
int x = 10;
int *ptr = &x; // ptr stores address of x
int y = *ptr; // y gets value at address stored in ptr (10)
int *ptr = &x; // ptr stores address of x
int y = *ptr; // y gets value at address stored in ptr (10)
What is the new and delete operator in C++?
new dynamically allocates memory. delete deallocates dynamically allocated memory.
// Single object
int *p = new int;
*p = 42;
delete p;
// Array
int *arr = new int[10];
delete[] arr;
int *p = new int;
*p = 42;
delete p;
// Array
int *arr = new int[10];
delete[] arr;
What are type casting operators in C++?
C++ provides four casting operators:
- static_cast: General-purpose cast
- dynamic_cast: For polymorphism and RTTI
- const_cast: Removes const/volatile qualifiers
- reinterpret_cast: Low-level reinterpretation
What is the difference between = and == operators?
= is the assignment operator (assigns value to variable).
== is the equality operator (compares two values for equality).
Common error: if (x = 5) instead of if (x == 5).
== is the equality operator (compares two values for equality).
Common error: if (x = 5) instead of if (x == 5).
What are overloaded operators in C++?
Operator overloading allows defining custom behavior for operators with user-defined types.
class Vector {
Vector operator+(const Vector& other) {
// Define vector addition
}
};
Vector v1, v2, v3;
v3 = v1 + v2; // Uses overloaded + operator
Vector operator+(const Vector& other) {
// Define vector addition
}
};
Vector v1, v2, v3;
v3 = v1 + v2; // Uses overloaded + operator
What is operator associativity in C++?
Associativity determines the order when operators have the same precedence:
- Left-to-right: Most operators (+, -, *, /, etc.)
- Right-to-left: Assignment, unary, conditional operators
Note: Understanding operators, their precedence, and associativity is crucial for writing correct and efficient C++ code. These concepts are frequently tested in technical interviews.