Java Programming
Java OOP, collections, multithreading
476 Questions 10 Topics Take Test
Advertisement
Showing 431–440 of 476 questions
Q.431 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.432 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
Q.433 Medium Basics & Syntax
Which method is called automatically when an object is garbage collected?
A finalize()
B destroy()
C delete()
D cleanup()
Correct Answer:  A. finalize()
EXPLANATION

The finalize() method is called by the garbage collector before an object is destroyed. It can be used to perform cleanup operations, though it's generally not recommended in modern Java.

Take Test
Q.434 Medium Basics & Syntax
Which of the following will compile and run without errors?
A public static void main() { }
B public static void main(String[] args) { }
C public void main(String[] args) { }
D static void main(String[] args) { }
Correct Answer:  B. public static void main(String[] args) { }
EXPLANATION

The correct signature for the main method is 'public static void main(String[] args)'. It must be public, static, void, named 'main', and accept a String array parameter. Any deviation will not be recognized as the entry point.

Take Test
Q.435 Medium Basics & Syntax
Which of the following correctly represents variable declaration and initialization in Java?
A int x; x = 5.5;
B double y = 10;
C boolean flag = true;
D String name = 123;
Correct Answer:  C. boolean flag = true;
EXPLANATION

Option C is correct. Option A causes a compilation error (cannot assign double to int). Option B works but there's implicit conversion. Option D causes a compilation error (cannot assign int to String).

Take Test
Advertisement
Q.436 Medium Basics & Syntax
Which statement about constructors is INCORRECT?
A Constructors can be overloaded
B Constructors must return a value
C Constructors are called automatically when an object is created
D A class can have multiple constructors
Correct Answer:  B. Constructors must return a value
EXPLANATION

Constructors do NOT have a return type, not even void. They automatically initialize objects when created. They can be overloaded and a class can have multiple constructors with different parameters.

Take Test
Q.437 Medium Basics & Syntax
What is the difference between 'break' and 'continue' in loop statements?
A Both terminate the loop
B break exits the loop; continue skips current iteration
C continue exits the loop; break skips current iteration
D They have the same functionality
Correct Answer:  B. break exits the loop; continue skips current iteration
EXPLANATION

'break' immediately exits the loop, while 'continue' skips the remaining statements in the current iteration and jumps to the next iteration.

Take Test
Q.438 Medium Basics & Syntax
Which access modifier allows a member to be accessible only within the same package?
A public
B private
C protected
D default (no modifier)
Correct Answer:  D. default (no modifier)
EXPLANATION

When no access modifier is specified, it's called package-private or default access. Members with default access are accessible only within the same package, not from other packages.

Take Test
Q.439 Medium Basics & Syntax
What will happen if you try to access an index out of bounds in an array?
A Returns 0
B Returns null
C Throws ArrayIndexOutOfBoundsException
D Compilation error
Correct Answer:  C. Throws ArrayIndexOutOfBoundsException
EXPLANATION

Accessing an array index that doesn't exist throws an ArrayIndexOutOfBoundsException at runtime. This is a checked exception that must be handled or declared.

Take Test
Q.440 Medium Basics & Syntax
Which of the following statements about static methods is correct?
A Static methods can access instance variables directly
B Static methods can be overridden by subclasses
C Static methods can only be called using the class name
D Static methods must have a return type
Correct Answer:  C. Static methods can only be called using the class name
EXPLANATION

Static methods belong to the class, not to any instance. They are called using the class name (e.g., ClassName.methodName()). They cannot directly access instance variables and cannot be overridden (though they can be hidden).

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