Data Structure And Algorithm Flashcards
What is an array in C++?
A collection of elements of the same type, stored in contiguous memory locations.
What is the starting index of an array in C++?
0
How is the size of an array determined in C++?
At compile time (static size).
What happens if you access an out-of-bounds index in a C++ array?
It can cause undefined behavior.
How do you access an element in an array?
Using the syntax array_name[index].
What is a pointer in C++?
A variable that stores the memory address of another variable.
What symbol is used to declare and dereference a pointer?
*.
What symbol is used to get the address of a variable?
&.
Can a pointer point to any data type in C++?
Yes, it can point to any data type (e.g., int, char).
What is the purpose of the dereferencing operator (*) in pointers?
To access the value stored at the memory address held by the pointer.
What is a null pointer?
A pointer initialized to nullptr or NULL that doesn’t point to any valid address.
What is pointer arithmetic?
Operations like ptr++ to navigate through array elements.
What is recursion?
A technique in which a function calls itself to solve a problem.
Why is a base case important in recursion?
To avoid infinite recursion.
What are some common problems solved using recursive algorithms?
Factorial, Fibonacci, and tree traversals.
What is a base case in recursion?
The condition under which the recursion ends.
What is a recursive case?
The part of the function where the function calls itself with a reduced problem size.
What does the factorial function return when n is 0?
1, which is the base case.
What happens when recursion goes too deep?
It can exhaust the stack memory, leading to a Stack Overflow.
What principle does a stack follow?
Last In First Out (LIFO).
What operation adds an element to the top of the stack?
Push.
What operation removes the top element from the stack?
Pop.
What does the ‘Top’ operation do in a stack?
Returns the top element without removing it.
What is a key limitation of a stack implemented using arrays?
It has a fixed size.
What does the constructor in the Stack class initialize?
It initializes the top to -1.
What message is displayed when a stack overflow occurs?
“Stack Overflow”.
What message is displayed when a stack underflow occurs?
“Stack Underflow”.