




























































































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!





























































































Full Practice Test Bank: Data Structures and Algorithms in Java, 6th Edition Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser Publisher: Wiley Edition: 6th Edition (2014) ISBN: 978 - 1 - 118 - 77133 - 4 Content Coverage: All 15 chapters, with 250+ multiple-choice questions, verified answers, and detailed rationales. CHAPTER 1: JAVA PRIMER
Q1. Which of the following is a primitive data type in Java? A) String B) Integer C) int D) Array Answer: C) int Rationale: int is one of the eight primitive data types in Java (byte, short, int, long, float, double, boolean, char). String and Integer are classes (reference types), and arrays are objects. Q2. What is the range of a Java int?
C) int[] arr = int[10]; D) array arr = new array(10); Answer: B) int arr[] = new int[10]; Rationale: In Java, arrays are declared using type[] name or type name[], and instantiated with new type[size]. Option A is incorrect because parentheses are used for constructors of classes, not arrays. Q4. What will be the output of the following code fragment?
int a = 0; int b = 10; while (a < b) { int c = b - a; while (c > a) { c--; a++; } } System.out.println(b - a);A) 1 B) 0 C) The loops never terminate D) 10 Answer: C) The loops never terminate
Rationale: Java String objects are immutable, meaning their value cannot be changed after creation. Any operation that appears to modify a String actually creates a new String object. Q6. What is the output of "SPIDER".substring(1, 4)? A) "PID" B) "SPI" C) "IDE" D) "PIDER" Answer: A) "PID" Rationale: The substring(int beginIndex, int endIndex) method returns the substring from beginIndex (inclusive) to endIndex
(exclusive). For "SPIDER", indices 1, 2, and 3 are 'P', 'I', and 'D', so the result is "PID". Q7. Which keyword is used to prevent a class from being extended? A) static B) final C) abstract D) private Answer: B) final Rationale: The final keyword, when applied to a class, prevents it from being subclassed. static is for class-level members, abstract allows extension but prevents instantiation, and private restricts access.
int[] arr = {1, 2, 3}; for (int x : arr) { x = x 2; } System.out.println(arr[0]);A) 1 B) 2 C) 4 D) 6 Answer: A) 1 Rationale: The enhanced for-loop variable x is a local copy of each element. Modifying x
does not affect the original array. To modify the array, you must use an index. Q10. Which of the following is a correct way to create a generic method that swaps two elements in an array? A) public static void swap(int[] arr, int i, int j) B) public static void swap(T[] arr, int i, int j) C) public static void swap(T[] arr, int i, int j) D) public static swap(T[] arr, int i, int j) Answer: B) public static void swap(T[] arr, int i, int j) Rationale: Generic methods require the type parameter `` before the return type. Option
D) The process of converting one data type to another Answer: B) The process of hiding implementation details and exposing only necessary interfaces Rationale: Encapsulation involves bundling data and methods within a class and restricting direct access to some of the object's components, typically using private fields and public methods. Q12. In Java, which access modifier makes a member accessible only within the same package? A) private B) protected C) public
D) (no modifier, i.e., package-private) Answer: D) (no modifier, i.e., package-private) Rationale: The default access level (no modifier) is package-private, meaning the member is accessible only to classes in the same package. private restricts to the same class, protected allows subclasses and same- package access, and public allows universal access. Q13. What is the purpose of the super keyword in Java? A) To refer to the current object B) To call a method or constructor of the superclass C) To create a new instance of a class
D) A variable being declared with the final keyword Answer: A) A subclass overriding a method of its superclass Rationale: Polymorphism allows objects of different subclasses to be treated as objects of a common superclass, and the appropriate method is called based on the actual object type at runtime. Method overriding is a key mechanism for this. Q15. What is an abstract class? A) A class that cannot be instantiated and may contain abstract methods B) A class that can be instantiated but has no methods
C) A class that is completely implemented and cannot be extended D) A class that contains only static methods Answer: A) A class that cannot be instantiated and may contain abstract methods Rationale: Abstract classes are declared with the abstract keyword. They cannot be instantiated directly and are intended to be subclassed. They may contain abstract methods (without a body) that subclasses must implement. Q16. What is the output of the following code?
class A { void show() { System.out.println("A"); } D) Runtime error Answer: B) B Rationale: This is an example of dynamic method dispatch (polymorphism). The reference type is `A`, but the actual object type is `B`. At runtime, the JVM calls the `show()` method of the actual object, which is `B`'s implementation. Q17. Which of the following is NOT a principle of object-oriented programming? A) Encapsulation B) Inheritance C) Polymorphism D) Compilation Answer: D) Compilation Rationale: The four main principles of OOP are encapsulation, inheritance, polymorphism, and abstraction. Compilation is a process of translating source code into machine code, not an OOP principle. Q18. What is an interface in Java? A) A class that can be instantiated B) A reference type that contains only abstract methods and constants C) A subclass of Object D) A method that is implemented in a class