Part 14
Core java interview questions and answers for 3-5 years experience covered in this post:
What are the primitive functions?
-
Primitives cannot use generic type argument, Java 8 has
provided some primitives functions for performance improvement.
-
There are 15 primitive functions.
1.
IntFunction<R> :
§
If your input is always int primitive then we should use this
function.
§ Method:
R apply(int
value);
2.
LongFunction<R>:
§
If your input is always long primitive then we should use
this function.
§ Method:
R apply(long
value);
3.
DoubleFunction<R>:
§
If your input is always double primitive then we should use
this function.
§ Method:
R apply(double
value);
4.
ToIntFunction<T>:
§
Input type can be anything but return type is always
int.
§ Method:
int
applyAsInt(T value);
5.
ToLongFunction<T>:
§
Input type can be anything but return type is always
long.
§ Method:
long
applyAsLong(T value);
6.
ToDoubleFunction<T>:
§
Input type can be anything but return type is always
double.
§ Method:
double
applyAsDouble(T value);
7.
IntToLongFunction<T>:
§
Input type must Int and output type must long then we should
use this function.
§ Method:
long
applyAsLong(int
value);
8.
IntToDoubleFunction<T>:
§
Input type must Int and output type must double then we
should use this function.
§ Method:
double
applyAsDouble(int
value);
9.
longToIntFunction<T>:
§
Input type must long and output type must int then we should
use this function.
§ Method:
int
applyAsInt(long
value);
10.
longToDoubleFunction<T>:
§
Input type must long and output type must double then we
should use this function.
§ Method:
double
applyAsDouble(long
value);
11.
doubleToIntFunction<T>:
§
Input type must double and output type must int then we
should use this function.
§ Method:
int
applyAsInt(double
value);
12.
doubleToLongFunction<T>:
§
Input type must double and output type must long then we
should use this function.
§ Method:
long
applyAsLong(double
value);
13.
ToIntBiFunction<T,U>:
§ Method:
int
applyAsInt(T t, U u);
14.
ToLongBiFunction<T,U>:
§ Method:
long
applyAsLong(T t, U u);
15. ToIntBiFunction<T,U>:
§ Method: int applyAsInt(T t, U u);
What is the difference between
IntFunction, ToIntFunction and IntToDoubleFunction?
|
IntFunction |
ToIntFunction |
IntToDoubleFunction |
1. |
If your input is always int primitive then we should use this
function. |
Input type can be anything but return type is always int. |
Input type must be Int and output type must double then we should
use this function. |
2. |
Method: R apply(int
value); |
Method: int
applyAsInt(T value); |
Method: double
applyAsDouble(int value); |
What is method Reference? How will you
implements method reference?
- Method reference can be used to use the existing method with
:: symbol.
- Please refer to the below example.
package simplifiedjava.crackedInterview.java8;
@FunctionalInterface
public interface
MyInterface {
public void
display(); } package simplifiedjava.crackedInterview.java8; public class MyClass{
public static void
main(String[] args) {
MyClass myClass = new
MyClass();
MyInterface myInterface = myClass::myMethod;
myInterface.display(); }
public void
myMethod() {
System.out.println("My Method Displayed.");
}
} Output: My Method Displayed. |
Write a program to call static methods
with method reference?
package simplifiedjava.crackedInterview.java8; public class Addition {
public static int
add(int num1, int num2) {
return num1 + num2;
} } package simplifiedjava.crackedInterview.java8; import java.util.function.BiFunction; public class AdditionDemo {
public static void
main(String[] args) {
BiFunction<Integer, Integer, Integer> bf
= Addition::add;
int result = bf.apply(100, 200);
System.out.println("Result "+ result);
}
}
Output: Result 300 |
Can you call static method with method
reference?
- Surely, we can call the static method with method
reference.
How does it different from Collection
API?
- Collection can be used for storing and manipulating a group
of data.
- Stream API is only used for processing groups of data.
What are the intermidiate operations?
Can you list out some intermediate operations?
- The operations which return another stream, as a result, are called
intermediate operations.
- Following are the intermediates operations.
|
Operation |
Processing way |
1. |
filter() |
Returns another stream of elements which satisfy given condition
inside filter. |
2. |
map() |
Returns a stream consisting of results after applying given
function to elements of the stream. |
3. |
sorted() |
Returns a stream consisting of elements sorted according to natural
sorting order. |
4. |
distinct() |
Returns a stream of unique elements. |
5. |
skip() |
Returns a stream after skipping first n elemetns. |
6. |
limit() |
Returns a stream containing first n elements. |
What are the terminal operations? Can
you list out some terminal operations?
- The operations which return non-stream values like null or collection
object or objects or primitive is called terminal operations.
- Following are the intermediates operations.
|
Operation |
Processing way |
1. |
collect() |
Returns processed collection object like list, set or map.
|
2. |
forEach() |
Perform an action on all elements of stream. |
3. |
min() |
Returns lowest element in the stream wrapped in optional
object. |
4. |
max() |
Returns highest element in the stream wrapped in optional
object. |
5. |
count() |
Returns the number of elements in a stream. |
6. |
findFirst() |
Returns the first element of a stream wrapped in Optional
object. |
7. |
findAny() |
Returns anyone element randomly from the stream. |
8. |
reduce() |
Performs reduction operation on elements of the stream using
initial value and binary operation. |
9. |
toArray() |
Returns an array containing elements of a stream. |
10. |
anyMatch() |
Returns true if anyone element of a stream matches with given
predicate. |
11. |
allMatch() |
Returns true if all elements of a stream matches with given
predicate. |
12. |
noneMatch() |
Returns true only if all the elements of a stream doesn’t match
with given predicate. |
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on some support
projects and then I moved to java projects. I had worked with many
multi-national companies like R-Systems, Tata Consultancy
Services, Cybage Softwares. Fortunately, TCS and Cybage
has given me an opportunity to take interviews for experienced
candidates. I have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments