wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Final Quiz

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

class MyThread extends Thread {

public void run() {

System.out.println("Child Thread");

}

public static void main(String[] args) {

MyThread t = new MyThread();

t.start();

System.out.println("Main Thread");

}

}

a)

Only "Child Thread"

b)

Only "Main Thread"

c)

"Main Thread" followed by "Child Thread"

d)

Either "Child Thread" or "Main Thread" may come first

2.

What will be the output if run() is called instead of start()?

MyThread t = new MyThread();

t.run();

System.out.println("Main Thread");

a)

"Child Thread" and "Main Thread" printed from different threads

b)

"Child Thread" and "Main Thread" printed from the same thread

c)

Only "Main Thread"

d)

Compilation error

3.

What is the state of a thread once the start() method is called but before run() is executed?

a)

New

b)

Runnable

c)

Running

d)

Blocked

4.

public class Test extends Thread {

public void run() {

System.out.println("Running");

}

public static void main(String[] args) {

Test t1 = new Test();

Test t2 = new Test();

t1.start();

t2.start();

}

}

How many threads are running?

a)

1

b)

2

c)

3

d)

4

5.

public class SyncTest {

public synchronized void test() {

System.out.println(Thread.currentThread().getName() + " is running");

}

public static void main(String[] args) {

SyncTest obj = new SyncTest();

Thread t1 = new Thread(() -> obj.test());

Thread t2 = new Thread(() -> obj.test());

t1.start();

t2.start();

}

}

a)

Both threads run together

b)

Only one thread runs

c)

Threads run one after another due to synchronization

d)

Compilation error

6.

public class MyClass {

public synchronized void methodA() {

System.out.println("Method A - " + Thread.currentThread().getName());

}

public void methodB() {

synchronized (this) {

System.out.println("Method B - " + Thread.currentThread().getName());

}

}

}

If two threads call methodA() and methodB() on the same object, which statement is true?

a)

Both can execute simultaneously

b)

Only one can execute at a time

c)

They may deadlock

d)

It causes a compilation error

7.

public class Counter {

private int count = 0;

public synchronized void increment() {

count++;

}

public int getCount() {

return count;

}

}

Two threads each call increment() 1000 times. What can be said about getCount()?

a)


Will cause an exception

b)

B. Always less than 2000

c)

Could be 2000 or less

d)

Always 2000

8.

Which statement is true about the difference between synchronized method and synchronized(this) block?

a)

synchronized method is faster

b)

Both lock different monitors

c)

Both lock the same object monitor (this)

d)

synchronized(this) cannot be used in constructors

9.

What does a static synchronized method lock on?

a)

The instance of the class

b)


The .class object (i.e., class-level monitor)

c)


The thread that calls it

d)

Nothing, it behaves like a non-synchronized method

10.

class Example {

public static synchronized void staticMethod() {

System.out.println("Static method");

}

public synchronized void instanceMethod() {

System.out.println("Instance method");

}

}

a)

Yes

b)

No

c)

Only if both methods are synchronized

d)

Only on different objects

11.

Which of the following is a valid lambda expression in Java?

a)

A. (int x, int y) => x + y

b)

B. x, y -> x + y

c)

C. (x, y) -> x + y

d)

D. (x, y) => { return x + y; }

12.

What is a functional interface?




a)

An interface with only static methods

b)

An abstract class with a lambda method

c)

An interface with default and static methods only

d)

An interface with exactly one abstract method

13.

Which built-in functional interface represents a function that takes one argument and returns a result?




a)

Function<T, R>

b)

Predicate<T>

c)

Supplier<T>

d)

Consumer<T>

14.

Predicate<String> startsWithJ = s -> s.startsWith("J");

System.out.println(startsWithJ.test("Java"));

a)

true

b)

false

c)

Compilation error

d)

J

15.

import java.util.Arrays;

import java.util.List;

public class TestLambda {

public static void main(String[] args) {

List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);

int sum = nums.stream()

.filter(n -> n % 2 == 0)

.map(n -> n * n)

.reduce(0, (a, b) -> a + b);

System.out.println(sum);

}

}

a)

20

b)

13

c)

30

d)

0

16.

Consider the in-order traversal of a binary tree: 10 20 30 40 50 60.

Which of the following could be the corresponding pre-order traversal?

a)

40 20 10 30 60 50

b)

30 20 10 40 50 60

c)

60 50 40 30 20 10

d)

10 20 30 40 50 60

17.

50

/ \

30 70

/ \ / \

20 40 60 80

What is the result of in-order traversal of this BST?

a)

50 30 20 40 70 60 80

b)

20 30 40 50 60 70 80

c)

80 70 60 50 40 30 20

d)

20 40 30 60 80 70 50

18.

Which of the following are true for a Binary Search Tree (BST)?


a)

All left descendants of a node are less than the node

b)

All right descendants of a node are greater than the node

c)

In-order traversal of BST gives sorted sequence

d)

The root must be the smallest element in the tree

19.

Which of the following are true about BST deletion?

a)

Deleting a leaf node is the easiest case

b)

Deleting a node with two children requires finding its in-order successor or predecessor

c)

Deletion always decreases tree height

d)

After deletion, BST property must still be preserved

20.

Which of the following sequences can not be the in-order traversal of a BST?

a)

10, 20, 30, 40, 50

b)

50, 40, 30, 20, 10

c)

20, 10, 30, 50, 40

d)

15, 25, 35, 45, 55