C++ Programming Practice Questions with Code Explanations
Free C++ programming practice questions with an explanation for every answer. Covers C++ basics, types and operators, control flow, functions, arrays, pointers and references, classes, and memory, with real code to trace.
Questions
What does this code print? int a = 7, b = 2; cout << a / b;
- 4
- 1
- 3.5
- 3
What does this code print? cout << 7 % 3 << " " << -7 % 3;
- 1 -1
- 2 -1
- 1 1
- 1 2
d is a double. What does this code print? double d = 7 / 2; cout << d;
- 3
- 3.5
- 4
- 0.5
What does this code print? cout << 7 / 2.0;
- 3.0
- 4
- 3.5
- 3
What does this code print? int x = 5; int y = x++ + 2; cout << x << " " << y;
- 5 7
- 5 8
- 6 8
- 6 7
What does this code print? int x = 5; int y = ++x + 2; cout << x << " " << y;
- 5 8
- 6 7
- 6 8
- 5 7
What does this code print? cout << 2 + 3 * 4 - 6 / 4;
- 12.5
- 14
- 13
- 18
What does this code print? int x = 10; x += 3; x *= 2; x %= 7; cout << x;
- 5
- 2
- 6
- 26
What does this code print? cout << (5 > 3 && 2 > 4) << " " << (5 > 3 || 2 > 4);
- false true
- 0 1
- 1 1
- 0 0
What does this code print? int n = static_cast<int>(9.99); cout << n;
- 9.9
- 10
- 9
- 9.99
What does this code print? char c = 'A'; c = c + 2; cout << c;
- B
- C
- 67
- A2
What does this code print? char c = 'A'; cout << c + 1;
- 65
- B
- 66
- A1
What does this code print? int x = 7; cout << x / 2 * 2;
- 6
- 3.5
- 7
- 1
What does this code print? int a = 5, b = 2; double r = static_cast<double>(a) / b; cout << r;
- 2.0
- 3
- 2
- 2.5
What does this code print? cout << 10 / 3 * 3.0;
- 3
- 9.0
- 10
- 9
What does this code print? cout << sizeof(char);
- 1
- 2
- 4
- 8
What does this code print? bool f = 0; cout << !f;
- 1
- true
- 0
- false
The user types 12.7. What is printed? int x; cin >> x; cout << x * 2;
- 25.4
- 0
- 25
- 24
The user types Ada Lovelace. What is printed? string name; cin >> name; cout << "Hi " << name;
- Hi
- Hi Lovelace
- Hi Ada
- Hi Ada Lovelace
The user types Ada Lovelace. What is printed? string name; getline(cin, name); cout << name.length();
- 11
- 12
- 3
- 13
Which header must be included to use cout and cin?
- <cout>
- <cstdio>
- <iostream>
- <string>
What does using namespace std; let you do?
- Write cout instead of std::cout
- Use cout without including any header
- Skip writing a main function
- Turn off the compiler's warnings
What type does auto give v here? auto v = 5 / 2.0;
- double
- int
- auto
- float
Which type is the best default for a value like 3.14159?
- double
- int
- long int
- char
Which line declares a constant that cannot be changed later?
- int MAX(100);
- const int MAX = 100;
- int const;
- #const MAX 100
Every C++ program starts running at the main function.
In C++, the names total and Total refer to the same variable.
endl ends the current line and flushes the output.
What does this code print? (number only) cout << 17 % 5 + 17 / 5;
What does this code print? (number only) int x = 4; int y = x * 3 + 2; cout << y;
What does this code print? int s = 85; if (s >= 90) cout << "A"; else if (s >= 80) cout << "B"; else cout << "C";
- BC
- B
- A
- C
These are two separate if statements. What prints? int s = 95; if (s >= 80) cout << "B"; if (s >= 90) cout << "A";
- B
- A
- AB
- BA
Notice the = in the condition. What prints? int x = 5; if (x = 0) cout << "zero"; else cout << "nonzero";
- A compile error
- nonzero
- zero
- Nothing is printed
What does this code print? int x = 3; if (x > 5) cout << "big"; cout << "done";
- done
- bigdone
- Nothing is printed
- big
Look closely at the end of the if line. What prints? int x = 3; if (x > 5); { cout << "big"; }
- big
- Nothing is printed
- A compile error
- bigbig
What does this code print? int d = 2; switch (d) { case 1: cout << "one "; case 2: cout << "two "; case 3: cout << "three "; break; default: cout << "other"; }
- two
- two three
- two three other
- one two three
What does this code print? int d = 4; switch (d) { case 1: cout << "one"; break; case 2: cout << "two"; break; default: cout << "other"; }
- other
- one two three other
- four
- Nothing is printed
What does this code print? char g = 'b'; switch (g) { case 'A': cout << "A"; break; case 'B': cout << "B"; break; default: cout << "?"; }
- Nothing is printed
- A
- b
- ?
Which type can NOT be used as the value tested by a switch?
- char
- int
- std::string
- an enum
What does this code print? int a = 4, b = 9; cout << (a > b ? a : b);
- 1
- 4
- 9
- 0
What does this code print? int x = 7; cout << (x % 2 == 0 ? "even" : "odd");
- 1
- odd
- even
- 7
What does this code print? int c = 0; if (c != 0 && 10 / c > 1) cout << "big"; else cout << "safe";
- safe
- Nothing is printed
- A runtime crash
- big
The indentation is misleading. What prints? int a = 5, b = 2; if (a > 3) if (b > 3) cout << "X"; else cout << "Y";
- Nothing is printed
- Y
- XY
- X
What does this code print? int t = 72; if (t > 60 && t < 70) cout << "mild"; else if (t >= 70) cout << "warm"; else cout << "cold";
- cold
- mild
- mildwarm
- warm
What does this code print? int x = 10; if (!(x > 5)) cout << "A"; else cout << "B";
- A
- Nothing is printed
- AB
- B
Which condition is true only when x is from 1 to 10, inclusive?
- x > 1 && x < 10
- x >= 1 || x <= 10
- x >= 1 && x <= 10
- 1 <= x <= 10
x is 15. What does this code print? int x = 15; cout << (1 <= x <= 10);
- 1
- 0
- 15
- A compile error
What does this code print? bool a = true, b = false; if (!a || b) cout << "A"; else if (b) cout << "B"; else cout << "C";
- B
- AC
- C
- A
What does this code print? int n = 4; if (n < 10) cout << "small "; if (n % 2 == 0) cout << "even"; else cout << "odd";
- small
- small even
- even
- small odd
In a switch, a case without break falls through to the next case.