CPP Syntax Fundamentals & Data Structures 1 — Questions and Answers
Question 1: Which symbol is used to terminate a statement in C++?
- .
- :
- ; (Correct answer)
- ,
Correct 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.
Question 2: What is the correct way to declare an integer variable in C++?
- integer x;
- x int;
- int x; (Correct answer)
- var x int;
Correct 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'.
Question 3: What does the 'new' keyword do in C++?
- Deletes a variable.
- Allocates memory on the stack.
- Allocates memory dynamically (Correct answer)
- Initializes a pointer to null.
Correct 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.
Question 4: Which data structure uses FIFO (First In, First Out)?
- Stack
- Queue (Correct answer)
- Tree
- Graph
Correct 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).
Question 5: Which keyword is used to create a constant in C++?
- static
- define
- const (Correct answer)
- fixed
Correct 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.
Question 6: What is the default access specifier for class members in C++?
- public
- protected
- private (Correct answer)
- global
Correct 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.
Question 7: Which container in C++ STL maintains elements in sorted order?
- vector
- set (Correct answer)
- list
- queue
Correct 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.
Question 8: Which of the following is used to define a function in C++?
- func()
- define()
- void functionName() {} (Correct answer)
- function {}
Correct 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.
Question 9: Which C++ data structure is best for LIFO (Last In, First Out)?
- Queue
- Graph
- Stack (Correct answer)
- Tree
Correct 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.
Which symbol is used to terminate a statement in C++?