Font size
WorksheetsFinal Quiz
Total questions: 20
Worksheet time: 10mins
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");
}
}
Only "Child Thread"
Only "Main Thread"
"Main Thread" followed by "Child Thread"
Either "Child Thread" or "Main Thread" may come first
What will be the output if run() is called instead of start()?
MyThread t = new MyThread();
t.run();
System.out.println("Main Thread");
"Child Thread" and "Main Thread" printed from different threads
"Child Thread" and "Main Thread" printed from the same thread
Only "Main Thread"
Compilation error
What is the state of a thread once the start() method is called but before run() is executed?
New
Runnable
Running
Blocked
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?
1
2
3
4
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();
}
}
Both threads run together
Only one thread runs
Threads run one after another due to synchronization
Compilation error
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?
Both can execute simultaneously
Only one can execute at a time
They may deadlock
It causes a compilation error
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()?
Will cause an exception
B. Always less than 2000
Could be 2000 or less
Always 2000
Which statement is true about the difference between synchronized method and synchronized(this) block?
synchronized method is faster
Both lock different monitors
Both lock the same object monitor (this)
synchronized(this) cannot be used in constructors
What does a static synchronized method lock on?
The instance of the class
The .class object (i.e., class-level monitor)
The thread that calls it
Nothing, it behaves like a non-synchronized method
class Example {
public static synchronized void staticMethod() {
System.out.println("Static method");
}
public synchronized void instanceMethod() {
System.out.println("Instance method");
}
}
Yes
No
Only if both methods are synchronized
Only on different objects
Which of the following is a valid lambda expression in Java?
A. (int x, int y) => x + y
B. x, y -> x + y
C. (x, y) -> x + y
D. (x, y) => { return x + y; }
What is a functional interface?
An interface with only static methods
An abstract class with a lambda method
An interface with default and static methods only
An interface with exactly one abstract method
Which built-in functional interface represents a function that takes one argument and returns a result?
Function<T, R>
Predicate<T>
Supplier<T>
Consumer<T>
Predicate<String> startsWithJ = s -> s.startsWith("J");
System.out.println(startsWithJ.test("Java"));
true
false
Compilation error
J
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);
}
}
20
13
30
0
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?
40 20 10 30 60 50
30 20 10 40 50 60
60 50 40 30 20 10
10 20 30 40 50 60
50
/ \
30 70
/ \ / \
20 40 60 80
What is the result of in-order traversal of this BST?
50 30 20 40 70 60 80
20 30 40 50 60 70 80
80 70 60 50 40 30 20
20 40 30 60 80 70 50
Which of the following are true for a Binary Search Tree (BST)?
All left descendants of a node are less than the node
All right descendants of a node are greater than the node
In-order traversal of BST gives sorted sequence
The root must be the smallest element in the tree
Which of the following are true about BST deletion?
Deleting a leaf node is the easiest case
Deleting a node with two children requires finding its in-order successor or predecessor
Deletion always decreases tree height
After deletion, BST property must still be preserved
Which of the following sequences can not be the in-order traversal of a BST?
10, 20, 30, 40, 50
50, 40, 30, 20, 10
20, 10, 30, 50, 40
15, 25, 35, 45, 55
