Java Interview Questions and Answers

Java Interview Questions:

What is Java?

Java is an object-oriented language which is secure and does not depend on the platform. It is a high-level robust language that has high performance and is portable. The creator of Java is James Gosling and it was first introduced in June of 1991. Along with being a programming language, it is also a platform because it has its own API and JRE.

Differentiate C++ and Java

Basis C++ Java
Platform dependency It is dependent on the platform. It does not depend on the platform.
Main use It is mostly used to program systems. It is mostly used to program application in the window, mobile, and web-based applications.
Goal for design It was designed to program systems and applications. It was extended from C programming. It was constructed to act as an interpreter to print systems but was further created to facilitate network computing. It was developed with the idea of making an easily usable programming language that can be made accessible to wider users.
Goto It supports goto statement. Goto statement is not supported in Java.
Multiple inheritance It supports multiple inheritance. In Java, multiple inheritance cannot be achieved by class. The users must use interfaces in order to achieve multiple inheritance.
Operator overloading Operator overloading is supported by it. Operator overloading is not supported by it.
Pointers Pointers are supported in it which means that pointer programs can be written in this language. In Java, the users cannot write pointer programs because pointer support is restricted in it. However, pointer is supported in Java internally.
Compiler and interpreter It only uses the compiler. It is compiled and operated through the compiler which converts source code to machine code. Therefore, this language is dependent on the platform. In Java, both interpreter and compiler are used. The source code in Java is converted to bytecode during the compile time and the interpreter runs this bytecode during the runtime and creates output. Since Java is interpreted, it is dependent on the platform.
Call by value and Call by reference Both call by value and call by reference are supported in this language. But Java only supports call by value, not call by reference.
Structure and Union Structure and union are supported in it. Structure and union are not supported in it.
Thread support There is no built-in support for threads in it. Therefore, it has to rely on third-party libraries to support threads. It has built-in thread support.
Documentation comment Documentation comment is not supported by it. Documentation comment is supported by Java. It is used to construct documentation for the source code.
Virtual Keyword The users can make the decision whether to override a function or not in this language since the virtual keyword is supported by it. Since Java does not support virtual keyword, the users can override all non-static methods by default.
unsigned right shift >>> The >>> operator is not supported by it. The >>> operator is supported by it.
Inheritance Tree It always constructs a new inheritance tree. It always uses a single inheritance tree as every class is the subclass of the object class in this language.
Hardware It is close to the hardware. Java does not interact much with hardware.
Object-oriented Though C++ is also an object-oriented language, it does not support single root hierarchy. Java is an object-oriented language with a single root hierarchy. This is because all the classes of Java are inherited from the object class.

What are the important properties of the Java programming language?

The following are the important properties of the Java programming language:

  • Simple:

Java is easier for users to learn since the syntax of Java has been derived from the syntax of C++. This makes it simple and convenient to write programs in Java.

  • Object-Oriented:

Java is an object-oriented language that facilitates the users to maintain the program code as the collection of various types of objects that includes both behavior and data.

  • Portable:

In Java, the users can run programs on all the machines since it follows the read-once-write-anywhere approach. The program in Java gets converted to bytecode which can be easily executed in all machines.

  • Platform Independent:

Java does not depend on the platform. It does not need a platform to execute like other programming languages such as C and C++. It has its own platform and also does not depend on the operating system to be operated.

  • Secured:

Java does not use explicit pointers so it is secure. Exception handling and bytecode concepts of Java make it more secure.

  • Robust:

Java uses string memory management so it is strong. Exception handling, Automatic garbage collection, and other similar concepts of it make it more robust.

  • Architecture Neutral:

Java does not depend on the architecture.

  • Interpreted:

In Java, the program is executed by using the JIT interpreter and the compiler.

  • High Performance:

The bytecode of Java is similar to the native code so it is faster than other programming languages.

  • Multithreaded:

The users can write programs in Java that can deal with multiple tasks at the same time by using multiple threads. This way, the users can save memory since memories for each thread are not differently occupied and share a common memory area.

  • Distributed:

Java is distributed as it allows users to construct distributed applications. The users can access the files from any machine on the internet by calling the methods through RMI and EJB features of Java.

  • Dynamic:

Java supports dynamic loading of classes so it is called a dynamic language. In other words, the classes can be loaded on demand.

What is JVM?

Java virtual machine can be defined as a virtual machine that facilitates the computer to execute the program in Java. It functions as a run-time engine that invokes the main method contained in the code in Java. JVM is implemented in the system and it compiles the code to be bytecode that is not dependent on the machine and is similar to the native code.

What is the difference between JDK, JRE, and JVM?

  • JDK

Java Development Kit is a software development environment that is used in order to create applications and applets in Java. It has physical existence and includes the JRE development tools. It is an implementation of Standard Edition Java Platform, Enterprise Edition Java Platform, or Micro Edition Java Platform.

  • JRE

Java Runtime Environment is the implementation of the Java virtual machine. It is a collection of software tools that are used in order to create applications in Java. It gives the runtime environment and has physical existence as well. It includes a collection of libraries and other files that the virtual machine of Java used during the runtime.

  • JVM

Java virtual machine is an abstract machine that gives the runtime environment in which the bytecode of Java can be run. It is a specification that specifies the operation of the virtual machine of the Java and Java runtime environment is the implementation of it.

It does not depend on the platform as it is available for various software and hardware platforms. It is a runtime instance that gets developed when the class of Java is executed. Specialization, instance, and implementation are the three notions of the Java virtual machine.

 

How many types of memory do the JVM allocate?

The following are the various types of memories that the Java virtual machine allocates:

  • Class area:

The per-class structures are stored in the class area like the field, the code for methods, method data, and the runtime constant pool.

  • Heap:

The heap memory is allocated to the objects. It is the runtime data area.

  • Stack:

The frames are stored in the stack memory. It includes the partial results and the local variables and is related to method calling and return.

A private Java virtual machine stack is created for each thread at the time when a thread is created. In other words, a new frame is constructed each time a method is called. When the method call is finished, then the frame is deleted.

  • Program Counter Register: It includes the address of the JVM instruction that is presently being run.
  • Native Method Stack: It includes all the native methods that are used in the application.

Define the JIT compiler

JIT stands for Just-In-Time compiler. It improves the performance by reducing the amount of time spent on the compilation. It does so by compiling the portions of the bytecode that have similar functions at the same time. Here, compiler means a translator from the instruction collection of a JVM to the instruction collection of a particular CPU.

What do you understand by platform?

A platform refers to software or a hardware environment in which a part of the software is run. Among the software-based and hardware-based types of platform, Java supplies the software-based platform.

How is Java platform different from other platforms?

Java is a software-based platform but other platforms may be both software-based and hardware-based. Java is executed on the top of the other hardware platforms. On the other hand, other platforms can only include the hardware components.

What provides the Java with its write once and run anywhere concept?

The Java write once and run anywhere concept is provided by the bytecode. The compiler converts the program into the bytecode which is the intermediate language between the machine code and the source code. This bytecode is not dependent on the platform and can be run on any computer.

What do you understand by classloader?

The classloader is a subsystem of the Java virtual machine that loads the class files. When a program is operated in Java, the classloader first loads it. The following are the three type of classloader in Java:

  • Bootstrap ClassLoader:

The bootstrap classloader is the first classloader which is the parent class of the extension classloader. The rt.jar file is loaded by it which includes all the class files of Java standard edition.

  • Extension ClassLoader:

The extension classloader is the sub classloader of the bootstrap classloader and the superclass of the system classloader. It loads the jar files contained in the ext directory inside the Java library.

  • System/Application ClassLoader:

The application or system classloader is the sub classloader of the extension classloader. The class files from the classpath are loaded by this classloader. The classpath is set to the current directory by default and the users can change the classpath as needed.

Does Java accept only .java file name as a valid source file name?

Yes, an Empty .java file name is accepted by Java as a valid source file name. The users can save their java file by only using .java and later compile the file by javac.java and operate it by the Java class name.

Are there delete, main, null, next, or exit keywords in Java?

No, there are not keywords in Java with such names.

When a user does not give any arguments on the command line, will the value stored in the string array passed into the main method be empty or null?

In such situations, the value stored in the string array will be empty, not null.

Will there be any consequence when a user writes static public void instead of public static void?

No, this won’t affect the program and the program will compile and operate correctly. This is due to the fact that the order of the specifiers does not affect the Java program.

List the default value of the local variables.

In Java, there are no default values, primitives, and object references that are used to initialize the local variables.

Define access specifiers with its types.

Access specifiers can be defined as the keywords that define the access scope of the variable, class, or a method. The following are the types of access specifiers in Java:

  • Public:

The variables, methods, and the classes that are defined using the public keyword can be accessed by any method or class.

  • Protected:

The variables, methods, and the classes that are defined using the protected keyword can be accessed by the class of the same package or by the child class of this class or inside the same class.

  • Default:

If there is no any access modifier is specified for a class, variable, or method – it is called default access modifier by default.

  • Private:

The variables, methods, and the classes that are defined using the private keyword can be accessed only inside the class.

What is the objective of the static methods and variables?

The variables and the methods that are defined by using the static keyword are shared among all the objects of the class. It is not a part of the object, but of the class.

The variables defined as static are kept in the class area and the users do not need to construct the object in order to access such variables. This is the reason why static is used in the situation in which the users need to define variables or methods that are common to all the objects of the class.

List the benefits of the packages in Java

The following are the several benefits of the packages in Java:

  • The name clashes are avoided by the packages.
  • Easier access control is provided by the packages.
  • The related classes are easier to locate.
  • The users can use hidden-classes within the package that are not shown outside the package.

Define the object-oriented paradigm

The object-oriented paradigm can be defined as a programming paradigm that is based on objects that have the methods and data defined in the class to which it belongs to. The main objective of the object-oriented paradigm is to make the program reusable and modular. Here, the objects are the instances of the classes that interact with each other in order to develop programs and applications.

The following are some of the important properties of the object-oriented paradigm:

  • It follows the bottom-up approach while developing the program.
  • It focuses on data with methods to run through the data of the object.
  • It contains features like abstraction and encapsulation that only makes the functionality visible to the users and hides the complex codes.
  • It implements abstraction, inheritance, and other real-time approaches.
  • C++, C#, Simula, and Smalltalk are a few examples of the object-oriented paradigm.

What do you understand by an object?

An object can be defined as a real-time entity that has behavior and state. It is an instance of the class that has the methods as the behavior and the instance variables as the state. The new keyword can be used in order to construct an object of a class.

What is the difference between an object-oriented programming language and object-based programming language?

Object-oriented language Object-based language
This language follows all the concepts of object-oriented programming. This language does not follow all the concepts of object-oriented programming such as polymorphism and inheritance.
They do not have inbuilt objects. They have inbuilt objects.
Java, Smalltalk, and C# are some of the examples of object-oriented language. VBScript and JavaScript are examples of object-based language.

What will be the beginning value of an object reference that is defined as an instance variable?

In Java, all the object references have null value initially.

What do you mean by constructor?

The default constructor is the no-argument constructor automatically generated unless you define another constructor. Any uninitialized fields will be set to their default values. The construct must not have an explicit return type and its name and the name of the class must be the same.

What are the types of constructors that are used in Java?

In Java, constructors have been divided into two typed on the basis of the parameters passed in them. They are:

  • Default Constructor: The default constructor is the type of constructor that does not take in any value. It initializes the instance variables with the default values and also carries out various important tasks while creating objects. In the case in which there is no constructor defined in the class, the compiler implicitly calls a default constructor.
  • Parameterized Constructor: The parameterized constructor initializes the instance variables with the provided values. That means the parameterized constructor is the type of constructor that takes in the arguments in Java.

What is the function of the default constructor?

The main function of the default constructor is assigning the default value to the objects. In the case in which there is no constructor defined in the class, the compiler implicitly develops a default constructor.

What is the return value of a constructor?

A constructor returns the present instance of the class implicitly.

Can a constructor be inherited?

No, a constructor cannot be inherited.

Can a constructor be defined as a final?

No, a constructor cannot be defined as a final.

Can a constructor be overloaded?

Yes, a constructor can be overloaded. This can be done either by changing the number of args taken in by the constructor or by changing the type of data of the parameters.

What is a copy constructor in Java?

In Java, a copy constructor does not exist but the users can use other methods to copy the values from one object to another. The following are the different methods used to perform such operations:

  • Through assigning the values of one object to another
  • Through a constructor
  • Through using the clone method of the object class

What are the differences between the methods and the constructors?

The following are the major differences between a method and a constructor in Java:

  • A method exposes the behavior of the object whereas a constructor initializes the state of the object.
  • A method must always have a return type but a constructor may or may not have a return type.
  • A method is called explicitly but a constructor is implicitly called.
  • The compiler does not provide a method but it provides a default constructor in the case in which there is no constructor defined in the class.
  • The name of a method must not be similar to that of the class whereas the name of the constructor and the name of the class must always be similar.

Define the static variable

The static variable in Java can be defined as the variable that refers to the common features of all objects. It belongs to the class, not the object and it helps the users to make their program more efficient by saving memory. Such variable only gets memory a single time in the class area during the time of class loading.

Define the static method

The static method is the method that accessed and modifies the value of the static variable. It belongs to the class, not the object and there is no need to construct an object in order to invoke the static methods.

List out the restrictions that are set to the static methods in Java

The following are the two important restrictions that are set to the static methods:

  • The static method cannot use non-static data member or directly invoke the non-static method.
  • This and super keyword cannot be used in the static context since they are non-static keywords.

What is the reason behind the main method being static?

The main method is static in Java because there is no need for an object in order to invoke the static method. If the main method is made non-static, then the Java virtual machine will have to first construct an object and then invoke the main method. This will cause the program to allocate extra memory which is not efficient for the users.

Can the static methods be overridden?

No, the static methods cannot be overridden.

Define static block

The static block initializes the static data member and is run before the main method during the time of class loading.

Can a program be executed without the main method?

Yes, the users can use the static block in order to execute the program without the main method.

What will happen if the static modifier is deleted from the signature of the main method?

In such situations, the program will compile but it will throw the NoSuchMethodError error during the runtime.

What are the differences between the static method and the instance method?

S.N Static method Instance method
1. A static method is a method that is declared as static. An instance method is a method that is not declared as static.
2. The users don’t need to construct objects in order to invoke the static methods. The users need to construct objects in order to invoke the instance methods.
3. The static method cannot directly access the non-static members in the static context. The instance method can access both the static and non-static variables.

Can a constructor be made static?

The static context belongs to the class rather than the object. As constructors are called only when the object is constructed, there is no need to make the constructors static. In the case in which the users do son, the compiler will throw the compiler error.

Can the abstract methods be made static in Java?

If the users make the abstract methods static in Java, it will become a portion of the call and the users can directly invoke it which is not necessary. Therefore it cannot be done as invoking an undefined method is not useful.

Can the static variables and methods be declared in an abstract class?

Yes, it is possible to declare the static variables and methods in the abstract class. The users can access the static context defined within the abstract class by using the name of the abstract class as there is no need to make the object to access the static context.

Define ‘this’ keyword

In Java, this keyword refers to the present object and has other various uses as well. It refers to the present class features like instance variables, methods, and constructors. It can also be passed as an arg into the constructors or the methods and can also be returned from the method as the present class instance. It is a reference variable.

List out the main usage of this keyword in Java.

The following are some of the main uses of this keyword in Java:

  • It refers to the present class instance variable.
  • It implicitly calls the present class method.
  • It calls the present class constructor.
  • It can be passed as an arg in the method invoke.
  • It can be passed as an arg in the constructor invoke.
  • It can be used to return the present class instance from the method.

Can the reference be assigned to this variable?

No, this cannot be assigned to any value as it always points to the present class object and this is the final reference in Java. The compiler error will be thrown if the users try to do so.

Can this keyword refer to static members?

Yes, this keyword can be used to refer to static members as this keyword is a reference variable that refers to the present class object. But it is not necessary to use objects to access static variables so it is not recommended to use this keyword to refer static members.

How does this keyword perform constructor chaining?

This keyword can be used to carry out constructor chaining inside the same class. Constructor chaining allows the users to invoke one constructor of the class with respect to the present class object.

What are the benefits of passing this into the method in place of the present class object?

This keyword refers to the present class object so it must be the same as the present class object. There are two main benefits of passing this into the method in place of the present class object and they are:

  • Since this is a final variable, it cannot be assigned to any new value. On the other hand, the present class object may or may not be final and can be modified if it not.
  • This keyword can also be used in the synchronized block.

What do you understand by the term inheritance in Java?

Inheritance refers to the process by which one object acquires all the features and behavior of another object of another class. It facilitates code reusability and overriding of the method. The users can develop a new class by using the existing classes through inheritance.

When inheritance is used to create a new class, the methods and the fields of the superclass can be reused in the child class. Further new methods and fields can also be added to the new class. It represents a parent-child relationship which is known as IS-A relationship in Java.

The following are the five type of inheritance present in Java:

  • Single-level inheritance
  • Multi-level inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

What is the main usage of inheritance in Java?

The following are the various benefits of using inheritance in Java:

  • The new class created by using inheritance inherits the methods and the fields of the superclass. Therefore, it facilitates code reusability.
  • Inheritance is necessary to achieve runtime polymorphism in Java.
  • Stimulating the inheritance of classes with the real-time object makes the object-oriented programming more similar to the real world.
  • Data hiding can be used through inheritance to hide the data of the superclass in the child class by making it private.
  • Inheritance is necessary to perform method overriding as well.

Which class is called the parent class of all the classes in Java?

The object class is the parent class of all the other classes in Java.

What is the reason behind multiple inheritances not being supported in Java?

The multiple inheritances are not supported in Java in order to reduce the complexity and to make the language simple.

Define aggregation

In Java, the term aggregation refers to the relationship that exists between two classes in which the aggregate class has a reference to the class it owns. It is known as a has-a relationship.

Define composition

The composition is the mechanism of holding the reference of a class inside some other class. It is the case in which an object contains another object and the contained object cannot be there without the container object being there. It is similar to aggregation but represents a stronger relationship between two objects.

What is the difference between aggregation and composition?

The function of aggregation and composition is similar and the only difference between them is that aggregation represents a weak relationship than the composition does.

Why are pointers not supported in Java?

The pointers are variables that represent the memory address. Java does not use pointers since they are unsecured and are difficult to understand.

Define the term super in Java

The super keyword is Java refers to the immediate superclass object. It is a reference variable that refers to the instance of the superclass that is created implicitly when the instance of the child class is created by the users. In the case in which, there is not super or this keyword defined in the class constructor then it is called implicitly by the compiler.

Why is the super keyword used in Java?

The following are the main usage of super keyword in Java:

  • It refers to the immediate superclass instance variable.
  • It calls the immediate superclass method.
  • It calls the immediate superclass constructor.
  1. Differentiate between this and super keyword.
S.N This keyword Super keyword
1. It always refers to the present class context. It always refers to the superclass contexts.
2. It differentiates between the local and the instance variables when used in the class constructor. It initializes the superclass variables inside the subclass constructor.

 

Can both this and super keyword used in a constructor?

No, both this and super keyword cannot be used in a constructor as the keywords need to be the beginning statement in the class constructor.

What do you understand by object cloning?

The object cloning is the mechanism that creates the exact copy of an object. The clone method of the object class is used to do so. The Cloneable interface must first be implemented by the class whose object clone the users want to construct. If the interface is not implemented and the users use the clone method, then the CloneNotSupportedException will be thrown.

What do you understand by method overloading?

Method overloading refers to the process within polymorphism feature that facilitates the users to develop various methods with the same name but different signature. Method overloading can be achieved in Java by two methods which are:

  • By modifying the number of arguments
  • By modifying the return type

Method overloading is performed to raise the readability of the program since it helps the users to figure out the program rapidly.

Why method overloading cannot be achieved by modifying the return type in Java?

Method overloading cannot be achieved by modifying the return type in Java in order to avoid ambiguity.

Can a method be overloaded by making it static?

No, a method cannot be overloaded by setting the static keyword to it.

Can the main method be overloaded?

Yes, the main method can be overloaded to have any number of main methods in the program.

What do you understand by method overloading with type promotion?

The term type promotion in method overloading refers that one type of data can be promoted to another type of data implicitly in the case in which there is no exact match. The byte can be promoted to short, long, int, float, or double. Similarly, the short data type can be promoted to int, float, long, or double. And the char data type can be promoted to int, float, long, or double.

Define method overriding.

Method overriding refers to the case in which the child class gives a particular implementation of a method that is already given by its superclass. It implements the interface methods and is used for runtime polymorphism.

The following are a few of the rules to take into account in method overriding:

  • The name of the method must be the same as in the superclass.
  • The signature of the method must be the same as in the superclass.
  • There must be the presence of IS-A relationship between two classes.

Can the static method be overridden?

No, the static method cannot be overridden because they belong to the class rather than the object.

What is the reason behind the overriding of the static method not possible?

The static method cannot be overridden as the static method belongs to the class and it bound with it. On the other hand, the instance method belongs to the object and is bound with it. The static allocates memory in the class area and the instance allocates memory in the heap area.

Can an already overloaded method be further overridden?

Yes, it is possible to do so in Java.

Differentiate between method overloading and overriding.

The following are the main differences between method overloading and overriding:

  • Method overloading makes the program easily readable whereas method overriding gives a particular implementation of the method that is already given by its parent class.
  • Method overloading takes place inside the class but the method overriding takes place in two different classes that are connected by an IS-A relationship.
  • In method overloading, the parameters must be different but in method overriding the parameters must be the same.

Can private methods be overridden?

No, the private methods cannot be overridden because the area of the private methods is bound to the class and the users cannot access them from outside the class.

Can the area of the overridden methods be changed in the child class?

Yes, it is possible to change the area of the overridden methods in the child class but the users need to notice that the accessibility of the method cannot be reduced.

The following are the points that the users must understand while modifying the accessibility of the method:

  • The private can be modified to public, protected, or default.
  • The protected can be modified to default or public.
  • The default can be modified to public.
  • The public cannot be modified, it always remains the same.

Can the throws clause of the parent class be modified while overriding it in the child class?

Yes, it is possible to modify the throws clause of the parent class while overriding it in the child class. But there are a few points that the users need to understand while overriding in the situation of exception handling and they are:

  • If an exception is not declared by the parent class, then the child class overridden method can only declare the unchecked exception, not the checked exception.
  • If an exception is declared by the parent class, then the child class overridden method cannot declare super exception but can declare the same and child class exception.

Are there virtual functions in Java?

Yes, all of the functions in Java of virtual type.

Define the covariant return type

The term covariant return type refers to the case in which a method is overridden by modifying the return type when the return type of the child class overriding method is child class type. It specifies that the return type may be different in the same direction as the child class.

What do you understand by a final variable?

The final variable restricts the users from changing it. When a final variable is initialized, its value cannot be modified. So the final variable is assigned to a value in order to stop it from being modified. The final variable can only be assigned by the class constructor if it is not assigned to any value.

What is the final method?

The method declared as a final is known as a final method. A method cannot be overridden once declared as a final.

Define final class

The class declared as a final is known as final class. A class cannot be inherited to any of the child classes once it is declared as a final.

Define a final blank variable

The final blank variable is the final variable that is not initialized during the declaration time. Such type of final variable cannot be initialized directly. The users need to use the class constructor to initialize it. It is mostly used when there is a presence of data which must never be modified by others.

Can the final blank variable be initialized?

Yes, it is possible to initialize a final blank variable in the constructor if it is not static. If it is static, then the static block must be used to initialize it.

Check also: Why Should You Learn Java?

 

 

Keywords: Java Interview Questions | Entry-level Java Interview Questions| Mid-level Java Interview Questions | Core Java Interview Questions | Senior level Java Interview Questions | All level Java Interview Questions | Java Interview Questions and answers | Java Interview Questions 2019 | Java interview questions for Entry-level | Java interview questions for fresh student | Java interview questions for mid-level | Java interview questions for all level | java interview questions pdf | java interview questions and answers for freshers | java interview questions for junior | advanced java interview questions | java interview questions for experienced professionals  | Frequently asked Java interview questions | Every candidates should know these Java interview questions | Java interview questions for experienced professionals | java interview questions for 1 years experience | java interview questions for 2 years experience | java interview questions for 3 years experience | Java interview questions for 4 years experience | java interview questions for 4 years experience | Java interview questions blog | Java interview questions website | All Java interview questions | Complete Java interview questions | Important Java interview questions | java interview questions for non-experience.

Leave a Reply

Your email address will not be published. Required fields are marked *