-
Which header must you include to use cout and cin?
- string
- vector
- iostream
- cstdio
Show answer
Answer: C · iostream
The iostream header declares cin, cout and the operators << and >>. The string header gives you the string type, and vector gives you vectors.
-
Which of these is a valid variable name in C++?
- total_marks
- 2nd_year
- my-score
- class
Show answer
Answer: A · total_marks
A name may contain letters, digits and underscores, but it cannot start with a digit, contain a hyphen, or be a keyword such as class.
-
Which loop always runs its body at least once, even when the condition is false from the start?
- for
- while
- range-based for
- do-while
Show answer
Answer: D · do-while
A do-while loop runs the body first and checks the condition afterwards, so the body runs at least once. The for and while loops check the condition before the first round.
-
The vector of int named v holds 40, 10, 30, 20 in that order. After sort(v.begin(), v.end()), what is v[1]?
- 10
- 20
- 30
- 40
Show answer
Answer: B · 20
The call sort arranges the elements in ascending order: 10, 20, 30, 40. Indexes start at 0, so v[1] is the second smallest value, 20.
-
After the statements int x = 7; x += 3; x *= x; x %= 7; what is x?
- 0
- 3
- 2
- 14
Show answer
Answer: C · 2
First x is 10, then 10 × 10 = 100, and 100 % 7 leaves remainder 2, because 7 × 14 = 98.
-
int n = 2; switch (n) { case 1: cout << "A"; case 2: cout << "B"; case 3: cout << "C"; break; default: cout << "D"; } What is printed?
- BC
- B
- BCD
- ABC
Show answer
Answer: A · BC
Execution starts at case 2 and prints B. There is no break, so it falls through and prints C, and then the break ends the switch. The default case is skipped.
-
A function is defined as void doubleIt(int& a) { a = a * 2; }. In main, int n = 4; and then doubleIt(n); is called twice. What is n?
- 4
- 8
- 16
- 32
Show answer
Answer: C · 16
The parameter is a reference, so each call changes n itself: 4 becomes 8, and then 16. With a plain int parameter, n would stay 4.
-
A class Account has only one constructor, Account(string name, double opening). Which line fails to compile?
- Account a("Ravi", 500);
- Account b = Account("Imran", 0);
- Account c("Meena", 1200.50);
- Account d;
Show answer
Answer: D · Account d;
Declaring any constructor stops the compiler from generating a default constructor, so Account d; has nothing it can call.
-
Two vectors of int are created: a(3, 7) with parentheses and b{3, 7} with braces. Which statement is true?
- a holds the three values 7, 7, 7 and b holds the two values 3 and 7
- a holds the two values 3 and 7 and b holds the three values 7, 7, 7
- Both hold the two values 3 and 7
- Both hold three copies of 7
Show answer
Answer: A · a holds the three values 7, 7, 7 and b holds the two values 3 and 7
Parentheses call the constructor that means count and value, so a is 7, 7, 7. Braces give a list of starting values, so b is 3, 7.
-
Shape has a pure virtual function area(). The class Square derives from Shape but does not override area(). What happens if main tries to create a Square object?
- It compiles, and area() returns 0
- The compiler reports an error, because Square is still abstract
- It compiles, but the program crashes when area() is called
- It compiles, and area() returns a random value
Show answer
Answer: B · The compiler reports an error, because Square is still abstract
A class that inherits a pure virtual function without overriding it is itself abstract, and objects of an abstract class cannot be created.
Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test