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!

Q1

Q1 Which of these is not a feature of Java?

A

Object-oriented

B

Platform-independent

C

Compiled

D

Interpreted language

Q2

Q2 What is the purpose of the PATH environment variable in Java?

A

To locate Java libraries

B

To store Java bytecode

C

To locate the Java compiler

D

To optimize Java code

Q3

Q3 In Java, how should class names be written?

A

camelCase

B

snake_case

C

PascalCase

D

kebab-case

Q4

Q4 Which of these is a single-line comment in Java?

A

/* comment */

B

// comment

C

<!-- comment -->

D

% comment

Q5

Q5 What is the default value of a boolean variable in Java?

A

true

B

false

C

0

D

null

Q6

Q6 What is the result of this operation in Java:
(int)(7.9)?

A

7

B

7.9

C

8

D

Syntax Error

Q7

Q7 Which keyword is used to define a constant variable in Java?

A

final

B

static

C

const

D

immutable

Q8

Q8 What is the range of the short data type in Java?

A

-32768 to 32767

B

-128 to 127

C

-2147483648 to 2147483647

D

0 to 65535

Q9

Q9 What will be the output of the following code snippet:
int a = 10;
int b = 20;
System.out.println(a + b);

A

10

B

20

C

30

D

Error

Q10

Q10 What does the following Java code print?
int x = 5;
int y = x++;
System.out.println(y);

A

5

B

6

C

Syntax Error

D

None of the above

Q11

Q11 Determine the output:
SET num = 8
IF num MOD 2 = 0
THEN PRINT "Even"
ELSE
PRINT "Odd"

A

Even

B

Odd

C

Error

D

No output

Q12

Q12 Identify the error in this code:
int[] nums = new int[2]; nums[0] = 1; nums[1] = 2; nums[2] = 3;

A

Array index out of bounds

B

Incorrect array declaration

C

No error

D

Syntax error

Q13

Q13 Which control structure is used to execute a block of code multiple times?

A

if

B

switch-case

C

for

D

try-catch

Q14

Q14 In a 'switch-case' statement, what is the role of the 'break' keyword?

A

To pause the execution

B

To terminate the case block

C

To skip to the next case

D

To repeat the case block

Q15

Q15 What will the following loop print?
for(int i = 0;
i < 3;
i++){
System.out.println(i);
}

A

0 1 2

B

1 2 3

C

0 1 2 3

D

None

Q16

Q16 What will this pseudocode output?

SET count = 5 DO PRINT count COUNTDOWN count

A

5 4 3 2 1

B

5

C

1 2 3 4 5

D

None

Q17

Q17 Identify the error in this code:
for(int i=0;
i<=5;
i++) {
System.out.println(i);
}
System.out.println(i);

A

Syntax error outside loop

B

No error

C

Variable i not accessible outside loop

D

Infinite loop

Q18

Q18 Find the error in this code:
int num = 1;
do{
System.out.println(num);
num++;
}
while(num <= 3);
System.out.println(num);

A

Infinite loop

B

Syntax error

C

Prints wrong values

D

No error

Q19

Q19 In Java, what is the default value of an array of integers?

A

0

B

null

C

1

D

-1

Q20

Q20 Which of these is not a valid way to instantiate an array in Java?

A

int[] arr = new int[5];

B

int arr[] = new int[5];

C

int arr[] = {1, 2, 3, 4, 5};

D

int arr[] = int[5];

Q21

Q21 What will the following code output:
int[] arr = {1, 2, 3};
for(int num : arr) {
System.out.println(num);
}

A

1 2 3

B

123

C

Error

D

None

Q22

Q22 What will be the result of executing this code snippet?
String[] names = {"Java", "Python", "C++"};
System.out.println(names[1].length());

A

4

B

5

C

6

D

Error

Q23

Q23 What will this pseudocode output?
SET arr = [1, 2, 3] FOR EACH num IN arr PRINT num

A

1 2 3

B

123

C

Error

D

None

Q24

Q24 What will be the result of this pseudocode?

SET matrix = [[1, 2], [3, 4]]
PRINT matrix[0][1]

A

1

B

2

C

3

D

4

Q25

Q25 Identify the issue in this code snippet:
int[] numbers = new int[5];
for(int i = 0;
i <= numbers.length;
i++) {
System.out.println(numbers[i]);
}

A

Out of bounds array access

B

No issue

C

Syntax error

D

Logic error

Q26

Q26 Find the error in this Java code:
int[][] matrix = new int[2][2];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
System.out.println(matrix[1][1]);

A

Prints incorrect value

B

No error

C

Syntax error

D

Missing initialization for matrix[1][1]

Q27

Q27 Identify the flaw in this Java code:
char[] chars = new char[-1];

A

Negative array size

B

Syntax error

C

No error

D

Logic error

Q28

Q28 Which of these is not a principle of Object-Oriented Programming?

A

Inheritance

B

Polymorphism

C

Compilation

D

Abstraction

Q29

Q29 What is an instance variable in Java?

A

A variable defined inside a method

B

A variable defined outside of any method but inside a class

C

A static variable

D

A final variable

Q30

Q30 What is the purpose of a constructor in Java?

A

To create an instance of a class

B

To initialize variables

C

To define methods

D

To declare classes