




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























Q1. Which of the following is NOT a Java keyword? A) class B) interface C) main D) extends Answer: C Rationale: main is a method name, not a keyword. Q2. The default value of an int instance variable is: A) 0 B) null C) 1 D) garbage Answer: A Rationale: Numeric instance variables default to 0. Q3. The size of char in Java is: A) 1 byte B) 2 bytes C) 4 bytes D) 8 bytes Answer: B Rationale: Java char is 16-bit Unicode (2 bytes). Q4. Which OOP principle is achieved by method overloading? A) Inheritance B) Polymorphism C) Encapsulation D) Abstraction Answer: B Rationale: Overloading = compile-time polymorphism. Q5. Method overriding is an example of: A) Compile-time polymorphism B) Run-time polymorphism C) Encapsulation D) Abstraction Answer: B Rationale: Overriding = run-time polymorphism. Q6. Which keyword prevents inheritance? A) static B) final C) private D) abstract
Answer: A Rationale: this = current object. Q13. Which access modifier is most restrictive? A) public B) protected C) default D) private Answer: D Rationale: private = most restrictive. Q14. Which access modifier is least restrictive? A) public B) protected C) default D) private Answer: A Rationale: public = least restrictive. Q15. A constructor: A) Has the same name as the class B) Has no return type C) Both D) Neither Answer: C Rationale: Constructor = class name + no return type. Q16. Constructor overloading means: A) Multiple constructors with different parameters B) Same parameters C) Both D) Neither Answer: A Rationale: Different parameter lists. Q17. A static method: A) Belongs to the class B) Belongs to the object C) Both D) Neither Answer: A Rationale: Static = class-level. Q18. A static method can access:
A) Static members only B) Instance members C) Both D) Neither Answer: A Rationale: Static methods cannot access instance members directly. Q19. The final keyword on a variable makes it: A) Constant B) Mutable C) Both D) Neither Answer: A Rationale: final = constant. Q20. The final keyword on a method prevents: A) Overloading B) Overriding C) Both D) Neither Answer: B Rationale: final method cannot be overridden. Q21. The final keyword on a class prevents: A) Inheritance B) Overloading C) Both D) Neither Answer: A Rationale: final class cannot be extended. Q22. Which of these is a wrapper class? A) int B) Integer C) Both D) Neither Answer: B Rationale: Integer is a wrapper; int is primitive. Q23. Autoboxing is: A) Primitive → wrapper B) Wrapper → primitive C) Both D) Neither Answer: A Rationale: Autoboxing = primitive to wrapper. Q24. Unboxing is:
A) Mutable B) Immutable C) Both D) Neither Answer: A Rationale: StringBuilder is mutable. Q31. Which is faster: StringBuffer or StringBuilder? A) StringBuffer B) StringBuilder C) Same D) Neither Answer: B Rationale: StringBuilder is not synchronized, so faster. Q32. The equals() method compares: A) Content B) Reference C) Both D) Neither Answer: A Rationale: equals() compares content; == compares reference. Q33. The == operator compares: A) Content B) Reference C) Both D) Neither Answer: B Rationale: == compares reference for objects. Q34. The hashCode() method returns: A) Memory address B) Hash value C) Both D) Neither Answer: B Rationale: hashCode() returns hash value. Q35. toString() is defined in: A) Object class B) String class C) Both D) Neither Answer: A Rationale: toString() is in Object. Q36. Every class in Java implicitly extends:
A) Object B) String C) Both D) Neither Answer: A Rationale: Object is the root class. Q37. Which of these is NOT an OOP principle? A) Encapsulation B) Inheritance C) Compilation D) Polymorphism Answer: C Rationale: Compilation is not an OOP principle. Q38. Encapsulation is achieved by: A) Private fields + public methods B) Public fields C) Both D) Neither Answer: A Rationale: Encapsulation = private + public accessors. Q39. Abstraction is achieved by: A) Abstract classes and interfaces B) Public fields C) Both D) Neither Answer: A Rationale: Abstraction = abstract classes/interfaces. Q40. A package is: A) A namespace B) A class C) Both D) Neither Answer: A Rationale: Package = namespace.
Q41. The default value of an int array element is: A) 0 B) null C) 1 D) garbage
Answer: C Rationale: 2D array = array of arrays. Q48. The number of rows in int[][] a = new int[3][4] is: A) 3 B) 4 C) 7 D) 12 Answer: A Rationale: First dimension = rows. Q49. The number of columns in int[][] a = new int[3][4] is: A) 3 B) 4 C) 7 D) 12 Answer: B Rationale: Second dimension = columns. Q50. The total elements in int[][] a = new int[3][4] is: A) 3 B) 4 C) 7 D) 12 Answer: D Rationale: 3 × 4 = 12. Q51. a.length for int[][] a = new int[3][4] is: A) 3 B) 4 C) 7 D) 12 Answer: A Rationale: a.length = number of rows. Q52. a[0].length for int[][] a = new int[3][4] is: A) 3 B) 4 C) 7 D) 12 Answer: B Rationale: a[0].length = number of columns. Q53. A jagged array is: A) Rows of different lengths B) Same lengths C) Both D) Neither
Answer: A Rationale: Jagged = irregular. Q54. Linear search has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1) Answer: A Rationale: Linear = O(n). Q55. Binary search has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1) Answer: B Rationale: Binary = O(log n). Q56. Binary search requires: A) Sorted array B) Unsorted C) Both D) Neither Answer: A Rationale: Binary = sorted. Q57. Bubble sort has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1) Answer: C Rationale: Bubble = O(n²). Q58. Selection sort has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1) Answer: C Rationale: Selection = O(n²). Q59. Insertion sort has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1)
Answer: A Rationale: Uppercase. Q66. String.trim() removes: A) Leading/trailing whitespace B) All whitespace C) Both D) Neither Answer: A Rationale: Trim = leading/trailing. Q67. String.replace('a','b') replaces: A) All 'a' with 'b' B) First C) Both D) Neither Answer: A Rationale: Replace all. Q68. String.split(",") returns: A) Array of substrings B) String C) Both D) Neither Answer: A Rationale: Split = array. Q69. String.equalsIgnoreCase() compares: A) Ignoring case B) Case-sensitive C) Both D) Neither Answer: A Rationale: Ignore case. Q70. String.compareTo() returns: A) int B) String C) boolean D) char Answer: A Rationale: compareTo returns int. Q71. A palindrome is: A) Reads same forwards/backwards B) Different C) Both D) Neither
Answer: A Rationale: Palindrome. Q72. An anagram is: A) Same letters, different order B) Same order C) Both D) Neither Answer: A Rationale: Anagram. Q73. A matrix transpose: A) Swaps rows and columns B) Same C) Both D) Neither Answer: A Rationale: Transpose = swap. Q74. A symmetric matrix satisfies: A) A = Aᵀ B) A = −Aᵀ C) Both D) Neither Answer: A Rationale: Symmetric = A = Aᵀ. Q75. A diagonal matrix has: A) Non-zero diagonal only B) All non-zero C) Both D) Neither Answer: A Rationale: Diagonal = diagonal only. Q76. An identity matrix has: A) 1s on diagonal, 0s elsewhere B) 0s on diagonal C) Both D) Neither Answer: A Rationale: Identity matrix. Q77. Matrix addition requires: A) Same dimensions B) Any dimensions C) Both D) Neither
Rationale: Base case stops recursion. Q83. Without a base case, recursion causes: A) StackOverflowError B) Infinite loop C) Both D) Neither Answer: A Rationale: Stack overflow. Q84. Factorial recursion: n! = A) n × (n−1)! B) n + (n−1)! C) Both D) Neither Answer: A Rationale: Factorial recurrence. Q85. Fibonacci recursion: F(n) = A) F(n−1) + F(n−2) B) F(n−1) × F(n−2) C) Both D) Neither Answer: A Rationale: Fibonacci recurrence. Q86. Recursive binary search has time complexity: A) O(n) B) O(log n) C) O(n²) D) O(1) Answer: B Rationale: Binary = O(log n). Q87. Tower of Hanoi with n disks requires moves: A) 2ⁿ − 1 B) 2ⁿ C) n² D) n! Answer: A Rationale: 2ⁿ − 1. Q88. GCD recursion uses: A) Euclid's algorithm B) Newton's C) Both D) Neither Answer: A
Rationale: Euclid = gcd. Q89. Recursion uses: A) Stack B) Queue C) Both D) Neither Answer: A Rationale: Recursion = stack. Q90. Tail recursion: A) Recursive call is last B) First C) Both D) Neither Answer: A Rationale: Tail = last. Q91. Dynamic programming: A) Stores subproblem results B) Recomputes C) Both D) Neither Answer: A Rationale: DP = memoization. Q92. Memoization is: A) Caching results B) Recomputing C) Both D) Neither Answer: A Rationale: Memoization = cache. Q93. Divide and conquer: A) Splits problem B) Combines results C) Both D) Neither Answer: C Rationale: Divide + conquer. Q94. Merge sort uses: A) Divide and conquer B) Greedy C) Both D) Neither Answer: A
Rationale: Backtracking = explore. Q101. N-Queens uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: N-Queens = backtracking. Q102. A knight's tour uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Knight's tour = backtracking. Q103. A maze solver uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Maze = backtracking. Q104. Sudoku solver uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Sudoku = backtracking. Q105. A permutation generator uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Permutations = backtracking. Q106. A combination generator uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A
Rationale: Combinations = backtracking. Q107. A subset generator uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Subsets = backtracking. Q108. A path finder uses: A) Backtracking B) Greedy C) Both D) Neither Answer: A Rationale: Path = backtracking. Q109. A graph traversal uses: A) BFS/DFS B) Greedy C) Both D) Neither Answer: A Rationale: BFS/DFS. Q110. BFS uses: A) Queue B) Stack C) Both D) Neither Answer: A Rationale: BFS = queue. Q111. DFS uses: A) Queue B) Stack C) Both D) Neither Answer: B Rationale: DFS = stack. Q112. BFS finds: A) Shortest path (unweighted) B) Longest C) Both D) Neither Answer: A