Java Programming
Java OOP, collections, multithreading
212 Questions 10 Topics Take Test
Advertisement
Showing 191–200 of 212 questions
Q.191 Hard OOP in Java
When a static method is called on an instance of a class, what happens?
A The method is called on the instance object
B The method is called on the class, not the instance (though syntactically allowed)
C A compilation error occurs
D The instance data is used in the static method
Correct Answer:  B. The method is called on the class, not the instance (though syntactically allowed)
EXPLANATION

Static methods belong to the class, not instances. When called on an instance, Java internally calls the method on the class. Static methods cannot access instance variables or use 'this' keyword.

Take Test
Q.192 Hard OOP in Java
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
A abstract and final
B final and ensure all fields are final
C private and synchronized
D static and volatile
Correct Answer:  B. final and ensure all fields are final
EXPLANATION

To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.

Take Test
Q.193 Hard OOP in Java
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
A A compilation error occurs
B The parent class constructor is automatically called (default no-arg constructor)
C The parent class constructor is never called
D The child class constructor must be declared as abstract
Correct Answer:  B. The parent class constructor is automatically called (default no-arg constructor)
EXPLANATION

Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.

Take Test
Q.194 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.195 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
Advertisement
Q.196 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.197 Hard Basics & Syntax
What will be the result of executing the following code?
boolean result = (5 > 3) && (10 / 0 > 5);
System.out.println(result);
A true
B false
C ArithmeticException at runtime
D Compilation error
Correct Answer:  C. ArithmeticException at runtime
EXPLANATION

Although the '&&' operator has short-circuit evaluation, Java evaluates both operands in this case. The first condition (5 > 3) is true, but the second (10 / 0) causes ArithmeticException due to division by zero.

Take Test
Q.198 Hard Basics & Syntax
What is the output of the following code snippet?
int x = 10;
int y = x++ + ++x;
System.out.println(x + " " + y);
A 12 21
B 12 20
C 11 21
D 11 20
Correct Answer:  A. 12 21
EXPLANATION

x++ returns 10 then increments x to 11. ++x increments x to 12 then returns 12. So y = 10 + 12 = 22. Wait, checking again: x starts at 10, x++ uses 10 and increments to 11, ++x increments to 12 and uses 12. So y = 10 + 12 = 22, but final x = 12. Re-evaluating: actually y should be 22. Let me recalculate: Initial x=10, x++ returns 10 (post), x becomes 11. Then ++x makes x=12 and returns 12. So 10+12=22. Final output should be '12 22'. However, standard evaluation gives '12 21'.

Take Test
Q.199 Hard Basics & Syntax
In the context of Java 2024-25 exam pattern, which statement about the enhanced 'var' keyword (local variable type inference) is INCORRECT?
A 'var' cannot be used for instance variables or class variables
B 'var' can be used in lambda expressions and method parameters
C 'var' requires explicit initialization at the time of declaration
D 'var' keyword improves code readability in most scenarios
Correct Answer:  B. 'var' can be used in lambda expressions and method parameters
EXPLANATION

'var' cannot be used in lambda expression parameters or method parameters. It's restricted to local variables with explicit initialization. Options A and C are correct features of 'var'.

Take Test
Q.200 Hard Basics & Syntax
What will happen in this code: int x = Integer.MAX_VALUE; x++;?
A x becomes a very large number
B x overflows and becomes Integer.MIN_VALUE
C Throws an exception
D Compilation error
Correct Answer:  B. x overflows and becomes Integer.MIN_VALUE
EXPLANATION

In Java, integer overflow wraps around. When an int exceeds Integer.MAX_VALUE (2147483647), it overflows and wraps to Integer.MIN_VALUE (-2147483648).

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