C++ mock tests
Test yourself on the whole course, against the clock: questions from every module, then coding problems you solve and run right here. Your score comes at the end, with every answer explained.
Each mock test has 12 questions drawn from the lessons and up to 3 coding problems, and 45 minutes on the clock, which keeps running if you reload. Questions are worth a point each, and each coding problem 4 points, shared by its tests. Score 60% or more to pass; your best score is kept.
- 01Mock test 112 questions · 3 coding problems · 45 minutes · 24 points
- 02Mock test 212 questions · 3 coding problems · 45 minutes · 24 points
- 03Mock test 312 questions · 3 coding problems · 45 minutes · 24 points
Part A · 12 questions
- What makes a class abstract?
- int a = 1, b = 2; int& r = a; r = b; What is a now?
- What happens with ages["Zoe"] if "Zoe" isn't in the map?
- What does cout do?
- Is 1900 a leap year under the rule above?
- After one full pass of bubble sort, where is the largest value?
- Why use getline(cin, s) instead of cin >> s for a full name?
- Which call sorts a vector v from largest to smallest?
- Which operator does cin use?
- What is << called when used with cout?
- In what order are base and derived parts constructed?
- Which loop counts down from 10 to 1?
Part B · 3 coding problems
Write each answer and run its tests as often as you like: everything is marked when you submit. Each problem is worth 4 points, shared by its tests.
- 1Total of a listMedium · 4 points
The first number says how many numbers follow. Read them all and print their total.
Input4 3 9 2 6
Expected output20
Loading the editor… - 2A times tableMedium · 4 points
Read
nand print its times table from 1 to 10, as7 x 3 = 21.Input7
Expected output7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
Loading the editor… - 3Passed by valueMedium · 4 points
addTenchanges only its copy, soscorestays 50. Rewrite it to return the new value and use the result, so the program prints60.Expected output60
Loading the editor…
Part A · 12 questions
- Code outside the class tries to read a private member. What happens?
- What does string("hello").substr(1, 3) give?
- What does this print? int s = 0; for (int i = 1; i <= 4; i++) s += i; cout << s;
- When is a && b true?
- What does 17 / 5 give in C++ when both are ints?
- What does const after a member function's parameter list promise?
- Which call adds 7 to the end of a vector v?
- How should a function take a big vector it only reads?
- What does the [] at the start of a lambda hold?
- What does marking a function virtual change?
- Why put bool isPrime(int n); above main?
- Is 1900 a leap year?
Part B · 3 coding problems
Write each answer and run its tests as often as you like: everything is marked when you submit. Each problem is worth 4 points, shared by its tests.
- 1A calculatorMedium · 4 points
Read an expression like
8 * 3and print the result,Cannot divide by zero, orUnknown operator.Input8 * 3
Expected output24
Loading the editor… - 2The largest and where it isMedium · 4 points
Read
nnumbers and print the largest and its position (counting from 0; the first one if there's a tie).Input5 3 9 2 9 1
Expected output9 at 1
Loading the editor… - 3A constantMedium · 4 points
Declare
PIas aconst double(3.14159) and print the area of a circle of radius 3 with two decimal places.Expected output28.27
Loading the editor…
Part A · 12 questions
- What happens when a matching case has no break?
- What does using namespace std; allow?
- Which of these can be a case label?
- Why pass a large string as const string& rather than string?
- What does isPrime return for 1?
- Which lookup throws out_of_range for a missing key?
- Given void f(long) and void f(double), why does f(5) fail to compile?
- Why does cout << a < b << endl; fail to compile?
- What is the difference between v[i] and v.at(i)?
- In what order does a range-for loop visit a std::map?
- Which is the preferred modern C++ way to convert total to a double?
- What does cout << fixed << setprecision(1) << 2.75; print?
Part B · 3 coding problems
Write each answer and run its tests as often as you like: everything is marked when you submit. Each problem is worth 4 points, shared by its tests.
- 1Grade from a scoreMedium · 4 points
Read a score from 0 to 100 and print
A(90+),B(80+),C(70+),D(60+) orF.Input95
Expected outputA
Loading the editor… - 2Skip and stopMedium · 4 points
Read
n. Add 1 tonbut skip multiples of 3 (continue) and stop before the total would pass 50 (break). Print the total.Input10
Expected output37
Loading the editor… - 3Palindrome numbersMedium · 4 points
Read a positive whole number and print
yesif it reads the same backwards, otherwiseno.Input1221
Expected outputyes
Loading the editor…