0
0
C++programming~20 mins

Character vs string comparison in C++ - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Character vs String Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of character and string comparison
What is the output of this C++ code snippet?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (c == s) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ACompilation error
BNot Equal
CEqual
DRuntime error
Attempts:
2 left
💡 Hint
Think about the types being compared: char and std::string.
Predict Output
intermediate
2:00remaining
Comparing char with string using c_str()
What is the output of this C++ code?
C++
#include <iostream>
#include <string>
#include <cstring>

int main() {
    char c = 'a';
    std::string s = "a";
    if (std::strcmp(&c, s.c_str()) == 0) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ARuntime error
BNot Equal
CEqual
DCompilation error
Attempts:
2 left
💡 Hint
Consider what &c points to and if it is a proper C-string.
Predict Output
advanced
2:00remaining
Output of comparing char and string with explicit conversion
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (std::string(1, c) == s) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ANot Equal
BRuntime error
CCompilation error
DEqual
Attempts:
2 left
💡 Hint
Look at how the char is converted to a string before comparison.
Predict Output
advanced
2:00remaining
Comparing char and string with operator== overload
What is the output of this C++ code?
C++
#include <iostream>
#include <string>

int main() {
    char c = 'a';
    std::string s = "a";
    if (c == s[0]) {
        std::cout << "Equal" << std::endl;
    } else {
        std::cout << "Not Equal" << std::endl;
    }
    return 0;
}
ARuntime error
BEqual
CCompilation error
DNot Equal
Attempts:
2 left
💡 Hint
Check what s[0] returns and how it compares to c.
🧠 Conceptual
expert
2:00remaining
Why direct char and string comparison fails in C++
Why does the following code fail to compile in C++? char c = 'a'; std::string s = "a"; if (c == s) { // ... } Choose the best explanation.
ABecause char variables cannot be used in if statements.
BBecause std::string cannot be compared with any other type using operator==.
CBecause char and std::string are different types and no operator== overload exists for comparing them directly.
DBecause the compiler requires explicit casting of char to int before comparison.
Attempts:
2 left
💡 Hint
Think about type compatibility and operator overloading in C++.