Core java interview Questions You'll Most Likely Be Asked (Second Edition)


What happens behind the scenes when the following code


Download 1.52 Mb.
Pdf ko'rish
bet63/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   59   60   61   62   63   64   65   66   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What happens behind the scenes when the following code
is executed?
class Test {
public static void main(String argument[]) {
System.out.println(“Thread Example...”);
}


166.
a.
b.
c.
d.
e.
f.
g.
167.
a.
b.
}
Answer:
When the above code is executed, the JVM creates a thread to
run this code. This thread executes the code in the main method
after which the program terminates.
Explain some of the main methods in the Thread class.
Answer:
start(): Initiates the thread and invoke the run() method
run(): Code that needs to be executed in a separate thread
needs to be placed in the run method
sleep(): makes the running thread pause for the specified
time
setName(): sets the name of the thread to the value specified
join(): waits for the thread to die for at most the specified
time
isAlive(): Returns a boolean value that indicates if the
current thread is alive or not
setPriority(): Changes the priority of the thread to the
specified value
Explain thread states.
Answer:
Thread exists in five states. They are as follows:
New: A thread is in new state when a new Thread is created
but the start method is not invoked.
Runnable: A Thread is in the Runnable state after the start()
method is invoked but the Thread scheduler has not yet


c.
d.
e.
168.
169.
started executing the thread.
Running: When the thread scheduler starts executing the
thread, it is in the Running state. The code within the body of
the run() method gets executed.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   59   60   61   62   63   64   65   66   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling