Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 841–850 of 958
Topics in Java Programming
Q.841 Hard OOP in Java
What is the output?

interface I1 {
void method1();
}
interface I2 extends I1 {
void method2();
}
class C implements I2 {
public void method1() { System.out.println("M1"); }
public void method2() { System.out.println("M2"); }
}
public class Test {
public static void main(String[] args) {
I1 obj = new C();
obj.method2();
}
}
A M2
B M1
C Compilation Error
D Runtime Error
Correct Answer:  C. Compilation Error
EXPLANATION

obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.

Take Test
Q.842 Medium OOP in Java
Which of the following statements about 'instanceof' operator is correct?
A It checks if an object is created from a class
B It checks if an object is an instance of a class or interface
C It checks if a variable is static
D It converts object type
Correct Answer:  B. It checks if an object is an instance of a class or interface
EXPLANATION

The 'instanceof' operator checks whether an object is an instance of a specific class or implements a specific interface.

Take Test
Q.843 Hard OOP in Java
What will be the output?

class Test {
int x = 5;
{
x = 10;
}
Test() {
x = 15;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
A 5
B 10
C 15
D Compilation Error
Correct Answer:  C. 15
EXPLANATION

Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.

Take Test
Q.844 Easy OOP in Java
Which keyword is used to make a variable immutable in Java?
A static
B final
C private
D const
Correct Answer:  B. final
EXPLANATION

'final' keyword makes a variable immutable. Once assigned, its value cannot be changed. 'const' is not a valid Java keyword.

Take Test
Q.845 Medium OOP in Java
What is the output?

class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super();
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}
A B then A
B A then B
C Only B
D Compilation Error
Correct Answer:  B. A then B
EXPLANATION

super() calls parent constructor first. Parent constructor A prints 'A', then child constructor B prints 'B'.

Take Test
Q.846 Easy OOP in Java
Which of the following correctly describes the 'this' keyword?
A Refers to the parent class
B Refers to the current object instance
C Refers to the class itself
D Refers to the method
Correct Answer:  B. Refers to the current object instance
EXPLANATION

'this' is a reference to the current object instance. It's used to refer to instance variables, call other constructors, or pass object reference.

Take Test
Q.847 Hard OOP in Java
What will be the output?

interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("B");
}
}
class C extends B {
public void show() {
System.out.println("C");
}
}
A obj = new C();
obj.show();
A A
B B
C C
D Compilation Error
Correct Answer:  C. C
EXPLANATION

Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.

Take Test
Q.848 Medium OOP in Java
What is the correct way to prevent a class from being inherited in Java?
A Mark it as 'private'
B Mark it as 'final'
C Mark it as 'abstract'
D Mark it as 'static'
Correct Answer:  B. Mark it as 'final'
EXPLANATION

The 'final' keyword prevents a class from being extended. String, Integer, and other wrapper classes are marked final for security and immutability.

Take Test
Q.849 Medium OOP in Java
Which of the following is true about method overloading?
A Methods must have different return types
B Methods must have same name but different parameters
C Methods must be in different classes
D Only constructors can be overloaded
Correct Answer:  B. Methods must have same name but different parameters
EXPLANATION

Method overloading requires methods to have the same name but different parameter lists (number, type, or order of parameters). Return type alone is insufficient.

Take Test
Q.850 Medium OOP in Java
Which of the following best describes encapsulation?
A Hiding implementation details and providing controlled access through methods
B Creating objects from classes
C Inheriting properties from parent class
D Using multiple methods with the same name
Correct Answer:  A. Hiding implementation details and providing controlled access through methods
EXPLANATION

Encapsulation involves bundling data (variables) and methods, hiding implementation details, and providing public methods for access. It protects data integrity.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips