25+ Core Java Interview Questions And Answers

Most Frequently asked Java Interview Questions:

Given below is a comprehensive list of most important and commonly asked basic and advanced Java programming interview questions with detailed answers.

Q1. What is the difference between an Inner Class and a Sub-Class?

Ans: An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.

A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.

Q2. What is JAVA?

Ans: Java is a high-level programming language and is platform independent.

Java is a collection of objects. It was developed by Sun Microsystems. There are a lot of applications, websites and Games that are developed using Java.

Q3. What are the features in JAVA?

Ans: Features of Java:

  • Simple - learning, and practicing java is easy because of resemblance with c and C++
  • Object Oriented Programming Language - Unlike C++, Java is purely OOP.Java is a fully object-oriented programming language. It has all OOP features such as abstraction, encapsulation, inheritance and polymorphism.
  • Distributed - Java is designed for use on network it has an extensive library which works in agreement with TCP/IP.
  • Secure - Java is designed for use on internet. Java enables the construction of virus-free, tamper free systems.
  • Robust (Strong/ Powerful - Java programs will not crash because of its exception handling and its memory management features.
  • Interpreted - Java promms are compiled to generate the byte code. this byte code can be downloaded and interpreted by the interpreter, .Class file will have byte code instructions an JVM which contains an interpreter will execute the byte code.
  • Portable - Java does not have implementation dependent aspects and it yields or gives same result on any machine.
  • Architectural Neutral Language- Java byte code is not machine dependent, it can run on any machine with any processor and with any OS.
  • High Performance - Along with interpreter there will be it (just in time) compiler which enhances the speed of execution.
  • Multithreaded - Executing different parts of program simultaneously is called multithreading. this is an essential feature to design server side programs.
  • Dynamic - We can develop programs in java which dynamically change on internet (e.g. applets).

Q4. What are the various access specifiers for Java classes?

Ans: In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:

Public : Class,Method,Field is accessible from anywhere.

Protected :Method,Field can be accessed from the same class to which they belong or from the sub-classes,and from the class of same package,but not from outside.

Default : Method,Field,class can be accessed only from the same package and not from outside of it's native package.

Private : Method,Field can be accessed from the same class to which they belong.

Q5. What Is Multi-Threading?

Ans: Multi-threading is a programming concept used to run multiple tasks in a concurrent manner within a single program.

Q6. What Is the Difference Between Static and Dynamic Loading?

Ans: Static class loading involves the creation of objects and instances using new keywords, and dynamic class loading is done when the name of the class is not known at compile time.

Q7. What Is the Difference Between Overloading and Overriding?

Ans: Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

Here are some important facts about Overriding and Overloading:

1. The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime. In contrast, reference type determines which overloaded method will be used at compile time.

2. Polymorphism applies to overriding, not to overloading.

3. Overriding is a run-time concept while overloading is a compile-time concept.

Q8. Why is String immutable in Java?

String is immutable in Java since Java designer thought that String will be greatly used, making it immutable. It lets some optimization easy sharing, and same String object between multiple clients.

A key step in that direction was the idea of putting away String literals in String pool. The aim was to moderate temporary String object by sharing them and in order to share, they must have to be from immutable class.It is worth noting, that it isn’t possible to share a mutable project with two parties which are unfamiliar to each other.

Q9. What's the purpose of Static methods and static variables?

Ans: When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.

Q10. What is the difference between double and float variables in Java?

Ans: In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.

Q11. What Is a Package?

Ans: A package is a collection of related classes and interfaces.

Q12. What is the difference between StringBuffer and String?

Ans: String is an Immutable class, i.e. you can not modify its content once created. While StringBuffer is a mutable class, means you can change its content later. Whenever we alter content of String object, it creates a new string and refer to that,it does not modify the existing one. This is the reason that the performance with StringBuffer is better than with String.

Q13. Difference in Set and List interface?

Ans: Set and List both are child interface of Collection interface. There are following two main differences between them

  • List can hold duplicate values but Set doesn’t allow this.
  • In List interface data is present in the order you inserted but in the case of Set insertion order is not preserved.

Q14. What are the Java IDE’s?

Ans: Eclipse and NetBeans are the IDE's of JAVA.

Q15. How does Java enable high performance?

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

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

Ans: 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.

Q17. What is an Object?

Ans: 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.

Q18. 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 Super class whereas the derived class is known as a sub class.

Q19. What is Encapsulation?

Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability.

Q20. What is Polymorphism?

Polymorphism means many forms.

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

Q21. How an object is serialized in java?

Ans: In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream.

Q22. When we should use serialization?

Ans: Serialization is used when data needs to be transmitted over the network. Using serialization, object's state is saved and converted into byte stream .The byte stream is transferred over the network and the object is re-created at destination.

Q23. Can we override static methods of a class?

Ans: We cannot override static methods. Static methods belong to a class and not to individual objects and are resolved at the time of compilation (not at runtime).Even if we try to override static method,we will not get an complitaion error,nor the impact of overriding when running the code.

Q24. What's the difference between an array and Vector?

Ans: An array groups data of same primitive type and is static in nature while vectors are dynamic in nature and can hold data of different data types.

Q25. How garbage collection is done in Java?

Ans: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically. For automatic garbage collection java calls either System.gc() method or Runtime.gc() method.

Q26. What's the benefit of using inheritance?

Ans: Key benefit of using inheritance is reusability of code as inheritance enables sub-classes to reuse the code of its super class. Polymorphism (Extensibility ) is another great benefit which allow new functionality to be introduced without effecting existing derived classes.

Q27. Give an example of use of Pointers in Java class.

There are no pointers in Java. So we can't use concept of pointers in Java.

Q28. What's difference between Stack and Queue?

Ans: Stack and Queue both are used as placeholder for a collection of data. The primary difference between a stack and a queue is that stack is based on Last in First out (LIFO) principle while a queue is based on FIFO (First In First Out) principle.

Q29. How objects are stored in Java?

Ans: In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects.

Q30. Why multiple inheritance is not supported in java?

Ans: Java supports multiple inheritance but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritance is to avoid the conflict and complexity arises due to it and keep Java a Simple Object Oriented Language. If we recall this in C++, there is a special case of multiple inheritance (diamond problem) where you have a multiple inheritance with two classes which have methods in conflicts. So, Java developers decided to avoid such conflicts and didn’t allow multiple inheritance through classes at all.

Get The Coaching Training Today!