A thread in waiting state is waiting for another thread to perform a particular action, such as calling notify() or notifyAll() on a shared object, or terminating a joined thread. A thread in waiting state can be interrupted by another thread, which will cause the waiting thread to throw an InterruptedException and return to the runnable state. Therefore, a thread in waiting state must handle InterruptedException, either by catching it or declaring it in the throws clause. References: Thread.State (Java SE 17 & JDK 17), [Thread (Java SE 17 & JDK 17)]
The code fragment is using the Java SE 17 API to get the current time and then truncating it to minutes. The result will be the current time truncated to minutes, which is why option B is correct. References:
The code fragment is creating a string variable “a” with the value “Hello! Java”. Then, it is printing the index of “Java” in “a”. Next, it is replacing “Hello!” with “Welcome!” in “a”. Then, it is printing the index of “Java” in “a”. Finally, it is creating a new StringBuilder object “b” with the value of “a” and printing the index of “Java” in “b”. The output will be 8109 because the index of “Java” in “a” is 8, the index of “Java” in “a” after replacing “Hello!” with “Welcome!” is 10, and the index of “Java” in “b” is 9. References: Oracle Java SE 17 Developer source and documents: [String (Java SE 17 & JDK 17)], [StringBuilder (Java SE 17 & JDK 17)]
Questions # 14:
Which statement is true?
Options:
A.
The tryLock () method returns a boolean indicator immediately regardless if it has or has not managed to acquire the lock.
B.
The tryLock () method returns a boolean indicator immediately if it has managed to acquire the lock, otherwise it waits for the lock acquisition.
C.
The lock () method returns a boolean indicator immediately if it has managed to acquire the lock, otherwise it waits for the lock acquisition.
D.
The Lock () method returns a boolean indicator immediately regardless if it has or has not managed to acquire the lock
The tryLock () method of the Lock interface is a non-blocking attempt to acquire a lock. It returns true if the lock is available and acquired by the current thread, and false otherwise. It does not wait for the lock to be released by another thread. This is different from the lock () method, which blocks the current thread until the lock is acquired, and does not return any value. References: https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/locks/Lock.html#tryLock(), 3, 4, 5
The code fragment is using the switch statement with the new Java 17 syntax. The switch statement checks the value of the variable rank and executes the corresponding case statement. In this case, the value of rank is 4, so the first case statement is executed, printing “Range1”. The second and third case statements are also executed, printing “Range2” and “Range3”. The default case statement is also executed, printing “Not a valid rank”. References: Java Language Changes - Oracle Help Center