C++ Operators Interview Questions

C++ Operators - All Types

What are the main categories of operators in C++?
C++ operators are categorized into:
  1. Arithmetic Operators
  2. Relational/Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Increment/Decrement Operators
  7. Conditional/Ternary Operator
  8. Special Operators
What are the basic arithmetic operators in C++?
OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / 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:
int i = 5;
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:
OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b
What are logical operators in C++?
Logical operators combine boolean expressions:
OperatorDescriptionExample
&&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
Example: if (ptr != nullptr && ptr->value > 0) - safe from null pointer dereference.
What are bitwise operators in C++?
Bitwise operators work on individual bits:
OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << n
>>Right shifta >> 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;
What are assignment operators in C++?
OperatorDescriptionEquivalent to
=Simple assignmenta = b
+=Add and assigna = a + b
-=Subtract and assigna = a - b
*=Multiply and assigna = a * b
/=Divide and assigna = a / b
%=Modulus and assigna = a % b
&=Bitwise AND and assigna = a & b
|=Bitwise OR and assigna = a | b
^=Bitwise XOR and assigna = a ^ b
<<=Left shift and assigna = a << b
>>=Right shift and assigna = a >> b
What is operator precedence in C++?
Operator precedence determines the order in which operators are evaluated. Highest to lowest precedence:
  1. Scope resolution (::), Function call, Subscript, Member access
  2. Unary operators (++, --, !, ~, +, -, *, &)
  3. Multiplicative (*, /, %)
  4. Additive (+, -)
  5. Shift (<<, >>)
  6. Relational (<, <=, >, >=)
  7. Equality (==, !=)
  8. Bitwise AND (&)
  9. Bitwise XOR (^)
  10. Bitwise OR (|)
  11. Logical AND (&&)
  12. Logical OR (||)
  13. Conditional (?:)
  14. Assignment (=, +=, -=, etc.)
  15. 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--) { ... }
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
Example: int x = ::globalVar;
What are member access operators in C++?
OperatorDescriptionUsage
.Direct member accessobject.member
->Indirect member accesspointer->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
What are the address-of and dereference operators?
& (address-of): Returns memory address of a variable.
* (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)
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;
What are type casting operators in C++?
C++ provides four casting operators:
  1. static_cast: General-purpose cast
  2. dynamic_cast: For polymorphism and RTTI
  3. const_cast: Removes const/volatile qualifiers
  4. 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).
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
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
Example: a = b = c is evaluated as a = (b = c) (right-to-left).
Note: Understanding operators, their precedence, and associativity is crucial for writing correct and efficient C++ code. These concepts are frequently tested in technical interviews.
C++ Operators Next