Java Thread Program Practice Test-3

What will be the output of the following java code?

public class Test {

public static void main (String[] args) throws Exception {

Thread.sleep(1000);

System.out.println("thread sleep");

}

}

What will be the output of the following java code?

public class Test implements Runnable {

public void run() {

for (int i = 0; i < 2; i++) {

System.out.print(i);

}

}

public static void main(String[] args) throws Exception {

Thread t = new Thread(new Test());

t.start();

System.out.print("started");

t.join();

System.out.print("completed");

}

}

What will be the output of the following java code?

public class Test extends Thread {

public void run() {

System.out.print("Start");

delay(1000);

System.out.print("End");

}

private static void delay(long n) {

try {

Thread.sleep(n);

} catch (Exception e) {

System.out.print("Error");

}

}

public static void main(String[] args) {

Thread t = new Thread(new Test());

t.start();

}

}

What will be the output of the following java code?

public class Test implements Runnable {

int a = 0;

public void run() {

int digit = 0;

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

digit = a;

System.out.print(digit + ",");

a = digit + 2;

}

}

public void hello() {

Test tst = new Test();

new Thread(tst).start();

new Thread(tst).start();

}

public static void main(String[] args) {

new Test().hello();

}

}

What will be the output of the following java code?

import java.util.ArrayList;

import java.util.List;

public class Test {

private List list = new ArrayList();

public synchronized void add(String name) {

list.add(name);

}

public synchronized void printData() {

for (int i = 0; i < list.size(); i++) {

System.out.print(list.get(i) + ",");

}

}

public static void main(String[] args) {

final Test tst = new Test();

for (int i = 0; i < 2; i++) {

new Thread() {

public void run() {

tst.add("java");

tst.add("spring");

tst.printData();

}

}.start();

}

}

}