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!

Q31

Q31 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

Q32

Q32 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

Q33

Q33 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

Q34

Q34 What will the following code output:
class Test {
static int x = 10;
} public class Main {
public static void main(String[] args) {
Test t1 = new Test(); System.out.println(t1.x); } }

A

10

B

0

C

Error

D

Null

Q35

Q35 Determine the output of this pseudocode:

CLASS Animal METHOD sound PRINT "Generic Sound" END CLASS Dog EXTENDS Animal METHOD sound PRINT "Bark" END CLASS Main CREATE Dog d CALL d.sound

A

Generic Sound

B

Bark

C

Error

D

None

Q36

Q36 What will be the result of this pseudocode?

CLASS Book VARIABLE title = "Java Programming" METHOD getTitle PRINT title END CLASS Main CREATE Book b CALL b.getTitle

A

Java Programming

B

null

C

Error

D

Title

Q37

Q37 Analyze this pseudocode:

CLASS Shape METHOD area RETURN 0 END CLASS Circle EXTENDS Shape VARIABLE radius = 5 METHOD area RETURN 3.14 * radius * radius END CLASS Main CREATE Circle c PRINT c.area

A

0

B

78.5

C

Error

D

None

Q38

Q38 Identify the issue in this code snippet:
class Book {
private String title; void setTitle(String title) {
title = title; }
}

A

The method does not set the instance variable

B

No issue

C

Syntax error

D

Logic error

Q39

Q39 Find the error in this Java code:
class Animal {
Animal() {
System.out.println("An animal is created");
}
} class Dog extends Animal {
Dog() { super();
System.out.println("A dog is created");
}
} public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}

A

Incorrect use of super()

B

No error

C

Constructor not called

D

Syntax error

Q40

Q40 How does StringBuilder differ from StringBuffer in Java?

A

StringBuilder is synchronized; StringBuffer is not

B

StringBuilder is faster as it is not synchronized

C

StringBuilder and StringBuffer have different methods

D

StringBuilder allows storing strings of variable length

Q41

Q41 In Java, what is an interface?

A

A fully abstract class

B

A regular class

C

A concrete class

D

A template class

Q42

Q42 Can an interface in Java contain default methods?

A

Yes, from Java 8 onwards

B

No, interfaces cannot have default methods

C

Only in abstract classes

D

Only static methods are allowed

Q43

Q43 Can an interface in Java extend multiple interfaces?

A

Yes

B

No

C

Only if they are marker interfaces

D

Only abstract classes can extend multiple interfaces

Q44

Q44 Analyze the output of this code snippet:
interface A {
int val = 5;
}
interface B extends A {
int val = 10;
}
class Test implements B {
void display() {
System.out.println(val);
}
} public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}

A

5

B

10

C

Error

D

None

Q45

Q45 What does the 'import' statement do in a Java program?

A

It includes native libraries

B

It enhances performance

C

It allows access to classes in packages

D

It compiles the Java code

Q46

Q46 What is the significance of the CLASSPATH environment variable in Java?

A

It specifies the installation directory of Java

B

It sets the maximum memory allocation for the Java Virtual Machine

C

It tells the JVM where to look for user-defined classes and packages

D

It configures the security settings of the JVM

Q47

Q47 What is an exception in Java?

A

An error during program execution

B

A type of Java class

C

A method declaration

D

A user input error

Q48

Q48 What happens if an exception is thrown in a try block and is not caught in the corresponding catch block?

A

The exception is ignored

B

The program terminates

C

The exception is handled by the default handler

D

The program continues execution

Q49

Q49 What is the difference between checked and unchecked exceptions in Java?

A

Checked exceptions are detected at compile-time, unchecked at runtime

B

Checked exceptions are fatal, unchecked are not

C

Checked exceptions are errors in the code, unchecked are system errors

D

There is no difference

Q50

Q50 Analyze the output of this code snippet:
try {
int[] arr = new int[5];
arr[10] = 100;
System.out.println("Value set");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index Error");
}

A

Value set

B

Index Error

C

Error

D

None

Q51

Q51 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

Q52

Q52 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

Q53

Q53 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

Q54

Q54 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

Q55

Q55 What are generics in Java?

A

Templates for creating collections

B

Special methods in Java

C

Memory allocation techniques

D

Data types in Java

Q56

Q56 Which collection type does not allow duplicate elements?

A

ArrayList

B

Vector

C

Set

D

List

Q57

Q57 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

Q58

Q58 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

Q59

Q59 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

Q60

Q60 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