Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

10 questions. Think of your answer first, then open “Show answer”.

  1. Which header must you include to use cout and cin?

    1. string
    2. vector
    3. iostream
    4. 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.

  2. Which of these is a valid variable name in C++?

    1. total_marks
    2. 2nd_year
    3. my-score
    4. 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.

  3. Which loop always runs its body at least once, even when the condition is false from the start?

    1. for
    2. while
    3. range-based for
    4. 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.

  4. The vector of int named v holds 40, 10, 30, 20 in that order. After sort(v.begin(), v.end()), what is v[1]?

    1. 10
    2. 20
    3. 30
    4. 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.

  5. After the statements int x = 7; x += 3; x *= x; x %= 7; what is x?

    1. 0
    2. 3
    3. 2
    4. 14
    Show answer

    Answer: C · 2

    First x is 10, then 10 × 10 = 100, and 100 % 7 leaves remainder 2, because 7 × 14 = 98.

  6. int n = 2; switch (n) { case 1: cout << "A"; case 2: cout << "B"; case 3: cout << "C"; break; default: cout << "D"; } What is printed?

    1. BC
    2. B
    3. BCD
    4. 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.

  7. 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?

    1. 4
    2. 8
    3. 16
    4. 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.

  8. A class Account has only one constructor, Account(string name, double opening). Which line fails to compile?

    1. Account a("Ravi", 500);
    2. Account b = Account("Imran", 0);
    3. Account c("Meena", 1200.50);
    4. 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.

  9. Two vectors of int are created: a(3, 7) with parentheses and b{3, 7} with braces. Which statement is true?

    1. a holds the three values 7, 7, 7 and b holds the two values 3 and 7
    2. a holds the two values 3 and 7 and b holds the three values 7, 7, 7
    3. Both hold the two values 3 and 7
    4. 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.

  10. 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?

    1. It compiles, and area() returns 0
    2. The compiler reports an error, because Square is still abstract
    3. It compiles, but the program crashes when area() is called
    4. 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