wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java MCQs for Placement Preparation

Total questions: 20

Worksheet time: 11mins

Name
Class
Date
1.

1. int a = 10; int b = a; b += 5; System.out.println(a + " " + b);

a)

a) 10 10

b)

b) 10 15

c)

c) 15 10

d)

d) 15 15

2.

String s1 = "Java"; String s2 = s1; s1 = s1.toUpperCase(); System.out.println(s1 + " " + s2);

a)

a) JAVA JAVA

b)

b) JAVA Java

c)

c) Java JAVA

d)

d) Java Java

3.

int x = -5; if(x < 0) if(x % 2 == 0) System.out.println("Even Negative"); else System.out.println("Odd Negative"); else System.out.println("Positive");

a)

Odd Negative

b)

Even Negative

c)

Positive

d)

Compilation Error

4.

int n = 1; switch(n){ case 1: System.out.print("A "); case 2: System.out.print("B "); default: System.out.print("C"); }

a)

A

b)

A B

c)

A B C

d)

C

5.

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

for(int x : arr) {

x *= 2;

}

for(int i = 0; i < arr.length; i++) {

arr[i] += 3;

}

System.out.println(Arrays.toString(arr));

a)

[2, 4, 6, 8]

b)

[4,5,6,7]

c)

[3, 4, 5, 6]

d)

[1, 2, 3, 4]

6.

ArrayList list = new ArrayList<>(); list.add("A"); list.add("B"); list.add(1, "C"); System.out.println(list);

a)

[A, B, C]

b)

[A, C, B]

c)

[C, A, B]

d)

[A, B]

7.

Set set = new HashSet<>(); set.add(5); set.add(3); set.add(5); set.add(2); System.out.println(set.contains(5) + " " + set.size());

a)

true 3

b)

true 4

c)

false 3

d)

false 4

8.

Map map = new HashMap<>(); map.put("X", 10); map.put("Y", 20); map.put("X", 30); map.put("X", 30); System.out.println(map.get("X") + " " + map.size());

a)

10 2

b)

30 2

c)

30 3

d)

10 3

9.

class A { void msg(){ System.out.println("A"); } } class B extends A { void msg(){ System.out.println("B"); } } class C extends B { } C obj = new C(); obj.msg();

a)

A

b)

B

c)

C

d)

Compilation Error

10.

class Test { void show(int a){ System.out.println("int"); } void show(double a){ System.out.println("double"); } } new Test().show(5);

a)

int

b)

double

c)

Compilation Error

d)

Runtime Error

11.

Recursion Output static int sum(int n){ if(n==0) return 0; return n + sum(n-1); } System.out.println(sum(3));

a)

6

b)

3

c)

0

d)

9

12.

What will be the output if the following code is executed? class A { void msg(){ System.out.println("A"); } } class B extends A { void msg(){ System.out.println("B"); } } A obj = new B(); obj.msg();

a)

B

b)

A

c)

A B

d)

Compilation Error

13.

ArrayList list = new ArrayList<>(Arrays.asList(1,2,3)); list.remove(1); System.out.println(list);

a)

[1,2,3]

b)

[1,3]

c)

[2,3]

d)

Error

14.

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

for(int i=0;i<arr.length/2;i++){

int temp = arr[i];

arr[i]=arr[arr.length-1-i];

arr[arr.length-1-i]=temp;

}

System.out.println(Arrays.toString(arr));

a)

[1,2,3]

b)

[3,2,1]

c)

[3,1,2]

d)

[2,1,3]

15.

class A {void msg() { System.out.println("A"); }}
class B extends A {void msg() { System.out.println("B"); }}

List list = new ArrayList<>();
list.add(new B());
list.add(new A());
for(A obj : list) obj.msg();

a)

a) A B

b)

b) B A

c)

c) Compile Error

d)

d) Runtime Error

16.

Match the OOP concepts with the correct description:

  1. Encapsulation

  2. Inheritance

  3. Polymorphism

  4. Abstraction

Descriptions:

  • (i) Hiding internal details and exposing essential features

  • (ii) Wrapping data + methods together

  • (iii) Using one interface to represent multiple forms

  • (iv) Acquiring properties of another class

Correct Mapping:

a)

1-ii, 2-iv, 3-iii, 4-i

b)

1-i, 2-ii, 3-iii, 4-iv

c)

1-iv, 2-i, 3-ii, 4-iii

d)

1-iii, 2-ii, 3-iv, 4-i

17.

Which statement about abstraction is correct?
a) Abstraction is achieved using abstract classes and interfaces
b) Abstraction is hiding data inside a class using private variables
c) Abstraction is achieved only through constructors
d) Abstraction means method overloading

a)

A

b)

B

c)

C

d)

D

18.

Why is String in Java considered immutable?
a) Because it is declared as final
b) Because changing a string creates a new object in the heap
c) Because it is a primitive type
d) Because it cannot be stored in heap memory

a)

A

b)

B

c)

C

d)

D

19.

Which of the following is correct?
a) Encapsulation = hiding internal details, Abstraction = binding data & methods
b) Abstraction = hiding implementation, Encapsulation = restricting direct access
c) Both are exactly the same concept
d) Encapsulation can only be achieved using interfaces

a)

A

b)

B

c)

C

d)

D

20.

class Test {

void show(int a, long b) { System.out.println("int-long"); }

void show(long a, int b) { System.out.println("long-int"); }

}

public class Main {

public static void main(String[] args) {

new Test().show(10, 10);

}

}

a)

int-long

b)

long-int

c)

Compile-time Error

d)

Runtime Error