Java Interview Questions

1. What’s the difference between Abstract vs. Interface?

ANSWER: An abstract class can’t be instantiated, but it can be subclassed. An abstract class usually contains abstract and non-abstract methods that subclasses are forced to provide an implementation for.
An interface is a completely “abstract class” that is used to group related methods with empty bodies.
You can use an interface to define a contract/template for the objects to follow. So, any class that implements the interface, they have to use all the methods in the interface.

Layman’s terms: An abstract class is like chill art class teacher while an interface is like a strict science teacher.

2. Whats the difference between a class method and an instance method?

ANSWER: Class methods are declared as static hence they can be called without creating an instance of a class. Class methods can only operate on class members and not on instance variables since they are unaware of the instance members.
In order to use an instance method, you must create a instance of a class using the new keyword. Also instance methods are not declared as static.
Example: <class_name> <object_name> = new <constructor>;

Layman’s terms: A class method is like talking to your family/cousins while an instance method is like talking to like a professor where you have to declare them as “prof” or “Mr.” or “Mrs.”.

3. When do you use abstract class?

ANSWER: You can use an abstract class when you want to make a partial define class. What it means is that you have provide implementation details for any class that extends the abstract class. Hence, the children classes inherit the abstract method(s).

Layman’s terms: You only inherit a few traits from your mom and dad hence your mom and dad are abstract classes.

4. Why do people use nested classes?

ANSWER:
– Child – Parent connection is simpler.
– Increases encapsulation.
– Increases readability and maintainability.

Layman’s terms: Wouldn’t it be easier to live in the same house as your parent enclosed in their protection.

5. What is meant by Java being platform independent.

ANSWER: Java is platform independent as it can be used on any system regardless of having different operating systems and file systems. The way it does this by using the Java virtual machine (“JVM”). Look at the flow chart below for how it does it.

As you can see, the java code you write is first compiled. Then it is transformed into the Byte Code which is a .class file. That is the file that is created in the same directory when you compile the program.

Layman’s terms: Java code is like a person that knows every language and can communicate with everyone in the world.

6. Can a Semaphore act as a Mutex?

ANSWER: Lets define both these terms.
Semaphore: A semaphore control access to a shared resource using a counter. The counter measures the number of permits that are allowed. If a counter is zero, then access to the resource is denied. If it is greater than zero, then access to the resource allowed. Everytime a thread acquires a resoruce, the counter is decremtned. When it is done using the resource, it releases the resource and the counter is incremented. Lastly, when the counter is zero, any thread that needs access to the resource has to wait for another thread to release the resource.

Flowchart of a Semaphore

Mutex: Mutex stands for mutual exclusion. When only one thread is allowed to have access to the thread at a time, it is called a mutex. Another thread has to wait for the resource to be freed in order to use it.

Important note: A semaphore can be a mutex when the semaphore counter permits only one resource. In this case, there is only one resource is available for allocation at that specific time.

Layman’s terms: Semaphores are like going to a club. You can go in as long as there is room in there. When someone comes out, you can go in. Mutex is like a bathroom, only one person at a time.

Leave a comment

Design a site like this with WordPress.com
Get started