Hi friends,
In this post, I am sharing core java interview questions and answers.
Question 1:
What is polymorphism and what are it's types?
Answer:
It is one of the OOPS feature that allows us to perform a single action in different ways.
Polymorphism is the capability of a method to do different things based on the object that it is acting upon.
Types of polymorphism are:
- Runtime polymorphism or Dynamic polymorphism
- Compile time polymorphism or Static polymorphism
Question 2:
What are early binding and late binding?
Answer:
Static binding or early binding:
The binding which can be resolved at compile time by the compiler is known as static or early binding.
The binding of static , private and final methods is compile time.Because these methods cannot be overridden and the type of the class is determined at compile time.
Dynamic binding or late binding:
When compiler is not able to resolve call/binding at compile time, such binding is known as dynamic binding or late binding.
Question 3:
What is the use of super keyword?
Answer:
The super keyword refers to the objects of immediate parent class.
Use of super keyword:
- To access the data members of parent class when both parent and child class have members with same name.
- To explicitly call the no-arg and parameterized constructor of parent class.
- To access the methods of parent class when child class has overridden that method.
Question 4:
What are the static and non-static methods in Thread class?
Answer:
Static methods in thread class are:
- yield()
- activeCount()
- currentThread()
- sleep()
Non-static methods in thread class are:
- start()
- run()
- join()
- getName()
- setName()
- getPriority()
- setPriority()
- interrupt()
- getState()
Question 5:
What is thread scheduler and Time slicing?
Answer:
Thread scheduler is OS service that allocates CPU time to the available running threads.
Time slicing is the process to divide the available CPU time to the available running threads.
Question 6:
How can we make sure that main() is the last thread to finish in java program?
Answer:
We can call join()[non-static method] method on threads to make sure all threads created by java program are dead before finishing the main() function.
Overloads of join allow a programmer to specify a waiting period. However, as with sleep(), join() is dependent on OS for timing, so we should not assume that join() will wait exactly as long as you specify.
Like sleep(), join() responds to an interrupt by exiting with an InterruptedException.
join() is a final method in java and we cannot override it.
Question 7:
What Thread class's sleep() and yield() methods are static?
Answer:
These methods work on currently executing thread. These are made static to avoid confusion to the programmer who might think , they can invoke these methods on some non-running threads.
Question 8:
How do we achieve thread safety?
Answer:
There are multiple mechanisms to achieve thread safety:
- Synchronization
- Atomic concurrent classes
- Concurrent locks
- volatile keyword
- Immutable classes
- Thread safe classes
Question 9:
What is ThreadLocal?
Answer:
Java ThreadLocal is used to create thread-local variables. We know that all threads of an Object share it's variables, so if the variable is not thread safe, we can use synchronization, but if we want to avoid synchronization, we can use ThreadLocal variables.
Every thread has it's own ThreadLocal variable and they can use it's get() and set() methods to get the default value or change it's value local to thread. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread.
Question 10:
What are the changes in java 10?
Answer:
There are multiple new changes that have been added in java 10.
- var keyword
- Unmodifiable collection enhancements
- G1GC performance improvements
- New JIT compiler [created in pure java]
- Alternate memory devices for allocating heap
- Application class data sharing
Question 11:
What is pagination?
Answer:
Pagination in web application is a mechanism to separate a big result set into smaller chunks. Providing a fluent pagination navigator could increase both the value of the website for search engines and enhance user experience through minimizing the response time.
The famous example of pagination is google's search result page. Normally,the result page is separated into several pages.
The spring framework provides an out of the box feature for pagination that needs the number of pages and number of elements per page. This is very useful when we would implement static pagination which can offer a user to choose between different pages.
That's all for this post.
Thanks for reading!!
No comments:
Post a Comment