Java OOPS Multiple Choice Questions
What does OOP stand for? a) Object-Oriented Programming b) Object-Oriented Protocol c) Object-Oriented Procedure d) Object-Oriented Processor Answer: a) Object-Oriented Programming
Which of the following is not a core OOP concept? a) Encapsulation b) Inheritance c) Abstraction d) Procedural Programming Answer: d) Procedural Programming
In Java, a class is a blueprint for creating: a) Objects b) Methods c) Interfaces d) Variables Answer: a) Objects
Which keyword is used to create an object in Java? a) object b) new c) create d) instance Answer: b) new
The process of bundling data and methods together into a single unit is called: a) Abstraction b) Inheritance c) Encapsulation d) Polymorphism Answer: c) Encapsulation
Which access modifier allows a class to be accessed only within the same package? a) private b) protected c) public d) default Answer: d) default
In Java, multiple inheritance is achieved through: a) Interfaces b) Abstract classes c) Nested classes d) Final classes Answer: a) Interfaces
Which keyword is used to achieve method overriding in Java? a) extends b) final c) implements d) @Override Answer: d) @Override
The process of creating a new class from an existing class is known as: a) Inheritance b) Abstraction c) Polymorphism d) Composition Answer: a) Inheritance
Which of the following is not a valid identifier in Java? a) 2variable b) variable c) variable2 d) $variable Answer: a) 2variable
The "this" keyword in Java refers to: a) The current class instance b) The parent class instance c) The child class instance d) The current method Answer: a) The current class instance
Which exception is thrown when an array index is out of bounds? a) IndexOutOfBoundsException b) ArrayIndexOutOfBoundsException c) ArrayOutOfBoundsException d) OutOfBoundsException Answer: b) ArrayIndexOutOfBoundsException
The process of converting an object from its own data type to another data type is called: a) Casting b) Parsing c) Wrapping d) Unboxing Answer: a) Casting
Which method is called automatically when an object is created? a) start() b) run() c) main() d) constructor() Answer: d) constructor()
The "final" keyword in Java can be used with: a) Variables b) Methods c) Classes d) All of the above Answer: d) All of the above
What is the output of the following code?
int x = 5; System.out.println(++x + x++);
a) 11 b) 12 c) 13 d) 14 Answer: c) 13
Which keyword is used to prevent a method from being overridden? a) final b) static c) private d) protected Answer: a) final
Which of the following is not a primitive data type in Java? a) int b) char c) string d) boolean Answer: c) string (Note: "string" should be capitalized as "String" to be a valid class in Java)
Which loop is guaranteed to execute at least once in Java? a) while loop b) do-while loop c) for loop d) enhanced for loop Answer: b) do-while loop
In Java, method overloading is achieved through: a) Changing the method name b) Changing the return type c) Changing the method signature d) Changing the method access modifier Answer: c) Changing the method signature
What is the output of the following code?
String str1 = "Hello"; String str2 = "Hello"; System.out.println(str1 == str2);
a) true b) false c) It will not compile d) It will throw an exception Answer: a) true
Which keyword is used to explicitly call the superclass constructor? a) this b) new c) super d) extend Answer: c) super
In Java, which keyword is used to create a constant variable? a) const b) constant c) final d) static Answer: c) final
What is the correct way to create an array of integers in Java? a) int[] arr = new int[]; b) int[] arr = new int[10]; c) int arr = new int[10]; d) int arr = new int[]; Answer: b) int[] arr = new int[10];
Which of the following is a runtime polymorphism example in Java? a) Method Overloading b) Method Overriding c) Multiple Inheritance d) Method Hiding Answer: b) Method Overriding
The "default" keyword in a switch statement corresponds to: a) case b) continue c) default case d) break Answer: c) default case
What is the output of the following code?
int x = 10; int y = x++ + ++x; System.out.println(y);
a) 20 b) 21 c) 22 d) 23 Answer: b) 21
Which of the following statements is true about an abstract class in Java? a) An abstract class cannot have abstract methods. b) An abstract class can be instantiated. c) An abstract class can have both abstract and non-abstract methods. d) An abstract class cannot have constructors. Answer: c) An abstract class can have both abstract and non-abstract methods.
What does the "static" keyword mean in Java? a) The method can only be called by static classes. b) The method can be called without creating an instance of the class. c) The method is synchronized and thread-safe. d) The method cannot be overridden. Answer: b) The method can be called without creating an instance of the class.
Which of the following is not a Java interface? a) Cloneable b) Runnable c) Serializable d) Finalizable Answer: d) Finalizable
What is the correct way to handle exceptions in Java? a) Using if-else statements b) Using loops c) Using try-catch blocks d) Using inheritance Answer: c) Using try-catch blocks
What is the output of the following code?
int x = 5; System.out.println(x > 3 ? "A" : "B");
a) A b) B c) true d) false Answer: a) A
Which method is used to release resources before an object is garbage collected? a) finalize() b) dispose() c) clean() d) clear() Answer: a) finalize()
In Java, what is the root class of all classes? a) Object b) Root c) Base d) Super Answer: a) Object
What is the output of the following code?
int[] arr = new int[3]; System.out.println(arr[0]);
a) 0 b) 1 c) null d) It will throw an exception Answer: a) 0
What is the purpose of the "break" statement in a loop? a) To exit the loop immediately b) To skip the current iteration and continue with the next iteration c) To continue executing the loop indefinitely d) To check a specific condition in the loop Answer: a) To exit the loop immediately
In Java, a package is used for: a) Storing compiled bytecode b) Storing class data members c) Grouping related classes and interfaces d) Declaring variables and methods Answer: c) Grouping related classes and interfaces
What is the output of the following code?
String str = "Hello"; str.concat(" World"); System.out.println(str);
a) Hello b) World c) Hello World d) It will not compile Answer: a) Hello
Which of the following is a checked exception in Java? a) NullPointerException b) ArrayIndexOutOfBoundsException c) IllegalArgumentException d) IOException Answer: d) IOException
What is the correct way to create and start a thread in Java? a) Thread t = new Thread(); t.run(); b) Thread t = new Thread(); t.execute(); c) Thread t = new Thread(); t.start(); d) Thread t = new Thread(); t.begin(); Answer: c) Thread t = new Thread(); t.start();
What is the purpose of the "equals()" method in Java? a) To compare two objects for equality based on their references b) To compare the contents of two objects for equality c) To check if an object is null d) To check if an object is of a certain type Answer: b) To compare the contents of two objects for equality
Which of the following is not a valid loop construct in Java? a) while b) loop c) for d) do-while Answer: b) loop
What is the output of the following code?
int x = 5; int y = x++ + --x; System.out.println(y);
a) 10 b) 11 c) 12 d) 13 Answer: b) 11
In Java, the "instanceof" operator is used for: a) Checking if a variable is of a certain data type b) Casting a variable to a different data type c) Checking if an object is of a certain class or interface type d) Comparing two variables for equality Answer: c) Checking if an object is of a certain class or interface type
What is the output of the following code?
String str1 = "Hello"; String str2 = new String("Hello"); System.out.println(str1 == str2);
a) true b) false c) It will not compile d) It will throw an exception Answer: b) false
Which of the following is not a type of constructor in Java? a) Default constructor b) Parameterized constructor c) Copy constructor d) Static constructor Answer: d) Static constructor
In Java, what is the purpose of the "finally" block? a) To handle exceptions b) To execute code whether an exception is thrown or not c) To terminate the program d) To force garbage collection Answer: b) To execute code whether an exception is thrown or not
What is the output of the following code?
int x = 10; System.out.println(x < 20 ? "A" : "B");
a) A b) B c) true d) false Answer: a) A
Which of the following is an example of method hiding in Java? a) Method Overloading b) Method Overriding c) Method Shadowing d) Method Wrapping Answer: c) Method Shadowing
What is the output of the following code?
int x = 5; x = x++ + ++x - x--; System.out.println(x);
a) 5 b) 6 c) 7 d) 8 Answer: c) 7
Which of the following statements is true about abstract classes in Java? a) Abstract classes can be instantiated. b) Abstract classes cannot have constructors. c) Abstract classes can have both abstract and final methods. d) Abstract classes cannot have instance variables. Answer: c) Abstract classes can have both abstract and final methods.
Which of the following is a valid method declaration for an abstract method in Java? a) public abstract void method(); b) abstract void method() {}; c) void abstract method(); d) private void abstract method(); Answer: a) public abstract void method();
What is the purpose of the "super" keyword in Java? a) To call the superclass constructor. b) To call a method of the parent class from the child class. c) To access the superclass's instance variables. d) To access the superclass's static methods. Answer: b) To call a method of the parent class from the child class.
In Java, a class that cannot be instantiated and can only be subclassed is known as a: a) Final class b) Abstract class c) Sealed class d) Static class Answer: b) Abstract class
What is the output of the following code?
class A { static void method() { System.out.println("A"); } } class B extends A { static void method() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.method(); } }
a) A b) B c) It will not compile d) It will throw an exception Answer: a) A
Which of the following is an example of composition in Java? a) Inheriting from a superclass. b) Using multiple inheritance through interfaces. c) Including an object of one class as a member of another class. d) Using method overloading. Answer: c) Including an object of one class as a member of another class.
What is the output of the following code?
class A { int x = 5; A() { print(); } void print() { System.out.println("A: " + x); } } class B extends A { int y = 10; B() { x = y; } void print() { System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { B obj = new B(); obj.print(); } }
a) A: 5 b) B: 10, 10 c) B: 5, 10 d) B: 10, 5 Answer: b) B: 10, 10
What is the output of the following code?
class A { static void method() { System.out.println("A"); } } class B extends A { static void method() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj1 = new A(); A obj2 = new B(); obj1.method(); obj2.method(); } }
a) A, A b) A, B c) B, A d) B, B Answer: a) A, A
What is the purpose of the "static" keyword for variables in Java? a) To indicate that the variable can be accessed from outside the class. b) To indicate that the variable can be modified by multiple threads simultaneously. c) To indicate that the variable is shared among all instances of the class. d) To indicate that the variable cannot be changed after initialization. Answer: c) To indicate that the variable is shared among all instances of the class.
What is the output of the following code?
class A { int x = 10; A() { print(); } void print() { System.out.println("A: " + x); } } class B extends A { int y = 20; B() { x = y; } void print() { System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { B obj = new B(); obj.print(); } }
a) A: 10 b) B: 20, 20 c) B: 10, 20 d) B: 20, 10 Answer: b) B: 20, 20
Which of the following is not a valid way to create an instance of an inner class in Java? a)
OuterClass.InnerClass obj = new OuterClass.InnerClass();
b)OuterClass.InnerClass obj =
outerObj.new
InnerClass();
c)InnerClass obj = new InnerClass();
d)OuterClass obj = new OuterClass(); InnerClass innerObj =
obj.new
InnerClass();
Answer: c)InnerClass obj = new InnerClass();
What is the output of the following code?
class A { A() { System.out.println("A"); } } class B extends A { B() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); } }
a) A b) B c) A, B d) B, A Answer: c) A, B
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.method(); } }
a) A b) B c) It will not compile d) It will throw an exception Answer: b) B
In Java, which keyword is used to prevent a class from being subclassed? a) static b) sealed c) abstract d) final Answer: d) final
What is the output of the following code?
class A { void method() { System.out.println("A");} } class B extends A { void method() { System.out.println("B"); } void anotherMethod() { System.out.println("Another method"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.method(); ((B) obj).anotherMethod(); } } ```
a) A, Another method b) B, Another method c) A, B d) B, A Answer: a) A, Another method
What is the output of the following code?
class A { A() { System.out.println("A"); } } class B extends A { B() { super(); System.out.println("B"); } } public class Main { public static void main(String[] args) { B obj = new B(); } }
a) A b) B c) A, B d) B, A Answer: c) A, B
In Java, what is the purpose of the "instanceof" operator when used with interfaces? a) To check if an object is an instance of a class. b) To check if an object is an instance of an abstract class. c) To check if an object implements a specific interface. d) To check if an object extends a specific class. Answer: c) To check if an object implements a specific interface.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj1 = new A(); A obj2 = new B(); obj1.method(); obj2.method(); } }
a) A, A b) B, B c) A, B d) B, A Answer: a) A, A
In Java, what is the purpose of the "this" keyword? a) To call a method of the parent class from the child class. b) To access the superclass's instance variables. c) To access the current class's instance variables. d) To create a new instance of a class. Answer: c) To access the current class's instance variables.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } public class Main { public static void main(String[] args) { B obj = new B(); obj.method(); } }
a) A b) B c) A, B d) B, A Answer: c) A, B
In Java, what is the purpose of the "volatile" keyword for variables? a) To indicate that the variable can be modified by multiple threads simultaneously. b) To indicate that the variable cannot be changed after initialization. c) To indicate that the variable is shared among all instances of the class. d) To indicate that the variable can be accessed from outside the class. Answer: a) To indicate that the variable can be modified by multiple threads simultaneously.
What is the output of the following code?
class A { A() { System.out.println("A"); } } class B extends A { B() { System.out.println("B"); } } class C extends B { C() { System.out.println("C"); } } public class Main { public static void main(String[] args) { C obj = new C(); } }
a) A, B, C b) C, B, A c) A, B d) B, C Answer: b) C, B, A
In Java, what is the purpose of the "transient" keyword for variables? a) To indicate that the variable cannot be changed after initialization. b) To indicate that the variable can be accessed from outside the class. c) To indicate that the variable is shared among all instances of the class. d) To indicate that the variable should not be serialized. Answer: d) To indicate that the variable should not be serialized.
What is the output of the following code?
class A { int x; A() { this(10); } A(int x) { this.x = x; System.out.println("A: " + x); } } class B extends A { int y; B() { y = 20; System.out.println("B: " + y); } B(int y) { super(y); System.out.println("B: " + y); } } public class Main { public static void main(String[] args) { B obj = new B(); } }
a) A: 10, B: 20 b) B: 20, A: 20 c) A: 10 d) B: 20 Answer: a) A: 10, B: 20
In Java, which of the following is true about anonymous inner classes? a) They can have constructors. b) They can be declared as static. c) They can extend multiple classes. d) They cannot access variables outside their scope. Answer: d) They cannot access variables outside their scope.
What is the output of the following code?
class A { int x = 5; void method() { System.out.println("A: " + x); } } class B extends A { int y = 10; void method() { System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.method(); } }
a) A: 5 b) B: 10, 10 c) B: 5, 10 d) B: 10, 5 Answer: c) B: 5, 10
In Java, what is the purpose of the "protected" access modifier for variables and methods? a) To allow access only within the same package. b) To allow access from any class within the same package and its subclasses outside the package. c) To allow access from any class within the same package and its subclasses regardless of the package. d) To allow access from any class within the same package. Answer: b) To allow access from any class within the same package and its subclasses outside the package.
What is the output of the following code?
class A { int x = 10; A() { System.out.println("A"); } A(int x) { this(); this.x = x; System.out.println("A: " + x); } } class B extends A { int y = 20; B() { super(); System.out.println("B"); } B(int x, int y) { super(x); this.y = y; System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { B obj = new B(5, 15); } }
a) A, A: 5, B: 5, 15 b) A, B, A: 5, B: 5, 15 c) A, A, B: 5, B: 5, 15 d) B, A: 5, B: 5, 15 Answer: c) A, A, B: 5, B: 5, 15
In Java, what is the purpose of the "assert" keyword? a) To check if an object is null. b) To specify the superclass of a class. c) To provide a hint to the compiler about optimization. d) To check conditions during program development and testing. Answer: d) To check conditions during program development and testing.
What is the output of the following code?
class A { A() { System.out.println("A"); } } class B extends A { B() { System.out.println("B"); } } class C extends B { C() { System.out.println("C"); } } public class Main { public static void main(String[] args) { C obj = new C(); } }
a) A, B, C b) C, B, A c) A, B d) B, C Answer: b) C, B, A
In Java, which of the following is true about inner classes? a) They can be declared as private. b) They cannot access variables outside their scope. c) They cannot extend other classes. d) They cannot have constructors. Answer: a) They can be declared as private.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { System.out.println("B"); } } class C extends A { void method() { System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj1 = new A(); A obj2 = new B(); A obj3 = new C(); obj1.method(); obj2.method(); obj3.method(); } }
a) A, B, C b) C, B, A c) A, A, A d) B, B, B Answer: c) A, A, A
In Java, what is the purpose of the "strictfp" keyword for classes and methods? a) To indicate that the class or method can be accessed from outside the package. b) To indicate that the class or method is synchronized and thread-safe. c) To indicate that the class or method should be included in the compiled bytecode. d) To ensure consistent floating-point calculations across different platforms. Answer: d) To ensure consistent floating-point calculations across different platforms.
What is the output of the following code?
class A { int x = 10; void method() { System.out.println("A: " + x); } } class B extends A { int x = 20; void method() { System.out.println("B: " + x); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.method(); } }
a) A: 10 b) B: 20 c) A: 20 d) B: 10 Answer: b) B: 20
In Java, what is the purpose of the "default" keyword for methods in interfaces? a) To indicate that the method can only be accessed within the same package. b) To indicate that the method is synchronized and thread-safe. c) To indicate that the method should be included in the compiled bytecode. d) To provide a default implementation for the method in case a class implementing the interface does not override it. Answer: d) To provide a default implementation for the method in case a class implementing the interface does not override it.
What is the output of the following code?
class A { A() { System.out.println("A"); } } class B extends A { B() { System.out.println("B"); } B(int x) { this(); System.out.println("B: " + x); } } public class Main { public static void main(String[] args) { B obj = new B(5); } }
a) A b) B c) A, B, B: 5 d) B, A, B: 5 Answer: c) A, B, B: 5
In Java, what is the purpose of the "strictfp" keyword for classes and methods? a) To indicate that the class or method can be accessed from outside the package. b) To indicate that the class or method is synchronized and thread-safe. c) To indicate that the class or method should be included in the compiled bytecode. d) To ensure consistent floating-point calculations across different platforms. Answer: d) To ensure consistent floating-point calculations across different platforms.
What is the output of the following code?
class A { int x = 10; A() { this(5); } A(int x) { this.x = x; System.out.println("A: " + x); } } class B extends A { int y = 20; B() { super(15); System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { B obj = new B(); } } ```
a) A: 5, B: 15, 20 b) B: 15, B: 10, 20 c) A: 5 d) B: 15 Answer: a) A: 5, B: 15, 20
In Java, what is the purpose of the "synchronized" keyword for methods? a) To indicate that the method can only be accessed within the same package. b) To indicate that the method is thread-safe and can be accessed by multiple threads simultaneously. c) To indicate that the method should be included in the compiled bytecode. d) To provide a default implementation for the method in case a class implementing the interface does not override it. Answer: b) To indicate that the method is thread-safe and can be accessed by multiple threads simultaneously.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } class C extends B { void method() { super.method(); System.out.println("C"); } } public class Main { public static void main(String[] args) { C obj = new C(); obj.method(); } }
a) A, B, C, A, B, C b) A, B, A, C, B, C c) C, B, A d) A, B, C Answer: b) A, B, A, C, B, C
In Java, what is the purpose of the "default" keyword for methods in interfaces? a) To indicate that the method can only be accessed within the same package. b) To indicate that the method is synchronized and thread-safe. c) To indicate that the method should be included in the compiled bytecode. d) To provide a default implementation for the method in case a class implementing the interface does not override it. Answer: d) To provide a default implementation for the method in case a class implementing the interface does not override it.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } class C extends B { void method() { super.method(); System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj1 = new A(); A obj2 = new C(); obj1.method(); obj2.method(); } }
a) A, A b) C, C c) A, B d) C, B Answer: a) A, A
In Java, what is the purpose of the "static" keyword for variables and methods in interfaces? a) To indicate that the variable or method can only be accessed within the same package. b) To indicate that the variable or method can be accessed without creating an instance of the class implementing the interface. c) To indicate that the variable or method is synchronized and thread-safe. d) To provide a default implementation for the variable or method in case a class implementing the interface does not override it. Answer: b) To indicate that the variable or method can be accessed without creating an instance of the class implementing the interface.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } class C extends B { void method() { super.method(); System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj1 = new C(); obj1.method(); } }
a) A, A, B, C b) A, C c) A, B, C d) C, B, A Answer: a) A, A, B, C
In Java, what is the purpose of the "protected" access modifier for variables and methods? a) To allow access only within the same package. b) To allow access from any class within the same package and its subclasses outside the package. c) To allow access from any class within the same package and its subclasses regardless of the package. d) To allow access from any class within the same package. Answer: b) To allow access from any class within the same package and its subclasses outside the package.
What is the output of the following code?
class A { int x = 10; A() { System.out.println("A"); } A(int x) { this(); this.x = x; System.out.println("A: " + x); } } class B extends A { int y = 20; B() { super(15); System.out.println("B: " + x + ", " + y); } } public class Main { public static void main(String[] args) { B obj = new B(); } }
a) A: 5, B: 15, 20 b) B: 15, B: 10, 20 c) A: 5 d) B: 15 Answer: a) A: 5, B: 15, 20
In Java, what is the purpose of the "synchronized" keyword for methods? a) To indicate that the method can only be accessed within the same package. b) To indicate that the method is thread-safe and can be accessed by multiple threads simultaneously. c) To indicate that the method should be included in the compiled bytecode. d) To provide a default implementation for the method in case a class implementing the interface does not override it. Answer: b) To indicate that the method is thread-safe and can be accessed by multiple threads simultaneously.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } class C extends B { void method() { super.method(); System.out.println("C"); } } public class Main { public static void main(String[] args) { C obj = new C(); obj.method(); } }
a) A, B, C, A, B, C b) A, B, A, C, B, C c) C, B, A d) A, B, C Answer: b) A, B, A, C, B, C
In Java, what is the purpose of the "static" keyword for variables and methods in interfaces? a) To indicate that the variable or method can only be accessed within the same package. b) To indicate that the variable or method can be accessed without creating an instance of the class implementing the interface. c) To indicate that the variable or method is synchronized and thread-safe. d) To provide a default implementation for the variable or method in case a class implementing the interface does not override it. Answer: b) To indicate that the variable or method can be accessed without creating an instance of the class implementing the interface.
What is the output of the following code?
class A { void method() { System.out.println("A"); } } class B extends A { void method() { super.method(); System.out.println("B"); } } class C extends B { void method() { super.method(); System.out.println("C"); } } public class Main { public static void main(String[] args) { A obj1 = new C(); obj1.method(); } }
a) A, A b) C, C c) A, B d) C, B, A Answer: a) A, A
Subscribe to my newsletter
Read articles from Anusree Anilkumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by