Breaking News

25 Java Interview Questions with a Answers



1. Define Class in Java.

In Java, a class is a template used to create objects and define the data type. It acts as a building block for Java language-oriented systems.


2. What Is the Difference Between Static and Dynamic Loading?

Static class loading involves the creation of objects and instances using new keywords, and dynamic class loading is done when the name of the class is not known at compile time.


3. What Is Multi-Threading?

Multi-threading is a programming concept used to run multiple tasks in a concurrent manner within a single program.


4. When and by Whom was Java Developed?

Java was developed by James Gosling in Sun Microsystem in 1995.


5. What Do JDK, JRE, and JVM Stand for?

•    JVM stands for Java Virtual Machine
•    JRE stands for Java Runtime Environment
•    JDK stands for Java Development Kit


6. Does Java Use Pointers?

No. Java doesn’t use pointers. It has a tough security. Instead of pointers, references are used in Java as they are safer and more secure when compared to a pointer.


7. How Do You Connect to a Database in Java?

The steps to connect to a database in Java are:
•    Registering the driver class
•    Creating connection
•    Creating statement
•    Executing queries
•    Closing connection


8. What Are the Functions of JVM and JRE?

JVM provides a runtime environment for Java Byte Codes to be executed. JRE includes sets of files required by JVM during runtime.


9. What Is the Difference Between Overloading and Overriding?

When there are two methods of the same name but different properties, it is overloading. Overriding occurs when there are two methods of the same name and properties, one is in the child class and the other is in the parent class. 


10. What Is the Default Size of the Load Factor in Hashing a Based Collection?

The default size is 0.75, and the default capacity is computed as:
  Initial capacity * Load factor 


11. What Is a Package?

A package is a collection of related classes and interfaces.


12. What's the Base Class of all Exception Classes?

 Java.lang.Throwable is the superclass of all exception classes, and all exception classes are derived from this base class.


13. What Is the Difference Between equals() and ==  ?

 Equals() method is used for checking the equality of two objects defined by business logic.
 == or the equality operator is used to compare primitives and objects.


14. State Two Differences Between an Inner Class and a Subclass

While Inner classes are in the same file, subclasses can be in another file. With that in mind, while subclasses have the methods of their parent class, inner classes get the methods they want.


15. How Are Destructors Defined in Java?

Since Java has its own garbage collection, no destructors are required to be defined.       Destruction of objects is automatically carried by the garbage collection mechanism.


16. Define JSON.

JSON is an acronym for JavaScript Object Notation. It uses JavaScript syntax, and the format is text only.


17. Name the Most Important Feature of Java

Java is a platform independent language.


18. What Is an Anonymous Class?

The class defined without a name in a single line of code using new keyword is known as an anonymous class.


19. What Is a JVM?

JVM is the Java Virtual Machine, which is a runtime environment for compiled Java class files.


20. Can a Dead Thread Be Started Again?

 No, a thread that is in the dead state can't be started again.


21. Are Arrays of Primitive Data Types?

No. In Java, Arrays are objects.


22. What Are Constructors in Java?

In Java, the constructor is a block of code used to initialize an object.
There are two types of constructors:
1. Default constructor: 
A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, the compiler creates a default constructor (with no arguments) for the class, as shown below:

 // no-argument constructor
importjava.io.*;
class Sprint{
   int num;
    String name;
    // this would be invoked while object
    // of that class created.
    Sprint(){
        System.out.println("Constructor called");
    }
}

class Z{
    public static void main (String[] args){
        // this would invoke default constructor.
        Sprint zeal1 = new  Sprint();
        // Default constructor provides the default
        // values to the object like 0, null
        System.out.println(zeal1.name);
        System.out.println(zeal1.num);
    }
}
2. Parameterized constructor
A constructor that has known parameters is a parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.
// Java Program to illustrate calling of // parameterized constructor. import java.io.*; class Sprint{ // data members of the class. String name; int id; // contructor would initialized data members // with the values of passed arguments while // object of that class created. Sprint(String name, int id){ this.name = name; this.id = id; } } class Z{ public static void main (String[] args){ // this would invoke parameterized constructor. Sprint zeal1 = new Sprint("adam", 1); System.out.println("Sprint Name :"+ zeal1.name +" and Sprint Id :" + zeal1.id); } }

24. Explain Garbage Collection in Java.

In Java, when an object is no longer used or referenced, garbage collection is called and the object is destroyed automatically.

25. What's the Difference Between Stack and Queue?

 The difference between a stack and a queue is that the stack is based on the Last in First out (LIFO) principle, and a queue is based on FIFO (First In, First Out) principle.

No comments