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"); } } thread sleep Compilation fails nothing is printed InterruptedException None 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"); } } 01startedcompleted startedcompleted Compilation fails started01completed None 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(); } } Exception occured StartEnd Compilation fails InterruptedException None 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(); } } 0,0,2,4,2,4, 0,2,4,6,8,10, 0,0,2,2,4,4 Compilation fails None 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(); } } } java,java,spring,spring,java,spring, Compilation fails java,java,java,spring,spring,spring, java,spring,java,spring,java,spring, None Time's up Java Thread Program Practice Test-3 Read More »