wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CODEBURST : JAVA EDITION

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.
Which of the following correctly overrides the `equals` method of the `Object` class?
a)
public boolean equals(Object obj)
b)
public void equals(Object obj)
c)
private boolean equals(Object obj)
d)
public boolean equals(MyClass obj)
2.
Which exception is thrown when a thread is waiting, sleeping, or occupied and the thread is interrupted?
a)
InterruptedException
b)
IllegalThreadStateException
c)
IOException
d)
TimeoutException
3.

What is the output of the following code?

String str = null;

System.out.println(str + "abc");

a)
nullabc
b)
abc
c)
Compile time error
d)
NullPointerException
4.

What is the result of compiling and running this code?

int x = 5;

System.out.println(x++ + ++x);

a)
11
b)
12
c)
10
d)
13
5.
Which of the following is used to restrict a class from being subclassed?
a)
static
b)
abstract
c)
final
d)
protected
6.
Which method must be implemented when a class implements the `Runnable` interface?
a)
start()
b)
run()
c)
execute()
d)
init()
7.
Which keyword is used to prevent method overriding?
a)
final
b)
static
c)
private
d)
abstract
8.
Which of the following statements is true about Java's garbage collection?
a)
You can directly invoke garbage collection.
b)
Java uses reference counting for garbage collection.
c)
The finalize() method is guaranteed to be called.
d)
Objects without references are eligible for GC.
9.
Which of these is not a functional interface in Java 8?
a)
Predicate
b)
Comparator
c)
Runnable
d)
String
10.
Which class is used to create an unmodifiable list in Java?
a)
Collections.unmodifiableList()
b)
List.unmodifiable()
c)
Arrays.asList()
d)
List.of()
11.

What is the output of the following code?

public class Test

{ public static void main(String[] args)

{ int a = 3;

int b = 4;

int result = a++ * ++b;

System.out.println(result); } }

a)
12
b)
15
c)
16
d)
20
12.

What is the output of the following code?

public class ArrayTest

{ public static void main(String[] args)

{ int[] arr = new int[5];

System.out.println(arr[2]); } }

a)
null
b)
0
c)
Compile-time error
d)
Garbage value
13.

What is the output of the following code?

public class ExceptionTest

{ public static void main(String[] args)

{ try

{ int x = 5 / 0; }

catch (ArithmeticException e)

{ System.out.print("A"); }

catch (Exception e)

{ System.out.print("B"); }

finally

{ System.out.print("C"); } } }

a)
A
b)
AC
c)
ABC
d)
BC
14.

Output of this code?

import java.util.*;

public class HashSetTest

{

public static void main(String[] args)

{

Set set = new HashSet<>();

set.add("one");

set.add("two");

set.add("one");

System.out.println(set.size()); }

}

a)
1
b)
2
c)
3
d)
Compile-time error
15.

What is the output of the following code?

class Parent

{

static void display()

{

System.out.println("Parent"); }

}

class Child extends Parent

{

static void display()

{ System.out.println("Child"); }}

public class Test

{

public static void main(String[] args)

{

Parent obj = new Child();

obj.display(); } }

a)
Parent
b)
Child
c)
Compile-time error
d)
Runtime error
16.

What is the output of the following code?

public class ShiftTest

{ public static void main(String[] args)

{

System.out.println(2 << 3); } }

a)
8
b)
10
c)
16
d)
6
17.

What is the output of the following code?

public class LoopTest

{

public static void main(String[] args)

{

for (int i = 0; i < 3; i++)

{

System.out.print(i);

}

System.out.print(i);

} }

a)
0122
b)
0123
c)
Compile-time error
d)
012
18.

What is the output of the following code?

public class StringPool

{

public static void main(String[] args)

{ String s1 = new String("java");

String s2 = "java";

System.out.println(s1 == s2); } }

a)
true
b)
false
c)
Compile error
d)
null
19.

What is the output of the following code?

import java.util.*;

public class ListTest

{

public static void main(String[] args)

{

List list = Arrays.asList(1, 2, 3);

list.set(0, 10);

System.out.println(list); } }

a)
[1, 2, 3]
b)
[10, 2, 3]
c)
Compile-time error
d)
UnsupportedOperationException
20.

What is the output of the following code?

public class XORTest

{ public static void main(String[] args)

{ int x = 5 ^ 3;

System.out.println(x); } }

a)
6
b)
2
c)
1
d)
8