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!

Q61

Q61 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

Q62

Q62 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

Q63

Q63 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

Q64

Q64 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

Q65

Q65 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

Q66

Q66 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

Q67

Q67 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

Q68

Q68 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

Q69

Q69 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

Q70

Q70 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

Q71

Q71 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

Q72

Q72 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

Q73

Q73 In Java, what is an interface?

A

A fully abstract class

B

A regular class

C

A concrete class

D

A template class

Q74

Q74 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

Q75

Q75 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

Q76

Q76 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

Q77

Q77 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

Q78

Q78 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

Q79

Q79 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

Q80

Q80 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

Q81

Q81 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

Q82

Q82 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

Q83

Q83 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

Q84

Q84 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

Q85

Q85 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

Q86

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

A

throw

B

throws

C

try

D

catch

Q87

Q87 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

Q88

Q88 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

Q89

Q89 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

Q90

Q90 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

ad verticalad vertical
ad