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 a thread. |
|
2: What will happen when you started the thread? |
A: “Thread is running” will be printed out. |
B: Will have an compiler error, you cannot access a static method in the thread. |
|
C: Exception will be thrown. |
|
D: Thread will be started but the “Thread is running” will not be printed since the run method is not called. |
|
3: How many and what threads is/are running when you successfully run the above code? |
A: 2, the main thread and the TestThread. |
B: 1, only the main thread |
|
C: 1, only the TestThread |
|
D: No thread is running |
|
4: What method is internally called by when we call the method start()? |
A: run() |
B: printMyName() |
|
C: main() |
|
D: None |
|
5: Now try to use the method getName() and print it out. What does the console say? |
A: Thread-0 |
B: The name of the class - TestThread |
|
C: None since we did not define a name for the thread. |
|
D: Syntax error, not such method exists. |
public class TestThread implements Runnable{
public static void main(String[] args){
TestThread t = new TestThread();
}
public void run() {
System.out.println("Running");
}
}
6: How will you be able to run this thread? |
A: by calling the run() method. |
B: By calling the start() method. |
|
C: This will not run since this is a runnable. You need to extend the Thread class. |
|
D: This will not run since there is no @override annotation in the run method. |
|
7: What will happen when you called the t.start() twice? |
A: IllegalStateThreadException will occur. |
B: The “Running” text will be printed twice |
|
C: Nothing. |
|
D: MulitThreadingException will occur |
|
8: What will happen when you called t.run() and t.start()? |
A: The “Running” text will be printed twice |
B: IllegalStateThreadException will occur. |
|
C: Nothing. |
|
D: MulitThreadingException will occur |
public class TestExample extends Thread{
public static void main(String argv[]){
TestExample t = new TestExample();
t.run();
}
public void start(){
for(int i = 0; i < 5; i++){
System.out.println("Count : " + i);
}
}
}
9: What happens when you compile? |
A: Nothing but it compiled. |
B: Syntax error. run() method is not defined. |
|
C: Compiler error. |
|
D: A run time exception that says no run method is defined in this class. |
public class TestExample implements Runnable{
public static void main(String[] args) throws InterruptedException{
Thread a = new Thread(new TestExample());
a.start();
System.out.println("Starting...");
a.join();
System.out.println("Ending...");
}
public void run(){
System.out.println("Running...");
}
}
10: What happens when you compile? |
A: Starting… Running… Ending… is printed |
B: Starting… Ending… Running… is printed |
|
C: Starting… Ending… is printed |
|
D: Runtime exception occurred. |
1)
start() method must be invoked to start the thread TestThread
public class TestThread extends Thread {
public TestThread()
{
start();
}
public static void main(String[] args) {
TestThread thread = new TestThread();
}
@Override
public void run() {
printMyName();
}
private void printMyName() {
System.out.println("Thread is running");
}
}
2)
a) Thread is running will be printed on the screen
3)
When thread is started 2 threads will be running i..e, Main thread & TestThread
4)
run() is internally called when the start() method is called
5)
Thread-0 will be printed on the screen
public class TestThread extends Thread {
public TestThread()
{
start();
}
public static void main(String[] args) {
TestThread thread = new TestThread();
}
@Override
public void run() {
printMyName();
}
private void printMyName() {
System.out.println(getName()+"Thread is running");
}
}
Output
Thread-0Thread is running
6)
b is correct option it can be accomplished with the below example
public class TestThread implements Runnable{
public TestThread()
{
new Thread(this).start();
}
public static void main(String[] args){
TestThread t = new TestThread();
}
public void run() {
System.out.println("Running");
}
}
Output
Running
7)
When starting thread twice IllegalThreadStateException will be thrown
8)
a is correct answer
9)
a is correct answer since it doesnot raise any compile time error
10)
a is correct answer since join() method waits for the current thread to finish its execution
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args)...
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...
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
public class SquareTest { public static void main(String[] args) { System.out.println("Number of sides is " + Square.NUM_OF_SIDES); Square s1 = new Square(-5.7f); System.out.println(s1); s1.setLength(0.001f); System.out.println(s1.getLength()); s1.setLength(-9999); System.out.println(s1.getLength()); System.out.println("Number of sides is " + s1.NUM_OF_SIDES); s1.calculateArea(); } }...
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...
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.
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.
import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 { public static void main(String[] args) { String name; String answer; int correct = 0; int incorrect = 0; Scanner phantom = new Scanner(System.in); System.out.println("Hello, What is your name?"); name = phantom.nextLine(); System.out.println("Welcome " + name + "!\n"); System.out.println("My name is Danielle Brandt. " +"This is a quiz program that...
What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i; System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); } }
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...