Wed, Nov 30, 2022
Most Asked Java Programming Interview Questions and Answers

The importance of having thorough knowledge about JAVA is huge. It adds a lot of value to your CV, but the real struggle is ace the interview where you will be questioned about your knowledge of Basic JAVA. So, to make things easier for you, we are providing you with a good set of questions. The most frequently asked Java interview questions and answers are as follows:

  1. What do you mean by JAVA?

Answer: Java is the high-level secure-programming language which is object-oriented, robust, platform-independent and portable. James Gosling, born in 1991, developed the language. It can also be known as the language which provides its own JRE and API.

  1. What do you understand by Java virtual machine?

Answer: As the name suggests, Java virtual machine is a virtual machine which enables the computer to run the JAVA program. It acts like a real run-time engine which calls the main method present in the JAVA code. JVM is one of those specifications which must be implemented in the computer system. Compiled by JVM, the Java code happens to be a Byte code which is machine independent and close to the native code.

  1. Name the types of memory areas allocated by JVM.

Answer: Following are the types of memory areas allocated by JVM:

  • Heap: It is the run-time data where memory is allocated to the objects
  • Stack: A stack stores frames. It is there to hold variables and partial results. Each thread has a private JVM stack which is created at the time similar as that of the thread. Whenever a method is invoked, a new frame is created and it is destroyed when its method invocation completes.
  • Class area: A class-area stores per-class structures which include method data, code for methods, runtime constant pool…
  • Program counter register: The program counter register contains the address of the Java virtual machine instruction which is currently executed.
  • Native method stack: All the native methods that are used in the application is contained by Native method stack.
  1. What is the platform?

Answer: A platform is basically the hardware or software environment within which a piece of software is executed. There are two types of platforms: a hardware-based platform and software-based platform. The one provided by Java is Software-based platform.

  1. Would it be correct to call an Empty .java to name a valid source file name?

Answer: Yes, we can call an Empty .java to name a valid source file name.

  1. Do the following keywords belong to JAVA: delete, next, main and exit?

Answer: No, these keywords do not belong to JAVA.

  1. What would happen if we write static public void instead of a public static void? What difference would it create?

Answer: There will be no change. The program would compile and run correctly and this is because the order of specifiers doesn’t matter in JAVA language.

  1. What is the default value of the local variables?

Answer: There is no default value as such. The local variables are not initialized to any default value and nor does primitives or object references.

  1. What are the different advantages of packages in Java?

Answer: Following are the advantages:

  • The package provides easier access control.
  • It is easier to locate related classes.
  • The packages avoid the name clashes.
  • There are also hidden classes that are not visible outside and are used by the package.
  1. What do you mean by an object?

Answer: An object in Java can be defined as a real-time entity which has some state and behavior. An object can be said to be an instance of the class having the instance variables like the state of the object. And the methods as the behavior of the object.

  1. Does constructor return any value?

Answer: Yes, it does. The constructor tacitly returns the current instance of the class.

  1. Is the constructor inherited or not?

Answer: No, it is not.

  1. Can we overload the constructors?

Answer: Yes, it is possible to overload the constructors. It can be overloaded by changing the number of … making changes to the denial of things and making adequate changes and making it look like the best thing we could own and there is literally nothing to be ashamed of. We are definitely going to make things durable if we know what difference it makes to the serial of the sequence and there is literally no changes that overloading of the constructors can make.

  1. Can you make constructor a final one?

Answer: Well, no. A constructor cannot be final.

  1. What is Static Method?

Answer: A static method is that method which belongs to the class rather than the object. There is no need as such to create the object to call the static methods. This method can access and change the value of the static variable.

  1. For what reason is the main method static?

Answer: The reason why the main method is static is that an object is not required to call the static method. To make the main method non-static, the JVM will be required to create its object first and then call a main() method which will then lead to the extra memory allocation.

  1. Is it possible to override the static methods?

Answer: No, it is not possible to override static methods.

  1. Can we execute a program without the main () method?

Answer: Yes, we can execute a program without the main () method and that can be done by using static block.

  1. What happens when the static modifier is removed from the signature of the main method?

Answer: An error “NoSuchMethodError” would occur at runtime but the program does compile.

  1. Is it possible to make constructors static?

Answer:  It makes no sense to make constructors static because constructors are invoked only when the object is created. However, if we still try to make the constructors static, the compiler will show the compiler error.

  1. Can we make the abstract methods static in Java?

Answer: If we make the abstract methods static in Java, it will become a part of the class and we’d be able to directly call it which is unnecessary. It is absolutely useless to call an undefined method and therefore it is not allowed.

  1. What are the main uses of the keyword- ‘this’?

Answer: ‘This’ keyword can be used to:

  • pass on as an argument in the constructor call
  • return the current class instance from the method
  • pass as an argument in the method call
  • invoke a current class method
  • invoke the current class constructor
  • refer to the current class instance variable
  1. Which class is said to be the superclass of all the classes?

Answer: The object class is called the superclass of all the classes.

  1. What is the reason behind Java not supporting pointers?

Answer: The variable –‘pointer’ refers to a memory address. These are not used in Java because in comprehending it, they can turn out to be complex and unspecific.

  1. State the main uses of the ‘super’ keyword.

Answer: Following are the main uses of the word ‘super’:

  • It would always point to the immediate parent class instance variable.
  • Super () can be used to invoke immediate parent class instructor.
  • It can be used to invoke the immediate parent class method.
  1. Is it possible to overload the main () method?

Answer: Yes, it is possible to overload the main () method in a Java program and that can be done by using method overloading.

  1. Is it possible to override the overloaded method?

Answer:  Yes, it is possible to override the overloaded method.

  1. Why can we not override static method?

Answer: It is not possible to override static method because it is a part of the class, and also it is bound with class whereas if we take instance method into consideration, it is bound with the object. The static gets memory in the class area and instance gets memory in a heap.

  1. Is it possible to override private methods?

Answer: No, it is not possible to override the private methods and this is because the scope of private methods is limited to the class and we cannot access them outside the class.

  1. Is it possible to have virtual junctions in java?

Answer: Yes, it is possible. In fact, all functions in Java are virtual by default.

  1. Is it possible to initialize the final blank variable?

Answer: If it is not static, it is possible to initialize the final blank variable. Whereas if it is static final blank variable, it can be initialized only in the static block.

  1. What is the basic difference between the abstract method and the final method?

Answer:  The basic difference between the two of these methods is that in the case of the abstract method, it cannot be final as we would need to override them in the subclass to give its definition.

  1. How are Abstraction and Encapsulation different?

Answer: Abstraction will hide the implementation details while codes and data are wrapped in a single unit in the case of Encapsulation.

  1. Is it possible to have an abstract method without an abstract class?

Answer: No, that is not possible. Having an abstract class is a must.

  1. Is it possible to use ‘abstract’ and ‘final’ both with a method?

Answer: No, it isn’t possible to do so since we need to override the abstract method to provide its implementation, whereas it isn’t possible to override the final method.

  1. Is there a possibility to instantiate the abstract class?

Answer: No, there is no possibility even if it contains a constructor and all of its methods are implemented.

  1. Will you be able to declare an interface method static?

Answer: No, it is not possible to do so because methods of an interface are abstract by default and we can’t use static and abstract together.

  1. Is it possible for the interface to be final?

Answer: No, we cannot make the interface to be final because an interface is required to be implemented by the other class. If it final, it cannot be implemented by any class.

  1. What do you mean by marker interface?

Answer: An interface which has no data member and member functions can be said to be a marker interface. Examples of marker interface are Cloneable, Serializable…  In order to declare a marker interface, you can do the following:

public interface Serializable {

}

  1. Is there a need to import the same package/ class twice? Will the packet be loaded twice by the JVM at runtime?

Answer: One can import the same package not just once, but multiple times. JVM and compiler: neither of these can complain about it. However, it is only once that JVM can load the class internally no matter how many times you import the same class.

  1. Would you need to import java.lang package anytime? State the reason.

Answer: No, it is loaded internally by the JVM by default.

  1. State the base class for Error and Exception.

Answer: The base class for Error and Exception is the Throwable class.

  1. Is it possible for an exception to be rethrown?

Answer: Yes, it is possible.

  1. Can you name some of the classes in til.regex package?

Answer: Following are the classes and interfaces present in java.til.regex package:

  • Pattern class
  • Matcher class
  • MatchResult Interface
  • PatternSyntaxException class
  1. Do you think there is any difference between nested classes and inner classes?

Answer: Yes, there is. Inner classes are actually non-static nested classes. If we say it in different words, then, inner classes are a part of nested classes.

  1. Is it possible for a class to have an interface?

Answer: Yes, an interface can be defined within the class and that is called a nested interface.

  1. Is it possible for an interface to have a class?

Answer: Yes, it is possible and they are static implicity.

  1. How can you make a class serializable in Java?

Answer: By implementing a Serializable Interface, a class can become serializable.

  1. Would you be able to access the private method from outside the class?

Answer: Yes, we can do it by changing the runtime behavior of a class in case the class is not secured.

  1. What is a native method?

Answer: A method that is implemented in a language other than Java. These native methods are sometimes also referred to as foreign methods.

  1. How are collection and collections similar and different?

Answer: Collections is a java class while Collection is just an interface but they are similar in a way that both are present in java.util package and both are a part of Java collections framework.

  1. Which of the collection classes are synchronized or thread safe?

Answer: Following are the collection classes:

  • Properties
  • Vector
  • Stack
  • Hashtable
  1. How do you differentiate List and Set?

Answer: List contains duplicate elements while set contains only unique elements. List happens to be ordered while Set is unordered. List has to maintain the order in which objects are added.

  1. How do you differentiate Set and map?

Answer: Set contains only unique values while Map has unique keys where each of them contains some value.

  1. What do you mean by iterator?

Answer: Iterator is an interface which is found in java.util package.

  1. Which methods would you need to override to use any object as a key in Hashmap?

Answer: In order to use any object as a key in HashMap, you would be required to implement equals () and hashcode method ().

  1. Is there a way to reverse the list in collections? If yes, how?

Answer: Yes, there is a way to reverse the list in collections. There is a built-in reverse method in Collections class and this list accepts List as a parameter.

Collections.reverse(listobject);

  1. Is it possible for two equal objects to have a different hash code?

Answer: No, according to the Hash code contract, it is not possible.

  1. What kind of a tree is required is used in order to implement TreeMap in Java?

Answer: The tree that is used to implement TreeMap in Java is Red Black Tree.

  1. Can we use random numbers in the hashcode () method?

Answer: No, that is not possible because the hashcode of an object must be the same.

  1. What do you mean by hash-collision in Hashtable and how is it handled in Java?

Answer: If there are two different keys which have the same hash value, then it will lead to hash-collision. A bucket of type LinkedList used to hold the different keys which have the same hash value.

  1. How would you sort ArrayList in the descending order?

Answer: To sort ArrayList, one line would be: Collections.sort(arraylist,Collections.reverseOrder());

  1. Name the common algorithms that are used in the Collections Framework.

Answer: The common algorithms that are used in the Collections framework are:

  • Searching
  • Sorting
  1. Which class is the superclass for all the classes?

Answer: The object class is said to be the superclass for all the classes.

  1. Why doesn’t Java support pointers?

Answer: The reason behind it is that they are unsafe (unsecured) and complex to understand.

  1. Can we overload the main () method?

Answer: Yes, it possible to overload the main () method and it is done by overloading.

  1. Can we override the private methods?

Answer: No, it is not possible to override the private methods and that is because the scope of private methods is limited to the class and we cannot access them outside the class.

  1. Why is it that the method of overloading is not possible to change the return type in Java?

Answer: Because ambiguity needs to be avoided, the method of overloading is not possible to change the return type in Java.

  1. Can we have virtual functions in Java?

Answer: Yes, all functions in Java are virtual by default.

  1. What is the difference between the abstract method and the final method?

Answer: The main difference between two of these methods is that one of them cannot be final as we need to override them in the subclass to give it a definition and that class is the abstract class.

Practice all the java questions and answers for the interview preparation.

EnhanceLearn provides Java Training and Placement for the candidates who want to begin or enhance the Java Programming. Please reach us at EnhanceLearn.com for any queries about the trainig course and program.

0 Comments

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.