





















































































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!






















































































Question 1. Which access modifier allows a class member to be accessed only within its own package and by subclasses in other packages? A) public B) private C) protected D) default Answer: C Explanation: The protected modifier grants package‑private access and also allows access from subclasses, even if they reside in different packages. Question 2. In Java, what is the primary purpose of a copy constructor? A) To create a new object with default values B) To clone an object by copying its field values C) To overload the default constructor D) To prevent inheritance Answer: B Explanation: A copy constructor receives an instance of the same class and copies its fields, creating a new object with the same state. Question 3. Which keyword is used to invoke a superclass constructor from a subclass? A) this B) super C) extends D) base Answer: B
Explanation: super(arguments) calls a constructor of the immediate superclass, enabling proper initialization of inherited fields. Question 4. Which of the following best describes encapsulation? A) Inheriting behavior from a parent class B) Hiding internal state and exposing it through methods C) Defining multiple methods with the same name D) Creating objects at runtime Answer: B Explanation: Encapsulation bundles data and methods together while restricting direct access to the internal state, typically via private fields and public getters/setters. Question 5. What does the final keyword prevent when applied to a method? A) The method from being called B) The method from being overridden in subclasses C) The method from being overloaded D) The method from throwing exceptions Answer: B Explanation: Declaring a method as final stops any subclass from providing a new implementation, preserving its behavior. Question 6. Which principle states that a class should have only one reason to change? A) Open/Closed Principle B) Single Responsibility Principle C) Liskov Substitution Principle D) Dependency Inversion Principle
C) It provides a way to create objects based on a prototype. D) It allows dynamic addition of responsibilities to an object. Answer: A Explanation: Builder separates object construction from representation, enabling step‑by‑step creation of complex objects without exposing the internal assembly process. Question 10. Which of the following is a behavioral design pattern? A) Adapter B) Decorator C) Strategy D) Composite Answer: C Explanation: Strategy encapsulates interchangeable algorithms, allowing the client to select behavior at runtime. Question 11. In Java, which interface should a class implement to allow it to be used as a key in a HashMap? A) Comparable B) Cloneable C) Serializable D) None; it must correctly override hashCode() and equals() Answer: D Explanation: Proper hashCode() and equals() implementations are required for correct key behavior; no specific interface is needed. Question 12. What does the @Override annotation indicate?
A) That the method is abstract B) That the method overloads another method in the same class C) That the method overrides a superclass or interface method D) That the method cannot be overridden further Answer: C Explanation: @Override tells the compiler the method should override a method from a superclass or interface, helping catch signature mismatches. Question 13. Which of the following statements about method overloading is true? A) Overloaded methods must have different return types only. B) Overloaded methods must have different parameter lists. C) Overloaded methods must be declared in different classes. D) Overloaded methods cannot be static. Answer: B Explanation: Overloading occurs when methods share the same name but differ in parameter type, number, or order. Question 14. Which Java keyword is used to prevent a class from being subclassed? A) static B) abstract C) final D) sealed Answer: C Explanation: Declaring a class as final stops any other class from extending it. Question 15. Which principle discourages the duplication of code across a codebase?
Question 18. What is the result of downcasting an object to an incompatible subclass without an explicit check? A) A compile‑time error B) A runtime ClassCastException C) The object is automatically converted D) The code will silently fail Answer: B Explanation: Downcasting to an unrelated type compiles but throws ClassCastException at runtime if the actual object isn’t an instance of the target class. Question 19. Which of the following best describes the Liskov Substitution Principle? A) Subtypes must be replaceable for their base types without altering program correctness. B) Classes should depend on concrete implementations. C) Interfaces should be as large as possible. D) Objects should be immutable wherever possible. Answer: A Explanation: LSP ensures that derived classes can stand in for base classes without breaking expected behavior. Question 20. Which Java feature allows an interface to provide a default implementation for a method? A) static methods B) abstract methods C) default methods (Java 8+) D) final methods Answer: C
Explanation: Default methods let interfaces supply a body, enabling backward compatibility and multiple inheritance of behavior. Question 21. What does the transient keyword indicate when applied to a field? A) The field is static. B) The field should be ignored during serialization. C) The field cannot be accessed outside its class. D) The field is final. Answer: B Explanation: transient marks a field to be skipped when an object is serialized, preventing its value from being persisted. Question 22. Which design pattern is most appropriate for converting an existing class’s interface into one that a client expects? A) Adapter B) Facade C) Proxy D) Bridge Answer: A Explanation: The Adapter pattern wraps an incompatible class and translates its interface to the one required by the client. Question 23. In Java generics, what does the wildcard ? super T represent? A) Any type that is a subclass of T B) Any type that implements T C) Any type that is a supertype of T
B) It allows multiple threads to execute the method concurrently. C) It acquires the object's monitor before execution, ensuring exclusive access. D) It disables garbage collection for the method’s objects. Answer: C Explanation: synchronized obtains the intrinsic lock (monitor) of the instance (or class for static methods), preventing concurrent entry by other threads. Question 27. Which principle encourages keeping designs simple and avoiding unnecessary complexity? A) KISS B) YAGNI C) SOLID D) DRY Answer: A Explanation: KISS (Keep It Simple, Stupid) promotes straightforward solutions over elaborate ones. Question 28. In the Factory Method pattern, who decides which concrete product class to instantiate? A) The client code directly uses new. B) The abstract creator class implements the logic. C) A concrete subclass overrides the factory method. D) The Java runtime decides based on reflection. Answer: C Explanation: Subclasses override the factory method to return instances of specific concrete products.
Question 29. Which of the following is true about the HashMap implementation? A) It maintains insertion order. B) It is synchronized by default. C) It uses the hashCode() of keys to locate entries. D) It does not allow null values. Answer: C Explanation: HashMap computes a hash of the key’s hashCode() to place entries into buckets. Question 30. What is the purpose of the java.io.Serializable marker interface? A) To provide a default serialization method implementation. B) To indicate that a class can be converted to a byte stream. C) To enforce that all fields are primitive types. D) To enable cloning of objects. Answer: B Explanation: Implementing Serializable signals the JVM that the class’s instances may be serialized; no methods are required. Question 31. Which design pattern is best suited for adding responsibilities to individual objects without affecting others? A) Decorator B) Composite C) Flyweight D) Mediator Answer: A Explanation: The Decorator pattern wraps an object and adds behavior dynamically, preserving the original object's interface.
Explanation: volatile ensures that reads/writes go directly to main memory, making updates visible to other threads immediately. Question 35. Which principle promotes building systems that are open for extension but closed for modification? A) Liskov Substitution Principle B) Open/Closed Principle C) Interface Segregation Principle D) Dependency Inversion Principle Answer: B Explanation: OCP encourages extending behavior via new subclasses or implementations rather than altering existing code. Question 36. Which of the following is a correct way to declare a generic class that accepts any type extending Number? A) class Box<T extends Number> B) class Box<T super Number> C) class Box<T implements Number> D) class Box<T : Number> Answer: A Explanation: The bounded type parameter syntax T extends Number restricts T to Number or its subclasses. Question 37. In the context of software design, what does high cohesion mean? A) A class has many responsibilities. B) Elements within a module are closely related in functionality. C) Modules depend heavily on each other.
D) The system has many layers of abstraction. Answer: B Explanation: High cohesion indicates that a module’s responsibilities are tightly related, improving maintainability. Question 38. Which method is invoked automatically when an object is created using the new keyword? A) finalize() B) clone() C) constructor D) static block Answer: C Explanation: The constructor (default, parameterized, or copy) runs during object creation to initialize state. Question 39. Which design pattern provides a simplified interface to a complex subsystem? A) Facade B) Proxy C) Bridge D) Chain of Responsibility Answer: A Explanation: Facade defines a higher‑level interface that makes the subsystem easier to use. Question 40. What is the effect of declaring a class field as static? A) Each instance gets its own copy. B) The field belongs to the class, not to any instance.
A) Flyweight B) Prototype C) Builder D) Singleton Answer: A Explanation: Flyweight shares intrinsic state among many objects, minimizing memory usage. Question 44. What does the instanceof operator evaluate? A) Whether a class implements an interface. B) Whether an object reference is null. C) Whether an object is an instance of a specified type. D) Whether a class is abstract. Answer: C Explanation: instanceof returns true if the left‑hand operand is a non‑null reference that can be cast to the right‑hand type. Question 45. Which of the following best illustrates the principle of composition over inheritance? A) Extending ArrayList to add a custom sorting method. B) Creating a Car class that contains an Engine object rather than inheriting from Engine. C) Using a static utility class for common functions. D) Declaring all methods in an interface and implementing them in a subclass. Answer: B Explanation: Composition (has‑a) – a Car has an Engine – provides flexibility and avoids the tight coupling of inheritance.
Question 46. Which collection implementation maintains elements in the order they were inserted and allows duplicate entries? A) HashSet B) TreeSet C) LinkedHashSet D) ArrayList Answer: D Explanation: ArrayList preserves insertion order and permits duplicates. Question 47. What is the purpose of the throws clause in a method signature? A) To catch an exception inside the method. B) To indicate that the method may propagate the listed checked exceptions to its caller. C) To convert a checked exception into an unchecked one. D) To re‑throw an exception after handling it. Answer: B Explanation: throws declares that the method does not handle the specified checked exceptions, delegating responsibility to the caller. Question 48. Which of the following is a functional interface introduced in Java 8? A) Runnable B) Comparator C) Callable D) All of the above Answer: D Explanation: All listed interfaces have a single abstract method, making them functional interfaces suitable for lambda expressions.
Explanation: It provides an unmodifiable wrapper; attempts to modify the view throw UnsupportedOperationException. Question 52. Which exception is thrown when a thread attempts to access an object that it does not have permission to? A) IllegalAccessException B) SecurityException C) IllegalStateException D) AccessControlException Answer: B Explanation: SecurityException indicates a security violation, such as illegal access to a protected resource. Question 53. In Java, which statement about the enum type is correct? A) Enums can extend other classes. B) Enums are implicitly final. C) Enums cannot implement interfaces. D) Enums cannot have constructors. Answer: B Explanation: Enum types are implicitly final; they cannot be subclassed, though they can implement interfaces and have private constructors. Question 54. Which design pattern decouples an abstraction from its implementation so that the two can vary independently? A) Bridge B) Adapter C) Proxy
D) Composite Answer: A Explanation: Bridge separates the abstraction hierarchy from the implementation hierarchy, allowing each to evolve without affecting the other. Question 55. Which of the following statements about java.util.Optional is false? A) It can contain a null reference. B) It provides methods to avoid NullPointerException. C) It is a container object which may or may not contain a non‑null value. D) It was introduced in Java 8. Answer: A Explanation: Optional never holds a null; an empty Optional represents the absence of a value. Question 56. What is the primary benefit of using the java.nio.file.Files class over older java.io classes? A) Automatic serialization of objects. B) Support for asynchronous I/O and better exception handling. C) Ability to read/write only text files. D) Built‑in GUI components. Answer: B Explanation: NIO.2 (java.nio.file) offers richer functionality, including atomic moves, better error reporting, and optional asynchronous channels. Question 57. Which of the following is a correct way to create an immutable class in Java? A) Declare all fields as public.