Online Coding Lessons Data Structures and Algorithms 1 — Questions and Answers
Question 1: What is an array in programming?
- A single variable that holds one value
- A collection of elements stored at contiguous memory locations (Correct answer)
- A function that sorts numbers
- A type of loop
Correct answer: A collection of elements stored at contiguous memory locations
An array is a data structure that stores a fixed-size collection of elements of the same type at contiguous memory locations.
Question 2: What is the time complexity of a linear search algorithm?
- O(1)
- O(log n)
- O(n) (Correct answer)
- O(n²)
Correct answer: O(n)
Linear search has O(n) time complexity because in the worst case, it must check every element in the list once.
Question 3: What is a stack data structure?
- A structure where elements are added and removed from the front
- A structure where elements follow Last-In-First-Out (LIFO) order (Correct answer)
- A sorted list of elements
- A collection of key-value pairs
Correct answer: A structure where elements follow Last-In-First-Out (LIFO) order
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where the last element added is the first to be removed.
Question 4: Which sorting algorithm works by repeatedly swapping adjacent elements if they are in the wrong order?
- Merge Sort
- Quick Sort
- Bubble Sort (Correct answer)
- Insertion Sort
Correct answer: Bubble Sort
Bubble Sort works by repeatedly comparing and swapping adjacent elements until the list is sorted, causing larger values to 'bubble' to the end.
Question 5: What is a queue data structure?
- Elements are removed from where they were added
- Elements follow First-In-First-Out (FIFO) order (Correct answer)
- Elements are sorted automatically
- A hierarchical structure
Correct answer: Elements follow First-In-First-Out (FIFO) order
A queue is a data structure that follows the First-In-First-Out (FIFO) principle, like a line of people waiting — the first in is the first out.
Question 6: What does 'index' mean in the context of an array?
- The total number of elements
- The position of an element within the array (Correct answer)
- The value stored in the array
- The type of data stored
Correct answer: The position of an element within the array
An index is a numerical position used to access a specific element in an array, typically starting from 0.
What is an array in programming?