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


Explain the ProcessHandle class and some of its


Download 1.52 Mb.
Pdf ko'rish
bet109/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   105   106   107   108   109   110   111   112   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain the ProcessHandle class and some of its
important methods.
Answer:
Java 9 has added a new class called
java.lang.ProcessHandle. This helps to manage processes
related to the operating system and helps to interact with the
operating system. Some of the methods on this class are as
follows:
current(): This is a static method that returns a
ProcessHandle object corresponding to the current
process.
isAlive(): This returns a boolean value that indicates
whether the process is alive or not.
pid(): This returns the id of the process.
children(): This returns a Stream of ProcessHandle
objects corresponding to the children of the current process.
In the code snippet below, explain what code should be
used at line 1 to print the output in the expected output
section below:
Optional price = ; //Line 1


280.
price.ifPresentOrElse(val–> System.out.println(“Price is
not null”), ()–> System.out.println(“Price not
specified”));
Expected output:
Price not specified
Answer:
The code above uses the ifPresentOrElse() method added
by Java 9 on the Optional class. This method accepts as
parameter a Consumer instance and a Runnable instance. If a
value is present in the Optional, it applies the Consumer on the
value in the Optional. If a value is not present in the Optional, it
executes the code specified on the Optional. In this case, we
want the Price Not Specified output to be printed. The Sysout
statement corresponding to this is specified in the Runnable.
So, the Optional price must be an empty Optional in order for this
code to be executed. So, Line 1 needs to be written as follows:
Optional price = Optional.empty();

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   105   106   107   108   109   110   111   112   ...   171




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