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.

212 questions · Computer Science · by Study Foxy

Study these questions free

Questions

  1. What does this code print? int a = 7, b = 2; cout << a / b;

    • 4
    • 1
    • 3.5
    • 3
  2. What does this code print? cout << 7 % 3 << " " << -7 % 3;

    • 1 -1
    • 2 -1
    • 1 1
    • 1 2
  3. d is a double. What does this code print? double d = 7 / 2; cout << d;

    • 3
    • 3.5
    • 4
    • 0.5
  4. What does this code print? cout << 7 / 2.0;

    • 3.0
    • 4
    • 3.5
    • 3
  5. What does this code print? int x = 5; int y = x++ + 2; cout << x << " " << y;

    • 5 7
    • 5 8
    • 6 8
    • 6 7
  6. What does this code print? int x = 5; int y = ++x + 2; cout << x << " " << y;

    • 5 8
    • 6 7
    • 6 8
    • 5 7
  7. What does this code print? cout << 2 + 3 * 4 - 6 / 4;

    • 12.5
    • 14
    • 13
    • 18
  8. What does this code print? int x = 10; x += 3; x *= 2; x %= 7; cout << x;

    • 5
    • 2
    • 6
    • 26
  9. What does this code print? cout << (5 > 3 && 2 > 4) << " " << (5 > 3 || 2 > 4);

    • false true
    • 0 1
    • 1 1
    • 0 0
  10. What does this code print? int n = static_cast<int>(9.99); cout << n;

    • 9.9
    • 10
    • 9
    • 9.99
  11. What does this code print? char c = 'A'; c = c + 2; cout << c;

    • B
    • C
    • 67
    • A2
  12. What does this code print? char c = 'A'; cout << c + 1;

    • 65
    • B
    • 66
    • A1
  13. What does this code print? int x = 7; cout << x / 2 * 2;

    • 6
    • 3.5
    • 7
    • 1
  14. 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
  15. What does this code print? cout << 10 / 3 * 3.0;

    • 3
    • 9.0
    • 10
    • 9
  16. What does this code print? cout << sizeof(char);

    • 1
    • 2
    • 4
    • 8
  17. What does this code print? bool f = 0; cout << !f;

    • 1
    • true
    • 0
    • false
  18. The user types 12.7. What is printed? int x; cin >> x; cout << x * 2;

    • 25.4
    • 0
    • 25
    • 24
  19. The user types Ada Lovelace. What is printed? string name; cin >> name; cout << "Hi " << name;

    • Hi
    • Hi Lovelace
    • Hi Ada
    • Hi Ada Lovelace
  20. The user types Ada Lovelace. What is printed? string name; getline(cin, name); cout << name.length();

    • 11
    • 12
    • 3
    • 13
  21. Which header must be included to use cout and cin?

    • <cout>
    • <cstdio>
    • <iostream>
    • <string>
  22. 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
  23. What type does auto give v here? auto v = 5 / 2.0;

    • double
    • int
    • auto
    • float
  24. Which type is the best default for a value like 3.14159?

    • double
    • int
    • long int
    • char
  25. Which line declares a constant that cannot be changed later?

    • int MAX(100);
    • const int MAX = 100;
    • int const;
    • #const MAX 100
  26. Every C++ program starts running at the main function.

    True or false

  27. In C++, the names total and Total refer to the same variable.

    True or false

  28. endl ends the current line and flushes the output.

    True or false

  29. What does this code print? (number only) cout << 17 % 5 + 17 / 5;

  30. What does this code print? (number only) int x = 4; int y = x * 3 + 2; cout << y;

  31. 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
  32. 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
  33. 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
  34. What does this code print? int x = 3; if (x > 5) cout << "big"; cout << "done";

    • done
    • bigdone
    • Nothing is printed
    • big
  35. 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
  36. 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
  37. 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
  38. 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
    • ?
  39. Which type can NOT be used as the value tested by a switch?

    • char
    • int
    • std::string
    • an enum
  40. What does this code print? int a = 4, b = 9; cout << (a > b ? a : b);

    • 1
    • 4
    • 9
    • 0
  41. What does this code print? int x = 7; cout << (x % 2 == 0 ? "even" : "odd");

    • 1
    • odd
    • even
    • 7
  42. 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
  43. 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
  44. 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
  45. What does this code print? int x = 10; if (!(x > 5)) cout << "A"; else cout << "B";

    • A
    • Nothing is printed
    • AB
    • B
  46. 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
  47. x is 15. What does this code print? int x = 15; cout << (1 <= x <= 10);

    • 1
    • 0
    • 15
    • A compile error
  48. 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
  49. 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
  50. In a switch, a case without break falls through to the next case.

    True or false

Showing 50 of 212. Open the quiz to study them all.