an HCL GUVI product

java programming banner

Java Programming Multiple Choice Questions (MCQs) and Answers

Master Java Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of Java Programming. Begin your placement preparation journey now!

Q121

Q121 Spot the mistake:
try {
String s = null;
System.out.println(s.length());
}
catch (NullPointerException e) {
System.out.println("Null Error");
}

A

No error

B

Null Error should not be caught

C

s should be initialized

D

The length() method cannot be used on strings

Q122

Q122 What is the primary advantage of using generics in Java?

A

Faster execution

B

Reduced memory usage

C

Stronger type checking at compile-time

D

Easier code maintenance

Q123

Q123 What is the difference between a HashMap and a Hashtable in Java?

A

HashMap is synchronized, Hashtable is not

B

Hashtable is synchronized, HashMap is not

C

HashMap allows one null key, Hashtable does not

D

All of the above

Q124

Q124 In Java Collections Framework, which interface represents a mapping from unique keys to values?

A

List

B

Set

C

Map

D

Queue

Q125

Q125 What happens when a duplicate key is put into a HashMap?

A

It replaces the existing key’s value

B

It is ignored

C

It throws an exception

D

A new entry is created

Q126

Q126 What is the difference between the add() and put() methods in Java's Collection Framework?

A

add() is for lists, put() is for maps

B

add() inserts at a specific index, put() does not

C

add() is for sets, put() is for lists

D

There is no difference

Q127

Q127 Which of the following is true about a TreeMap in Java?

A

It does not maintain an order

B

It is not based on the Map interface

C

It orders elements based on natural ordering or a comparator

D

It allows null keys

Q128

Q128 What does this Java code output?
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Python");
System.out.println(map.size());

A

1

B

2

C

3

D

None

Q129

Q129 What will this Java code print?
Set set = new HashSet<>();
set.add(1); set.add(1);
System.out.println(set.size());

A

1

B

2

C

0

D

Error

Q130

Q130 What is the result of executing this code?
Map<String, Integer> map = new TreeMap<>();
map.put("Python", 3);
map.put("Java", 1);
System.out.println(map);

A

{Java=1, Python=3}

B

{Python=3, Java=1}

C

{1=Java, 3=Python}

D

Error

Q131

Q131 Determine the output of this pseudocode:
CREATE list = ["Java", "Python", "Java"] FOR EACH element IN list PRINT element

A

Java Python Java

B

Java Python

C

Java

D

Python Java

Q132

Q132 Analyze this pseudocode:

CREATE set ADD "Java" TO set ADD "Python" TO set ADD "Java" TO set IF "Java" IN set PRINT "Yes" ELSE PRINT "No"

A

Yes

B

No

C

Error

D

None

Q133

Q133 Identify the issue in this code snippet:
List list = new ArrayList<>(); list.get(0);

A

The list is empty

B

The get method is used incorrectly

C

The list should be a LinkedList

D

No error

Q134

Q134 Find the error in this Java code:
Map<String, Integer> map = new HashMap<>();
map.put(null, 1);
map.put(null, 2);
System.out.println(map.get(null));

A

HashMap does not allow null keys

B

Prints 1

C

No error

D

Syntax error

Q135

Q135 Identify the flaw in this Java code:
List list = new LinkedList<>(); list.add("Java"); list.add(1, "Python"); list.remove(2);

A

The remove index is out of bounds

B

LinkedList does not allow insertion at an index

C

No error

D

Syntax error

Q136

Q136 How does StringBuilder differ from StringBuffer in Java?

A

StringBuilder is immutable, StringBuffer is not

B

StringBuilder is faster as it is not synchronized

C

StringBuilder and StringBuffer have different methods

D

There is no difference

Q137

Q137 Why is it recommended to use StringBuilder or StringBuffer for string manipulation in loops?

A

To reduce memory usage

B

To increase execution speed

C

To avoid creating many intermediate String objects

D

Both B and C

Q138

Q138 In Java, what happens when two string literals with the same content are created?

A

They refer to different objects in memory

B

They refer to the same object in the String Constant Pool

C

A runtime error occurs

D

A new object is created each time

Q139

Q139 What will this Java code output?
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);

A

true

B

false

C

An error occurs

D

None of the above

Q140

Q140 What does this Java code do?
StringBuilder sb = new StringBuilder("Java");
sb.append(" Programming");
System.out.println(sb);

A

Appends " Programming" to sb and prints "Java Programming"

B

Creates a new StringBuilder object

C

Throws an error

D

Prints "Java"

Q141

Q141 What is the main difference between a single-threaded and a multi-threaded application?

A

Single-threaded applications can perform multiple tasks at once

B

Multi-threaded applications are always faster

C

Single-threaded applications use less memory

D

Multi-threaded applications can perform multiple tasks at the same time

Q142

Q142 In Java, what is the difference between a user thread and a daemon thread?

A

User threads are low priority, daemon threads are high priority

B

User threads run in the background, daemon threads do not

C

User threads prevent the JVM from exiting, daemon threads do not

D

Daemon threads are used for garbage collection

Q143

Q143 What is the primary purpose of the wait() and notify() methods in Java?

A

To start and stop threads

B

To manage thread priorities

C

To handle synchronization between threads

D

To handle exceptions in threads

Q144

Q144 Analyze the output of this code snippet:
class MyThread extends Thread { public void run() {
for(int i = 0;
i < 5;
i++) {
System.out.println(i); }
}
} public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
for(int i = 5;
i < 10;
i++) {
System.out.println(i); }
}
}

A

The numbers 0 to 9 in sequential order

B

The numbers 5 to 9 followed by 0 to 4

C

A mix of numbers 0 to 9 in no particular order

D

A runtime error

Q145

Q145 Spot the mistake:
synchronized void myMethod() { notify(); wait(); }

A

wait() should be called before notify()

B

notify() and wait() cannot be in the same method

C

No error

D

The method is not properly synchronized

Q146

Q146 How does the Date and Time API in Java 8 improve over the older java.util.Date class?

A

It provides more methods for date manipulation

B

It is thread-safe and immutable

C

It supports internationalization better

D

All of the above

Q147

Q147 What is the purpose of the CompletableFuture class in Java?

A

To simplify multithreading programming

B

To replace the use of raw threads

C

To provide a more complex future interface

D

To handle synchronous programming

Q148

Q148 In Java, what are the benefits of using Callable and Future interfaces?

A

To handle synchronization between threads

B

To perform operations in the background and retrieve their results

C

To handle exceptions in multithreaded code

D

To manage thread lifecycle

Q149

Q149 Analyze the output of this code snippet:
List numbers = Arrays.
asList(1, 2, 3);
numbers.stream().
map(n -> n * n).
forEach(System.out::println);

A

Prints the squares of the numbers

B

Prints the numbers multiplied by 2

C

Prints the sum of the numbers

D

Prints the numbers as they are

Q150

Q150 What is a key consideration when designing an application to support multiple languages?

A

Ensuring all text is in English and translated later

B

Using non-localized date and time formats

C

Using external files for text to facilitate easy translation

D

Hardcoding all strings in the application