Latest Core Java Interview Questions with answers | 2019 | Download PDF

Scholarship Examination in India

Examin, an online Exam software, which also facilitates users to practice various competitive exams and take up mock tests like JEE Mock testNEET Mock test, UPSC Mock Test, Railway exam mock test & more. In this article, Examin provides here Latest Core Java Interview Questions with answers | 2019.  The most important Java Interview Questions and Answers are provided here. Readers can also download the PDF from the link given below. 

 

Basic Java Interview Questions.


1. Why method overloading is not possible by changing the return type in java? 

In C++ and Java, functions cannot be overloaded if they differ only in the return type. The return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. The No of arguments, Type of arguments & Sequence of arguments are the parameters which are used to generate the unique mangled name for each function. It is on the basis of these unique mangled names that compiler can understand which function to call even if the names are same(overloading).


2. Can we override private methods in Java?

No, a private method cannot be overridden since it is not visible from any other class.

 

3. What is the blank final variable?

A final variable in Java can be assigned a value only once, we can assign a value either in the declaration or later.

    final int i = 10;
    i = 30; // Error because i is final.

blank final variable in Java is a final variable that is not initialized during declaration. Below is a simple example of blank final.

    // A simple blank final example 
    final int i;
    i = 30;

 

4. What is “super” keyword in java?

The super keyword in Java is a reference variable that is used to refer parent class objects. The keyword “super” came into the picture with the concept of Inheritance. Whenever you create the instance of a subclass, an instance of the parent class is created implicitly i.e. referred by super reference variable.
Various scenarios of using java super Keyword:

  • super is used to refer to the immediate parent instance variable
  • super is used to call parent class method
  • super() is used to call the immediate parent constructor

 

5. What is static variable in Java?

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static can be:

  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class

 

6. What are the differences between HashMap and HashTable in Java?

1. HashMap is non-synchronized. It is not thread safe and can’t be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.
2. HashMap allows one null key and multiple null values whereas Hashtable doesn’t allow any null key or value.
3. HashMap is generally preferred over HashTable if thread synchronization is not needed

 

7. How are Java objects stored in memory?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate abject using new(), the object is allocated on Heap, otherwise on Stack if not global or static.
In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on heap

 

8. What are C++ features missing in Java?

Following features of C++ are not there in Java.

No pointers
No need to mention the size of operator
No scope resolution operator
Local variables in functions cannot be static
No Multiple Inheritance
No Operator Overloading
No preprocessor and macros
No user suggested inline functions
No goto
No default arguments
No unsigned int in Java
No -> operator in java
No stack allocated objects in java
No delete operator in java due to Java’s garbage collection
No destructor in java
No typedef in java
No global variables, no global function because java is pure OO.
No friend functions
No friend classes
No templates in java

 

9. Explain JDK, JRE, and JVM?

JDK vs JRE vs JVM

JDK JRE JVM
It stands for Java Development Kit. It stands for Java Runtime Environment. It stands for Java Virtual Machine.
It is the tool necessary to compile, document and package Java programs. JRE refers to a runtime environment in which Java bytecode can be executed. It is an abstract machine. It is a specification that provides run-time environment in which Java bytecode can be executed.
Along with JRE, it includes an interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development. In short, it contains JRE + development tools. It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime. So JRE is a software package that contains what is required to run a Java program. Basically, it’s an implementation of the JVM which physically exists. JVM follows three notations: Specification(the document that describes the implementation of the Java virtual machine), Implementation(program that meets the requirements of JVM specification) and Runtime Instance (instance of JVM is created whenever you write a java command on the command prompt and run class).

10. Explain public static void main(String args[]).

  • public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
  • static: It is a keyword in java which identifies it is class-based i.e it can be accessed without creating the instance of a Class.
  • void: It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[]: It is the parameter passed to the main method.

 

11. Why Java is platform independent?

Platform independent practically means “write once run anywhere”. Java is called so because of its bytecodes which can run on any system irrespective of its underlying operating system.

 

12. Why java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

 

13. What are wrapper classes?

Wrapper classes converts the java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class, and constructor argument. 

WrapperClass - Java Interview Questions - Edureka

 

14. What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

  1. Default constructor
  2. Parameterized constructor

 

15. What is singleton class and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

 

16. What is the difference between Array list and vector?

Array List Vector
Array List is not synchronized. Vector is synchronized.
Array List is fast as it’s non-synchronized. Vector is slow as it is thread safe.
If an element is inserted into the Array List, it increases its Array size by 50%. Vector defaults to doubling size of its array.
Array List does not define the increment size. Vector defines the increment size.
Array List can only use Iterator for traversing an Array List. Except for Hashtable, Vector is the only other class which uses both Enumeration and Iterator.

17. What is the difference between equals() and == ?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example, a method can be overridden like String class. equals() method is used to compare the values of two objects.

 

18. What are the differences between Heap and Stack Memory?

The major difference between Heap and Stack memory are:

Features Stack Heap
Memory Stack memory is used only by one thread of execution. Heap memory is used by all the parts of the application.
Access Stack memory can’t be accessed by other threads. Objects stored in the heap are globally accessible.
Memory Management Follows LIFO manner to free memory. Memory management is based on generation associated to each object.
Lifetime Exists until the end of execution of the thread. Heap memory lives from the start till the end of application execution.
Usage Stack memory only contains local primitive and reference variables to objects in heap space. Whenever an object is created, it’s always stored in the Heap space.

 

 

19. How does Java enable high performance?

Java uses Just In Time compiler to enable high performance. JIT is used to convert the instructions into bytecodes.

 

20. What are the Java IDEs?

Eclipse and NetBeans are the IDEs of JAVA.

 

21. What do you mean by Constructor?

  • When a new object is created in a program a constructor gets invoked corresponding to the class.
  • The constructor is a method which has the same name as class name.
  • If a user doesn’t create a constructor implicitly a default constructor will be created.
  • The constructor can be overloaded.
  • If the user created a constructor with a parameter then he should create another constructor explicitly without a parameter.

 

22. What is meant by Local variable and Instance variable?

Local variables are defined in the method and scope of the variables that have existed inside the method itself.

An instance variable is defined inside the class and outside the method and scope of the variables exist throughout the class.

 

23. What is a Class?

All Java codes are defined in a class. A Class has variables and methods.

Variables are attributes which define the state of a class.

Methods are the place where the exact business logic has to be done. It contains a set of statements (or) instructions to satisfy the particular requirement.

Example:

1 public class Addition{ //Class name declaration
2 int a = 5//Variable declaration
3 int b= 5;
4 public void add(){ //Method declaration
5 int c = a+b;
6 }
7 }

 

24. What is an Object?

An instance of a class is called object. The object has state and behavior.

Whenever the JVM reads the “new()” keyword then it will create an instance of that class.

Example:

1 public class Addition{
2 public static void main(String[] args){
3 Addion add = new Addition();//Object creation
4 }
5 }

The above code creates the object for the Addition class.

 

25. What are the Oops concepts?

Oops concepts include:

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Interface

 

26. What is Inheritance?

Ans: Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class.

Existing class is known as the Super class whereas the derived class is known as a sub class.

Example:

1 Super class:
2 public class Manupulation(){
3 }
4 Sub class:
5 public class Addition extends Manipulation(){
6 }

Inheritance is applicable for public and protected members only. Private members can’t be inherited.

 

27. What is Encapsulation?

Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability.

Example:

We are declaring ‘a’ as an integer variable and it should not be negative.

1 public class Addition(){
2 int a=5;
3 }

If someone changes the exact variable as “a = -5” then it is bad.

In order to overcome the problem we need to follow the below steps:

  • We can make the variable as private or protected one.
  • Use public accessor methods such as set<property> and get<property>.

So that the above code can be modified as:

1 public class Addition(){
2 private int a = 5//Here the variable is marked as private
3 }

Below code shows the getter and setter.

Conditions can be provided while setting the variable.

get A(){
}
set A(int a){
if(a>0){// Here condition is applied
.........
}
}

For encapsulation, we need to make all the instance variables as private and create setter and getter for those variables, which in turn will force others to call the setters rather than access the data directly.

 

28. What is Polymorphism?

Ans: Polymorphism means many forms.

A single object can refer to the super class or sub-class depending on the reference type which is called polymorphism.

Example:

1 Public class Manipulation(){ //Super class
2 public void add(){
3 }
4 }
5 public class Addition extends Manipulation(){ // Sub class
6 public void add(){
7 }
8 public static void main(String args[]){
9 Manipulation addition = new Addition();//Manipulation is reference type and Addition is reference type
10 addition.add();
11 }
12 }

Using Manipulation reference type we can call the Addition class “add()” method. This ability is known as Polymorphism.

Polymorphism is applicable for overriding and not for overloading.

 

 

29. What is meant by Method Overriding?

Ans: Method overriding happens if the sub class method satisfies the below conditions with the Super class method:

  • Method name should be the same.
  • The argument should be the same
  • Return type also should be the same.

The key benefit of overriding is that the Sub class can provide some specific information about that sub class type than the super class.

Example:

public class Manipulation{ //Super class
public void add(){
………………
}
}

Public class Addition extends Manipulation(){
Public void add(){
………..
}
Public static void main(String args[]){
Manipulation addition = new Addition(); //Polimorphism is applied
addition.add(); // It calls the Sub class add() method
}
}

addition.add() method calls the add() method in the Sub class and not the parent class. So it overrides the Super class method and is known as Method Overriding.

 

 

30. What is meant by Overloading?

Ans: Method overloading happens for different classes or within the same class.

For method overloading, the subclass method should satisfy the below conditions with the Super class method (or) methods in the same class itself:

  • Same method name
  • Different argument type
  • May have different return types

Example:

public class Manipulation{ //Super class
public void add(String name){ //String parameter
………………
}
}

Public class Addition extends Manipulation(){
Public void add(){//No Parameter
………..
}
Public void add(int a){ //integer parameter

}
Public static void main(String args[]){
Addition addition = new Addition(); 
addition.add(); 
}
}

Here the add() method having different parameters in the Addition class is overloaded in the same class as well as with the super class.

Note: Polymorphism is not applicable for method overloading.

 

 

31. What is meant by Interface?

Ans: Multiple inheritances cannot be achieved in java. To overcome this problem Interface concept is introduced.

An interface is a template which has only method declarations and not the method implementation.

Example:

1 Public abstract interface IManupulation{ //Interface declaration
2 Public abstract void add();//method declaration
3 public abstract void subtract();
4 }
  • All the methods in the interface are internally public abstract void.
  • All the variables in the interface are internally public static final that is constants.
  • Classes can implement the interface and not extends.
  • The class which implements the interface should provide an implementation for all the methods declared in the interface.
1 publicclassManupulation implementsIManupulation{ //Manupulation class uses the interface
2 Public voidadd(){
3 ……………
4 }
5 Public voidsubtract(){
6 …………….
7 }
8 }

 

 

32. What is meant by Abstract class?

Ans: We can create the Abstract class by using “Abstract” keyword before the class name. An abstract class can have both “Abstract” methods and “Non-abstract” methods that are a concrete class.

Abstract method:

The method which has only the declaration and not the implementation is called the abstract method and it has the keyword called “abstract”. Declarations are the ends with a semicolon.

Example:

1 public abstract class Manupulation{
2 public abstract void add();//Abstract method declaration
3 Public void subtract(){
4 }
5 }
  • An abstract class may have a Non-abstract method also.
  • The concrete Subclass which extends the Abstract class should provide the implementation for abstract methods.

 

33. Difference between Array and Array List.

Ans: The Difference between Array and Array List can be understood from the below table:

                        Array                                           Array List    
Size should be given at the time of array declaration.

String[] name = new String[2]

Size may not be required. It changes the size dynamically.

ArrayList name = new ArrayList

To put an object into array we need to specify the index.

name[1] = “book”

No index required.

name.add(“book”)

Array is not type parameterized ArrayList in java 5.0 are parameterized.

Eg: This angle bracket is a type parameter which means a list of String.

 

34. What is “this” keyword in java?

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
Usage of this keyword

  • Used to refer current class instance variable.
  • To invoke current class constructor.
  • It can be passed as an argument in the method call.
  • It can be passed as argument in the constructor call.
  • Used to return the current class instance.
  • Used to invoke current class method (implicitly)

 

35. What is an abstract class? How abstract classes are similar or different in Java from C++?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

 

  • Like C++, in Java, an instance of an abstract class cannot be created, we can have references of abstract class type though.
  • Like C++, an abstract class can contain constructors in Java. And a constructor of the abstract class is called when an instance of an inherited class is created
  • In Java, we can have an abstract class without any abstract method. This allows us to create classes that cannot be instantiated but can only be inherited.
  • Abstract classes can also have final methods (methods that cannot be overridden). For example, the following program compiles and runs fine.

 

36. Which class is the superclass for every class?

Object class

 

37. Can we overload main() method?

The main method in Java is no extra-terrestrial method. Apart from the fact that main() is just like any other method & can be overloaded in a similar manner, JVM always looks for the method signature to launch the program.

  • The normal main method acts as an entry point for the JVM to start the execution of program.
  • We can overload the main method in Java. But the program doesn’t execute the overloaded main method when we run your program, we need to call the overloaded main method from the actual main method only.

 

38. What is object cloning?

Object cloning means to create an exact copy of the original object. If a class needs to support cloning, it must implement java.lang.Cloneable interface and override clone() method from Object class. The syntax of the clone() method is :

protected Object clone() throws CloneNotSupportedException

If the object’s class doesn’t implement Cloneable interface then it throws an exception ‘CloneNotSupportedException’.

 

 

39. How is inheritance in C++  different from Java?

  1. In Java, all classes inherit from the Object class directly or indirectly. Therefore, there is always a single inheritance tree of classes in Java, and Object class is root of the tree.
  2. In Java, members of the grandparent class are not directly accessible. 
  3. The meaning of protected member access specifier is somewhat different in Java. In Java, protected members of a class “A” are accessible in other class “B” of the same package, even if B doesn’t inherit from A (they both have to be in the same package).
  4. Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an inheritance specifier like the public, protected or private. Therefore, we cannot change the protection level of members of the base class in Java, if some data member is public or protected in base class then it remains public or protected in the derived class. Like C++, private members of base class are not accessible in the derived class.
    Unlike C++, in Java, we don’t have to remember those rules of inheritance which are the combination of base class access specifier and inheritance specifier.
  5. In Java, methods are virtual by default. In C++, we explicitly use the virtual keyword. 
  6. Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
  7. Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.
  8. In C++, default constructor of the parent class is automatically called, but if we want to call parametrized constructor of a parent class, we must use the Initializer list. Like C++, default constructor of the parent class is automatically called in Java, but if we want to call parameterized constructor then we must use super to call the parent constructor.

 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 

Print Friendly, PDF & Email

Leave a Reply