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!

Q91

Q91 Analyze this pseudocode:

SET num = 0 WHILE num <= 5 IF num MOD 2 = 0 THEN PRINT num END IF INCREMENT num

A

0 2 4

B

0 1 2 3 4 5

C

2 4

D

0 2 4 6

Q92

Q92 Spot the mistake:
int counter = 0;
while(counter < 5){
counter++;
}
System.out.println("Count: " + counter);

A

Loop never terminates

B

Counter not incremented

C

No error

D

Print statement incorrect

Q93

Q93 Which class is commonly used for simple keyboard input in Java?

A

Scanner

B

InputStream

C

BufferedReader

D

FileReader

Q94

Q94 What does 'BufferedReader' in Java provide that 'Scanner' does not?

A

Faster input reading

B

Input from file only

C

Different data types

D

Graphical interface

Q95

Q95 How do you access the third element in an array named 'arr'?

A

arr[2]

B

arr[3]

C

arr(2)

D

arr(3)

Q96

Q96 What will happen if you try to access an index outside the bounds of an array in Java?

A

Compile-time error

B

Runtime error: ArrayIndexOutOfBoundsException

C

No error

D

Logical error

Q97

Q97 What is the purpose of the 'length' attribute in a Java array?

A

To determine the size of the array

B

To modify the size of the array

C

To sort the array

D

To initialize the array

Q98

Q98 In a multi-dimensional array, how do you access the element in the second row and third column of an array named 'matrix'?

A

matrix[1][2]

B

matrix[2][3]

C

matrix[2][2]

D

matrix[1][3]

Q99

Q99 What does this Java code do?
int[][] arr = {{1, 2}, {3, 4}};
for(int i = 0;
i < arr.length;
i++) {
for(int j = 0;
j < arr[i].length;
j++) {
System.out.print(arr[i][j] + " "); } System.out.println();
}

A

Prints a 2D array in matrix form

B

Prints only the first row of the array

C

Prints the sum of the array elements

D

Gives an error

Q100

Q100 Analyze this code:
int[] nums = new int[3];
nums[1] = 10;
int x = nums[1] + nums[0];
System.out.println(x);

A

0

B

10

C

Error

D

None

Q101

Q101 Determine the output of this pseudocode:

SET arr = [10, 20, 30]
SET sum = 0
FOR i = 0 TO LENGTH(arr) - 1
INCREMENT sum BY arr[i]
PRINT sum

A

60

B

10 20 30

C

Error

D

None

Q102

Q102 Spot the mistake in this code:
String[] names = {"Java", "Python", "C"};
for(int i = 0;
i < names.length;
i++) {
System.out.println(names[i].length());
}

A

Prints incorrect lengths

B

No error

C

Syntax error

D

Logic error

Q103

Q103 What is the primary feature of Object-Oriented Programming in Java?

A

Inheritance

B

Abstraction

C

Encapsulation

D

Polymorphism

Q104

Q104 What does 'inheritance' in Java imply?

A

A class can call methods of another class

B

A class shares structure and behaviors from another class

C

A class must override all methods of another class

D

A class is a blueprint for objects

Q105

Q105 Which keyword is used for inheritance in Java?

A

extends

B

implements

C

inherits

D

super

Q106

Q106 Which access modifier in Java makes a member accessible only within its own class?

A

private

B

public

C

protected

D

default

Q107

Q107 Analyze the output of this code snippet:
class Car {
String model; Car(String model) { this.model = model;
}
} public class Main {
public static void main(String[] args) { Car myCar = new Car("Tesla"); System.out.println(myCar.model); } }

A

Tesla

B

null

C

Error

D

Model

Q108

Q108 What does this Java code do?
class Animal {
void sound() {
System.out.println("Generic Sound");
}
} class Dog extends Animal { void sound() { System.out.println("Bark"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Dog(); myAnimal.sound(); } }

A

Prints "Generic Sound"

B

Prints "Bark"

C

Error

D

Does nothing

Q109

Q109 What will this pseudocode output?
CLASS Vehicle SET wheels = 4 METHOD showWheels PRINT wheels END CLASS Main CREATE Vehicle v CALL v.showWheels

A

4

B

0

C

Error

D

None

Q110

Q110 Spot the mistake:
class Calculator {
static void sum(int a, int b) {
System.out.println(a + b);
}
public static void main(String[] args) {
sum(10, 20); }
}

A

No static context for sum

B

No error

C

sum method should return a value

D

Incorrect parameters

Q111

Q111 What is an abstract class in Java?

A

A class that cannot be instantiated and has at least one abstract method

B

A class with only static methods

C

A final class

D

A class that cannot be extended

Q112

Q112 What happens when a class implements an interface in Java?

A

It must provide implementation for all methods in the interface

B

It becomes an abstract class

C

It can no longer extend other classes

D

It automatically inherits from java.lang.Object

Q113

Q113 What will this Java code output?
interface Printable {
void print();
}
class Test implements Printable { public void print() {
System.out.println("Hello");
} }
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.print(); }
}

A

Hello

B

Error

C

Nothing

D

Printable

Q114

Q114 What is the primary purpose of a package in Java?

A

To provide network support

B

To optimize code performance

C

To group related classes and interfaces

D

To manage software versions

Q115

Q115 How do you access a class from a package in Java?

A

By using the 'package' keyword before the class name

B

By importing the class

C

By inheriting the class

D

By implementing an interface

Q116

Q116 What is the purpose of a try-catch block in Java?

A

To handle exceptions

B

To optimize code

C

To declare variables

D

To control program flow

Q117

Q117 Which keyword is used to manually throw an exception in Java?

A

throw

B

throws

C

try

D

catch

Q118

Q118 What will the following code output:
try {
int a = 5 / 0;
System.out.println(a);
}
catch (ArithmeticException e) {
System.out.println("Arithmetic Error");
}

A

Arithmetic Error

B

5

C

Error

D

None

Q119

Q119 What will this pseudocode output?
SET a = 10
SET b = 0
TRY
SET c = a / b
CATCH EXCEPTION PRINT "Error" END TRY

A

Error

B

10

C

0

D

None

Q120

Q120 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