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


Download 1.52 Mb.
Pdf ko'rish
bet66/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   62   63   64   65   66   67   68   69   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

174.
175.
Executing this code first prints “Thread 1” 1000 times and
then prints the “Thread 2” 1000 times. If this is incorrect,
explain the reasons.
Answer:
On executing the above code, it will not print Thread 1 1000
times and then Thread 2 1000 times. The order in which the
threads execute is up to the Thread scheduler. So though both
threads print their names 1000 times, the order in which they
print it is not known.
Write a code sample that makes a thread pause for ten
minutes.
Answer:
In order to make a thread pause for ten minutes, you need to
invoke the sleep() method within the run() method. The sleep()
method accepts as parameter a long value which accepts the
time that the thread should sleep in milliseconds. The following
code demonstrates this:
try {
Thread.sleep(10 * 60 * 1000)
}
catch(InterruptedException exp) {
}
What happens when a synchronized method is invoked?
Answer:
Whenever a synchronized method is executed, the thread that
invokes the method acquires a lock and holds the lock under the
end of the method. Only after the current thread completes


executing the method it releases the lock. So other threads can
then acquire the lock and execute the method. Thus, the
synchronized keyword prevents multiple threads from
simultaneously executing the code within the synchronized
method.



176.
177.
CHAPTER
17
Java IO API
What happens when the following code is executed?
File myFile = new File(“CoreJava.txt”);
Answer:
When the code above is executed, the JVM creates a Java object
called myFile in the memory but the actual file will not be
created on the file system. In order to actually create the file, the
myFile.createNewFile() method needs to be invoked.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   ...   171




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