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!

Q31

Q31 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

Q32

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

A

Scanner

B

InputStream

C

BufferedReader

D

FileReader

Q33

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

A

0

B

null

C

1

D

-1

Q34

Q34 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

Q35

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

A

arr[2]

B

arr[3]

C

arr(2)

D

arr(3)

Q36

Q36 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

Q37

Q37 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

Q38

Q38 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];

Q39

Q39 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]

Q40

Q40 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

Q41

Q41 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

Q42

Q42 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

Q43

Q43 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

Q44

Q44 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

Q45

Q45 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

Q46

Q46 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

Q47

Q47 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

Q48

Q48 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

Q49

Q49 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]

Q50

Q50 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

Q51

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

A

Inheritance

B

Abstraction

C

Encapsulation

D

Polymorphism

Q52

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

A

Inheritance

B

Polymorphism

C

Compilation

D

Abstraction

Q53

Q53 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

Q54

Q54 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

Q55

Q55 Which keyword is used for inheritance in Java?

A

extends

B

implements

C

inherits

D

super

Q56

Q56 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

Q57

Q57 In Java, how is a 'static' variable different from an 'instance' variable?

A

Static variables are shared across all instances of a class

B

Static variables are reinitialized with each object instance

C

Instance variables are shared across all instances of a class

D

Instance variables are constants

Q58

Q58 What is polymorphism in Java?

A

The ability of a variable to hold different data types

B

The ability of a class to implement multiple methods with the same name

C

The ability of a method to perform different tasks based on the context

D

The ability of a class to extend multiple classes

Q59

Q59 What distinguishes an abstract class from a regular class in Java?

A

An abstract class cannot create objects

B

An abstract class only contains abstract methods

C

An abstract class cannot contain constructors

D

An abstract class cannot have instance variables

Q60

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

A

private

B

public

C

protected

D

default

ad verticalad vertical
ad