Chapter 4 Beginner 57 Questions

Practice Questions — Operators in C++

← Back to Notes
11 Easy
13 Medium
8 Hard

Topic-Specific Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 10 + 3 << endl;
    cout << 10 - 3 << endl;
    cout << 10 * 3 << endl;
    return 0;
}
Basic arithmetic operations.
13
7
30
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 17 / 5 << endl;
    cout << 17 % 5 << endl;
    return 0;
}
Integer division truncates. Modulus gives remainder.
3
2
Question 3
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << (10 > 5) << endl;
    cout << (10 == 5) << endl;
    cout << (10 != 5) << endl;
    return 0;
}
Relational operators return 1 (true) or 0 (false).
1
0
1
Question 4
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    x += 3;
    cout << x << endl;
    x *= 2;
    cout << x << endl;
    return 0;
}
+= adds and assigns, *= multiplies and assigns.
8
16
Question 5
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    cout << ++a << endl;
    cout << a << endl;
    return 0;
}
Pre-increment: increment first, then use the value.
11
11
Question 6
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    cout << a++ << endl;
    cout << a << endl;
    return 0;
}
Post-increment: use the value first, then increment.
10
11
Question 7
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << -7 % 3 << endl;
    cout << 7 % -3 << endl;
    return 0;
}
In C++11, the result of % takes the sign of the left operand.
-1
1
Question 8
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 5, b = 3;
    cout << (a & b) << endl;
    cout << (a | b) << endl;
    cout << (a ^ b) << endl;
    return 0;
}
Convert to binary: 5 = 0101, 3 = 0011.
1
7
6
Question 9
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << (1 << 3) << endl;
    cout << (16 >> 2) << endl;
    return 0;
}
Left shift multiplies by 2^n. Right shift divides by 2^n.
8
4
Question 10
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    cout << (x > 3 ? "Yes" : "No") << endl;
    cout << (x > 10 ? "Yes" : "No") << endl;
    return 0;
}
Ternary operator: condition ? true_value : false_value.
Yes
No
Question 11
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    cout << (x & 1 == 0) << endl;
    cout << ((x & 1) == 0) << endl;
    return 0;
}
== has higher precedence than &.
0
0
Question 12
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 3;
    int b = (a++, ++a, a + 10);
    cout << a << " " << b << endl;
    return 0;
}
Comma operator evaluates all operands left to right, returns the last.
5 15
Question 13
Easy
What is the difference between / and % operators?
One gives the quotient, the other gives the remainder.
/ gives the quotient (result of division). % gives the remainder. For 17 / 5: quotient = 3, remainder = 2. The identity is: a == (a/b)*b + (a%b).
Question 14
Medium
What is short-circuit evaluation? Why is it useful?
What happens to the right operand when the left operand determines the result?
In a && b, if a is false, b is not evaluated (result must be false). In a || b, if a is true, b is not evaluated (result must be true). This is useful for safety: ptr != NULL && ptr->val == 5 prevents accessing a null pointer.
Question 15
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 0;
    int b = (a++ || ++a);
    cout << a << " " << b << endl;
    return 0;
}
a++ returns 0 (falsy), so the right side of || must be evaluated.
2 1
Question 16
Hard
Why is x ^ x always 0 and x ^ 0 always x? How is this used in competitive programming?
Think about what XOR does to identical bits.
XOR of identical bits gives 0 (1^1=0, 0^0=0), so x ^ x = 0. XOR with 0 leaves each bit unchanged (1^0=1, 0^0=0), so x ^ 0 = x. In CP, this is used to find the single unique element in an array where every other element appears twice: XOR all elements and pairs cancel out.
Question 17
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << (1 < 5 < 3) << endl;
    return 0;
}
C++ does not support chained comparisons like Python.
1
Question 18
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << (~0) << endl;
    return 0;
}
~0 flips all bits. In two's complement, what is that?
-1
Question 19
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 10;
    cout << !x << endl;
    cout << !!x << endl;
    return 0;
}
! converts non-zero to 0 and 0 to 1.
0
1
Question 20
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 6;
    cout << (a & 1) << endl;
    a = 7;
    cout << (a & 1) << endl;
    return 0;
}
& 1 checks the last bit. Even numbers have last bit 0, odd have 1.
0
1

Mixed & Application Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 20, b = 3;
    cout << a / b << " " << a % b << endl;
    return 0;
}
20 divided by 3 gives quotient 6 and remainder 2.
6 2
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << (5 > 3 && 2 < 4) << endl;
    cout << (5 > 3 && 2 > 4) << endl;
    return 0;
}
&& is true only when both operands are true.
1
0
Question 3
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    int a = x++;
    int b = ++x;
    cout << a << " " << b << " " << x << endl;
    return 0;
}
Track x: starts at 5, post-increment, then pre-increment.
5 7 7
Question 4
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 12, b = 5;
    int max = (a > b) ? a : b;
    int min = (a < b) ? a : b;
    cout << max << " " << min << endl;
    return 0;
}
Ternary operator selects based on condition.
12 5
Question 5
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int n = 7;
    cout << (n ^ n) << endl;
    cout << (n ^ 0) << endl;
    return 0;
}
XOR of a number with itself is 0. XOR with 0 is the number itself.
0
7
Question 6
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 10;
    int y = 20;
    x ^= y;
    y ^= x;
    x ^= y;
    cout << x << " " << y << endl;
    return 0;
}
This is the XOR swap algorithm.
20 10
Question 7
Easy
Write a program that reads two integers and prints which one is larger using the ternary operator. If they are equal, print "Equal".
Use nested ternary: (a > b) ? ... : (a < b) ? ... : "Equal"
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << ((a > b) ? "First is larger" : (a < b) ? "Second is larger" : "Equal") << endl;
    return 0;
}
Input: 7 3 Output: First is larger
Question 8
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 5;
    cout << (a << 1) << endl;
    cout << (a << 2) << endl;
    cout << (a << 3) << endl;
    return 0;
}
Left shift by n multiplies by 2^n.
10
20
40
Question 9
Medium
Write a program that checks if a given integer is a power of 2 using bitwise operators only (no loops or division).
A power of 2 has exactly one bit set. n & (n-1) clears the lowest set bit.
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    if (n > 0 && (n & (n - 1)) == 0) {
        cout << n << " is a power of 2" << endl;
    } else {
        cout << n << " is NOT a power of 2" << endl;
    }
    return 0;
}
Input: 16 Output: 16 is a power of 2
Question 10
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 2;
    int y = (x *= 3, x += 2, x);
    cout << x << " " << y << endl;
    return 0;
}
Comma operator: evaluate all, return the last.
8 8
Question 11
Medium
How do you check if a number is even or odd using bitwise operators?
Even numbers have their last bit as 0.
Use (n & 1). If the result is 0, n is even. If the result is 1, n is odd. This works because the last bit of any integer determines its parity. n & 1 isolates the last bit.
Question 12
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(3 + 4) << endl;
    cout << sizeof(3 + 4.0) << endl;
    return 0;
}
3+4 is int. 3+4.0 promotes to double.
4
8

Multiple Choice Questions

MCQ 1
What is the result of 17 / 5 in C++ when both operands are int?
  • A. 3
  • B. 3.4
  • C. 4
  • D. 3.0
Answer: A
A is correct. Integer division truncates the decimal part. 17 / 5 = 3 with remainder 2. The .4 is discarded.
MCQ 2
Which operator is used to find the remainder in C++?
  • A. /
  • B. %
  • C. \
  • D. mod
Answer: B
B is correct. The % operator (modulus) returns the remainder of integer division. 17 % 5 = 2.
MCQ 3
What does the && operator do in C++?
  • A. Bitwise AND
  • B. Logical AND
  • C. Address-of
  • D. Reference
Answer: B
B is correct. && is logical AND. & (single ampersand) is bitwise AND. & is also the address-of operator in pointer contexts.
MCQ 4
What is the output of cout << (10 > 5);?
  • A. true
  • B. 1
  • C. 10
  • D. 5
Answer: B
B is correct. Relational operators return a bool. By default, cout prints bool values as integers: true = 1, false = 0.
MCQ 5
What does the ternary operator ?: do?
  • A. Declares three variables
  • B. Loops three times
  • C. Returns one of two values based on a condition
  • D. Performs three operations
Answer: C
C is correct. condition ? value_if_true : value_if_false evaluates the condition and returns one of two values. It is a compact if-else expression.
MCQ 6
What is the value of -7 % 3 in C++11?
  • A. 2
  • B. -2
  • C. 1
  • D. -1
Answer: D
D is correct. In C++11 and later, the result of % takes the sign of the left operand (dividend). Since -7 is negative, the result is -1. Verify: -7 = (-2)*3 + (-1).
MCQ 7
What is the difference between ++x and x++?
  • A. No difference
  • B. ++x increments first then returns new value; x++ returns current value then increments
  • C. x++ increments by 2
  • D. ++x can only be used with int
Answer: B
B is correct. Pre-increment (++x) increments first, then returns the new value. Post-increment (x++) saves the current value, increments, then returns the saved (old) value.
MCQ 8
What is 5 & 3?
  • A. 1
  • B. 7
  • C. 6
  • D. 8
Answer: A
A is correct. 5 = 0101, 3 = 0011. Bitwise AND: 0001 = 1. Each bit is 1 only if both corresponding bits are 1.
MCQ 9
What is 1 << 10?
  • A. 10
  • B. 100
  • C. 1024
  • D. 512
Answer: C
C is correct. 1 << 10 = 1 * 2^10 = 1024. Left shifting 1 by n positions gives 2^n.
MCQ 10
In the expression a & b == 0, which operator is evaluated first?
  • A. & (bitwise AND)
  • B. == (equality)
  • C. Both have equal precedence
  • D. It depends on the compiler
Answer: B
B is correct. == has higher precedence than &. So a & b == 0 is parsed as a & (b == 0), not (a & b) == 0. This is a notorious C++ precedence trap.
MCQ 11
What is ~0 in C++ (assuming 32-bit int)?
  • A. 0
  • B. 1
  • C. -1
  • D. 2147483647
Answer: C
C is correct. ~0 flips all bits: 00...00 becomes 11...11. In two's complement, all bits set to 1 represents -1. This is true regardless of int size.
MCQ 12
What does n & (n - 1) do?
  • A. Checks if n is odd
  • B. Returns n-1
  • C. Clears the lowest set bit of n
  • D. Sets all bits of n
Answer: C
C is correct. n & (n-1) clears the lowest (rightmost) set bit of n. For example, 12 (1100) & 11 (1011) = 8 (1000). This is used to check if n is a power of 2: if n & (n-1) == 0 and n > 0, then n is a power of 2.
MCQ 13
What is the result of int x = (2, 3, 5);?
  • A. 2
  • B. 3
  • C. 5
  • D. Compilation error
Answer: C
C is correct. The comma operator evaluates all operands left to right and returns the value of the last operand. So (2, 3, 5) evaluates 2, then 3, then returns 5.
MCQ 14
What is the output of the following?
int a = 5; cout << (a++ + ++a);
  • A. 11
  • B. 12
  • C. 10
  • D. Undefined behavior
Answer: D
D is correct. Modifying a variable more than once in the same expression without an intervening sequence point is undefined behavior in C++. The compiler can produce any result. Never write expressions like a++ + ++a.
MCQ 15
Which of the following correctly checks if bit position k (0-indexed from right) is set in integer n?
  • A. n & k
  • B. n & (1 << k)
  • C. n | (1 << k)
  • D. n ^ (1 << k)
Answer: B
B is correct. 1 << k creates a mask with only bit k set. n & (1 << k) isolates bit k. If the result is non-zero, bit k is set. Option A checks if any common bits exist between n and k. Option C sets bit k. Option D toggles bit k.
MCQ 16
What is the result of 10 % 3?
  • A. 3
  • B. 1
  • C. 0
  • D. 3.33
Answer: B
B is correct. 10 % 3 = 1 because 10 = 3*3 + 1. The modulus operator returns the remainder of integer division.
MCQ 17
What is the value of 5 | 3?
  • A. 1
  • B. 6
  • C. 7
  • D. 8
Answer: C
C is correct. 5 = 0101, 3 = 0011. Bitwise OR: 0111 = 7. Each bit is 1 if either corresponding bit is 1.
MCQ 18
What does short-circuit evaluation mean for false && someFunction()?
  • A. someFunction() is always called
  • B. someFunction() is never called
  • C. The result is true
  • D. Compilation error
Answer: B
B is correct. With &&, if the left operand is false, the entire expression must be false regardless of the right operand. So someFunction() is never called. This is short-circuit evaluation.
MCQ 19
What is the output of cout << (true + true + false);?
  • A. 2
  • B. true
  • C. 1
  • D. Compilation error
Answer: A
A is correct. In arithmetic expressions, true is promoted to 1 and false to 0. So 1 + 1 + 0 = 2.

Coding Challenges

Challenge 1: Swap Two Numbers Without Temp Variable

Easy
Write a C++ program that reads two integers and swaps them using the XOR swap technique (no temporary variable, no arithmetic swap). Print the values before and after swapping.
Sample Input
10 20
Sample Output
Before: a=10, b=20 After: a=20, b=10
Do not use a third variable. Use only XOR (^) operations.
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << "Before: a=" << a << ", b=" << b << endl;
    a ^= b;
    b ^= a;
    a ^= b;
    cout << "After: a=" << a << ", b=" << b << endl;
    return 0;
}

Challenge 2: Bit Counter

Medium
Write a program that reads a non-negative integer and counts the number of 1-bits (set bits) in its binary representation using n & (n-1) technique. Also print the binary representation.
Sample Input
13
Sample Output
Binary of 13: 1101 Number of set bits: 3
Use the n & (n-1) trick to count bits. Print binary by checking each bit from MSB to LSB.
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    int original = n;
    
    // Print binary
    cout << "Binary of " << original << ": ";
    bool started = false;
    for (int i = 31; i >= 0; i--) {
        if (n & (1 << i)) {
            started = true;
            cout << 1;
        } else if (started) {
            cout << 0;
        }
    }
    if (!started) cout << 0;
    cout << endl;
    
    // Count set bits
    int count = 0;
    int temp = original;
    while (temp) {
        temp = temp & (temp - 1);
        count++;
    }
    cout << "Number of set bits: " << count << endl;
    return 0;
}

Challenge 3: All Operators Calculator

Easy
Write a program that reads two integers a and b, then prints the result of all arithmetic operations (+, -, *, /, %), all relational comparisons (==, !=, <, >, <=, >=), and the bitwise AND, OR, XOR.
Sample Input
15 4
Sample Output
15 + 4 = 19 15 - 4 = 11 15 * 4 = 60 15 / 4 = 3 15 % 4 = 3 15 == 4: 0 15 != 4: 1 15 < 4: 0 15 > 4: 1 15 <= 4: 0 15 >= 4: 1 15 & 4 = 4 15 | 4 = 15 15 ^ 4 = 11
Handle all operators. Print results clearly with labels.
#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    cout << a << " + " << b << " = " << a + b << endl;
    cout << a << " - " << b << " = " << a - b << endl;
    cout << a << " * " << b << " = " << a * b << endl;
    cout << a << " / " << b << " = " << a / b << endl;
    cout << a << " % " << b << " = " << a % b << endl;
    cout << a << " == " << b << ": " << (a == b) << endl;
    cout << a << " != " << b << ": " << (a != b) << endl;
    cout << a << " < " << b << ": " << (a < b) << endl;
    cout << a << " > " << b << ": " << (a > b) << endl;
    cout << a << " <= " << b << ": " << (a <= b) << endl;
    cout << a << " >= " << b << ": " << (a >= b) << endl;
    cout << a << " & " << b << " = " << (a & b) << endl;
    cout << a << " | " << b << " = " << (a | b) << endl;
    cout << a << " ^ " << b << " = " << (a ^ b) << endl;
    return 0;
}

Challenge 4: Find Unique Element (XOR)

Medium
Write a program that reads n integers where every element appears exactly twice except one element which appears once. Find the unique element using XOR. Input: first line is n, second line has n space-separated integers.
Sample Input
5 2 3 5 3 2
Sample Output
Unique element: 5
O(n) time, O(1) space. Use the property that a ^ a = 0 and a ^ 0 = a.
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    int result = 0;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        result ^= x;
    }
    cout << "Unique element: " << result << endl;
    return 0;
}

Challenge 5: Precedence Demonstrator

Hard
Write a program that demonstrates 5 operator precedence traps. For each, show the expression, the result with default precedence, and the result with explicit parentheses. Include: bitwise vs relational, multiplication vs addition, logical vs bitwise, shift vs addition, and assignment in condition.
Sample Input
(No input required)
Sample Output
Trap 1: x & 1 == 0 => 0 (parsed as x & (1==0)) Trap 1: (x & 1) == 0 => 1 (correct) ...
Use specific examples with actual values. Explain each trap in output.
#include <iostream>
using namespace std;
int main() {
    int x = 6;
    cout << "Trap 1: x=6, x & 1 == 0  => " << (x & 1 == 0) << " (parsed as x & (1==0))" << endl;
    cout << "Trap 1: x=6, (x & 1) == 0 => " << ((x & 1) == 0) << " (correct: 6 is even)" << endl;
    
    cout << "Trap 2: 2 + 3 * 4 = " << 2 + 3 * 4 << " (not 20)" << endl;
    cout << "Trap 2: (2 + 3) * 4 = " << (2 + 3) * 4 << " (correct if you want 20)" << endl;
    
    cout << "Trap 3: 1 << 2 + 1 = " << (1 << 2 + 1) << " (parsed as 1 << (2+1) = 8)" << endl;
    cout << "Trap 3: (1 << 2) + 1 = " << ((1 << 2) + 1) << " (correct: 5)" << endl;
    
    cout << "Trap 4: 1 < 5 < 3 = " << (1 < 5 < 3) << " (parsed as (1<5) < 3 = 1 < 3 = true)" << endl;
    int val = 5;
    cout << "Trap 4: 1 < val && val < 3 = " << (1 < val && val < 3) << " (correct: false)" << endl;
    
    cout << "Trap 5: 3 | 4 == 4 = " << (3 | 4 == 4) << " (parsed as 3 | (4==4) = 3 | 1 = 3)" << endl;
    cout << "Trap 5: (3 | 4) == 4 = " << ((3 | 4) == 4) << " (correct: 7 == 4 = false)" << endl;
    
    return 0;
}

Challenge 6: Power of 2 Checker with Bit Position

Medium
Write a program that reads an integer n. If n is a power of 2, print which power it is (e.g., 16 is 2^4). If not, print the nearest lower and higher powers of 2. Use bitwise operators only.
Sample Input
16
Sample Output
16 is 2^4
Use n & (n-1) to check power of 2. Use bit shifting to find the power.
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    if (n > 0 && (n & (n - 1)) == 0) {
        int power = 0;
        int temp = n;
        while (temp > 1) {
            temp >>= 1;
            power++;
        }
        cout << n << " is 2^" << power << endl;
    } else {
        int lower = 1, upper = 1;
        int lp = 0, up = 0;
        while (lower * 2 < n) {
            lower <<= 1;
            lp++;
        }
        upper = lower << 1;
        up = lp + 1;
        cout << n << " is NOT a power of 2" << endl;
        cout << "Lower: 2^" << lp << " = " << lower << endl;
        cout << "Upper: 2^" << up << " = " << upper << endl;
    }
    return 0;
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn C++ with a live mentor?

Explore our C++ Masterclass