TechieClues TechieClues
Updated date Jan 27, 2024
Frequently asked 50 Java interview questions and answers for freshers as well as experienced java developers.
  • 3.7k
  • 0
  • 0

1. What do you mean by JAVA?

Java is a high-level object-oriented programming language. It does not depend on a specific platform. It consists of several objects. In today’s world, there are different applications, websites, and games that are developed using Java.

2. What are the main characteristics of Java?

Java follows oops concepts such as – Object-oriented, Inheritance, Polymorphism, Encapsulation, and Abstraction

  • Java is platform-independent.
  • It supports multithreading.
  • It provides high performance.

3. What is JVM in Java?

JVM stands for Java Virtual Machine which enables the computer to run java code. JVM is considered a run-time engine that involves calling the main method during execution. JVM works as a specification that needs to be implemented in the computer system before writing code. JVM helps in the compilation of the source code and transfer it into bytecode.

4. What do you mean by wrapper classes in Java?

Each type of primitive data has a specific class that is dedicated to it. These classes are known as a wrapper class. In the other words, wrapper class means it is wrapping primitive data type into an object of that class. It is used to convert java primitives into the reference type.

5. What do you mean by 'write once and run anywhere' in Java?

Java compiler is involved to convert java programs into the class file (Byte code) that is the intermediate language between source code and machine understanding code. This intermediate code of byte code does not depend on a specific platform and can be executed in any platform.

6. What is a constructor in Java?

A constructor is a method having the same name as the class name. A constructor gets invoked when a new object is created in a program. If a program does not have code for a constructor, a default constructor will create by default.

7. Why we do not use the pointer in Java?

The pointer is not safe and involved in increasing complexity in the program. As Java is a simple programming language and JVM is present in Java for implicit memory allocation, to avoid direct access, pointers are not allowed in Java.

8. What values are considered as a default in the case of local variables?

There is no initialization of local variables (neither primitives nor object references).

9. What is the java class?

A class in java is a framework having all data. A class has several fields (variables) and methods to explain the behavior of an object.

Syntax of a class:

class Test 
{
   // Member variables

   // Methods
}

10. What are the basic differences between JVM and JDK?

JVM (Java Virtual Machine) JDK (Java Development Kit)
It is a part of the execution of Java programs. It is a development kit used for developing purposes.
JVM performs execution of a program that is machine independence. It consists of tools, executables, and binaries for compiling, debugging, and executing a program.

11. “Java does not support multiple inheritances” – Why?

Due to the “Diamond Problem”, Java does not support multiple inheritances in classes. As the parent class makes an ambiguity, the compiler becomes confused about which superclass method to execute.

12. Explain different access specifiers in Java.

Access specifiers are keyword in Java used for defining the access scope of the particular method, variable, or class. Public, protected, private, and default – these four types of access specifiers present in Java.

13. Why does Java use Packages?

There are different benefits of Packages in Java.

  • The package delivers easy access to control.
  • There are a few hidden classes present that are not visible outside and used by the package.
  • It does not consider the name clashes.

14. What is an object in Java?

The object is considered as a real-time entity with having some state and behavior. It is also known as an instance of the class consisting of variables and the methods. The ‘new’ keyword is used to create an object of a class.

15.  What is method overriding?

In method overriding, method, argument, and return type should be the same. The advantage of using overriding is that the Sub-class can deliver a few particular information about the sub class type than superclass.

16. What is overloading in Java?

In overriding, method names are the same but argument types are different. In method overriding, the subclass method is involved in satisfying these three conditions with the superclass method.

17.  What do you mean by abstract class in Java?

The ‘Abstract’ keyword is used to create a new abstract class before the class name. An abstract class can have both abstract methods and non-abstract methods or concrete classes.

Abstract method – This method only has a declaration and not the definition. It also uses the ‘abstract’ keyword.

Syntax:

public abstract void add();

18. Why do we need to use static methods and variables?

When the methods or variables are static, it can be shared among all the objects of the class. Static is considered as a part of a class, not of an object. So, static variables are present in the class area and we don’t need to access these variables. Hence, the static keyword will be used when we need to define a variable or method which is common in all the objects.

19. What is the importance of the default parameter in Java?

The importance of a default constructor is assigning the default value to the objects. In each execution of a java program, a default constructor is created implicitly when there is no constructor in the class.

20. Does Java allow to overload a constructor?

Yes, a constructor can be overloaded in Java. It can be overloaded with changing the number of arguments accepted by the constructor or changing the argument type of the parameters.

21. Discuss the restrictions that are applied to java static methods.

Two generic restrictions are applied to the Java static method.

A static method can not use non-static data members. It also can not call the non-static method directly.

The ‘this’ and ‘super’ keywords are used static context as they are considered as non-static.

22. What is the importance of ‘this’ keyword in Java?

 The ‘this’ keyword is a reference variable used to refer to the current object. It is used to refer to current class properties including instance variables, methods, constructors, etc. It can be passed through the method or constructor as an argument.

23. What is the importance of using inheritance?

The advantages of using inheritance are mentioned below:

  • Inheritance gives code reusability.
  • It does not support the run time polymorphism.
  • We can simulate the inheritance process with real-time objects.
  • It delivers data hiding ability.
  • It does not support method overriding.

24. What do you mean by aggregation?

Aggregation is a relationship that exists between two classes where one aggregate class has a reference to another one. It is used to describe the has-a relationship.

25. What is the importance of a super keyword in Java?

The super keyword is considered as a reference variable that can be used for referring to the immediate parent class object. When an object of a subclass is created, an object of the parent class will be also created implicitly which is referred to by a super reference variable.

26. What are the advantages of using a super keyword?

The advantages of super keywords are:

  • It can be used for referring to the immediate parent class instance variable.
  • It can invoke the immediate parent class method.
  • It is also used to invoke immediate parent class constructor.

27.  What is polymorphism?

Polymorphism is an ability of a variable or function or object to form multiple copies. It is another main concept of object-oriented programming. In other words, polymorphism is ‘one interface, many implementations’. There are two types of polymorphism: compile time and run time polymorphism. 

28. What is encapsulation?

Encapsulation is a process of binding data and code together as a single unit. In this case, data is hidden from the outer world and can be accessed only through current class methods. It helps data from unauthorized modification.

29. Define object cloning in Java.

Object cloning means creating the same copy of an object. It delivers an ability to create an object with having the same state just as the original one. Java has a method clone() for doing so. This method is involved in forming a new instance of the class of the current object and then initializes all its fields with having the same contents of the corresponding file.

30. What do you mean by copy constructor?

A copy constructor is considered as a member function that is involved in initializing an object using another object of the same class. As all objects are passed by reference, there is no need for a copy constructor.

31. What are the basic difference between this() and super() methods?

this() super()
It represents the current instance of a class. It represents the current instance of a parent class.
It is used to access methods of a current class. It is used to access methods of a parent class.
It also used to call the default constructor of a same class. It is used to call the default constructor of the parent class.

32. Can a static method override in Java?

No, we can’t override a static method because they are considered as a part of the class, not the object. Static is bounded with class and gotten memory in the class area.

33. What are the basic differences between method overloading and method overriding?

Method Overloading Method Overriding
It increases the readability of the program. It delivers a specific implementation of the method.
It occurs within a class. This occurs in two classes that have IS-A relationship.
All parameters must be different. All parameters must be same.

34. Can private methods be overridden in Java?

No, we cannot override private methods in Java. Because the scope of private methods is limited for the class and we will not be able to access them outside of the class.

35. What is the importance of the final variable in java?

In Java, the final variables are used to restrict the user from updating. When a variable is finalized, the value of it will not be changed. In other words, when a final variable is assigned a value, it cannot be changed. The advantages of using Java final keyword are:

  • It stops value changes.
  • It stops method overriding.
  • It stops inheritance.

36. What is the final blank variable?

The final variable is not initialized at the time declaration. This variable is called a final blank variable. We are not allowed to initialize the final bank variable directly. Because we need to initialize it by using the constructor. It is used for some data that must not be changed by others such as ID numbers.

37. What do you mean by Java String Pool?

Java string pool is a collection of Strings that can be stored in heap memory. When a new object is created, the String pool will check whether the object is already present or not. If an object is already present, the same reference is returned to the variable otherwise new object is created in String Pool.

38. What is the classloader in Java?

The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is used to load the class files. When a java program is executed, it will load by the classloader.java to deliver three built-in classloaders:

  • Bootstrap ClassLoader
  • Extension ClassLoader
  • System/Application ClassLoader

39. What is an interface in Java?

The interface in java is just like a blueprint of a class. It is a collection of abstract methods and static constant. In an interface, each method is considered static and abstract and it does not have any constructor. Hence, the interface is a group of relevant methods having empty bodies.

40. What are the basic differences between abstract classes and interface?

Abstract class Interfaces
An abstract class can deliver default, complete, code that needs to be overridden. It cannot deliver any code at all, just gives the signature.
An abstract class may have non-abstract methods. The interface has all abstract methods.
An abstract class may contain constructors and instance variables. The interface does not contain any constructor and instance variables.

41. What do you mean by the association in Java?

In association, all objects have their lifecycle and there is no particular one owner present. Association shows a relationship between these objects. As an instance, multiple employees are associated with one leader, and one employee is associated with multiple leaders but there is no ownership between objects.

42. What do you mean by composition in Java?

The composition is considered as a specialized form of an aggregation. It is also called a ‘death relationship’. Child object does not consider any lifecycle and if a parent object is deleted, all child objects will be deleted.           

43. Can we declare the main method as final?

Yes, we can declare the main method as final.

Syntax:

public static final void main(String[] ar)
{}

44. What do you mean by Request Dispatcher Interface?

Request Dispatcher Interface is involved to deliver the request to another source that can be HTML or JSP or another servlet for the same application. It can also be used for including the content of another resource to the response. Two types of methods present: void include() and void forward ().

45. What is servlet?

Java servlet is considered as server-side technologies used to extend the ability of web servers through delivering support for dynamic response. There are a total of five stages in a life cycle of a servlet such as loading, instantiated, initialized, request, and destroyed.

46. Is there any virtual methods in Java?

Yes, Java consists of virtual functions by default.

47. Does Java support the deceleration of interface as final?

No, Java does not support the declaration of the interface as final because the interface is involved in implementation by some class in order to deliver definition. Hence, there is no need to make an interface final.

48. Is there any option in java to overload the main () method?

Yes, we can use method overloading to get any number of main methods in a java program.

49. What do you mean by covariant return type?

In Java5, we can override any method just changing the return type of the subclass. It is called a covariant return type. It is involved in specifying the return type that may vary in the same direction as the subclass.

50. Can we declare a constructor as a final?

No, we can't declare a constructor as final because it cant be inherited. It is not an ordinary method. So, if we try to do so, the compiler will throw an error.   

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!