โ† All CPP Flashcard Decks

Syntax Fundamentals & Data Structures Flashcards

7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Syntax Fundamentals & Data Structures flashcards as text
  1. Which of the following correctly declares a lambda that captures all local variables by reference?

    Answer: [&](int x){ return x; }

    [&] in the capture list tells the lambda to capture all local variables from the enclosing scope by reference.

  2. What is the output of this code? ```cpp std::vector v = {1, 2, 3}; v.erase(v.begin() + 1); std::cout << v[1]; ```

    Answer: 3

    Erasing index 1 (value 2) shifts the remaining elements left, so v[1] becomes 3.

  3. What is a std::pair most analogous to?

    Answer: A struct with two fields 'first' and 'second' of the specified types

    std::pair holds exactly two values accessible as .first and .second of the types specified in the template arguments.

  4. Which of the following is NOT a valid way to initialize a variable in C++11?

    Answer: int x := 5;

    C++ does not use := for initialization; that syntax belongs to languages like Pascal or Go.

  5. What happens when you access a std::map with a key that doesn't exist using operator[]?

    Answer: It returns a default-constructed value and inserts the key

    operator[] on std::map value-initializes and inserts a new element for missing keys, returning a reference to it.

  6. In C++, what is 'object slicing'?

    Answer: Losing derived class data when assigning a derived object to a base class by value

    Object slicing occurs when a derived class object is assigned/copied to a base class variable by value, discarding the derived portion.

  7. Which STL algorithm finds the first element in a range satisfying a predicate?

    Answer: std::find_if

    std::find_if takes a unary predicate and returns an iterator to the first element for which the predicate returns true.