Skip to content

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 catch always
  • 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) StringBuilder is immutable
  • D) == always compares string content safely

Q12. Preferred content comparison for two Java String objects:

  • A) ==
  • B) .equals()
  • C) =
  • D) compare keyword 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

QAnsBrief explanation
1BEncapsulation + access modifiers hide data.
2BOverriding → dynamic dispatch at runtime.
3BClass = blueprint/template.
4BConstructor named like class; no return type.
5Bjavac → bytecode for JVM.
6Cprivate = same class only.
7Bstatic members are class-level.
8BMultiple interfaces OK; single class extend.
9CAbstract classes may have constructors.
10Bfinally usually runs after try/catch.
11BString objects are immutable.
12Bequals compares content.
13Ctuple immutable; list/dict/set mutable.
14BKeys must be hashable.
15BIndentation defines blocks.
16B*args collected as tuple.
17B'w' truncates or creates for write.
18BStack = LIFO.
19BQueue = FIFO.
20BUndo maps naturally to stack.
21CMust walk nodes → O(n).
22BStandard BST ordering.
23BBinary search needs sorted data.
24BHalving each step → O(log n).
25BMerge sort Θ(n log n) typical.
26BBad pivots → O(n²).
27BSelection is O(n²).
28CMerge is stable O(n log n) worst.
29BBase case stops recursion.
30CLinear scan/loop → O(n).
31BDirect indexing → O(1).
32BBFS uses queue.
33BDFS uses stack/recursion.
34BOverloading resolved at compile time.
35Cset stores unique elements.
36BSparse graphs → adjacency lists efficient.
37BChecked exceptions: handle or declare.
38BMutable buffer is efficient in loops.
39CSkewed tree height ≈ n.
40BClassic asymptotic growth order.

Count: 40 MCQs