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 output of the following code? ```cpp int x = 5; std::cout << x++ << " " << ++x; ```
Answer: 5 7
x++ returns 5 (post-increment), then ++x increments to 7 and returns 7, giving '5 7'.
Which container provides O(1) average-case lookup by key in C++ STL?
Answer: std::unordered_map
std::unordered_map uses a hash table, giving O(1) average-case lookup, unlike std::map which is O(log n).
What does the following declaration create? ```cpp int* const ptr = &x; ```
Answer: A constant pointer to an integer
int* const ptr declares a constant pointer (the pointer itself cannot be reassigned) to a non-const integer.
Which C++ standard introduced the range-based for loop?
Answer: C++11
The range-based for loop (for (auto x : container)) was introduced in C++11.
What is the size of an empty class in C++?
Answer: 1 byte
C++ guarantees that every object has a unique address, so an empty class has size 1 byte.
Which of the following correctly initializes a std::vector with five zeros?
Answer: std::vector v(5);
std::vector v(5) constructs a vector of 5 elements, all value-initialized to 0.
What is the difference between 'struct' and 'class' in C++?
Answer: struct members are public by default; class members are private by default
The only default difference is access: struct defaults to public, class defaults to private for both members and inheritance.