Java Programming — Basics & Syntax
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 11–20 of 100 questions in Basics & Syntax
Q.11 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.12 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.13 Easy Basics & Syntax
Which of the following statements about Java's garbage collection is TRUE?
A Garbage collection is guaranteed to run at specific time intervals
B The programmer must explicitly call System.gc() to free memory
C Garbage collection automatically reclaims memory used by unreferenced objects
D Java does not have automatic garbage collection mechanism
Correct Answer:  C. Garbage collection automatically reclaims memory used by unreferenced objects
EXPLANATION

Java's garbage collector automatically identifies and reclaims memory of objects that are no longer referenced. While System.gc() can be suggested, it's not guaranteed to execute immediately.

Take Test
Q.14 Easy Basics & Syntax
Consider the following code snippet. What is the primary purpose of the 'final' keyword when applied to a class in Java?
A Prevents the class from being instantiated
B Prevents the class from being inherited by other classes
C Makes all methods in the class abstract
D Allows the class to be accessed globally without import
Correct Answer:  B. Prevents the class from being inherited by other classes
EXPLANATION

The 'final' keyword when applied to a class prevents it from being extended or inherited. This is a fundamental OOP concept in Java used to restrict inheritance.

Take Test
Q.15 Easy Basics & Syntax
What is the output of: System.out.println(5 > 3 ? "Yes" : "No");?
A 5
B 3
C Yes
D No
Correct Answer:  C. Yes
EXPLANATION

This uses the ternary operator (conditional operator). The condition 5 > 3 is true, so it returns the value after the '?', which is "Yes".

Take Test
Advertisement
Q.16 Easy Basics & Syntax
Which of the following is NOT a Java keyword?
A synchronized
B volatile
C thread
D transient
Correct Answer:  C. thread
EXPLANATION

'thread' is not a Java keyword. 'synchronized', 'volatile', and 'transient' are all valid Java keywords used for special purposes.

Take Test
Q.17 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
Q.18 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.19 Hard Basics & Syntax
What is the result of: System.out.println('A' + 'B');?
A AB
B 131
C Compilation error
D 131.0
Correct Answer:  B. 131
EXPLANATION

Characters are promoted to their integer ASCII values in arithmetic operations. 'A' has ASCII value 65 and 'B' has ASCII value 66, so 65 + 66 = 131.

Take Test
Q.20 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
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