Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Comparison Questions - Part 2
C++

Comparison Questions - Part 2

Practice 15 more C++ comparison questions designed to test subtle language rules and common interview traps.

16. What does this == on string sizes print?

#include <iostream>
#include <string>

int main() {
    std::string s1 = "ab";
    std::string s2 = "abc";
    std::string s3 = "ab";

    std::cout << (s1 == s3) << "\n";
    std::cout << (s1.size() == s2.size()) << "\n";
    std::cout << (s1.compare(s2) < 0) << "\n";
    return 0;
}

Output:

1
0
1

s1 == s3 compares content → 1. s1.size() is 2, s2.size() is 3, so 2 == 30. s1.compare(s2) is negative ("ab" < "abc"), so < 01.

17. What does this == on nullptr print?

#include <iostream>

int main() {
    int *p = nullptr;
    int *q = nullptr;
    int x = 5;
    int *r = &x;

    std::cout << (p == q) << "\n";
    std::cout << (p == r) << "\n";
    std::cout << (r == nullptr) << "\n";
    return 0;
}

Output:

1
0
0

p == q — two nullptrs → 1. p == r — null vs a real address → 0. r == nullptrr points at x, not null → 0. Comparing pointers with nullptr is well-defined; two null pointers are always equal.

18. What does this == on string find print?

#include <iostream>
#include <string>

int main() {
    std::string s = "hello world";
    std::string sub = "world";

    std::cout << (s.find(sub) == std::string::npos) << "\n";
    std::cout << (s.find('o') == 4) << "\n";
    std::cout << (s.find("xyz") == std::string::npos) << "\n";
    return 0;
}

Output:

0
1
1

"world" is found at index 6, so s.find(sub) == npos is 0 (found, not npos). 'o' first appears at index 41. "xyz" is absent, so find returns npos1. find returns npos (-1 as size_t) when not found.

19. What does this == on string front/back print?

#include <iostream>
#include <string>

int main() {
    std::string s = "level";

    std::cout << (s.front() == s.back()) << "\n";
    std::cout << (s.front() == 'l') << "\n";
    std::cout << (s[1] == s[3]) << "\n";
    return 0;
}

Output:

1
1
1

s.front() is 'l', s.back() is 'l'1. s.front() == 'l'1. s[1] is 'e', s[3] is 'e'1. "level" is a palindrome, so the mirrored comparisons hold.

20. What does this == on vector back print?

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {3, 1, 4, 1, 5};

    std::cout << (v.front() == v.back()) << "\n";
    std::cout << (v[0] < v[4]) << "\n";
    std::cout << (v.size() == 5) << "\n";
    return 0;
}

Output:

0
1
1

v.front() is 3, v.back() is 53 == 5 is 0. 3 < 51. v.size() is 51.

21. What does this == on string empty print?

#include <iostream>
#include <string>

int main() {
    std::string a;
    std::string b = "";

    std::cout << (a == b) << "\n";
    std::cout << (a.empty()) << "\n";
    std::cout << (a == " ") << "\n";
    return 0;
}

Output:

1
1
0

Both a and b are empty strings, so a == b is 1. a.empty() is true1. a == " " compares empty against a single space → 0. An empty string is not a space.

22. What does this == on char uppercase/lowercase print?

#include <iostream>
#include <cctype>

int main() {
    char a = 'A';
    char b = 'a';

    std::cout << (a == b) << "\n";
    std::cout << (std::toupper(a) == std::toupper(b)) << "\n";
    std::cout << (std::tolower(a) == std::tolower(b)) << "\n";
    return 0;
}

Output:

0
1
1

'A' != 'a'0. std::toupper('A') is 'A', std::toupper('a') is 'A'1. std::tolower of both is 'a'1. Case-insensitive comparison requires normalizing both sides.

23. What does this == on int truncation print?

#include <iostream>

int main() {
    int a = 7 / 2;
    int b = 3;
    double c = 7 / 2.0;

    std::cout << (a == b) << "\n";
    std::cout << (7 / 2 == 3) << "\n";
    std::cout << (c == 3.5) << "\n";
    return 0;
}

Output:

1
1
1

7 / 2 with two ints truncates to 3, so a == b1. 7 / 2 == 31. 7 / 2.0 is 3.5 because one operand is double, so c == 3.51. Integer division truncates; mixing in a floating operand gives exact division.

24. What does this == on modulo print?

#include <iostream>

int main() {
    std::cout << (10 % 3 == 1) << "\n";
    std::cout << (10 % 3 == 2) << "\n";
    std::cout << (-7 % 3 == -1) << "\n";
    std::cout << (-7 % 3 == 2) << "\n";
    return 0;
}

Output:

1
0
1
0

10 % 3 is 1, so == 11, == 20. In C++, the % result takes the sign of the dividend: -7 % 3 is -1, so == -11 and == 20. Unlike some languages, C++ never returns 2 for -7 % 3.

25. What does this == on pointer equality with same address print?

#include <iostream>

int main() {
    int x = 42;
    int *p1 = &x;
    int *p2 = &x;
    int *p3 = p1;

    std::cout << (p1 == p2) << "\n";
    std::cout << (p1 == p3) << "\n";
    std::cout << (*p1 == *p3) << "\n";
    return 0;
}

Output:

1
1
1

p1, p2, and p3 all hold &x, so all pointer comparisons are 1. *p1 and *p3 both dereference to 42, so 42 == 421. Pointer equality checks the address, not the pointee value.

26. What does this == on string reversed print?

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string s = "madam";
    std::string t = s;
    std::reverse(t.begin(), t.end());

    std::cout << (s == t) << "\n";
    std::cout << (t == "madam") << "\n";
    return 0;
}

Output:

1
1

s is "madam", t after reversal is also "madam", so s == t1. t == "madam"1. Reversing a palindrome yields the same content.

27. What does this == on pointer type compare print?

#include <iostream>

int main() {
    int x = 5;
    double d = 5.0;

    std::cout << (x == d) << "\n";
    std::cout << (&x == reinterpret_cast<int*>(&d)) << "\n";
    std::cout << (static_cast<int>(d) == x) << "\n";
    return 0;
}

Output:

1
0
1

x == d compares 5 == 5.01 (numeric comparison). &x and reinterpret_cast<int*>(&d) compare addresses — x and d live in different memory → 0. static_cast<int>(5.0) == 51. Numeric comparison converts; pointer comparison compares addresses.

28. What does this == on struct pointer member print?

#include <iostream>

struct Node {
    int data;
    Node *next;
};

int main() {
    Node a = {1, nullptr};
    Node b = {1, nullptr};

    std::cout << (a.data == b.data) << "\n";
    std::cout << (a.next == b.next) << "\n";
    std::cout << (&a == &b) << "\n";
    return 0;
}

Output:

1
1
0

a.data and b.data are both 11. a.next and b.next are both nullptr1. &a and &b are different objects → 0. Same as with structs: compare members, not whole structs.

29. What does this == on string with whitespace print?

#include <iostream>
#include <string>

int main() {
    std::string s = " hello";
    std::string t = "hello";

    std::cout << (s == t) << "\n";
    std::cout << (s < t) << "\n";
    return 0;
}

Output:

0
1

" hello" has a leading space, so s != t0. s < t — space (' ' = 32) is less than 'h' (104), so " hello" < "hello"1. Leading whitespace changes both equality and ordering.

30. What does this == on logical and print?

#include <iostream>

int main() {
    int a = 5;
    int b = 0;

    std::cout << (a && b) << "\n";
    std::cout << ((a > 0) == !b) << "\n";
    std::cout << (a && 1) << "\n";
    return 0;
}

Output:

0
1
1

a && b5 && 0 is falsy → 0. (a > 0) is true, !b is !0 = true, so true == true1. a && 15 && 1 is truthy → 1. Logical operators yield 0/1.

My Private Notes

Notes are auto-saved locally to this device.