Top 14 Java Interview Questions and Answers for freshers 2021

Technology

Written by:

908 Views

Java is a programming language that hits the top list of all the programming languages and has a huge number of applications like games, desktop applications, and mobile systems among many others, and no less in job opportunities. The following Java Interview Questions for Freshers is here to get you to familiarize yourself with the nature of the question you can experience during your interview.

Here is a list of 14 Important Java Interview Questions for freshers

1. What is Java?

Answer: Java was developed by James Gosling in June 1991 which is an object-oriented, high-level language that is platform-independent, high performance, and portable programming language.

2. What are the differences between C++ and Java?

Answer: Here is the important difference between C++ and Java:

C++Java
Platform dependent.Platform-independent.
C++ uses compiler onlyJava uses compiler and interpreter both
C++ supports operator overloading and method overloading.Java supports method overloading.
Doesn’t support documentation comments.Supports documentation comment.
Supports pointers.No concept of pointers.
Supports multiple inheritances.Doesn’t support multiple inheritances.

3. List the features of the Java Programming language.

Answer: Here is the features list of Java Programming language

  • Simple: Java is easy to learn. The syntax of Java is based on C++ which makes java easy to learn and write the program in it. 
  • Object-Oriented: Java is a pure object-oriented which allows us to maintain our code that incorporates both data and behavior.
  • Portable: Java can be executed on every machine which supports the read-once-write-anywhere approach. 
  • (.java) is converted to bytecode (.class) which makes the program run on every machine.
  • Platform Independent: Java is a platform-independent programming language that doesn’t depend upon the operating system to be executed.
  • Secured: Java doesn’t use explicit pointers and it has a concept of ByteCode and Exception handling which makes it more secured.
  • Robust: Java uses strong memory management and the concepts like Automatic garbage collection, Exception handling, etc. make it more robust.
  • High Performance: Java is a high-performance programming language because Java bytecode is “close” to native code. (NOTE: Jave is a bit slower than C++ Language.)
  • Multithreaded: Java is multithreaded as it doesn’t occupy memory for each thread and java can perform can many tasks at once by defining multiple threads. Threads are important for multi-media, Web applications, etc.
  • Dynamic: Java supports dynamic loading of classes i.e. classes are loaded on-demand.It also supports functions from its native languages, i.e., C and C++.
Also Read  How to Grow Your Business Sales with the Help of Python?

4. Mention some of the different access specifiers for Java classes?

Answer: Access specifiersrefer to the access scope in Java and they are said to be the keywords that are used before a class name.

  • Public: Field, method, class is accessible from anywhere.
  • Protected: Field and method can be accessed from the same class to where they belong, but not from outside.
  • Default: The class can be accessed from the same package only and not from an outside package.
  • Private: Can be accessed from the same class to where they belong.

5. Explain JDK, JRE, and JVM?
Answer: Here is the Difference between JDK, JRF and JVM

JDKJREJVM
Java Development Kit.Java Runtime Environment.Java Virtual Machine.
It is the tool to compile, document, and package Java programs.In JRE Java bytecode can be executed.It is an abstract machine in which java bytecode can be executed.
It contains JRE + development tools.JVM is the implementation that physically exists.JVM follows Specification, Implementation, and Runtime Instance.

6. Mention some of the differences between an Inner Class and a Sub-Class?

Answer: Here are the two major differences between an Inner Class and a Sub-Class

  • An Inner class is nested within another class that 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 called superclass as it inherits from another class that can access all public and protected methods.

7. What is the static keyword?

Answer: The static keyword is used with methods and to make it global so all the objects will be able to share the same variable. It can also be used with methods.

Also Read  Top 5 Biotechnology Innovations in 2021

you can also visit the SQL interview questions.

8. What are finally and finalize in Java?

Answer:

FactorFinallyFinalize
DefinitionFinally is used for Exception HandlingFinalize is used for Garbage Collection
ApplicationFinally the block is used along with a try and catches blockFinalize is used with objects which are no longer in use
FunctionFinally helps in cleaning up the resources used in the try block.Finalize helps in clean up activities before it is destroyed by the garbage collector
ExecutionExecutes right after the execution of try-catch blockIt executes just before an object is destroyed

9. What is Typecasting in Java?

Answer: Typecasting is when we assign a value of one primitive data type to a different primitive data type then these two data types might not be compatible with each other and needs conversion. Syntax: dataType variablename = (dataType) variableToConvert;

10. What is Inheritance?

Answer: Inheritance means one class can acquire another class. The existing class is known as the Superclass whereas the derived class is known as a subclass.


11. What is Encapsulation?
Answer: Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability.

Polymorphism means many forms when the same concept is applied in Java it defines as the ability to process objects of various types and classes through a single, uniform interface.

12. Difference between Array and Array List.

Answer: Here are the major difference between Array and Array List:-

ArrayArray List
At the time of array, declaration size is given.   String[] name = new String[2]Size may not be required. It changes the size dynamically.   ArrayList name = new ArrayList
The index is required.   name[1] = “Black”No index required.   name.add(“Black”)
The array is not type parameterizedArrayList in java 5.0 is parameterized.

13.  How to get the database server details in the Java program?

Also Read  The Cost-Effective Advantage: How Professional SEO Boosts Your Bottom Line?

Answer:

  • “ DatabaseMetaData”  can be used to get the database server details. When the database connection is created successfully, getMetaData() method can be called.
  • DatabaseMetaData metadata = con.getMetaData();
  • String dbProduct = metadata.getdatabaseProductName;

14. Name the different modules of the Spring framework?

Answer: There are different modules in the Spring framework:

  • Web module
  • Struts module
  • Servlet module
  • Core Container module
  • Aspect-Oriented Programming (AOP)
  • Application Context module
  • MVC framework module
  • JDBC abstraction and DAO module
  • OXM module
  • Expression Language module
  • Transaction module
  • Java Messaging Service (JMS) module
  • ORM integration module