Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

958 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 861–870 of 958
Topics in Java Programming
Q.861 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.862 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.863 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.864 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.865 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.866 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.867 Easy Basics & Syntax
What will happen when you execute the following code?
int[] arr = new int[5];
System.out.println(arr[5]);
A Output: 0
B Output: null
C ArrayIndexOutOfBoundsException at runtime
D Compilation error
Correct Answer:  C. ArrayIndexOutOfBoundsException at runtime
EXPLANATION

An array of size 5 has valid indices from 0 to 4. Accessing arr[5] throws ArrayIndexOutOfBoundsException at runtime because index 5 is out of bounds.

Take Test
Q.868 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
Q.869 Medium Basics & Syntax
Consider a scenario where you need to create a variable that can hold references to objects of any type. Which of the following approaches would be most appropriate in Java 2024 standards?
A Use raw type: List myList = new ArrayList();
B Use generics: List myList = new ArrayList();
C Use Object type: Object obj = new Object();
D Use var keyword: var myList = new ArrayList();
Correct Answer:  B. Use generics: List myList = new ArrayList();
EXPLANATION

Using generics with wildcards (List<?>) is the safest and most type-safe approach to handle objects of any type in modern Java. The 'var' keyword is also acceptable but wildcards provide better type checking.

Take Test
Q.870 Medium Basics & Syntax
What will be the output of the following code?
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
A true
B false
C null
D Compilation error
Correct Answer:  B. false
EXPLANATION

The '==' operator compares object references, not content. str1 refers to the string pool while str2 is a new object in heap memory, hence they have different references.

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