Mostly asked Core Java Interview Questions with Answers in 2020

TechiesHub9
10 min readNov 1, 2020

Q.1. What is a Class in Java?

Ans- Class is a skeleton/blueprint for creating Objects. It represents the set of properties or methods that are common to all objects of one type.

Class name should always starts with an uppercase first letter, and that the name of the java file should match with class name. Everything in Java is associated with classes and objects.

Q.2. What is Object of class in Java?

Ans — Object is a runtime instance of a class. Every program runs on RAM. JVM is a program itself.

The Object has State and behavior. You can create multiple objects of one class.

You can also create an Object of a class and access it in another class.

An Object is a real-world, runtime entity.

Q.3. What is main() method in Java ?

Ans — The main method is a starting execution point of programme.

If main() method is not written in a Java programme, JVM will not execute it.

main() method is the starting point for JVM to start the execution of Java programme.

Q.4. What is String args[]?

Ans — main() method also accepts some data from us.

Ex. It accepts a group of Strings, which is also called a String type array.

This array is String args[], which is written along with the main() method as: public static void main(String args[])

This array can also store a group of numbers but in the form of strings only. Values passed to main method are called Arguments.

These arguments are stored into args[] array, so the name args [] is generally used for it.

Q.5. What is void in main method?

Ans — Void means no value. main() method does not return any value, so void should be written before the method’s name.

Q.6. What is static?

Ans — A method is executed only when it is called.

An object could be created only after calling the main method.

But for calling main() method first of all we require an Object.

Now how is it possible to create an object before calling main(). So we should call the main() method without creating an object.

Such methods are called static methods and should be declared as static.

Static is a global access specifier. Static block executes first.

Static methods are the methods, which can be called and executed without creating the objects. Since we want to call main() method without using an object, we should declare main() method as static.

-main() method can be called and executed by using classname.methodname().

JVM calls main method as classname.methodname() at the time of running the programme.

Q.7. What is public in main method ?

Ans — JVM is a program written by JavaSoft people(Java development Team) and main() method is written by us.

Since main() should be available to JVM, it should be declared as public.

If we don’t declare main() method as public, then it doesn’t make itself available to JVM and JVM cannot execute it.

Q.8. What if main method is written without String args[] ?

Ans — The code will compile but JVM cannot run the code because it cannot recognize the main() method as the method from where it should start execution of Java program. Remember JVM always looks for main() method with String type array as parameter.

Method should be called using objectname.methodname().

So to call print() method, we should create an object to the class to which print() method belongs. print() method belongs to PrintStream class.

PrintStream obj.print(“Welcome to Java”);

But it is not possible to create the object to printStream class directly, an alternative is System.out — System is the class name and out is a static variable in System class. out is called a field in System class. When we call this field, a printStream class object will be created internally.

Q.9. What is an instance of a Class ?

Ans — Method and variable that are not a class method or class variables are known as Instance method and instance variable.

Class variables and class methods are associated with a class and Occur once per class.

Instance methods and variables occur once per instance of a class.

Q.10. Difference between Class and Object ?

Ans –

Object ClassObject is an instance of a class.Class is a blueprint or template from which object are created. Object is a real world entity such aspen,laptop, mobile, etc.Class is a group of similar Objects.Object is a physical entity.Class is a logical entity.Object is created through new keyword mainly Student S1=new Student();Class is declared using class Keyword. Ex.class Student{}Object is created many times as per requirementClass is Declared Once.Objects allocates memory when it is created Class doesn’t allocated memory when it is created. There are many ways to create object in java such as new keyword,newInstance() method, clone()method and deserialization.There is only one way to define classin java using class keyword.

Q.11. What is a Variable and Type of Variables?

Ans –

Variable is a piece of memory and used to store information.

The memory stored the one information at a time.

Type of variable:

  1. Local Variable:-
  2. They are declared inside the methods.
  3. It’s scope is only inside the method, in which it is declared.
  4. Instance Variable:-
  5. It is defined inside the class by outside of all methods.
  6. Scope of the variable is inside inter class, we can access this variable in only method of a class.
  7. Static Variable:-Static variable does not make by local variable, because the static only access to class variable. It is not to object memory.

Q.12. Difference between Print and Println Method ?

Ans –

Println method prints the String and Moves the cursor to a new line.

Print method prints just the string but does not move the cursor to a new line.

Q.13. Difference between this and final keyword?

Ans — Final is a non-access modifier applicable only to a variable, a method or a class. It is used in different contexts.Final variable –> To create constant variables.Final methods–> Prevent method overriding.Final Classes –> Prevent Inheritance.
When a variable is declared with final keyword, it’s value can’t be modified, essentially a constant.If you make any variable as final, you can not change the value of final variable(It will be constant).

We can not change the value of a final variable once it is initialized. If you make any method as final, you cannot override it which means even though a sub class can call the final method of parent class without any issues but it can not override it. Final method is inherited but you can not override it. If you make any class as final, you cannot extend it. We can’t declare a constructor as final because constructor is never inherited.

this keyword in Java is a reference variable in Java which refers to current object. It can be used to refer current class instance variable.Usage:1. It can be used to refer instance variable of current class. 2. It can be used to invoke or initiate current class constructor.3. It can be passed as an argument in the method call.4. It can be passed as an argument in constructor call.5. It can be used to return the current class instance.

Q.14. What is Abstraction in Java ?

Ans — It’s an OOP’s concept where we can hide implementation details of a method.

Abstract method:

A method for which its declaration is written in an abstract class and its definition is written in child class of an abstract class.

Abstract class:

A class which is declared using abstract keyword and it can contain abstract methods is called as abstract class.Abstract class can contain non-abstract methods also.It is not mandatory to have at least one abstract method in abstract class, there can be zero abstract methods in abstract class.Using Abstract class (0% — 100%) abstraction
Using interfaces (100%) abstraction

Limitations / drawbacks of abstract class:

1. Using abstract class, abstraction is not guaranteed to be 100%.

2. We can achieve 100% abstraction using abstract class, but it’s not guaranteed.

Reason: We can write non-abstract methods also in an abstract class. So, if we have even a single non-abstract method declared in an abstract class, then abstraction will fall below 100%.

Q.15. Can we create an Object of Abstract class?

Ans — NO.

Reason– We can’t create an object of an abstract class. If java allows user to create an object of an abstract class, then user can call abstract methods also for execution. In this scenario, compiler will not get the definition of an abstract method for execution. So, it will be a confusing situation for the compiler and our program execution will fail. So, to avoid this situation it is not allowed to create an object of an abstract class. (we can create main method, but it is of no use in abstract class as we can’t create object of the abstract class).

Q.16. What is Polymorphism in Java with example ?

Ans — Polymorphism is an OOP’s ie. Object Oriented Programming concept in Java. Poly means many. Polymorphism in Java means a single method will be used in different ways. In Java multiple methods with same name but difference in their count, type and sequence of parameters is called Polymorphism.

Polymorphism is of two types: 1. Method Overloading 2. Method Overriding

Read more about Polymorphism here.

Q. 17. What is Constructor in Java ?

Ans — Constructor in Java belongs to a Class. If you want to use properties of method before calling it, then Constructor is used. The sole purpose of Constructor is to initialize instance variables. Read more about Constructor here.

Q. 18. What is Interface in Java?

Ans — These are the commonly asked interview questions.

So Interface is also an Oop’s concept. In Java Interface is nothing but a blueprint of class. We can achieve 100% abstraction with an Interface. It contains only abstract methods. Here you will get to know more about Interfaces.

Q.19. What is super keyword in Java?

Ans — super keyword refers to Inheritance concept in Java. It is used to call methods and variables of parent class in its child class. Whenever we create an object of a subclass, it’s member variables and methods can be accessible, but for accessing parent class members and variables ‘super’ keyword is used. super keyword can be used to refer to super class constructor.

Q.20. What is difference between throw and throws in Java?

Ans — throw and throws clauses are related to Exception Handling in Java. throw is used when the programmer wants to throw an exception explicitly and wants to handle it using catch block. throws clause is used when the programmer does not want to handle the exception and throw it out of a method.

Q.21. What is Exception in Java?

Ans — Basically, An Exception in Java is a runtime error. An Exception is an error that can be handled. It means when an Exception happens, the programmer can do something to avoid any harm. But an error is an error that cannot be handled, it happens and the programmer cannot do anything. To know more about Exception check here.

Q.22. Which is the super class for every class ?

AnsObject class is the super class for all classes in Java which is in java.lang package.

Q.23. What is Collection Framework?

Ans — A Collection Framework is a class library to handle groups of objects which means store and manipulate the group of objects. Collection Framework is implemented in java.util package. To know more click here. It is a combination of Classes as ArrayList, Vector, Stack, and HashSet, etc. and Interfaces such as List, Queue, Set, etc.

Q.24. Can we store a primitive data type into a collection ?

Ans — No, Collections store only objects.

Q.25. What is the difference between Iterator and ListIterator ?

Ans — Iterator and ListIterator are use to retrieve elements from a collection. Iterator can retrieve the elements only in forward direction whereas ListIterator can retrieve the elements in forward and backward direction also.

Q.26. What is auto boxing in Java ?

Ans — Auto boxing is done in generic types. Converting a primitive data type into an object form automatically is called ‘Auto boxing’.

Q.27. What is HashMap ?

Ans — Maps stores elements in key, value pair format. If the key is given then it’s associated value can be achieved. Key must have unique value. HashMap is a Java defined class which implements Map Interface. HashMap stores data in key-value pair. It removes duplicate keys and values associated with duplicate keys automatically.

Syntax: class HashMap<K,V>

here, K is th key and V is the value related to key.

Q.28. What is ArrayList in Collection Framework ?

Ans — ArrayList is a Java Defined class which implements List interface. Sequence of a data which is added into ArrayList is maintained because Java assign index starting from zero to all data values.

ArrayList can store duplicate values. It is not synchronized.

Syntax: class ArrayList<E> where, E is type of element.

Q.29. Explain try, catch, finally in Java?

Ans — try, catch and finally keywords are related to Exception handling in Java. They are used to handle Exceptions in Java.

try block contains the set of statements or code of action to be performed. Exception can be occur here. try block always followed by catch block where exception can be caught. try block must be followed by catch block or finally block. finally block displays the statement independent on whether exception occurs or not.

Q.30. Difference between final and finally ?

Ansfinal keyword is a non-access modifier. It is applicable only to a variable, a method or a class. It has different meaning in different contexts.

final variable — It creates constant variables.

final method — It prevents method overriding.

final class — It doesn’t allow inheritance of classes means can’t extend the class.

finally is a reserved keyword in Java. finally keyword is related to Exception handling in Java in try-catch block. finally block is an exceptional block which is followed by try block. try block must be followed by catch or finally or both blocks. finally block contains the statements which are not dependent on whether an exception occurs or not. It displays by default. finally block is associated with try block, without using try we can’t use it.

--

--