Topic MCQs — Programming, OOP & Data Structures
40 original MCQs | Mix: Java, Python, OOP, DS | Answers at end
Q1. Which OOP pillar hides internal data using access control?
- A) Inheritance
- B) Encapsulation
- C) Polymorphism
- D) Recursion
Q2. Runtime polymorphism in Java is mainly achieved by:
- A) Method overloading only
- B) Method overriding
- C) Operator overloading (as in C++)
- D) Multiple inheritance of classes
Q3. A class is best described as:
- A) A runtime instance in memory
- B) A blueprint for creating objects
- C) Always equal to one object
- D) A JVM process
Q4. A Java constructor:
- A) Must return
int - B) Has the same name as the class and no return type
- C) Cannot be overloaded
- D) Is called only by the garbage collector
Q5. Java source is compiled mainly into:
- A) Native machine code only
- B) Bytecode executed by the JVM
- C) Python bytecode
- D) HTML only
Q6. Which access modifier allows access only within the same class?
- A) public
- B) protected
- C) private
- D) default
Q7. A static method in Java:
- A) Belongs to each object separately with different copies always
- B) Belongs to the class and is shared
- C) Cannot be called at all
- D) Must override an abstract method
Q8. A Java class can:
- A) Extend multiple classes
- B) Implement multiple interfaces
- C) Neither extend nor implement
- D) Only extend interfaces
Q9. Which is true?
- A) Abstract classes can be instantiated directly
- B) Interfaces cannot be implemented
- C) Abstract classes may contain constructors
- D) Interfaces must always have instance fields
Q10. The finally block in Java:
- A) Runs only if an exception occurs
- B) Typically executes whether or not an exception is thrown
- C) Replaces the need for
catchalways - D) Is used only for inheritance
Q11. Which statement about String in Java is correct?
- A) Strings are mutable by default
- B) Strings are immutable
- C)
StringBuilderis immutable - D)
==always compares string content safely
Q12. Preferred content comparison for two Java String objects:
- A)
== - B)
.equals() - C)
= - D)
comparekeyword only
Q13. In Python, which type is immutable?
- A) list
- B) dict
- C) tuple
- D) set
Q14. Python dict keys must be:
- A) Always lists
- B) Hashable (typically immutable)
- C) Only floating numbers
- D) Duplicated freely as keys
Q15. Python uses indentation primarily to:
- A) Improve color themes only
- B) Define code blocks
- C) Declare variable types
- D) Link C libraries
Q16. In def f(*args):, args inside the function is typically a:
- A) dict
- B) tuple of positional arguments
- C) set
- D) file handle
Q17. Opening a file with mode 'w' in Python will:
- A) Always fail if file exists
- B) Truncate/create for writing
- C) Open read-only
- D) Append only without create
Q18. Stack follows:
- A) FIFO
- B) LIFO
- C) Random access only
- D) Priority by key only
Q19. Queue follows:
- A) LIFO
- B) FIFO
- C) Sorted order always
- D) Binary heap order only
Q20. Which structure is best for undo operations?
- A) Queue
- B) Stack
- C) Only hash table
- D) Only graph
Q21. In a singly linked list, random access by index is typically:
- A) O(1)
- B) O(log n)
- C) O(n)
- D) O(1) with hashing always
Q22. In a Binary Search Tree (typical ordering):
- A) Left child > node > right child
- B) Left child < node < right child
- C) All children equal to root
- D) No ordering rule
Q23. Binary search requires the array to be:
- A) Unsorted
- B) Sorted
- C) Circular only
- D) Linked as a graph
Q24. Worst-case time of binary search on a sorted array of n elements:
- A) O(n)
- B) O(log n)
- C) O(n²)
- D) O(1)
Q25. Average time complexity of merge sort:
- A) O(n)
- B) O(n log n)
- C) O(n²)
- D) O(2ⁿ)
Q26. Worst-case time of quicksort (naive pivot):
- A) O(n log n)
- B) O(n²)
- C) O(log n)
- D) O(1)
Q27. Selection sort’s time complexity is typically:
- A) O(n) best and worst
- B) O(n²) in common cases
- C) O(n log n) always
- D) O(log n)
Q28. Which sorting algorithm is generally stable and O(n log n) worst case?
- A) Quicksort (typical in-place)
- B) Selection sort
- C) Merge sort
- D) Heap sort (typical)
Q29. Recursion requires:
- A) No base case ever
- B) A base case to terminate
- C) Only global variables
- D) A queue instead of stack always
Q30. Time complexity of a simple loop from 1 to n is:
- A) O(1)
- B) O(log n)
- C) O(n)
- D) O(n!)
Q31. Accessing a[i] in an array is typically:
- A) O(n)
- B) O(1)
- C) O(n log n)
- D) O(2ⁿ)
Q32. BFS on a graph commonly uses a:
- A) Stack only
- B) Queue
- C) Only BST
- D) Only StringBuilder
Q33. DFS on a graph commonly uses a:
- A) Queue only
- B) Stack (or recursion call stack)
- C) Only hash set without structure
- D) Only circular array always
Q34. Method overloading is an example of:
- A) Runtime polymorphism only
- B) Compile-time polymorphism
- C) Encapsulation only
- D) Abstraction only
Q35. Which Python structure does not allow duplicate elements?
- A) list
- B) tuple
- C) set
- D) list of lists only
Q36. Adjacency list representation of a graph is often preferred when the graph is:
- A) Extremely dense always
- B) Sparse
- C) Only a single node
- D) Impossible to traverse
Q37. Checked exceptions in Java:
- A) Need not be declared or handled
- B) Must be handled or declared
- C) Are the same as Errors only
- D) Occur only in Python
Q38. StringBuilder is preferred over String concatenation in loops mainly because:
- A) It is immutable
- B) It is mutable and avoids many temporary String objects
- C) It cannot append
- D) It runs outside the JVM
Q39. Height of a skewed BST with n nodes is:
- A) O(1)
- B) O(log n)
- C) O(n)
- D) O(n²)
Q40. Big-O order from slowest-growing to fastest-growing:
- A) O(n²), O(n), O(1)
- B) O(1), O(log n), O(n), O(n log n), O(n²)
- C) O(n!), O(1), O(n)
- D) O(2ⁿ), O(log n), O(1)
Answer Key
| Q | Ans | Brief explanation |
|---|---|---|
| 1 | B | Encapsulation + access modifiers hide data. |
| 2 | B | Overriding → dynamic dispatch at runtime. |
| 3 | B | Class = blueprint/template. |
| 4 | B | Constructor named like class; no return type. |
| 5 | B | javac → bytecode for JVM. |
| 6 | C | private = same class only. |
| 7 | B | static members are class-level. |
| 8 | B | Multiple interfaces OK; single class extend. |
| 9 | C | Abstract classes may have constructors. |
| 10 | B | finally usually runs after try/catch. |
| 11 | B | String objects are immutable. |
| 12 | B | equals compares content. |
| 13 | C | tuple immutable; list/dict/set mutable. |
| 14 | B | Keys must be hashable. |
| 15 | B | Indentation defines blocks. |
| 16 | B | *args collected as tuple. |
| 17 | B | 'w' truncates or creates for write. |
| 18 | B | Stack = LIFO. |
| 19 | B | Queue = FIFO. |
| 20 | B | Undo maps naturally to stack. |
| 21 | C | Must walk nodes → O(n). |
| 22 | B | Standard BST ordering. |
| 23 | B | Binary search needs sorted data. |
| 24 | B | Halving each step → O(log n). |
| 25 | B | Merge sort Θ(n log n) typical. |
| 26 | B | Bad pivots → O(n²). |
| 27 | B | Selection is O(n²). |
| 28 | C | Merge is stable O(n log n) worst. |
| 29 | B | Base case stops recursion. |
| 30 | C | Linear scan/loop → O(n). |
| 31 | B | Direct indexing → O(1). |
| 32 | B | BFS uses queue. |
| 33 | B | DFS uses stack/recursion. |
| 34 | B | Overloading resolved at compile time. |
| 35 | C | set stores unique elements. |
| 36 | B | Sparse graphs → adjacency lists efficient. |
| 37 | B | Checked exceptions: handle or declare. |
| 38 | B | Mutable buffer is efficient in loops. |
| 39 | C | Skewed tree height ≈ n. |
| 40 | B | Classic asymptotic growth order. |
Count: 40 MCQs