Advanced Object-Oriented Programming Practice Quiz
- Which of the following is a core concept of Object-Oriented Programming (OOP)?
- A) Procedures
- B) Encapsulation
- C) Arrays
- D) Conditional Statements
Answer: B) Encapsulation
- What does inheritance allow in OOP?
- A) Code duplication
- B) Reusability of code
- C) Data hiding
- D) Function overloading
Answer: B) Reusability of code
- Which of the following is NOT a type of polymorphism in OOP?
- A) Compile-time polymorphism
- B) Runtime polymorphism
- C) Data polymorphism
- D) Method overloading
Answer: C) Data polymorphism
- In OOP, what is encapsulation primarily used for?
- A) Improving performance
- B) Hiding the internal state of an object
- C) Reusing code
- D) Supporting multiple inheritance
Answer: B) Hiding the internal state of an object
- Which of the following keywords is used to declare an abstract class in Java?
- A) abstract
- B) interface
- C) class
- D) void
Answer: A) abstract
- What is the main difference between a class and an interface in Java?
- A) A class can have methods with implementation, while an interface can only have method declarations.
- B) A class can only inherit from one class, while an interface can inherit from multiple interfaces.
- C) A class cannot have fields, but an interface can.
- D) There is no difference; they are the same in Java.
Answer: A) A class can have methods with implementation, while an interface can only have method declarations.
- Which of the following best describes the concept of polymorphism in OOP?
- A) An object can assume many forms.
- B) An object can inherit from multiple classes.
- C) A class can contain multiple constructors.
- D) An object’s state is hidden.
Answer: A) An object can assume many forms.
- In OOP, what does the “this” keyword refer to?
- A) The current class
- B) The current object
- C) A reference variable
- D) The superclass
Answer: B) The current object
- What is the purpose of a constructor in OOP?
- A) To initialize the object
- B) To destroy the object
- C) To define methods
- D) To handle exceptions
Answer: A) To initialize the object
- Which of the following is true about a static method in Java?
- A) It can access instance variables of the class.
- B) It can be called without creating an instance of the class.
- C) It requires an instance to be invoked.
- D) It cannot be overridden.
Answer: B) It can be called without creating an instance of the class.
- What does method overloading mean in OOP?
- A) Methods with the same name and different return types
- B) Methods with the same name and different parameters
- C) Methods with the same name and the same parameters
- D) Methods with different names and the same parameters
Answer: B) Methods with the same name and different parameters
- Which access modifier allows a member to be accessible only within its own package?
- A) public
- B) private
- C) protected
- D) default
Answer: D) default
- Which of the following statements about inheritance is correct?
- A) A subclass can access all private members of its superclass.
- B) A subclass can only access public members of its superclass.
- C) Inheritance does not allow access to the superclass’s members.
- D) A subclass can override final methods of the superclass.
Answer: B) A subclass can only access public members of its superclass.
- In Java, what does the term “abstract” mean for a method?
- A) The method cannot be inherited.
- B) The method does not have a body and must be implemented by subclasses.
- C) The method has no parameters.
- D) The method is private and cannot be overridden.
Answer: B) The method does not have a body and must be implemented by subclasses.
- What is the purpose of the “super” keyword in Java?
- A) To call the constructor of the current class
- B) To call the constructor of the superclass
- C) To define a superclass
- D) To create a new instance of a class
Answer: B) To call the constructor of the superclass
- Which of the following is a feature of an abstract class?
- A) It can have both abstract and non-abstract methods.
- B) It cannot have fields.
- C) It can only inherit from interfaces.
- D) It cannot be extended by other classes.
Answer: A) It can have both abstract and non-abstract methods.
- What is the main advantage of using interfaces in Java?
- A) They provide a mechanism for method overloading.
- B) They allow multiple inheritance of behavior.
- C) They can provide default implementations of methods.
- D) They reduce memory usage.
Answer: B) They allow multiple inheritance of behavior.
- In Java, which method is used to start a thread?
- A) run()
- B) execute()
- C) start()
- D) begin()
Answer: C) start()
- Which of the following is true for an interface?
- A) An interface can extend a class.
- B) All methods in an interface must be abstract by default.
- C) An interface can have instance variables.
- D) An interface can provide method implementations.
Answer: B) All methods in an interface must be abstract by default.
- Which of the following is an example of method overriding in Java?
- A) A subclass method with the same name, same parameter types, but different return type.
- B) A subclass method with the same name and the same parameter types as the superclass method.
- C) A subclass method with a different name but similar functionality.
- D) A superclass method being made static.
Answer: B) A subclass method with the same name and the same parameter types as the superclass method.
- What is the primary difference between a deep copy and a shallow copy in object cloning?
- A) A shallow copy creates copies of the objects, while a deep copy references the original object.
- B) A deep copy copies all the objects and their references, while a shallow copy copies only the references.
- C) A deep copy can be done only for primitive types, while a shallow copy can be done for objects.
- D) There is no difference between the two.
Answer: B) A deep copy copies all the objects and their references, while a shallow copy copies only the references.
- What is the purpose of the “instanceof” operator in Java?
- A) To check if an object is an instance of a class or implements an interface
- B) To instantiate a new object
- C) To create a new class
- D) To return the class type of an object
Answer: A) To check if an object is an instance of a class or implements an interface
- Which of the following is true about the “final” keyword in Java?
- A) A final class cannot be subclassed.
- B) A final method cannot be overridden.
- C) A final variable cannot be reassigned.
- D) All of the above
Answer: D) All of the above
- Which of the following is NOT an advantage of using inheritance?
- A) Code reusability
- B) Hierarchical classification
- C) Tight coupling
- D) Extending functionality
Answer: C) Tight coupling
- What is the result of trying to access a private member from a subclass in Java?
- A) Compile-time error
- B) The value is accessible, but cannot be modified
- C) Runtime error
- D) The value can be accessed and modified
Answer: A) Compile-time error
- What is the purpose of the “clone()” method in Java?
- A) To create a new object of the same class
- B) To create an exact copy of the object
- C) To copy the object’s variables only
- D) To compare two objects
Answer: B) To create an exact copy of the object
- What does the “static” keyword mean when used in a class method?
- A) The method is not associated with any instance of the class.
- B) The method can be called only from an instance of the class.
- C) The method must be overridden by subclasses.
- D) The method can be called without an object but requires a reference variable.
Answer: A) The method is not associated with any instance of the class.
- Which of the following terms refers to the ability to redefine a method in a subclass?
- A) Polymorphism
- B) Inheritance
- C) Abstraction
- D) Encapsulation
Answer: A) Polymorphism
- What does the “super()” keyword do?
- A) Calls the constructor of the current class
- B) Calls the constructor of the superclass
- C) Calls the constructor of the subclass
- D) Accesses the superclass methods
Answer: B) Calls the constructor of the superclass
- In Java, which of the following can be inherited?
- A) Variables
- B) Methods
- C) Constructors
- D) A and B only
Answer: D) A and B only
- Which of the following can be used to restrict inheritance in Java?
- A) private
- B) static
- C) final
- D) transient
Answer: C) final
- In Java, which of the following types of inheritance is not allowed?
- A) Single inheritance
- B) Multiple inheritance (using interfaces)
- C) Multiple inheritance (using classes)
- D) Multilevel inheritance
Answer: C) Multiple inheritance (using classes)
- What happens when an object is passed to a method by reference in Java?
- A) A new object is created inside the method.
- B) The original object is modified outside the method.
- C) The original object is not modified.
- D) The method does not have access to the object.
Answer: B) The original object is modified outside the method.
- What does the “default” keyword in an interface provide?
- A) A method with no implementation
- B) A default constructor
- C) A method with a default implementation
- D) A default value for variables
Answer: C) A method with a default implementation
- What is the output of calling super() in a constructor of a subclass?
- A) It will call the constructor of the superclass with no arguments.
- B) It will call the constructor of the superclass with arguments.
- C) It will cause a compilation error.
- D) It will call the constructor of the current class.
Answer: A) It will call the constructor of the superclass with no arguments.
- Which of the following will be the correct implementation of a singleton class in Java?
- A) Using a private constructor and a public static method
- B) Using a public constructor and a private static method
- C) Using a static constructor
- D) Using a public static instance variable only
Answer: A) Using a private constructor and a public static method
- Which of the following is NOT a feature of an interface in Java?
- A) It can have only abstract methods.
- B) It can have static methods.
- C) It can be implemented by multiple classes.
- D) It can have instance variables.
Answer: D) It can have instance variables.
- What is the role of the instanceof operator in Java?
- A) To check if a class is an instance of a subclass.
- B) To check whether an object is an instance of a specific class or implements an interface.
- C) To create a new instance of an object.
- D) To check whether a method exists in a class.
Answer: B) To check whether an object is an instance of a specific class or implements an interface.
- Which of the following is the correct way to call an overridden method from the superclass in Java?
- A) super.methodName();
- B) this.methodName();
- C) methodName();
- D) superCall.methodName();
Answer: A) super.methodName();
- In the context of Java, which of the following statements is true regarding method overriding?
- A) The method signature must be exactly the same as in the superclass.
- B) The return type can be different from the superclass.
- C) The method can have fewer parameters than in the superclass.
- D) The method must have the same name, return type, and parameters.
Answer: A) The method signature must be exactly the same as in the superclass.
- Which of the following methods is used to compare two objects for equality in Java?
- A) compare()
- B) equals()
- C) ==
- D) isEqual()
Answer: B) equals()
- Which of the following access modifiers allows a class to be visible only to classes within the same package?
- A) private
- B) public
- C) protected
- D) default
Answer: D) default
- What does the “volatile” keyword do in Java?
- A) Makes a variable accessible to only one thread.
- B) Ensures a variable is not cached locally in a thread.
- C) Makes a variable constant.
- D) Makes a variable inaccessible to subclasses.
Answer: B) Ensures a variable is not cached locally in a thread.
- What does it mean for a class to be “immutable” in Java?
- A) The class cannot be extended.
- B) The objects of the class cannot be modified after creation.
- C) The class can only have one instance.
- D) The class does not have any constructors.
Answer: B) The objects of the class cannot be modified after creation.
- Which of the following is the correct way to declare an abstract method in a Java interface?
- A) abstract void method();
- B) void abstract method();
- C) abstract void method() { }
- D) void method() { }
Answer: A) abstract void method();
- What is a “factory method” in object-oriented design?
- A) A method that instantiates an object based on some input parameters.
- B) A method used to create a new subclass.
- C) A method that factory objects with default behavior.
- D) A method used to store object instances.
Answer: A) A method that instantiates an object based on some input parameters.
- Which of the following best describes the principle of “composition” in OOP?
- A) An object is composed of multiple methods.
- B) An object is composed of several subclasses.
- C) An object is composed of other objects, each performing a distinct function.
- D) An object is composed of several interfaces.
Answer: C) An object is composed of other objects, each performing a distinct function.
- What happens if a subclass does not implement an abstract method of an interface in Java?
- A) The code will compile without any issues.
- B) The code will compile, but the method will throw an exception.
- C) The subclass will be treated as an abstract class.
- D) The code will not compile.
Answer: D) The code will not compile.
- Which of the following statements is true regarding a Java constructor?
- A) A constructor can return a value.
- B) A constructor must have the same name as the class.
- C) A constructor cannot be overloaded.
- D) A constructor is called only once during the class’s lifecycle.
Answer: B) A constructor must have the same name as the class.
- Which of the following is an example of a correct constructor chaining in Java?
- A) Calling the constructor of the subclass from the superclass.
- B) Calling one constructor of the same class from another constructor of the same class.
- C) Calling a superclass constructor from the subclass using this().
- D) Calling a superclass constructor from the subclass using super() or this().
Answer: B) Calling one constructor of the same class from another constructor of the same class.
- Which of the following OOP principles enables hiding the internal state of an object and only exposing necessary behavior to the outside world?
- A) Polymorphism
- B) Encapsulation
- C) Inheritance
- D) Abstraction
Answer: B) Encapsulation
- What is the purpose of the “final” keyword when used on a variable in Java?
- A) The variable can be assigned only once.
- B) The variable can be modified, but cannot be overridden.
- C) The variable is a constant, but can be reassigned.
- D) The variable can only be accessed by the class.
Answer: A) The variable can be assigned only once.
- Which of the following is the purpose of a “getter” method in OOP?
- A) To retrieve an object’s state.
- B) To initialize an object’s state.
- C) To modify an object’s state.
- D) To compare two objects for equality.
Answer: A) To retrieve an object’s state.
- What does the “abstract” keyword mean when used in a class definition?
- A) The class cannot be instantiated.
- B) The class can only have abstract methods.
- C) The class can only be extended by interfaces.
- D) The class must implement all abstract methods.
Answer: A) The class cannot be instantiated.
- Which of the following is NOT a feature of Java’s “Object” class?
- A) The toString() method.
- B) The equals() method.
- C) The hashCode() method.
- D) The clone() method.
Answer: D) The clone() method.
- In the context of OOP, which of the following is an example of “polymorphism”?
- A) Multiple classes sharing a single method name, but each with different implementations.
- B) A class that can inherit from multiple classes.
- C) A class using multiple constructors to instantiate objects.
- D) A class with the same name in multiple packages.
Answer: A) Multiple classes sharing a single method name, but each with different implementations.
- What is the purpose of the “transient” keyword in Java?
- A) It ensures that a variable is not serialized.
- B) It ensures that a variable is not assigned a value.
- C) It makes the variable volatile.
- D) It ensures that a variable is immutable.
Answer: A) It ensures that a variable is not serialized.
- Which of the following is true regarding the relationship between objects in composition?
- A) The composed object can exist independently of the containing object.
- B) The composed object cannot exist without the containing object.
- C) Composition creates a one-to-one relationship between objects.
- D) Composition is a form of inheritance.
Answer: B) The composed object cannot exist without the containing object.
- Which of the following is an example of the “open/closed principle” in OOP?
- A) Extending a class without modifying its existing code.
- B) Modifying the behavior of an existing class.
- C) Directly editing a class’s fields.
- D) Using a single class to handle all tasks.
Answer: A) Extending a class without modifying its existing code.
- What does “method hiding” mean in OOP?
- A) A method in a subclass with the same signature as a method in the superclass.
- B) A method in the superclass is inaccessible from the subclass.
- C) A method in the subclass is hidden from the superclass.
- D) A method is hidden because of access control modifiers.
Answer: A) A method in a subclass with the same signature as a method in the superclass.
- Which of the following best describes the concept of “polymorphism” in OOP?
- A) Multiple methods with the same name but different implementations.
- B) One method in a class.
- C) Multiple classes with identical functionality.
- D) A class that extends multiple classes.
Answer: A) Multiple methods with the same name but different implementations.
- What is the purpose of a “constructor” in a class?
- A) To provide a default value for an object.
- B) To initialize objects of a class.
- C) To allow an object to perform specific tasks.
- D) To create a clone of an object.
Answer: B) To initialize objects of a class.
- In Java, what does the term “encapsulation” refer to?
- A) Hiding internal data and exposing only necessary functionality.
- B) Reusing a method across classes.
- C) The ability to create multiple classes with the same behavior.
- D) Having multiple inheritance paths.
Answer: A) Hiding internal data and exposing only necessary functionality.
- Which of the following is true about interfaces in Java?
- A) An interface can have instance fields.
- B) An interface can provide implementation for its methods.
- C) A class can implement multiple interfaces.
- D) A class can extend multiple interfaces.
Answer: C) A class can implement multiple interfaces.
- What does the “this” keyword represent in Java?
- A) A reference to the current class.
- B) A reference to the current method.
- C) A reference to the current object.
- D) A reference to the parent class.
Answer: C) A reference to the current object.
- Which of the following best describes “inheritance” in OOP?
- A) The process by which one class acquires the properties and behaviors of another class.
- B) The process of hiding the internal workings of a class.
- C) The ability to create multiple versions of a method.
- D) The creation of a new class by combining two classes.
Answer: A) The process by which one class acquires the properties and behaviors of another class.
- In the context of Java, what does the “super” keyword do?
- A) Refers to the parent class’s constructor.
- B) Refers to the current instance of the class.
- C) Refers to the method in the current class.
- D) Refers to a sibling class.
Answer: A) Refers to the parent class’s constructor.
- Which of the following statements is true regarding the “final” keyword when used with a method in Java?
- A) It prevents the method from being overridden in a subclass.
- B) It allows the method to be modified.
- C) It makes the method static.
- D) It makes the method abstract.
Answer: A) It prevents the method from being overridden in a subclass.
- Which of the following is a valid way to call a method from an interface in Java?
- A) Directly invoking the method from the interface.
- B) Using super to invoke the method from the interface.
- C) Using this to invoke the method from the interface.
- D) Implementing the method in a class and invoking it from an object of that class.
Answer: D) Implementing the method in a class and invoking it from an object of that class.
- What is the term for hiding the implementation details of a class and showing only the necessary functionality?
- A) Abstraction
- B) Inheritance
- C) Encapsulation
- D) Polymorphism
Answer: A) Abstraction
- What will happen if a subclass attempts to call a private method of its superclass in Java?
- A) It will result in a compilation error.
- B) The method will be called successfully.
- C) It will result in a runtime error.
- D) It will call the method but not display any output.
Answer: A) It will result in a compilation error.
- Which of the following correctly describes a static method in Java?
- A) It can only be called by instances of the class.
- B) It can be called using the class name or an object reference.
- C) It can be called only from a method in the same class.
- D) It can access instance variables of the class directly.
Answer: B) It can be called using the class name or an object reference.
- What is the effect of making a class “abstract” in Java?
- A) It prevents the class from being instantiated.
- B) It makes the class accessible only within its package.
- C) It automatically defines abstract methods.
- D) It prevents the class from being inherited.
Answer: A) It prevents the class from being instantiated.
- In Java, what happens if a subclass does not override a method defined in an interface?
- A) The program will fail to compile.
- B) The subclass will inherit the method from the interface.
- C) The method from the interface will be invoked as-is.
- D) The subclass will be required to implement the method.
Answer: D) The subclass will be required to implement the method.
- Which of the following is NOT a valid method for a class to override in Java?
- A) toString()
- B) equals()
- C) hashCode()
- D) clone()
Answer: D) clone()
- In Java, what is the difference between an abstract class and an interface?
- A) An abstract class can have concrete methods, while an interface cannot.
- B) An abstract class cannot have methods, while an interface can.
- C) An abstract class can be extended, but an interface cannot.
- D) An interface can be extended by another interface, but an abstract class cannot.
Answer: A) An abstract class can have concrete methods, while an interface cannot.
- What is the purpose of the instanceof operator in Java?
- A) To check the type of an object at runtime.
- B) To create a new instance of a class.
- C) To convert one class type into another.
- D) To compare two objects for equality.
Answer: A) To check the type of an object at runtime.
- What does method overloading allow you to do in Java?
- A) It allows multiple methods with the same name but different parameter lists.
- B) It allows multiple methods with the same signature.
- C) It allows methods from different classes to be merged.
- D) It allows a method to be invoked using multiple instances.
Answer: A) It allows multiple methods with the same name but different parameter lists.
- Which of the following is the primary purpose of the final keyword in Java when applied to a class?
- A) The class cannot be extended.
- B) The class cannot be instantiated.
- C) The class cannot be abstract.
- D) The class can only contain final variables.
Answer: A) The class cannot be extended.
- What happens when you invoke super() in a constructor of a subclass in Java?
- A) It calls the constructor of the superclass.
- B) It invokes the constructor of the subclass.
- C) It calls a method from the superclass.
- D) It invokes a constructor of the current class.
Answer: A) It calls the constructor of the superclass.
- Which of the following is true about method overriding in Java?
- A) The method signature must match exactly between the superclass and subclass.
- B) The return type must be the same or a subtype of the return type in the superclass.
- C) The overriding method can have a different name from the method in the superclass.
- D) The access modifier can be more restrictive than the superclass method.
Answer: B) The return type must be the same or a subtype of the return type in the superclass.
- In which of the following cases will a NullPointerException be thrown in Java?
- A) Calling a method on a null object reference.
- B) Attempting to access an index in an empty array.
- C) Dividing by zero.
- D) Using an undefined variable.
Answer: A) Calling a method on a null object reference.
- Which of the following is NOT true about constructors in Java?
- A) A constructor must have the same name as the class.
- B) A constructor can return a value.
- C) A constructor can have parameters.
- D) A constructor is automatically called when an object is created.
Answer: B) A constructor can return a value.
- What happens when an object is passed by reference to a method in Java?
- A) A copy of the object is created inside the method.
- B) The original object can be modified inside the method.
- C) The object cannot be modified inside the method.
- D) The method cannot access the object.
Answer: B) The original object can be modified inside the method.
- Which of the following is a correct way to create an array of objects in Java?
- A) Object[] arr = new Object[10];
- B) Object arr = new Object[10];
- C) Object arr[] = new Object(10);
- D) Object[] arr = new Object[10];
Answer: A) Object[] arr = new Object[10];
- What is the effect of using a protected access modifier for a variable in Java?
- A) The variable is only accessible within its own class.
- B) The variable is accessible by classes within the same package.
- C) The variable is accessible by subclasses and classes in the same package.
- D) The variable is accessible by any class.
Answer: C) The variable is accessible by subclasses and classes in the same package.
- What will happen if a subclass does not implement an abstract method of an interface in Java?
- A) The program will compile successfully.
- B) The program will throw a runtime exception.
- C) The program will fail to compile.
- D) The method will be automatically implemented by the superclass.
Answer: C) The program will fail to compile.
- Which of the following can be used to prevent method overriding in Java?
- A) protected
- B) private
- C) final
- D) static
Answer: C) final
- What does the term “overriding” mean in the context of OOP?
- A) A method in the subclass has the same name, return type, and parameters as a method in the superclass.
- B) A subclass calls a method from its parent class.
- C) A method in the superclass has a different return type than the subclass method.
- D) A subclass replaces the parent class with a new class.
Answer: A) A method in the subclass has the same name, return type, and parameters as a method in the superclass.
- What is a common use case of an abstract class in Java?
- A) To define a base class with some method implementations but leave others abstract for subclasses.
- B) To allow objects to be created directly from the abstract class.
- C) To define a class with only static methods.
- D) To define a class that cannot be inherited.
Answer: A) To define a base class with some method implementations but leave others abstract for subclasses.
- What is the main benefit of using an interface in Java?
- A) To allow multiple inheritance.
- B) To implement abstract methods.
- C) To provide a common set of methods for all implementing classes.
- D) To inherit methods from multiple classes.
Answer: C) To provide a common set of methods for all implementing classes.
- Which of the following is true about a static block in Java?
- A) It is executed when an object of the class is created.
- B) It is executed when the class is loaded into memory.
- C) It is executed only once when the class is compiled.
- D) It cannot access static members of the class.
Answer: B) It is executed when the class is loaded into memory.
- Which of the following is a key feature of the Singleton Design Pattern?
- A) It ensures a class has only one instance and provides a global point of access to it.
- B) It allows a class to inherit from multiple classes.
- C) It allows a class to have multiple constructors.
- D) It provides a template for other classes to follow.
Answer: A) It ensures a class has only one instance and provides a global point of access to it.
- What happens when a subclass does not override a method from its parent class in Java?
- A) The parent class method is inherited by the subclass and used as is.
- B) A compilation error occurs because all methods must be overridden.
- C) The subclass must declare the method as abstract.
- D) The subclass can implement the method with its own logic.
Answer: A) The parent class method is inherited by the subclass and used as is.
- What is the primary difference between == and .equals() in Java?
- A) == compares object references, while .equals() compares the content of objects.
- B) == compares the content of objects, while .equals() compares references.
- C) == works only with primitive types, while .equals() works only with objects.
- D) There is no difference between == and .equals().
Answer: A) == compares object references, while .equals() compares the content of objects.
- Which of the following best describes a “factory method” in object-oriented design?
- A) A method that initializes an object in a subclass.
- B) A method used to create objects without specifying the exact class of object that will be created.
- C) A method that constructs a class object directly.
- D) A method used to destroy objects after they are no longer needed.
Answer: B) A method used to create objects without specifying the exact class of object that will be created.
- Which of the following best defines “composition” in object-oriented programming?
- A) A subclass inherits from a superclass.
- B) A class includes objects of other classes as its members.
- C) A class implements multiple interfaces.
- D) A class defines methods that are implemented by other classes.
Answer: B) A class includes objects of other classes as its members.
- In Java, which of the following is true about method overriding?
- A) The method in the subclass must have the same signature as the method in the superclass.
- B) The method in the subclass can have a different name.
- C) The method in the subclass can have a different return type.
- D) Method overriding can occur only with private methods.
Answer: A) The method in the subclass must have the same signature as the method in the superclass.
- Which of the following statements about Java’s exception handling is true?
- A) Exception handling is based on the principle of try-catch blocks.
- B) Java does not allow checked exceptions.
- C) Exceptions are only thrown by the throw keyword.
- D) Unchecked exceptions are only used in runtime scenarios.
Answer: A) Exception handling is based on the principle of try-catch blocks.
- What does the “abstract” keyword indicate when used on a method in Java?
- A) The method has no implementation in the current class and must be implemented by subclasses.
- B) The method is protected and cannot be overridden.
- C) The method is final and cannot be overridden by subclasses.
- D) The method is static and belongs to the class itself.
Answer: A) The method has no implementation in the current class and must be implemented by subclasses.
- In Java, what is the difference between ArrayList and LinkedList?
- A) ArrayList is backed by an array, and LinkedList is backed by a doubly linked list.
- B) ArrayList is slower than LinkedList for inserting and deleting elements.
- C) ArrayList allows faster access to elements compared to LinkedList.
- D) All of the above are true.
Answer: D) All of the above are true.
- Which of the following is a characteristic of the Proxy Design Pattern?
- A) It provides a surrogate or placeholder for another object.
- B) It allows objects to be created dynamically.
- C) It ensures that only one instance of a class is created.
- D) It enforces access control to a resource.
Answer: A) It provides a surrogate or placeholder for another object.
- Which of the following is true regarding the super() method in Java?
- A) It can be used to call the constructor of a parent class.
- B) It can only be used to access instance variables of the parent class.
- C) It can only be used within a method, not within constructors.
- D) It cannot be used if the class does not have a parent class.
Answer: A) It can be used to call the constructor of a parent class.
- Which of the following statements about the clone() method in Java is correct?
- A) The clone() method is used to copy the contents of an object.
- B) The clone() method is a static method that does not require an object instance.
- C) The clone() method must be overridden in every class.
- D) The clone() method is used to create a deep copy of an object automatically.
Answer: A) The clone() method is used to copy the contents of an object.
- In object-oriented design, what does the “Liskov Substitution Principle” state?
- A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
- B) Subclasses should implement methods with the same return type as the superclass.
- C) All subclasses should be abstract classes.
- D) A class should inherit from multiple classes.
Answer: A) Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.
- What is the effect of the static keyword in Java when applied to a variable?
- A) It makes the variable unique to each instance of the class.
- B) It makes the variable shared across all instances of the class.
- C) It makes the variable visible only within the method in which it is defined.
- D) It makes the variable behave like a constant.
Answer: B) It makes the variable shared across all instances of the class.
- What is the primary advantage of using the Composite Design Pattern?
- A) It allows clients to treat individual objects and compositions of objects uniformly.
- B) It separates the concerns of object creation and usage.
- C) It provides flexibility for code that deals with collections.
- D) It ensures that objects are only created once.
Answer: A) It allows clients to treat individual objects and compositions of objects uniformly.
- Which of the following is a true statement about method signatures in Java?
- A) Method signatures include the method name and parameter types.
- B) Method signatures only include the method name.
- C) Method signatures include the return type and method name.
- D) Method signatures do not include parameter types.
Answer: A) Method signatures include the method name and parameter types.
- What is a key advantage of using the Iterator Design Pattern?
- A) It allows sequential access to elements of a collection without exposing the underlying structure.
- B) It allows the modification of the elements while iterating.
- C) It allows direct access to specific elements in a collection.
- D) It eliminates the need for indexing.
Answer: A) It allows sequential access to elements of a collection without exposing the underlying structure.
- In Java, what does the finally block do in exception handling?
- A) It is executed regardless of whether an exception was thrown or not.
- B) It is executed only if an exception is thrown.
- C) It is executed only if no exception is thrown.
- D) It prevents exceptions from being thrown.
Answer: A) It is executed regardless of whether an exception was thrown or not.
- What is the purpose of the transient keyword in Java?
- A) It indicates that a variable should not be serialized.
- B) It makes a variable available to all subclasses.
- C) It marks a method as private.
- D) It makes a variable constant across all instances of the class.
Answer: A) It indicates that a variable should not be serialized.
- Which of the following is true about the default method in Java interfaces (introduced in Java 8)?
- A) It allows interfaces to have method implementations.
- B) It is used to define a constructor in an interface.
- C) It makes a method static.
- D) It prevents a class from implementing multiple interfaces.
Answer: A) It allows interfaces to have method implementations.
- What is the key benefit of using the Strategy Design Pattern?
- A) It allows algorithms to be defined at runtime and replaced easily.
- B) It provides a way to make objects immutable.
- C) It creates a global access point for an object.
- D) It allows the construction of objects with a complex structure.
Answer: A) It allows algorithms to be defined at runtime and replaced easily.
- Which of the following methods is used to obtain a thread’s current execution state in Java?
- A) Thread.currentState()
- B) Thread.getState()
- C) Thread.currentThread()
- D) Thread.getPriority()
Answer: B) Thread.getState()
- In Java, which of the following classes is used to implement a thread-safe collection?
- A) ArrayList
- B) Vector
- C) HashMap
- D) LinkedList
Answer: B) Vector
- Which of the following best describes the Adapter Design Pattern?
- A) It allows incompatible interfaces to work together.
- B) It ensures a class has only one instance.
- C) It allows the class behavior to change dynamically.
- D) It allows different types of objects to be treated uniformly.
Answer: A) It allows incompatible interfaces to work together.
- In Java, which of the following statements about abstract classes is true?
- A) An abstract class can have concrete methods (methods with implementation).
- B) An abstract class cannot have any methods with implementation.
- C) An abstract class must only contain static methods.
- D) An abstract class cannot have constructors.
Answer: A) An abstract class can have concrete methods (methods with implementation).
- What is the purpose of the volatile keyword in Java?
- A) It ensures that a variable is shared between threads.
- B) It makes a variable constant during the runtime.
- C) It allows a variable to be accessed without synchronization.
- D) It makes a variable immutable.
Answer: A) It ensures that a variable is shared between threads.
- Which of the following is true about the java.util.concurrent package in Java?
- A) It provides support for multi-threading and concurrent programming.
- B) It is used only for network programming.
- C) It includes classes that are used exclusively for creating user interfaces.
- D) It is used for object serialization.
Answer: A) It provides support for multi-threading and concurrent programming.
- In Java, which of the following is true about an anonymous inner class?
- A) It is a class defined within a method with no name.
- B) It is a type of static inner class.
- C) It can only be used for event handling.
- D) It must be declared as final.
Answer: A) It is a class defined within a method with no name.
- Which of the following is the best description of the Observer Design Pattern?
- A) It allows one object to notify a list of dependent objects automatically.
- B) It allows a class to modify its behavior at runtime.
- C) It provides a way to combine multiple objects into a single object.
- D) It hides the details of a system’s complexity from users.
Answer: A) It allows one object to notify a list of dependent objects automatically.
- Which of the following is the main purpose of the “Builder” Design Pattern?
- A) To construct a complex object step by step.
- B) To ensure that a class has only one instance.
- C) To separate the creation of an object from its usage.
- D) To define a family of algorithms.
Answer: A) To construct a complex object step by step.
- In Java, what is the effect of the synchronized keyword?
- A) It makes the method or block thread-safe by allowing only one thread to access it at a time.
- B) It prevents a method from being inherited by subclasses.
- C) It makes the method static.
- D) It ensures that the method is executed in a separate thread.
Answer: A) It makes the method or block thread-safe by allowing only one thread to access it at a time.
- What does the term “polymorphism” mean in object-oriented programming?
- A) The ability of different objects to respond to the same message in different ways.
- B) The ability of a class to inherit methods and attributes from another class.
- C) The ability to combine multiple objects into one.
- D) The ability of a class to implement multiple interfaces.
Answer: A) The ability of different objects to respond to the same message in different ways.
- Which of the following is a disadvantage of using multiple inheritance in Java?
- A) It can lead to ambiguity and conflicts in method inheritance.
- B) It makes the code easier to understand.
- C) It can improve the performance of the application.
- D) It allows easier debugging.
Answer: A) It can lead to ambiguity and conflicts in method inheritance.
- In Java, which of the following will result in a compilation error when using an interface?
- A) Defining instance variables in the interface.
- B) Implementing multiple interfaces in a class.
- C) Using default methods in the interface.
- D) Using the extends keyword to inherit an interface.
Answer: A) Defining instance variables in the interface.
- What is the key feature of the Command Design Pattern?
- A) It encapsulates a request as an object, thus allowing for parameterization of clients with queues, requests, and operations.
- B) It creates a global point of access to an object.
- C) It allows a class to have multiple constructors.
- D) It defines methods that can be invoked by external classes.
Answer: A) It encapsulates a request as an object, thus allowing for parameterization of clients with queues, requests, and operations.
- Which of the following is true about the Object.clone() method in Java?
- A) It creates a shallow copy of the object by default.
- B) It cannot be overridden.
- C) It only works for primitive types.
- D) It is automatically invoked when an object is created.
Answer: A) It creates a shallow copy of the object by default.
- Which of the following best describes the Facade Design Pattern?
- A) It provides a simplified interface to a complex system of classes.
- B) It creates an object for each element in a system.
- C) It allows clients to interact with multiple systems simultaneously.
- D) It implements a specific algorithm for each client.
Answer: A) It provides a simplified interface to a complex system of classes.
- What is the purpose of the java.util.Optional class in Java?
- A) It is used to represent a value that may or may not be present.
- B) It is used for working with streams of data.
- C) It is used to represent a value that is always present.
- D) It provides methods for handling thread synchronization.
Answer: A) It is used to represent a value that may or may not be present.
- Which of the following statements about Java interfaces is true?
- A) An interface can extend multiple other interfaces.
- B) An interface can extend a class.
- C) An interface cannot contain any methods.
- D) An interface can contain both instance and static variables.
Answer: A) An interface can extend multiple other interfaces.
- What is the main advantage of using the Composite Design Pattern?
- A) It allows you to treat individual objects and compositions of objects uniformly.
- B) It guarantees that objects are always mutable.
- C) It ensures that each object is initialized before it is used.
- D) It provides a mechanism for creating objects dynamically at runtime.
Answer: A) It allows you to treat individual objects and compositions of objects uniformly.
- Which of the following is true about Java’s static keyword?
- A) It allows a method or variable to belong to the class rather than any instance of the class.
- B) It can only be used in static nested classes.
- C) It ensures that a method or variable is synchronized.
- D) It makes a method or variable accessible only within the class in which it is declared.
Answer: A) It allows a method or variable to belong to the class rather than any instance of the class.
- What does it mean when a class is said to be “immutable”?
- A) Its state cannot be modified after it is created.
- B) It cannot be inherited by other classes.
- C) It cannot contain any methods.
- D) It can only have a single constructor.
Answer: A) Its state cannot be modified after it is created.
- What is the purpose of using the final keyword in Java?
- A) It is used to prevent a method from being overridden or a class from being subclassed.
- B) It allows a method to be used asynchronously.
- C) It is used to initialize instance variables only.
- D) It makes a class thread-safe.
Answer: A) It is used to prevent a method from being overridden or a class from being subclassed.
- Which of the following statements is true about the Singleton Design Pattern?
- A) It ensures that a class has only one instance and provides a global point of access to it.
- B) It allows multiple instances of a class to be created dynamically.
- C) It helps in creating independent objects that do not rely on each other.
- D) It provides a mechanism for creating objects using factory methods.
Answer: A) It ensures that a class has only one instance and provides a global point of access to it.
- Which of the following is a characteristic of a deep copy of an object in Java?
- A) A deep copy creates an independent copy of the object and all objects it refers to.
- B) A deep copy is only applicable to primitive types.
- C) A deep copy shares references to the objects in the original instance.
- D) A deep copy only works for arrays.
Answer: A) A deep copy creates an independent copy of the object and all objects it refers to.
- What does the term “encapsulation” mean in object-oriented programming?
- A) The concept of hiding the internal state and requiring all interaction to be performed through methods.
- B) The process of combining multiple classes into a single class.
- C) The inheritance of properties and methods from a superclass.
- D) The ability of a class to have multiple constructors.
Answer: A) The concept of hiding the internal state and requiring all interaction to be performed through methods.
- Which of the following Java collection classes is synchronized and thread-safe?
- A) ArrayList
- B) HashMap
- C) Hashtable
- D) LinkedHashMap
Answer: C) Hashtable
- Which design pattern is typically used to decouple the creation of an object from its use, especially when the exact type of object is not known until runtime?
- A) Factory Method Pattern
- B) Singleton Pattern
- C) Proxy Pattern
- D) Observer Pattern
Answer: A) Factory Method Pattern
- What is the main purpose of the super keyword in Java?
- A) It refers to the immediate parent class of the current object.
- B) It invokes the current method recursively.
- C) It prevents a subclass from accessing the superclass.
- D) It refers to the current instance of a class.
Answer: A) It refers to the immediate parent class of the current object.
- Which of the following is the purpose of the java.io.Serializable interface in Java?
- A) It allows objects to be serialized and written to an output stream.
- B) It provides an API for handling network operations.
- C) It allows objects to be cloned.
- D) It ensures that classes are thread-safe.
Answer: A) It allows objects to be serialized and written to an output stream.
- What does it mean to override a method in Java?
- A) It means defining a new version of a method in a subclass that replaces the inherited method.
- B) It means creating a new method in the superclass.
- C) It means making a method static.
- D) It means defining a method in the interface.
Answer: A) It means defining a new version of a method in a subclass that replaces the inherited method.
- Which of the following is a feature of the Prototype Design Pattern?
- A) It allows objects to be cloned or copied at runtime.
- B) It ensures that a class has only one instance.
- C) It separates the creation of an object from its usage.
- D) It allows a class to be created dynamically.
Answer: A) It allows objects to be cloned or copied at runtime.
- What is the purpose of the instanceof operator in Java?
- A) It is used to check if an object is an instance of a specific class or interface.
- B) It is used to create a new instance of an object.
- C) It is used to compare the equality of two objects.
- D) It is used to clone an object.
Answer: A) It is used to check if an object is an instance of a specific class or interface.
- What is the main disadvantage of using the Factory Design Pattern?
- A) It can lead to an increased number of classes and complexity.
- B) It is difficult to implement with polymorphism.
- C) It does not provide flexibility in object creation.
- D) It makes the system less flexible.
Answer: A) It can lead to an increased number of classes and complexity.
- Which of the following is true about the Object.equals() method in Java?
- A) It checks if two objects are logically equal (based on their state).
- B) It checks if two objects are the same instance.
- C) It checks if two objects are of the same class.
- D) It cannot be overridden by subclasses.
Answer: A) It checks if two objects are logically equal (based on their state).
- Which of the following is a feature of the State Design Pattern?
- A) It allows an object to change its behavior when its internal state changes.
- B) It is used to modify the behavior of methods dynamically.
- C) It ensures that only one instance of a class is created.
- D) It allows a class to adapt to multiple inheritance.
Answer: A) It allows an object to change its behavior when its internal state changes.
- What does the term “inheritance” mean in object-oriented programming?
- A) The process by which one class can inherit properties and methods from another class.
- B) The concept of hiding the internal details of a class.
- C) The ability of objects to respond to the same method in different ways.
- D) The process of creating an object from a class.
Answer: A) The process by which one class can inherit properties and methods from another class.
- Which of the following is true about the finalize() method in Java?
- A) It is called by the garbage collector before an object is removed from memory.
- B) It is used to mark methods as unchangeable.
- C) It ensures that the memory allocated to an object is deallocated immediately.
- D) It is used to lock the methods of a class for thread safety.
Answer: A) It is called by the garbage collector before an object is removed from memory.