1. Analyze the following code:
public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } }
1. The code compiles but will not print anything since t does not invoke the run method.
2. The code will not compile since you cannot invoke "this" in a static method.
3. The program compiles, runs, and prints tests on the console.
2. What will the following example print to the console?
// Test.java: Define threads using the Thread class public class Test { /** Main method */ public static void main(String[] args) { new Test(); } public Test() { // Create threads PrintChar printA = new PrintChar('a', 4); PrintChar printB = new PrintChar('b', 4); // Start threads printA.run(); printB.run(); } class PrintChar implements Runnable { private char charToPrint; // The character to print private int times; // The times to repeat // Construct a thread with specified character and number of times to print the character public PrintChar(char c, int t) { charToPrint = c; times = t; } // Override the run() method to tell the system what the thread will do public void run() { for (int i = 0; i < times; i++) System.out.print(charToPrint); } } }
1. ababababab
2. a and b will be printed in random order.
3. aaaaabbbbb
3. Analyze the following code:
public class Test implements Runnable { public static void main(String[] args) { Test t = new Test(); t.start(); } public void run() {} }
1. The program compiles and runs fine.
2. The program compiles, but will not run since run method is not properly implemented.
3. The program will not compile since the Test does not implement the start method.
4. Which of the following must be true for the following code to compile
using Thread = new Thread(object)
1. object instanceof Runnable
2. object instanceof Frame
3. object instanceof Applet
5. Which of the following methods in the Thread class is deprecated?
1. yield()
2. suspend()
3. stop()
6. You can use ... to temporarily release processor time for other threads.
1. stop()
2. suspend()
3. yield()
7. Which of the following methods are defined in the Object class?
1. sleep(long)
2. yield()
3. join()
4. notify()
8.
When you run the following program what will happen?
public class Test extends Thread { public static void main(String[] args) { Test t = new Test(); t.start(); t.start(); } public void run() { int a = 0; for(int i = 0; i < 10E10; i++) a += a + i; } }
1. java.lang.IllegalThreadStateException may occur since you started a thread even if the thread did not finish work before calling it again.
2. The program prints tests once.
3. Nothing written for console.
9. Which of the following methods in Thread can throw InterruptedException?
1. sleep(long)
2. run()
3. start()
10. Given the following code, which code snippet can be used to display time every second?
import java.util.*; public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { for(;;) { // CODE GOES HERE System.out.printf("%tT %n", new Date()); } } }
1. try { t.sleep(1000); } catch(InterruptedException e) { }
2. try { Thread.sleep(1000); } catch(InterruptedException e) { }
11. Which of the following statements is true?
1. You can use hours or thread to control animation.
2. Event action in the GUI is performed by an "event dispatcher" thread
3. If a timer and event action are running on the same thread then the time between running ActionEvent may be greater than a defined value in Timer.
1) 1. The code compiles but will not print
anything since t does not invoke the run method.
2)3. aaaaabbbbb
3) 3. The program will not compile since the Test
does not implement the start method.
4) using Thread = new Thread(object)
1. object instanceof Runnable
5)3. stop()
6)3. yield()
7)4. notify()
8)
1. java.lang.IllegalThreadStateException may occur since
you started a thread even if the thread did not finish work before
calling it again.
9) 1. sleep(long)
10) 2. try { Thread.sleep(1000); } catch(InterruptedException e) { }
}
11)
2. Event action in the GUI is performed by an
"event dispatcher" thread
Thanks, PLEASE COMMENT if there is any concern.
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
1. public class Threads5 { 2. public static void main (String[l args) 3. new Thread(new Runnable() 4. public void run() 5. System.out.print("bar"); 6. start(; 7 What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes normally and prints "bar". D. The code executes normally, but nothing prints
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.
Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } The variable t is private and therefore cannot be accessed in the main method. The program compiles and runs fine. t is non-static and it cannot be referenced in a static context in the main method. The variablet is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.
class Test public static void main(String args) { Aa=new AO: a.printo: class A private String s; public A (String news) { 8 = news: public void print { System.out.println(s): The program would compile and run if you change A a new Alto Aa=new A('5'). The program has a compilation error because the instance variables in class A is not public. The program has a compilation error because class A does not have a no-arguments constructor The program compiles and runs...
What is wrong the following code? Explain. public class Test { public static void main(String[] args) { A a = new A(5.0); } class A { int value = 2; } }
c) public class Test { public static void main(String[] args) { T t1 = new T(); T t2 = new T(); System.out.println("t1's i = " + t1.i + " and j = " + t1.j); System.out.println("t2's i = " + t2.i + " and j = " + t2.j); } } class T { static int i = 0; int j = 0; T() { i++; j = 1; } } Why is t1's i = 2 ? It should be...
1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...
What is the output of the following code? public class Test { pub be static void main(String() args) { Object o 1 -new Object(); Object o2 = new Object(); System out.print((o1 = o2) + "" + (o1 equals(o2))); } } A. false false B. true true C. false true D. true false Analyze the following code. //Program 1: public class Test { public static void main(String[] args) { Object circle 1 = new Circle(); Object circle2 = new Circle(); System...
Question 28 public class Main public static void main(String args[]) { Iparentp=new Child (1.2); p.printo: interface Iparent public int x = 1; public void printo: class Child implements Iparent private int y public Child(int a, int b) y b: public void printot System.out.print("Child"-yl: Child 2 public int x = 1; public void print(); class Child implements Iparent! private int y public Child(int a, in lb) y=b; public vold print! System.out.pnt("Child"+y): Child 2 1 None of the above • Previous