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
What is the purpose of 'std::unique_ptr'?
Answer: To provide exclusive ownership of a heap-allocated object with automatic deletion
std::unique_ptr enforces single ownership and automatically deletes the managed object when the unique_ptr goes out of scope.
Which of the following statements about std::array is TRUE?
Answer: Its size is fixed at compile time and it is stack-allocated by default
std::array has a compile-time fixed size N and is stored on the stack like a raw array, but provides STL-compatible interface.
What does the 'override' specifier do in C++11?
Answer: Explicitly declares that a function overrides a base class virtual function and causes a compile error if it doesn't
override tells the compiler to verify the function actually overrides a base virtual function, catching spelling or signature mismatches at compile time.
What is the key difference between 'delete' and 'delete[]' in C++?
Answer: delete is for single objects; delete[] is required to free arrays allocated with new[]
Using delete on a new[]-allocated array is undefined behavior; delete[] correctly calls the destructor for every element and frees the memory.
Which of the following is an advantage of using std::list over std::vector?
Answer: O(1) insertion and deletion anywhere in the list given an iterator
std::list (doubly-linked list) allows O(1) insertion/deletion at any position given an iterator, whereas std::vector requires O(n) shifting.
What does the 'constexpr' keyword guarantee about a function in C++11?
Answer: The function can be evaluated at compile time when given constant arguments
constexpr functions can be evaluated by the compiler at compile time when their arguments are constant expressions, enabling compile-time computation.
In C++, what is the result of 5 / 2 when both operands are of type int?
Answer: 2
Integer division truncates toward zero in C++, so 5/2 yields 2, discarding the fractional part.