Skip to main content

Core Java Interview Q&A List

In this blog, I am going to list most of the vital core java interview questions. The core java interview questions are categorized in the Basics of features, oops, string, Collections, Exceptions, etc.

Interview Questions and Answer 

1). What is Java?
  • is a general-purpose, object-oriented programming language developed by Sun Microsystems of the USA in 1991.
  • was designed for the development of software consumer electronic devices like TVs, VCRs, and other electronic machines.
  • is a high-level programming language and is platform-independent
2). Describe the features of JAVA
  • Compiled and Interpreted 
  • Platform Independent and portable
  • Object-oriented
  • Robust and Secure
  • Multithreaded and Interactive
  • High Performance
3). Is JAVA an Object-oriented based Programming language? Describe it.
  • Yes, JAVA is an Object-Oriented based Programming language
  • OOPs concepts are given below 
    • Object
      • It is a basic unit of Object-Oriented Programming and represents real-life entities like a pen, fruit, table, computer, etc.
      • For Instance, Graphics Designer may have objects such as 'Circle', 'Menu', etc.
      • Another Instance, While doing shopping on an e-commerce website, might have some objects such as 'Cart', 'Product', 'Wallet', etc.
    • Class
      • A Class is a user-defined layout from which objects are created.
      • Set of Properties or methods.
    • Inheritance
      • is the mechanism where everything is an object that interacts with one another.
      • It helps to reuse the code and establish a relationship between different classes. 
    • Polymorphism
      • It means many forms.
      • 'Poly' = many and 'morphs' =  forms.
      • Two Types: Compile-time and Runtime 
    • Abstraction
      • is a process of hiding actual implementation details and a user can access functionality only.
      • For Instance, a man is driving a car. The man has known only the functionality of the car. So He is pushing the command only like increase the speed, apply the brake, etc. He really doesn't know about the inner mechanism of the car or the implementation of accelerators, brakes, etc in the car. 
    • Encapsulation
      • is a process of wrapping code and data in a single unit.
      • For Instance, a capsule is mixed with medicines.
      • Java Bean class is the best instance of fully encapsulated.
4). Difference between Java and C
Java C
Doesn't include a unique statement It used a unique statement like sizeof, typeof
Doesn't contain data type like struct, union It used struct, union
Doesn't define the type modifiers keywords auto, extern, register, signed, and unsigned we can define the type modifiers keywords auto, extern, register, signed, and unsigned
Doesn't support an explicit pointer type It does support.
Doesn't have a preprocessor like '#define', '#include' etc. It requires to use of a preprocessor before compiling the program.
 

5). Difference between Java and C++ 

Comparison C++ Java
Platform-independent No Yes
Inheritance Yes Yes but Java doesn't support multiple inheritances. This is accomplished using a new feature called 'interface'
Thread Support No, but it relies on third-party libraries Yes
Global Variables Yes No
Pointers Yes No
 

6). Difference Between Java and Python

Comparison JAVA Python
Interpreted Language Support   Yes, It is a complied + interpreted language.  It is an interpreted language.
Code It has long lines of code. It has fewer lines of code
Inheritance Yes but It doesn't support multiple Inheritance Yes, Pythons supports both single and multiple Inheritance
Portable It can run on any devices which can run with JVM  It needs an interpreter installed on the target machine to translate code. So Python is less portable.

7). Write a 'Hello, World!' example in Java and explain it.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}
 
  • public
    • The Keyword public is an access modifier that declares the main method as unprotected and therefore making it accessible to all other classes. 
  • static
    • It is a keyword in java that identifies it as class-based. 
    • main() is made static in Java so that it can be accessed without creating the instance of a Class. 
    • In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class. 
  • void
    • It is the return type of the method. 
    • Void is a keyword and 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.
  • System.out.println('Hello, World!')
    • It will print 'Hello, World!' to the console.
    • 'System' is a class.
    • 'out' is an object of PrintStream class.
    • 'println()' is method of PrintSteam class.

8). Explain about 'final' keyword 

  • It is the keyword in java and is used as a non-access modifier. 
  • It can apply to a variable, method & class.
  • Final Variable:  Its value can be changed once assigned.
  • Final Method: When a method is declared final then it can't be overridden by the inheriting class.
  • Final Class: It can't be extended by another class.
9). Types of Constructors
  • Three types of constructors
    • Default
    • No-args
    • Parameterized
  • any contractor can't be final.
  • Default
    • If you don't implement any constructor in your class, java compile inserts a default constructor.
    • This constructor is known as the default constructor.
    • It will be added during  compile-time and exists in the .class file 
  • No-Args 
    • A constructor with no arguments is known as the No-args Constructor.
    • The signature is the same as the default constructor but the body can have some code implementation, unlike the default Constructor where the body is empty.
  • Parameterized Constructor
    • Constructor with arguments is known as Parameterized Constructor.
    • It is used to assign a user-wanted specific value to the instance variables of different objects. 
    • It is written explicitly by the developer.
10). Is Java a pure Object-oriented based programming language?
  • No, Java isn't a pure Object-oriented based programming language because Java has predefined primitive data types such as boolean, byte, char, int, float, double, long, short which aren't objects.
  • For primitive data types, Java provides Wrapper classes to convert into objects.
11). What are wrapper classes in Java?
  • Wrapper classes convert the Java primitive data types into reference objects.
  • Every Primitive data type has a class dedicated to it like 'boolean'-> Boolean class, 'int' -> Integer class, etc.
12). Explain String class in JAVA.
  • String class is immutable in JAVA.So it's suitable to use in a multi-threaded environment.
  • It represents a sequence of characters in UTF-16 format.
  • When we initialize a new String, JVM looks for the string with the value in the JVM String Pool. If it finds a value, then it will return the reference otherwise will create a string object and place the object in the String pool.
13). Difference between String, String Buffer, String Builder 

Comparison String String Builder String Buffer
Mutability   immutable Mutable Mutable
Thread-Safe Yes No Yes
Performance Fast  More Efficient  Less Efficient

14). Access modifiers in Java 
  • Four access modifiers in Java 
    • private
      • It is specified by using the private keyword.
      • The methods or data members declared as private are accessible only within the class in which they are declared. 
    • default
      • When no access modifier is specified for a class, a variable, a method - It is said to be having the default access modifier by default.
      • It is accessible only within the same package.
    • protected
      • It is specified by using the protected keyword
      • The methods or data members declared as private are accessible only within the same packages or subclasses in different packages.
    • public
      • It is specified by using the public keyword.
      • Class, methods, data members as the public are accessible from everywhere in the program. 
Comparison Default Private Protected Public
Same Class Yes Yes Yes Yes
Same Package sub-class Yes No Yes Yes
Same Package non-sub-class Yes No Yes Yes
Different Package sub-class No No Yes Yes
Different Package non-sub-class No No Yes Yes

15). What is Polymorphism? and explain the types of Polymorphism.
  • It means many forms.
  • 'Poly' = many and 'morphs' =  forms.
  • Two Types
    • Compile-time Polymorphism
    • Runtime Polymorphism
  •  Compile-time polymorphism is method overloading whereas Runtime Polymorphism is method overriding.
16). What do you mean by Compile-time Polymorphism?
  • It is also known as static binding.
  • It is a method of overloading where the same name but a different signature. 
  •  We can implement two ways
    • By changing the numbers of arguments
    • By changing the return type.
17). What do you mean by Runtime Polymorphism?
  • It is also known as Dynamic Method dispatch. 
  • It is method overriding which is implemented by using Interface and Inheritance. 
  • In this process,  an overridden method is called through the reference variable of the superclass.
  • The method must have the same name & same parameter list as in the parent class.
  • There must be an IS-A relationship (inheritance).
18). What do you mean by Abstraction?
  • It is a process of hiding the implementation details from the user and revealing only the functionality to them. 
  • 'abstract' is a keyword.
  • A class that is declared as abstract is known as an abstract class.
  • It can have abstract and non-abstract methods.
  • It can have a constructor and static- method also.
19). What do you mean by Interface?
  • An interface can have methods and variables.
  • "interface" is a keyword.
  • An interface doesn't contain any constructor.
  • All of the methods in an interface are abstract.
  • An Interface can extend multiple interfaces.
  • An Interface can not be extended by class, it is implemented by a class only.
20). What is the difference between abstract and interface?
Abstract Class Interface
An abstract class can have final, non-final, static, non-static variables. The interface has only static and final variables.
Abstract class doesn't support multiple inheritances. The Interface support multiple inheritances.
An abstract class can provide the implementation of the interface. The interface can not provide the implementation of the abstract class.
An abstract class can extend another class and implement multiple JAVA interfaces. An Interface can extend another JAVA interface only.
An abstract class can be extended using the keyword "extends". An interface can be implemented using the keyword "implements".

21).  What is inheritance?
  • It is a process to inherit the properties (Data member) and functionalities(methods) from one class to another class.
  • It allows us to reuse a code, it improves reusability in your application.
  • Parent Class:
    • The class whose properties and functionalities are used by another class is known as a parent class, superclass, or base class.
  • Child Class:
    • The class that extends the features of another class is known as child class, subclass, or derived class.
  • extends is the keyword used to inherit the properties of a class.
  • If you don't want other classes to inherit from a class, use the final keyword
22).  What are Method Overloading and Method Overriding?
  • Method overloading
    • It allows different methods to have the same name but different signatures where signatures can differ by the number of input parameters or type of input parameters or both.   
    • It can be done by changing the number of arguments or by changing the data type of arguments.
    • In addition, If two or more methods have the same name and the same parameter list but differ in the return type can not be overloaded. 
    • It is a compile-time polymorphism.
  • Method overriding
    • It is a process of overriding the base class method by a derived class with a more specific definition.
    • It is performed between two classes inheritance relation.
    • In this case, the methods of both classes must have the same name and same parameters.
    • It is runtime polymorphism.
    • private, static, and final methods can not be overridden.
23). What is the difference between Heap and Stack Memory in Java?
Features Stack Heap
Application It stores items that have a short life such as methods, variables, and reference variables of the objects. It stores objects and Java Runtime Environment(JRE) classes.
Ordering It follows the LIFO order. It does not follow any order because it is a dynamic memory allocation and does not have any fixed pattern for allocation and deallocation of memory blocks.
Efficiency  It has faster access, allocation, and deallocation. It has slower access, allocation, and deallocation.
Java Options Used We can increase the stack size by using the JVM option -Xss. We can increase or decrease the heap memory size by using the -Xmx and -Xms JVM options.
Exception Throws JVM throws the java. lang.StackOverFlowError if the stack size is greater than the limit. To avoid this error, increase the stack size. JVM throws the java. lang.OutOfMemoryError if the JVM is unable to create a new native method.


Popular posts from this blog

Java interview questions and answers for experienced (Core Java, Spring Boot, Hibernate, JPA)

 Here is a list of interview questions for the experienced guy who had more than 5 years of experience in Java, Spring Boot, Spring MVC, Hibernate, JPA. Please follow the questions bank for it.   @component vs @service vs @repository @Component: It is to mark a bean as a Spring-managed component. It is a generic stereotype for any spring-managed component. Spring will only pick up and register beans with @component and doesn't look for @service and @Repository @service:  The @Service annotation represents that our bean holds some business logic. @Repository: @Repository is a stereotype for the persistence layer and it is also a type of component. Its job is to catch all persistence-related exceptions and rethrow them as Spring DataAccessExceptions. Explain spring Bean Lifecycle A Spring bean needs to be instantiated when the container starts, based on JAVA or XML bean definition. ...

Replace Email Template Using Java Pattern and Matcher Class

Let's say you have some simple email template that contains details like email address, phone/mobile or activation link, etc and you want to generate an email template without any free marker dependency using Java Pattern and Matcher class. So let's try to make that and also download a link is given bottom side. Step 1:    Here I created one email template which HTML code looks below. Hi {{user.first_name}} {{user.last_name}}, Welcome and thank you for subscribing. Now that you’ve subscribed, you’ll be receiving our emails {{freq.duration}}. These emails will contain our most recent article or news. You will also receive exclusive offers, promotions, and information on events. Our team is happy to have you on board. If you have any questions or comments, please don’t hesitate to contact us. You can simply reply to our emails or contact me directly using the contact information below. Thanks again! Step 2:  As you can see...

Java Collections Interview Q&A List

 The collection framework is the most important concept in java programming. It is necessary that you should have strong knowledge of the Collection framework. Through this article, I will share the topmost interview question and answers that will definitely help you in clearing your interview with flying colors. 1). What is the collection in java? List down interfaces and classes. The Collection is a set framework of interfaces and classes that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on data such as searching, sorting, insertion, manipulation, and deletion. 2).  What are the advantages of the Collection framework? Feature Description Performance The collection framework provides highly effective and efficient data structures that result in enhancing the speed and accuracy of a program. Maintainability The code developed ...