C++ Quiz: 10 Questions, Answers and Code Explanations
Ten C++ practice questions with worked explanations of strings, conversions, loops, value parameters and dynamic storage.

These 10 C++ practice questions cover strings, numeric conversions, loops, value parameters and dynamic storage. Each question has an answer and a short explanation, with complete programs where running the code helps. Use C++17 or later for the examples.
Try to explain the result before compiling. If a result surprises you, reduce the program to the smallest example that shows the behavior. Our practice hub has more questions and a suggested starting point.
1. Is C++ another name for C#?
Answer: no. C++ and C# are separate programming languages. Similar names and some shared syntax do not make them aliases or make their code interchangeable.
2. Which C++ type stores a string of text?
Answer: `std::string`. It is a standard-library class, declared in <string>. It is not a built-in keyword such as int. Use the std:: qualification unless you have deliberately introduced the name into scope.
#include <iostream>
#include <string>
int main() {
std::string text = "CS Code";
std::cout << text << '\n';
}3. Which declaration keeps the fractional part of 2.8?
Answer: `double x = 2.8;`. The declaration int x = 2.8; is also valid, but its conversion discards the fractional part and stores 2. That does not make it a suitable declaration when you want to keep the fraction. Floating-point values are approximate in general.
#include <iostream>
int main() {
double x = 2.8;
int whole = 2.8;
std::cout << x << " " << whole << '\n';
}4. Should a string literal use single or double quotes?
Answer: double quotes for a string literal, single quotes for an ordinary character literal. "CS Code" can initialize a std::string; 'a' is a character. Do not write a quoted word with single quotes and expect it to be a string.
#include <iostream>
#include <string>
int main() {
std::string text = "CS Code";
char letter = 'a';
std::cout << text << '\n' << letter << '\n';
}5. Which statement exits the current loop?
Answer: `break`. It exits the nearest enclosing loop or switch. continue starts the next loop iteration instead; it does not exit the loop. In nested loops, one break does not automatically exit every loop.
#include <iostream>
int main() {
int m = 5;
while (true) {
if (m == 1) {
break;
}
--m;
}
std::cout << m << '\n';
}6. What does adding 1.1 to an int print?
#include <iostream>
int main() {
int m = 11;
m += 1.1;
std::cout << m << '\n';
}Answer: `12`. The addition uses floating-point arithmetic, and the assignment converts its result back to int, discarding the fractional part. This is an implicit conversion, not an explicit cast. The example stays within the range of int; an out-of-range floating-to-integer conversion is a different problem. See the C++ floating-integral conversion rules.
7. Can a value parameter change the caller's int?
#include <iostream>
void add_ten(int value) {
value += 10;
}
int main() {
int s = 48;
add_ten(s);
std::cout << s << '\n';
}Answer: `48`. The parameter is a separate int initialized from the argument. Changing that parameter does not change s. A reference parameter, such as int& value, would let the function modify the caller's object. This question is about an int, not about what a copied pointer might point to.
8. What does strlen return for an embedded null character?
#include <cstring>
#include <iostream>
int main() {
char text[] = "Com\0puter";
std::cout << std::strlen(text) << '\n';
}Answer: `3`. std::strlen stops at the first null character, after Com. The array still contains the later characters and the final terminator; its array size is 10. C-style string length, array size and std::string::size() are different concepts.
9. What happens when an if condition has a trailing semicolon?
#include <iostream>
int main() {
int m = 1;
if (m == 2); // An empty statement is the entire if body.
std::cout << "Hii";
std::cout << " Hello\n";
}Answer: `Hii Hello`. The if controls only the empty statement ending at that semicolon. Both output statements are outside it, so both execute. This is valid syntax but usually a mistake. Use braces to make the intended body clear, and read compiler warnings.
10. Where does ordinary new allocate an object?
Answer: dynamic storage, commonly called the heap. An ordinary allocating new expression creates an object with dynamic storage duration. C++ specifies storage duration and allocation rules rather than requiring one physical stack/heap layout for every implementation. Placement new is a special case that uses supplied storage.
#include <iostream>
int main() {
int* value = new int(42);
std::cout << *value << '\n';
delete value;
}The pointer variable and the dynamically created int have different lifetimes. The example pairs new with delete; in application code, prefer ownership tools such as std::unique_ptr when suitable. Reference: C++ dynamic storage duration.
How to compile these C++ examples
Save a complete example as quiz.cpp and compile it with warnings enabled:
c++ -std=c++17 -Wall -Wextra -Wpedantic quiz.cpp -o quiz
./quizQuestions 3 and 6 intentionally demonstrate fractional conversion, and question 9 intentionally demonstrates an empty if body. Warnings about these examples are useful evidence of the traps being explained.
Where to go next
Compare the C quiz answers, then study arrays and indexing or practise TCS CodeVita problems with solutions. Write down the expected output and the language rule before running a program.



