Syntax Fundamentals & Data Structures Flashcards
9 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 9 Syntax Fundamentals & Data Structures flashcards as text
Which symbol is used to terminate a statement in C++?
Answer: ;
In C++, the semicolon (;) acts as a statement terminator, signaling the end of a complete instruction or declaration to the compiler. Without it, the compiler would not know where one statement ends and the next begins, leading to syntax errors. This is a fundamental rule for structuring C++ code.
What is the correct way to declare an integer variable in C++?
Answer: int x;
In C++, variables must be declared with their data type followed by the variable name. 'int' is the keyword used to specify an integer data type, and 'x' is the chosen variable name. This syntax `int x;` correctly informs the compiler to reserve memory for an integer variable named 'x'.
What does the 'new' keyword do in C++?
Answer: Allocates memory dynamically
The `new` keyword in C++ is used for dynamic memory allocation. It requests memory from the heap at runtime, rather than from the stack at compile time, and returns a pointer to the newly allocated memory. This allows programs to manage memory more flexibly, especially for objects whose size or number isn't known until execution.
Which data structure uses FIFO (First In, First Out)?
Answer: Queue
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means the first element added to the queue is the first one to be removed, similar to how people wait in a line. Elements are added to the rear (enqueue) and removed from the front (dequeue).
Which keyword is used to create a constant in C++?
Answer: const
The `const` keyword in C++ is used to declare a constant, meaning its value cannot be changed after initialization. This helps enforce data integrity and makes code more robust by preventing accidental modifications to important values. It can be applied to variables, function parameters, and member functions.
What is the default access specifier for class members in C++?
Answer: private
In C++, if no access specifier (public, protected, or private) is explicitly stated for class members, they are `private` by default. This means that these members can only be accessed from within the class itself, promoting encapsulation and data hiding, which are core principles of object-oriented programming.
Which container in C++ STL maintains elements in sorted order?
Answer: set
The `std::set` container in the C++ Standard Template Library (STL) automatically stores its elements in sorted order. It is an associative container that holds unique elements, and whenever an element is inserted, the set ensures it maintains its sorted property. This makes `set` ideal for scenarios requiring ordered, unique data and efficient searching.
Which of the following is used to define a function in C++?
Answer: void functionName() {}
In C++, a function definition specifies the function's return type (e.g., `void` for no return value), its name (`functionName`), and its parameters (empty parentheses `()` for no parameters in this example), followed by the function body enclosed in curly braces `{}`. This structure defines what the function does and how it can be called.
Which C++ data structure is best for LIFO (Last In, First Out)?
Answer: Stack
A stack is a linear data structure that operates on the Last In, First Out (LIFO) principle. This means the last element added to the stack is the first one to be removed. Operations on a stack typically include `push` (adding an element to the top) and `pop` (removing the top element), mimicking a physical stack of items.