Recall & Review
beginner
What is the difference between a character and a string in C++?
A character (char) holds a single letter or symbol, while a string (std::string) holds a sequence of characters, like a word or sentence.
Click to reveal answer
beginner
Can you compare a char and a std::string directly using == in C++?
No, you cannot directly compare a char and a std::string with == because they are different types. You must convert the char to a string or compare the char to a single character in the string.
Click to reveal answer
beginner
How do you compare a single character to the first character of a std::string?
You can compare a char variable to the first character of a string using: if (myChar == myString[0]) { ... }
Click to reveal answer
intermediate
What happens if you compare a char to a string literal using == in C++?
Comparing a char to a string literal (like 'a' == "a") is invalid because 'a' is a char and "a" is a const char array (string literal). They are different types and cannot be compared directly.Click to reveal answer
intermediate
How can you convert a char to a std::string in C++?
You can convert a char to a std::string by using the string constructor: std::string s(1, myChar);
Click to reveal answer
Which type holds a single character in C++?
✗ Incorrect
A char holds a single character, while std::string holds multiple characters.
What is the correct way to compare a char 'c' to the first character of a string 'str'?
✗ Incorrect
You compare the char to the first character of the string using indexing: str[0].
Can you directly compare a char and a std::string using ==?
✗ Incorrect
Direct comparison is invalid because char and std::string are different types.
How do you convert a char to a std::string?
✗ Incorrect
Use the string constructor with count 1 and the char to create a string.
What type is a string literal like "a" in C++?
✗ Incorrect
String literals are const char arrays, not std::string or char.
Explain how to compare a single character with a string in C++.
Think about the types and how to access characters inside a string.
You got /4 concepts.
Describe why comparing a char to a string literal directly is invalid in C++.
Consider the types involved and what a string literal really is.
You got /4 concepts.