C++ Data Types Quiz: Overflow, Casts and Floating Point
Test your C++ types knowledge with 14 questions and three programs on integer overflow, floating-point comparison, casts, const, constexpr, auto and enum class.
- Course: C++ study plan
- Module: Fundamental types
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Fundamental types is the checkpoint that closes the Fundamental types module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: the built-in types and their sizes on this platform, integer arithmetic with its overflow rules and bit operators, floating-point representation and printing, conversions and the four casts, const, constexpr and auto, and literals and scoped enumerations.
How it works. Fourteen questions and three programs; 70% on the questions and every program accepted clears the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What happens when an
intholding2147483647is incremented, and how do you detect it first? - What are
-7 / 2and-7 % 2? - Why is
0.1 + 0.2 == 0.3false, and what do you compare with instead? - Which initialisation form rejects
int x = 3.7? - What does
auto s = "text";deduce? - Why is
-1 < 1ufalse, and which C++20 function gets it right? - What does
enum classchange aboutint n = Red;?
The three programs are a checked running product, an invoice kept in integer cents, and a literal classifier that names each token's type and value — read its input format twice; the cases include suffixes and a char literal.
Common questions
What are -7 / 2 and -7 % 2 in C++?
-7 / 2 is -3, because integer division truncates toward zero, and -7 % 2 is -1, because the remainder takes the sign of the dividend. The identity (a / b) * b + a % b == a always holds.
Which initialisation form rejects int x = 3.7?
Brace initialisation. int x{3.7}; is a compile error because list-initialisation refuses narrowing conversions, whereas int x = 3.7; compiles and silently stores 3.
What does enum class change about int n = Red;?
With a scoped enumeration, int n = Red; no longer compiles, for two reasons: the enumerator must be qualified, as Colour::Red, and there is no implicit conversion to int. Write int n = static_cast<int>(Colour::Red); when the number is really wanted.
Exercises
Checked product
Read an integer n, then n integers that each fit in a long long. Multiply them into a long long accumulator that starts at 1, checking every step with __builtin_mul_overflow(product, x, &next). If everything fits print product=<value>; if multiplying by the factor at 0-based index i would overflow, print overflow at index <i> and stop. Widening cannot help here — the product of two 64-bit values needs 128 bits — so the checked builtin is the tool.
Input: n, then n integers. Output: one line.
Example: input 3 2 3 4 → product=24; input 2 3037000500 3037000500 → overflow at index 1
Invoice in cents
Read an integer n, then n lines quantity unit_cents, then one line with an integer tax percentage. Compute the subtotal as the sum of quantity * unit_cents, the tax as subtotal * percent / 100 rounded half up — in integers, (subtotal * percent + 50) / 100 — and the total. Print subtotal=, tax= and total=, each as <dollars>.<two-digit cents> built from integers with std::format("{}.{:02}", cents / 100, cents % 100).
No double anywhere: every amount is a long long number of cents, so the arithmetic is exact and the rounding rule is yours.
Input: n, n item lines, then the tax percentage. Output: three lines.
Example: input 2 / 3 199 / 1 1000 / 8 →
subtotal=15.97
tax=1.28
total=17.25What literal is that?
Read an integer n, then n tokens. Classify each as a C++ literal and print <token>: <type> <value>:
- digits only →
intif the value is at most2147483647, otherwiselong; - digits with a suffix
u→unsignedif at most4294967295, otherwiseunsigned long;l→long;ll→long long;ul→unsigned long;ull→unsigned long long; - digits with a decimal point →
double, orfloatwith anfsuffix, orlong doublewith anLsuffix — print the value with exactly three decimals; 'x'(three characters) →charand the character's code;true/false→bool 1/bool 0;- anything else →
<token>: unknown.
Parse by hand: walk the token once, accumulating the integer part in an unsigned long long and the fraction in a double (fraction = fraction * 10 + digit, scale *= 10), then treat everything after the digits as the suffix. Every value fits the type named.
Input: n, then n tokens separated by whitespace. Output: n lines.
Example: input 4 42 42u 3.5 'A' →
42: int 42
42u: unsigned 42
3.5: double 3.500
'A': char 65In this module: Fundamental types
← Literals and enumerations · Branching — if, else and the conditional operator →