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!

Q91

Q91 Determine the output of this pseudocode:
TRY PRINT "Start" THROW NEW EXCEPTION "Failed" CATCH EXCEPTION PRINT "Caught" END TRY PRINT "End"

A

Start Caught End

B

Start Failed

C

Caught

D

Start Caught

Q92

Q92 What will be the result of this pseudocode?
SET arr = [1, 2, 3]
TRY
PRINT arr[3]
CATCH EXCEPTION
PRINT "Array Error" END TRY

A

1 2 3

B

Array Error

C

Error

D

None

Q93

Q93 Identify the issue in this code snippet:
try {
int x = 5 / 0;
}
catch (Exception e) {
e.printStackTrace();
}
finally {
System.out.println("Done");
}

A

Division by zero

B

No error

C

Missing throw statement

D

Syntax error

Q94

Q94 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

Q95

Q95 Find the error in this Java code:
try {
File file = new File("test.txt");
FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e) {
System.out.println("File not found");
}

A

File not found

B

No error

C

Missing import statements for File and FileReader

D

Syntax error

Q96

Q96 Identify the flaw in this Java code:
class CustomException extends Exception { }
try {
throw new CustomException();
}
catch (CustomException e) {
System.out.println("Custom exception caught");
}

A

CustomException should not be caught

B

CustomException should extend RuntimeException

C

Missing try block

D

No error

Q97

Q97 What are generics in Java?

A

Templates for creating collections

B

Special methods in Java

C

Memory allocation techniques

D

Data types in Java

Q98

Q98 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

Q99

Q99 Which collection type does not allow duplicate elements?

A

ArrayList

B

Vector

C

Set

D

List

Q100

Q100 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

Q101

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

A

List

B

Set

C

Map

D

Queue

Q102

Q102 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

Q103

Q103 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

Q104

Q104 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

Q105

Q105 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

Q106

Q106 Analyze the output of this code snippet:
List list = new ArrayList<>();
list.add("Java");
list.add("Java");
System.out.println(list.get(1));

A

Java

B

Error

C

null

D

None

Q107

Q107 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

Q108

Q108 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

Q109

Q109 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

Q110

Q110 What will be the result of this pseudocode?

CREATE map SET map["Java"] = 1 SET map["Python"] = 2 SET map["Java"] = 3 PRINT map["Java"]

A

1

B

2

C

3

D

Error

Q111

Q111 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

Q112

Q112 What will this pseudocode output?

CREATE map SET map[1] = "Java" SET map[2] = "Python" SET map[1] = "C++" PRINT map[1]

A

Java

B

Python

C

C++

D

Error

Q113

Q113 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

Q114

Q114 Spot the mistake:
Set set = new TreeSet<>(); set.add(null);

A

TreeSet does not allow null

B

No error

C

Set should be HashSet

D

Syntax error

Q115

Q115 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

Q116

Q116 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

Q117

Q117 What is the String Constant Pool in Java?

A

A collection of all String literals

B

A memory area for constant String storage

C

A pool for reusable String objects

D

All of the above

Q118

Q118 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

Q119

Q119 What is the result of concatenating strings using the '+' operator in Java?

A

A new String object is created every time

B

The original string is modified

C

No new objects are created

D

A StringBuilder object is used internally

Q120

Q120 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

ad verticalad vertical
ad