Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java8 Interview Questions Object Oriented Programming in Java JVM Java Programming

Some of the frequently asked Java Interview Questions :

Question 1: What is the difference between Constructor and Method ?
1.1. Constructor: Constructor is used to construct or create an instance of a class.

Properties of a constructor:
1. Has the same name as Class name.
2. Used to create and initialize fields of a class
3. Same name as class name
4. Has no return type
5. Default constructor without any parameters is added automatically by the ciompiler to the class If no constructor is defined for the class.
6. If we add any parameterized constructor to the class then default constructor is not added automatically to the class by compiler, if we need a default constructor we have to add it explicitly.
7. Constructors can be overloaded
8. Can be private as well
9. Constructor can be kept in the scenarios like if a class contains all static members or we want to make the class Singleton.
10. Keyword ‘this’ in a constructor is used to call another constructor of the same class

Signature of a constructor :

 (,  , ….){
So Constructors will have an access modifier, name same as class name and can have multiple parameters.

1.2. Method : In comparison to constructors, methods are more generic. A method's basic task is to define a behaviour of the class of we can say is to provide java code for execution to perform some task depending on the method’s behaviour.

Properties of a method:
1. A method defines the behaviour of a class
2. Do not have the same name as class, can have different names
3. Has a return type
4. No default method is added by constructor.
5. Methods can be overloaded
6. Can be private as well
7. Keyword ‘this’ in a method is used to refer to the current object

Question 2 : Why does an abstract class in Java have a constructor ?
Answer: We cannot initiate an abstract class but abstract class can contain fields and usual flavour of methods apart from abstract methods.
Since the constructor of the super class is always invoked by the inheriting class with an implicit call to constructor so abstract classes also supposed to have a constructor as abstract classes are implicitly created to be inherited.
Also when abstract class is inherited by some concrete class and then through that concrete class we can call the abstract class constructor and can initiate the fields of the abstract class.

Question 3 : Is JVM Platform Independent ?
Answer: No. Every operating system has its own version of JVM.
JVM is a software that is machine dependent but makes the Java Platform Independent.

Question 4 : Why do we need a private constructor in Java ?Traversing Through a Collection
Answer: • In case of singleton object
• When we do not want the class to be extended, as while extending will get a compilation error.
• If all the methods of the class are static then also constructor can be kept private.

Question 5 : Traversing Collections/What are the ways to traverse over the collections in java ?
Answer: Follow the link - Traversing Through Collections .

Question 6 : When to use iterator to traverse the collections ?
Answer: Iterator should be used when we need to remove the element while traversing.
Iterator provides remove() method, which removes the last element from the collection that was returned by the next. It can be called only once after the next() has been called otherwise throws exception. Iterator is the only safe way to modify the collection during iteration. In any other case unexpected events can happen like concurrent modification exception.

Question 7 : How ArrayList is implemented internally ? Or How Arraylist works internally ?
Answer: For ArrayList internal implementation, Follow link - ArrayList Internal Implementation

Question 8 : How Vector is implemented internally ? Or How Vector works internally ?
Answer: For Vector internal implementation, Follow link - Vector Internal Implementation

Question 9 : How HashSet is implemented internally ? Or How HashSet works internally ?
Answer: For HashSet internal implementation, Follow link - HashSet Internal Implementation

Question 10 : How HashMap is implemented internally ? Or How HashMap works internally ?
Answer: For HashMap internal implementation, Follow link - HashMap Internal Implementation

Question 11 : How LinkedHashMap is implemented internally ? Or How LinkedHashMap works internally ?
Answer: For LinkedHashMap internal implementation, Follow link - LinkedHashMap Internal Implementation

Question 12 : How ConcurrentHashMap is implemented internally ? Or How ConcurrentHashMap works internally ?
Answer: For ConcurrentHashMap internal implementation, Follow link - ConcurrentHashMap Internal Implementation

Question 13 : What is a lambda expression in Java 8 ?
Answer: To know about lambda, Follow link - Lambda Expression

Question 14 : What is a functional interface in Java 8 ?
Answer: To know about functional interfaces, Follow link - Java Functional Interfaces

Question 15 : What is streams/parallel stream in Java 8 ?
Answer: To know about Java Streams, Follow link - Java Streams APIs

Question 16 : What is CompletionStage in Java 8 ?
Answer: To know about CompletionStage, Follow link - CompletionStage

Question 17 : What is CompletableFuture in Java 8 ?
Answer: To know about CompletableFuture, Follow link - CompletableFuture

Question 17 : What is immutability and how to create immutable classes ?
Answer: To know about immutability and how to create immutable classes, Follow link - CompletableFuture

Question 18 : How to create immutable classes ?
Answer: To know on how to create immutabe classes, Follow link - CompletableFuture

Top Java Interview Quenstions.