OOP Final Exam
OOP Final Exam
java
CopyEdit
class Test {
int x = 5;
void display() {
System.out.println(x);
t.display();
A) 0
B) 5
C) Compilation error
D) Runtime error
java
CopyEdit
class Car {
Car() {
System.out.println("Car created");
}
}
34. The keyword ________ is used to refer to the immediate parent class
constructor.
37. ________ allows a class to have multiple methods with the same name but
different parameters.
java
CopyEdit
class A {
void show() {
System.out.println("A");
class B extends A {
void show() {
System.out.println("B");
}
}
public class Main {
obj.show();
}
A) A
B) B
C) Compilation error
D) Runtime error
42. What access modifier allows a class member to be accessible within the same
package and subclasses?
A) public
B) private
C) protected
D) default
java
CopyEdit
class Test {
Test() {
count++;
System.out.println(Test.count);
A) 0
B) 1
C) 2
D) Compilation error
44. Which of the following is NOT a feature of OOP?
A) Polymorphism
B) Inheritance
C) Encapsulation
D) Compilation
46. What will happen if you do not override the toString() method in a class?
A) Compilation error
B) Default implementation from Object class is used
C) Runtime error
D) The program crashes
50. Which OOP principle helps to reduce software complexity by hiding unnecessary
details?
A) Encapsulation
B) Polymorphism
C) Inheritance
D) Abstraction
51. What happens if two interfaces have a method with the same signature and a
class implements both?
A) Compilation error
B) The class must override the method once
C) Only one interface’s method is used automatically
D) Runtime error
java
CopyEdit
class Test {
int x = 10;
int x = 20;
System.out.println(x);
System.out.println(x);
}
}
A) 10 10
B) 20 20
C) Compilation error
D) 20 10
java
CopyEdit
class Test {
int x = 5;
void setX(int x) {
this.x = x;
void display() {
System.out.println(x);
}
public class Main {
t.setX(10);
t.display();
}
A) 5
B) 10
C) Compilation error
D) Runtime error
66. What happens if a class does not implement all methods of an interface?
A) The class must be declared abstract
B) Compilation error
C) Methods are auto-implemented
D) Runtime error
CopyEdit
Test() { count++; }
System.out.println(count);
A) 0
B) 1
C) 2
D) 3
71. What does polymorphism allow in OOP?
A) Multiple inheritance
B) Overloading constructors only
C) Using a single interface to represent different types
D) Hiding data
java
CopyEdit
class A {
class B extends A {
class C extends B {}
public class Main {
obj.method();
A) A
B) B
C) C
D) Compilation error
75. True or False: You can declare a method both abstract and final.
A) True
B) False
76. True or False: Interfaces can extend other interfaces.
A) True
B) False
class Test {
void method() {
System.out.println(x);
t.method();
A) 0
B) 10
C) Compilation error
D) Runtime error
81. Which of the following is the correct way to declare an array of integers in Java?
A) int[] arr;
B) int arr[];
C) Both A and B
D) array int arr;
82. What will happen if you try to access an array index out of bounds in Java?
A) Compilation error
B) ArrayIndexOutOfBoundsException at runtime
C) Returns null
D) Returns zero
84. Which method signature correctly overrides the equals() method from the Object
class?
A) public boolean equals(Object obj)
B) public boolean equals(Test obj)
C) private boolean equals(Object obj)
D) public void equals(Object obj)
85. What does the static keyword mean for a method or variable?
A) It belongs to the class, not instances
B) It can only be accessed inside the class
C) It is private by default
D) It is final
java
CopyEdit
class Test {
static int a = 5;
t1.increment();
t2.increment();
System.out.println(Test.a);
}
A) 5
B) 6
C) 7
D) 8
90. What will happen if you declare a method as private and try to override it in a
subclass?
A) Compiles and overrides
B) Compilation error
C) The subclass method is treated as a new method
D) Runtime error
92. Which access modifier allows members to be accessed only within the same
class?
A) public
B) private
C) protected
D) default
93. What does the abstract keyword do when applied to a method?
A) Provides a default implementation
B) Declares the method without a body
C) Makes the method static
D) Makes the method final
java
CopyEdit
class Parent {
p.show();
c.show();
}
A) Parent Parent
B) Child Child
C) Parent Child
D) Child Parent
96. Which statement about interfaces is true?
A) They can have instance variables
B) They can have constructors
C) They can have default and static methods
D) They cannot extend other interfaces
97. True or False: The super keyword can be used to access superclass variables
and methods.
A) True
B) False
101. What is the default return type of the main method in Java?
A) void
B) int
C) String
D) No return type
102. What will happen if a class implements an interface but does not
implement all of its methods?
A) Compilation error
B) Runtime error
C) The class is abstract by default
D) Methods are auto-implemented
103. Which of the following statements is correct about the final keyword?
A) Can be used with variables, methods, and classes
B) Makes variables modifiable
C) Allows methods to be overridden
D) Allows classes to be subclassed
104. What exception is thrown when you try to access a null object reference?
A) NullPointerException
B) IOException
C) ArrayIndexOutOfBoundsException
D) ClassCastException
105. Which of these keywords is used to declare a package?
A) package
B) import
C) include
D) module
java
CopyEdit
class A {
class B extends A {
class C extends B {
obj.print();
A) A
B) B
C) C
D) Compilation error
109. What will happen if you do not implement all abstract methods of an
abstract class in its subclass?
A) Compile-time error
B) Runtime error
C) The subclass becomes abstract automatically
D) The program crashes
java
CopyEdit
int x = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Error");
} finally {
System.out.println("Finally block");
}
}
A) Error
B) Finally block
C) Error
Finally block
D) Compilation error
java
CopyEdit
String s1 = "hello";
String s2 = "hello";
System.out.println(s1 == s2);
}
A) true
B) false
C) Compilation error
D) Runtime error
class Test {
int x = 5;
A) 0
B) 5
C) Compilation error
D) Runtime error
16. Which exception is checked at compile time?
A) NullPointerException
B) IOException
C) ArithmeticException
D) RuntimeException
17. What happens if you don’t handle a checked exception?
A) Compile-time error
B) Runtime error
C) Warning only
D) Nothing
18. What is the default value of an int instance variable in Java?
A) 0
B) null
C) Undefined
D) 1
19. Can static methods be overridden?
A) Yes
B) No
20. What keyword is used to define a constant in Java?
A) const
B) static
C) final
D) constant
int x = 10;
int x = 20;
System.out.println(x);
}
A) 10
B) 20
C) Compilation error
D) Runtime error
28. What keyword is used to handle exceptions?
A) try
B) catch
C) finally
D) All of the above
29. What is the use of the finally block?
A) To catch exceptions
B) To execute code after try-catch regardless of exception
C) To throw exceptions
D) To declare exceptions
30. What will happen if a subclass does not implement all abstract methods of its
abstract superclass?
A) Compile-time error
B) Runtime error
C) Subclass becomes abstract automatically
D) The program compiles fine
31. What does static mean for variables and methods?
A) They belong to the class, not instances
B) They belong to each instance
C) They are private
D) They are final
32. Can a static method access instance variables directly?
A) Yes
B) No
33. What is the output?
System.out.println(count);
A) 0
B) 1
C) 2
D) Compilation error
34. What is method overriding used for?
A) To provide specific implementation in subclass
B) To overload methods
C) To define multiple constructors
D) To create static methods
35. Which of the following is NOT true about constructors?
A) They have the same name as the class
B) They have no return type
C) They can be inherited
D) They initialize objects
36. What is a package in Java?
A) A collection of classes and interfaces
B) A variable type
C) A method type
D) A class constructor
37. What is the purpose of the import statement?
A) To include packages or classes in your program
B) To create new classes
C) To declare variables
D) To handle exceptions
38. What will happen if you declare a class as final?
A) It can be subclassed
B) It cannot be subclassed
C) It must have abstract methods
D) It cannot have methods
39. Can interface methods have implementations?
A) Yes, default and static methods can
B) No, all methods must be abstract
C) Yes, all methods can have implementations
D) Only private methods can have implementations
40. What does the instanceof keyword do?
A) Checks if an object is an instance of a class or subclass
B) Casts an object to a class
C) Creates an instance of a class
D) Deletes an object
41. Which of these exceptions is unchecked?
A) IOException
B) NullPointerException
C) FileNotFoundException
D) SQLException
42. What is the superclass of all classes in Java?
A) Object
B) Class
C) String
D) Main
43. What is the output?
A) true
B) false
C) Compilation error
D) Runtime error
44. Which of these can be used to document Java code?
A) /* comment /
B) // comment
C) /* Javadoc comment */
D) All of the above
45. What is the output?
System.out.println(arr.length);
A) 3
B) 2
C) Compilation error
D) Runtime error
46. What does throws keyword do?
A) Catches exceptions
B) Declares exceptions a method might throw
C) Throws exceptions
D) Handles exceptions
int x;
System.out.println(x);
}
A) Prints 0
B) Compilation error (variable x might not have been initialized)
C) Runtime error
D) Prints garbage value
62. What is the effect of the break statement inside a loop?
A) Skips current iteration
B) Exits the loop immediately
C) Continues to the next iteration
D) Causes an error
63. Which of these is NOT a Java primitive type?
A) int
B) boolean
C) String
D) double
64. Which of the following is true about ArrayList?
A) Fixed size array
B) Resizable array implementation
C) Only stores primitive types
D) Cannot store objects
65. What is the difference between throw and throws?
A) throw declares an exception, throws throws it
B) throw throws an exception, throws declares it
C) Both throw exceptions
D) Both declare exceptions
66. What is the output?
System.out.println(arr[0]);
A) 0
B) null
C) Compilation error
D) Runtime error
67. Which of the following statements about garbage collection in Java is true?
A) It is manual and controlled by the programmer
B) It is automatic and runs periodically
C) It deletes all objects instantly
D) It is not supported in Java
68. What keyword is used to inherit from a superclass?
A) super
B) inherit
C) extends
D) implements
69. Which of the following is true about private members?
A) Accessible in all classes
B) Accessible only within the declaring class
C) Accessible in the same package
D) Accessible in subclasses only
70. What is the output?
str = str.toUpperCase();
System.out.println(str);
}
A) hello
B) HELLO
C) Compilation error
D) Runtime error
71. Which of these is true about abstract methods?
A) They have a body
B) They do not have a body
C) They can be static
D) They can be private
72. What happens if a class implements multiple interfaces with the same method
signature?
A) Compiler error
B) Class must override the method
C) Uses method from first interface only
D) Uses method from last interface only
73. What does the keyword static mean when applied to a block?
A) The block executes once when class is loaded
B) The block executes every time an instance is created
C) The block cannot be static
D) The block executes on method call
74. Which of these is NOT a valid Java identifier?
A) _myVar
B) 2ndVar
C) $value
D) var_123
75. What is polymorphism in Java?
A) Multiple inheritance of classes
B) Ability of an object to take many forms
C) Creating many objects from one class
D) Writing many methods in a class
76. Which method is called when an object is created?
A) main()
B) finalize()
C) constructor
D) static block
77. Which of the following is true about final variables?
A) Can be reassigned
B) Cannot be reassigned once initialized
C) Are static by default
D) Can be declared inside methods only
78. What will happen if you call a method on a null reference?
A) NullPointerException at runtime
B) Compilation error
C) Method executes normally
D) Returns null
79. What is the correct way to declare a main method in Java?
A) public static void main(String[] args)
B) static public main(String[] args)
C) void main(String args)
D) public void main(String[] args)
80. What is the difference between abstract class and interface?
A) Abstract classes can have implemented methods, interfaces cannot
B) Interfaces can have implemented methods, abstract classes cannot
C) Abstract classes cannot have constructors
D) Interfaces can be instantiated
81. Which of the following is true about the this keyword?
A) Refers to the current object
B) Refers to the superclass
C) Refers to a static variable
D) Refers to the main method
82. What is method signature?
A) Method name and return type
B) Method name and parameters list
C) Method name only
D) Parameters list only
83. What will be the output?
int a = 5;
int b = ++a + a++;
System.out.println(b);
A) 11
B) 12
C) 10
D) 9
84. What keyword is used to prevent inheritance?
A) static
B) final
C) abstract
D) private
85. Which of these access modifiers allows access only within the same
package?
A) public
B) private
C) protected
D) default (package-private)
86. What will happen if you try to override a static method?
A) It is allowed and works as overriding
B) It causes a compile-time error
C) It hides the method, not overrides it
D) It makes method abstract
87. What is the output?
String s = null;
A) null test
B) Compilation error
C) Runtime error
D) test null
88. Which of these is NOT true about super keyword?
A) Can be used to call superclass constructor
B) Can be used to call superclass method
C) Can be used to access private members of superclass
D) Can be used to access immediate parent class members
89. What is the purpose of the instanceof operator?
A) To create new instance
B) To check object type
C) To cast objects
D) To delete objects
90. What is the result of this code?
int x = 10;
int y = x > 5 ? 1 : 0;
System.out.println(y);
A) 10
B) 1
C) 0
D) Compilation error
91. What is the default value of a boolean instance variable?
A) true
B) false
C) null
D) 0
92. Can constructors be overloaded?
A) Yes
B) No
93. What is the output?
int i = 0;
while (i < 3) {
i++;
A) 0 1 2
B) 0 1 2 3
C) 1 2 3
D) Compilation error
94. What is the output?
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
A) true
B) false
C) Compilation error
D) Runtime error
95. What is the main advantage of using interfaces?
A) To support multiple inheritance
B) To allow private methods
C) To create objects directly
D) To override constructors
96. Which statement is true about try-catch?
A) try block contains code that might throw exceptions
B) catch block handles exceptions
C) Both A and B
D) Neither A nor B
97. Which is true about static blocks?
A) They execute once when the class is loaded
B) They execute every time an instance is created
C) They can throw checked exceptions
D) They are optional
98. What is the output?
int b = 2;
System.out.println(a / b);
A) 2
B) 2.5
C) Compilation error
D) Runtime error
99. What does encapsulation protect against?
A) Direct access to object data
B) Inheritance
C) Polymorphism
D) Abstraction
100. Which of the following is true about final classes?
A) They can be subclassed
B) They cannot be subclassed
C) They must have abstract methods
D) They can implement interfaces