Hi Friends, In this post, I'm writing Java and Android interview questions for freshers:
Question 1:
What is Polymorphism and many types of polymorphism are there?
Answer:
It is one of the OOPS concepts that allows us to perform 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:
- Runtime Polymorphism or Dynamic Polymorphism [Method Overloading]
- Compile time polymorphism or static polymorphism [Method Overloading]
Question 2:
What is static binding and dynamic binding?
Answer:
Static binding or Early binding:
The binding which can be resolved at compile time by compiler is known as static or early binding. The binding of static , private and final methods is compile-time. Because these methods can't be overridden and the type of the class is determined at the 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.
Method overriding is the perfect example of dynamic binding.
Question 3:
What are the use of super keyword?
Answer:
The super keyword refers to the object of immediate parent class.
Use of super keyword:
1). To access the data members of parent class when both the child and parent class have member with same name.
2). To explicitly call no-arg and parameterized constructor of parent class.
3). To access the method of parent class when child class has overridden that method.
Question 4:
What is the difference between implements Runnable and extends Thread?
Answer:
There are few differences which are described as below:
- Inheritance Option
- Loosely-coupled
- Functions overhead
- Thread represents "how a thread of control runs". And Runnable represents "what a Thread runs".
Inheritance Option: Means, If class implements Runnable interface, it can extends one class as well. But if class extends Thread, then it cannot extend any other class.
Loosely-coupled: Runnable object can be used in multiple threads. And a Runnable can be changed independently.
Functions Overhead: If we extends Thread class just to run a task, then there will be overhead of all Thread functions.
Question 5:
What is difference between user thread and daemon thread?
Answer:
When we create a thread in java application, it is called user thread.
Daemon thread is a thread that acts as a service provider for other threads running in the same process. e.g. GC.
Daemon thread is terminated by JVM, when there is no other thread running.
Question 6:
Why will we take Daemon thread in our application?
Answer:
If we take a user thread and if it is polling some websites continuously, then if we exit the app, then user thread keeps running and it will not let JVM shutdown.
But, if we make it a daemon thread , then this thread will be terminated by JVM, when there is no other thread running.
Question 7:
What do you understand about thread priority?
Answer:
Every thread has a priority. Usually higher priority thread gets precedence in execution , but it depends upon Thread Scheduler implementation that is OS dependent.
We can specify thread priority using setPriority() method. We can use getPriority() method to get priority.
Thread priority is an int whose value varies from 1 to 10 where 1 is the lowest priority thread and 10 is the highest priority thread.
There are 3 variables defined in Thread class for priority:
- MIN_PRIORITY
- NORM_PRIORITY : This is the default priority. It has value 5. Default priority of a thread depends upon priority of parent thread.
- MAX_PRIORITY
Question 8:
What is thread scheduler and time slicing?
Answer:
Thread scheduler is OS service that allocates CPU time to the available threads.
Time slicing is the process of divide the available CPU time to the available runnable threads.
That's all for this post.
Thanks for reading!!
No comments:
Post a Comment