Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 421–430 of 476 questions
Q.421 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.422 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.423 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
Q.424 Medium OOP in Java
What will happen when you try to instantiate an interface in Java?
A It will create an instance successfully
B It will throw a RuntimeException
C It will cause a compilation error
D It depends on the JVM version
Correct Answer:  C. It will cause a compilation error
EXPLANATION

Interfaces cannot be instantiated directly. You must create a class that implements the interface. Attempting this causes a compilation error.

Take Test
Q.425 Medium OOP in Java
Which of the following statements about abstract classes is TRUE?
A Abstract classes can be instantiated
B Abstract classes cannot have concrete methods
C Abstract classes can have abstract methods and concrete methods
D Abstract classes must extend another abstract class
Correct Answer:  C. Abstract classes can have abstract methods and concrete methods
EXPLANATION

Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.

Take Test
Q.426 Medium OOP in Java
What is the output of the following code?

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

Variable overriding doesn't work in Java like method overriding. obj.x accesses the variable from reference type A, which has value 10.

Take Test
Q.427 Medium Basics & Syntax
In Java, what is the relationship between an interface and a class in terms of implementation?
A A class can implement multiple interfaces but can extend only one class
B A class can extend multiple interfaces and implement one class
C A class can only implement one interface and extend one class
D An interface and a class cannot be used together in Java
Correct Answer:  A. A class can implement multiple interfaces but can extend only one class
EXPLANATION

Java supports multiple interface implementation but single class inheritance. A class uses 'implements' keyword for interfaces and 'extends' for classes. This is a fundamental OOP concept.

Take Test
Q.428 Medium Basics & Syntax
Which of the following statements about Java's String class is TRUE?
A Strings are mutable objects in Java
B The String class is marked as 'final' to prevent modification of its behavior
C Creating a new String using 'new' keyword always creates a new object in the string pool
D String concatenation using '+' operator is more efficient than StringBuilder for single operations
Correct Answer:  B. The String class is marked as 'final' to prevent modification of its behavior
EXPLANATION

The String class is declared as 'final' in Java, which prevents inheritance and ensures immutability. Strings are immutable, 'new' creates objects in heap not pool, and StringBuilder is generally better for concatenation.

Take Test
Q.429 Medium Basics & Syntax
Consider a real-world scenario where you need to implement a logging system. Which access modifier would you use for internal helper methods that should not be accessible outside the class?
A public
B protected
C private
D default (package-private)
Correct Answer:  C. private
EXPLANATION

'private' access modifier restricts the method to be accessible only within the same class, making it ideal for internal helper methods. This follows encapsulation principles.

Take Test
Q.430 Medium Basics & Syntax
Which of the following correctly demonstrates method overloading in Java?
A public void display(int x) { } public int display(int y) { return 0; }
B public void display(int x) { } public void display(float x) { }
C public void display(int x) { } private void display(int x) { }
D public void display(int x) { } public void Display(int x) { }
Correct Answer:  B. public void display(int x) { } public void display(float x) { }
EXPLANATION

Method overloading requires methods with the same name but different parameter types or number of parameters. Option A has different return types (not sufficient), C has different access modifiers (not overloading), D has different case names (different methods).

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