UNIT I: Classes and Objects & Inheritance (40 MCQs)
1. What is a class in Object-Oriented Programming?
a) A blueprint for creating objects.
b) A specific instance of an object.
c) A method within a program.
d) A variable that stores data.
Answer: a) A blueprint for creating objects.
2. The concept of bundling data and methods that operate on that data into a single unit is known
as:
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Abstraction
Answer: c) Encapsulation
3. Which keyword is used to create an instance of a class?
a) class
b) new
c) object
d) instance
Answer: b) new
4. A constructor in Java is used to:
a) Destroy an object.
b) Return a value from a method.
c) Initialize an object.
d) Define the class variables.
Answer: c) Initialize an object.
5. What is the name of the special method that is called by the garbage collector when an object is
about to be destroyed?
a) destroy()
b) cleanup()
c) finalize()
d) close()
Answer: c) finalize()
6. The this keyword is a reference to:
a) The superclass of the object.
b) The current object.
c) The method being executed.
d) A static variable.
Answer: b) The current object.
7. Which of the following is true about constructor overloading?
a) It allows a constructor to have a return type.
b) It is the process of creating multiple constructors with the same name but different parameter lists.
c) It allows a constructor to be inherited.
d) It is not allowed in Java.
Answer: b) It is the process of creating multiple constructors with the same name but different parameter
lists.
8. A copy constructor is a constructor that:
a) Creates a new object that is a copy of an existing object.
b) Creates a deep copy of an object.
c) Creates a shallow copy of an object.
d) Copies the superclass's constructor.
Answer: a) Creates a new object that is a copy of an existing object.
9. A variable declared with the static keyword:
a) Is tied to a specific object instance.
b) Is shared by all objects of the class.
c) Can only be accessed within the constructor.
d) Is a local variable.
Answer: b) Is shared by all objects of the class.
10. A static method can access:
a) Both static and non-static data members.
b) Only non-static data members.
c) Only static data members.
d) Non-static members using the this keyword.
Answer: c) Only static data members.
11. What is the process by which a new class can acquire the properties and methods of an existing
class?
a) Encapsulation
b) Polymorphism
c) Inheritance
d) Abstraction
Answer: c) Inheritance
12. In inheritance, the class that is being inherited from is called the:
a) Subclass
b) Derived class
c) Superclass
d) Child class
Answer: c) Superclass
13. What does it mean for a class to be abstract?
a) It can be instantiated but cannot be inherited.
b) It cannot be instantiated and may contain abstract methods.
c) All of its methods must be defined.
d) It can only have static members.
Answer: b) It cannot be instantiated and may contain abstract methods.
14. The keyword used to prevent a method from being overridden in a subclass is:
a) override
b) final
c) static
d) abstract
Answer: b) final
15. What is the key characteristic of a final class?
a) It must contain a final method.
b) It cannot be inherited.
c) Its objects cannot be modified.
d) It must be instantiated using a specific constructor.
Answer: b) It cannot be inherited.
16. Which of the following statements is true about inheriting constructors?
a) Constructors are automatically inherited by a subclass.
b) Constructors are not inherited, but a subclass can call the superclass's constructor using super().
c) Constructors are only inherited if they are public.
d) Constructors are inherited only if the subclass does not define its own.
Answer: b) Constructors are not inherited, but a subclass can call the superclass's constructor using
super().
17. What is method overriding?
a) Defining multiple methods with the same name but different parameters in the same class.
b) Defining a method in a subclass that has the same name, parameters, and return type as a method in
its superclass.
c) Defining a static method with the same name as an instance method.
d) Calling a method from a different class.
Answer: b) Defining a method in a subclass that has the same name, parameters, and return type as a
method in its superclass.
18. 'Polymorphism' literally means:
a) One name, many forms.
b) Many objects, one class.
c) One method, many parameters.
d) Many forms, one type.
Answer: a) One name, many forms.
19. Which of the following is an example of compile-time polymorphism?
a) Method overriding
b) Inheritance
c) Method overloading
d) Dynamic method dispatch
Answer: c) Method overloading
20. A copy constructor takes as an argument:
a) A primitive data type.
b) A reference to the object of the same class.
c) An array of objects.
d) The superclass of the object.
Answer: b) A reference to the object of the same class.
21. What is the default access level for class members if no access modifier is specified?
a) public
b) private
c) protected
d) package-private
Answer: d) package-private
22. What happens if a constructor is not explicitly defined in a class?
a) The compiler generates a default constructor with no arguments.
b) The class cannot be instantiated.
c) An error is generated at compile time.
d) The class inherits the constructor from its superclass.
Answer: a) The compiler generates a default constructor with no arguments.
23. When is the finalize() method invoked?
a) After the garbage collector runs.
b) Just before the garbage collector reclaims an object's memory.
c) When an object goes out of scope.
d) When the new keyword is used.
Answer: b) Just before the garbage collector reclaims an object's memory.
24. The keyword super is used to:
a) Refer to the current object.
b) Call the superclass's constructor or method.
c) Define an interface.
d) Declare a static variable.
Answer: b) Call the superclass's constructor or method.
25. The process of creating a new class from an existing class is called:
a) Abstraction
b) Instantiation
c) Derivation
d) Inheritance
Answer: d) Inheritance
26. A class can inherit from multiple classes in Java.
a) True
b) False
Answer: b) False (Java does not support multiple inheritance of classes)
27. A class declared as abstract:
a) Can be instantiated.
b) Must be inherited to be used.
c) Cannot have constructors.
d) Cannot have concrete methods.
Answer: b) Must be inherited to be used.
28. The final keyword can be applied to:
a) Classes, methods, and variables.
b) Only classes.
c) Only methods.
d) Only variables.
Answer: a) Classes, methods, and variables.
29. What is a copy constructor often used for?
a) To prevent a class from being inherited.
b) To create a new object from scratch.
c) To create a new object that is an exact replica of an existing one.
d) To initialize static members.
Answer: c) To create a new object that is an exact replica of an existing one.
30. A static method belongs to:
a) A specific object.
b) The class itself.
c) The superclass.
d) The subclass.
Answer: b) The class itself.
31. If a subclass has a constructor, what must be the first statement in it?
a) A call to a method in the superclass.
b) A call to this() to invoke another constructor in the same class.
c) A call to super() to invoke the superclass's constructor.
d) A declaration of a new variable.
Answer: c) A call to super() to invoke the superclass's constructor.
32. The process of creating a simple, user-friendly interface to a complex system is called:
a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism
Answer: b) Abstraction
33. Which of the following is NOT a type of constructor?
a) Default constructor
b) Parameterized constructor
c) Overridden constructor
d) Copy constructor
Answer: c) Overridden constructor
34. How is a static method called?
a) By creating an object of the class.
b) Using the class name, without creating an object.
c) Only from within another static method.
d) Using the this keyword.
Answer: b) Using the class name, without creating an object.
35. The finalize() method is guaranteed to be called before an object is garbage collected.
a) True
b) False
Answer: b) False (Its execution is not guaranteed by the JVM)
36. Which of the following is NOT part of the method signature for overloading?
a) Method name
b) Return type
c) Number of parameters
d) Type of parameters
Answer: b) Return type
37. What happens if a method is declared final?
a) It cannot be overridden by subclasses.
b) It cannot be accessed by other classes.
c) It can only be called from within the same class.
d) It must be implemented by all subclasses.
Answer: a) It cannot be overridden by subclasses.
38. Which of the following statements is true about abstract methods?
a) They must be defined in an abstract class.
b) They cannot have a body.
c) They are not inherited.
d) Both a and b.
Answer: d) Both a and b.
39. What is the default value for an uninitialized static variable of type int?
a) null
b) 0
c) 1
d) It depends on the JVM.
Answer: b) 0
40. How can you prevent an instance variable from being changed after it is initialized?
a) By declaring it static.
b) By declaring it private.
c) By declaring it final.
d) By declaring it protected.
Answer: c) By declaring it final.
UNIT II: Interfaces, Packages (40 MCQs)
41. An interface in Java is a:
a) Class that can be inherited.
b) Blueprint for a class, containing only method signatures and constants.
c) A special type of abstract class that can be instantiated.
d) A class with only static methods.
Answer: b) A blueprint for a class, containing only method signatures and constants.
42. A class uses which keyword to indicate that it will provide implementations for the methods of an
interface?
a) extends
b) implements
c) inherits
d) uses
Answer: b) implements
43. Which of the following is true about interfaces?
a) An interface can contain constructors.
b) An interface can have private methods.
c) An interface can have non-abstract methods with method bodies (since Java 8, with default and static
keywords).
d) An interface can be instantiated.
Answer: c) An interface can have non-abstract methods with method bodies (since Java 8, with default
and static keywords).
44. An interface can extend another interface using the keyword:
a) implements
b) extends
c) inherits
d) uses
Answer: b) extends
45. A package is a way of:
a) Grouping related classes and interfaces.
b) Encapsulating data within a class.
c) Defining a new class.
d) Declaring a static variable.
Answer: a) Grouping related classes and interfaces.
46. What is the purpose of the import statement?
a) To declare a new package.
b) To allow a class to inherit from another class.
c) To make classes from another package accessible without using their fully qualified names.
d) To define an interface.
Answer: c) To make classes from another package accessible without using their fully qualified names.
47. The public access modifier means a member is accessible from:
a) Only within the same class.
b) Only within the same package.
c) Within the same package and by subclasses in other packages.
d) Anywhere.
Answer: d) Anywhere.
48. A member with the private access modifier is accessible from:
a) Anywhere.
b) Only within the same class.
c) Only by its subclasses.
d) Only within the same package.
Answer: b) Only within the same class.
49. What is the access level of a member declared with the protected modifier?
a) Accessible only within the same class.
b) Accessible within the same package and by all subclasses.
c) Accessible within the same package and by subclasses in other packages.
d) Accessible anywhere.
Answer: c) Accessible within the same package and by subclasses in other packages.
50. What is the default access level for a class itself?
a) public
b) protected
c) private
d) Package-private
Answer: d) Package-private (A top-level class cannot be private or protected)
51. How is a class placed in a package?
a) By using the import statement.
b) By using the package statement at the beginning of the source file.
c) By saving the file in a directory with the same name as the package.
d) Both b and c.
Answer: b) By using the package statement at the beginning of the source file.
52. Can a class implement multiple interfaces?
a) Yes
b) No
Answer: a) Yes
53. Can an interface extend multiple interfaces?
a) Yes
b) No
Answer: a) Yes
54. If a class implements an interface, it must:
a) Implement all the methods of the interface.
b) Implement only the public methods.
c) Override the default methods.
d) Not define any new methods.
Answer: a) Implement all the methods of the interface.
55. The * in an import statement, such as import java.util.*;, means:
a) Import the java.util package itself.
b) Import all classes and interfaces in the java.util package.
c) Import a specific class named *.
d) This syntax is not valid.
Answer: b) Import all classes and interfaces in the java.util package.
56. All methods in an interface are implicitly:
a) private
b) protected
c) static
d) public and abstract
Answer: d) public and abstract (before Java 8)
57. All data members (fields) in an interface are implicitly:
a) public static final
b) private
c) protected
d) default
Answer: a) public static final
58. What is a package hierarchy?
a) The order in which packages are imported.
b) A nested structure of packages, like a folder structure.
c) The process of creating a new package.
d) The relationship between a superclass and a subclass.
Answer: b) A nested structure of packages, like a folder structure.
59. Can an abstract class implement an interface?
a) No, only concrete classes can.
b) Yes, but it is not required to implement all the interface methods.
c) Yes, but it must implement all the interface methods.
d) Yes, but it cannot be inherited.
Answer: b) Yes, but it is not required to implement all the interface methods.
60. Which of the following statements about access control is true?
a) A private member is visible to its subclasses.
b) A protected member is visible to all classes.
c) A default member is visible only within its package.
d) A public member is visible only to its subclasses.
Answer: c) A default member is visible only within its package.
61. What happens if a class does not implement all the methods of an interface?
a) It must be declared as an abstract class.
b) It will cause a compile-time error.
c) The compiler will provide a default implementation.
d) It can be instantiated but will throw an error at runtime.
Answer: a) It must be declared as an abstract class.
62. What is the correct way to declare an interface?
a) class MyInterface { ... }
b) interface MyInterface { ... }
c) implements MyInterface { ... }
d) abstract class MyInterface { ... }
Answer: b) interface MyInterface { ... }
63. Which keyword is optional when declaring a method in an interface?
a) public
b) abstract
c) static
d) All of the above (for methods before Java 8).
Answer: b) abstract
64. If you want to use a class from a different package, but do not want to use the import statement,
what must you do?
a) You cannot use the class.
b) You must use the fully qualified name of the class every time.
c) You must put the class in the same package.
d) The compiler will handle it automatically.
Answer: b) You must use the fully qualified name of the class every time.
65. A member declared with protected access is accessible by:
a) Subclasses in the same package only.
b) Subclasses in other packages only.
c) All classes in the same package and subclasses in any package.
d) All classes in the same package only.
Answer: c) All classes in the same package and subclasses in any package.
66. Can an interface have a method with a body?
a) No, never.
b) Yes, with the default or static keyword (since Java 8).
c) Yes, only if the interface is abstract.
d) Yes, if it is a private method.
Answer: b) Yes, with the default or static keyword (since Java 8).
67. What is the correct syntax for a package declaration?
a) package com.mycompany.app;
b) import package com.mycompany.app;
c) package app.mycompany.com;
d) use package com.mycompany.app;
Answer: a) package com.mycompany.app;
68. What is the relationship between an interface and a class that implements it?
a) A has-a relationship.
b) An is-a relationship.
c) A knows-a relationship.
d) A contained-in relationship.
Answer: b) An is-a relationship.
69. Which access modifier allows for the highest level of data hiding?
a) public
b) protected
c) default
d) private
Answer: d) private
70. The concept of an interface is similar to a:
a) Concrete class.
b) final class.
c) Completely abstract class.
d) Utility class with only static methods.
Answer: c) Completely abstract class.
71. A class that has no package statement is placed in the:
a) default package.
b) java.lang package.
c) import package.
d) unnamed package.
Answer: d) unnamed package.
72. If a class implements an interface, it must provide a definition for all abstract methods in the
interface, unless:
a) It is also declared as a final class.
b) It extends another class.
c) It is declared as an abstract class.
d) The interface has a default method.
Answer: c) It is declared as an abstract class.
73. The import statement must appear:
a) At the beginning of the class definition.
b) After the package statement, but before the class definition.
c) Anywhere in the file.
d) Before the package statement.
Answer: b) After the package statement, but before the class definition.
74. A member declared with no access modifier can be accessed by:
a) Subclasses in other packages.
b) Classes in the same package only.
c) Subclasses in the same package only.
d) Classes in the same package and subclasses in other packages.
Answer: b) Classes in the same package only.
75. An interface can have fields, but they must be:
a) private
b) protected
c) static final
d) default
Answer: c) static final
76. If a class A is in package p1 and class B is in package p2, can class B directly access a protected
member of class A?
a) No, never.
b) Yes, always.
c) Yes, if class B inherits from class A.
d) Yes, if class A is also declared public.
Answer: c) Yes, if class B inherits from class A.
77. What happens if a default method in an interface is not overridden by a class that implements it?
a) The program will not compile.
b) The class will inherit the default implementation from the interface.
c) A runtime error will occur.
d) The compiler will generate a warning.
Answer: b) The class will inherit the default implementation from the interface.
78. Which of the following is NOT a purpose of packages?
a) To prevent naming conflicts.
b) To provide a level of access control.
c) To organize classes and interfaces.
d) To allow a class to inherit from multiple classes.
Answer: d) To allow a class to inherit from multiple classes.
79. A class can be public or default. What are the restrictions on this?
a) There can only be one public class per file.
b) A file can have multiple public classes.
c) A file must have at least one public class.
d) A file cannot have any public classes.
Answer: a) There can only be one public class per file.
80. A class declared as final can still implement an interface.
a) True
b) False
Answer: a) True