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


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

273.
CHAPTER
03
Miscellaneous
What changes are made by Java 9 to interfaces and why?
Answer:
Java 9 allows private methods in interfaces. Before Java 8,
interfaces could only have abstract methods. Java 8 added
support for static and default interface methods. Default and
static methods are nothing but interfaces with method bodies.
Java 9 goes a step further and allows private methods in
interfaces. Private interface methods allow reusing code. So, if an
interface has several default and static methods and there is
some common code across these methods, this code can be
moved to a private method. The following code demonstrates
this:
public interface Sample {
default method1(){
//some code
doSomething();
}
default method2(){
//some code
doSomething();
}
private void doSomething(){
System.out.println(“Doing something..”);
}


274.
275.
}
Here, the doSomething() is a private interface method that is
used within the default methods method1() and method2().
What is JShell?
Answer:
Jshell is basically a command line tool. It allows you to write and
run Java code without creating a class file. It can be launched by
typing jshell on the command prompt. Once Jshell opens up,
you can type in any Java code and JShell will display the
output of the code. So, for example if you type the following:
System.out.println(“Hello World”);
JShell will print the text “Hello World”.
JShell adds REPL support to Java. REPL stands for Read
Evaluate Print Loop Many languages like Python also provide
REPL capabilities.
What is the output of the following code snippet?
Optional myDoubleOptional = Optional.of(10.0);
Optional defaultOptional = Optional.of(50.0);
double value = myDoubleOptional.or(()–>
defaultOptional).get();
System.out.println(value);
Answer:
Java 9 has added a new method on the Optional class called
or(). This accepts as parameter a Supplier instance that
produces an Optional. It returns an Optional. So, if the Optional



Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   103   104   105   106   107   108   109   110   ...   171




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