Core-java-interview-questions
Java is one of the most widely used programming languages in top tech companies due to its reliability, scalability, and performance. It is a popular choice for building enterprise-level applications and backend systems.
7 min read

Java Interview questions for Freshers
1. Is Java Platform Independent?
Java is a platform-independent programming language because its source code is compiled into bytecode rather than machine-specific code. This bytecode can run on any operating system that has a compatible Java Virtual Machine (JVM) installed.
The javac compiler converts Java source code into platform-independent bytecode (.class file).
A JVM is available for different operating systems, allowing the same bytecode to run everywhere.
This enables Java to follow the principle "Write Once, Run Anywhere (WORA)."
2. What are the top Java Features?
Java is one the most famous and most used language in the real world, there are many features in Java that makes it better than any other language some of them are mentioned below:
Simple: Java is quite simple to understand and the syntax
Platform Independent: Java is platform independent means we can run the same program in any software and hardware and will get the same result.
Interpreted: Java is interpreted as well as a compiler-based language.
Robust: features like Garbage collection, exception handling, etc that make the language robust.
Object-Oriented: Java is an object-oriented language that supports the concepts of class, objects, four pillars of OOPS, etc.
Secured: As we can directly share an application with the user without sharing the actual program makes Java a secure language.
High Performance: faster than other traditional interpreted programming languages.
Dynamic: supports dynamic loading of classes and interfaces.
Distributed: feature of Java makes us able to access files by calling the methods from any machine connected.
Multithreaded: deal with multiple tasks at once by defining multiple threads
Architecture Neutral: it is not dependent on the architecture.
3. What is JVM?
The Java Virtual Machine (JVM) is a virtual machine that executes Java bytecode and provides a runtime environment for Java applications. It acts as a bridge between the Java program and the underlying operating system.
JVM loads, verifies, and executes Java bytecode (.class files).
It provides platform independence by allowing the same bytecode to run on different operating systems.
JVM also manages memory, garbage collection, and runtime execution.
4. What is JIT?
JIT stands for (Just-in-Time) compiler is a part of JRE(Java Runtime Environment), it is used for better performance of the Java applications during run-time. The use of JIT is mentioned in step by step process mentioned below:

JIT
Source code is compiled with javac to form bytecode
Bytecode is further passed on to JVM
JIT is a part of JVM, JIT is responsible for compiling bytecode into native machine code at run time.
The JIT compiler is enabled throughout, while it gets activated when a method is invoked. For a compiled method, the JVM directly calls the compiled code, instead of interpreting it.
As JVM calls the compiled code that increases the performance and speed of the execution.
5. What are Memory storages available with JVM?
JVM consists of a few memory storages as mentioned below:

Memory storages
Class(Method) Area: stores class-level data of every class such as the runtime constant pool, field, and method data, and the code for methods.
Heap: Objects are created or objects are stored. It is used to allocate memory to objects during run time.
Stack: stores data and partial results which will be needed while returning value for method and performing dynamic linking
Program Counter Register: stores the address of the Java virtual machine instruction currently being executed.
Native Method Stack: stores all the native methods used in the application.
6. What is a classloader?
Classloader is a JVM component responsible for loading Java classes into memory at runtime. It loads classes only when they are needed, improving memory efficiency and application performance.
Loads .class files into JVM memory dynamically.
Supports dynamic class loading during program execution.

classloader
7. Difference between JVM, JRE, and JDK.
JVM, JRE, and JDK are the three main components of the Java platform. Together, they provide the environment required to develop, compile, and run Java applications.

JVM (Java Virtual Machine): Executes Java bytecode and provides platform independence.
JRE (Java Runtime Environment): Contains JVM and libraries required to run Java applications.
JDK (Java Development Kit): Contains JRE along with development tools such as the compiler (javac) and debugger.
8. What are the differences between Java and C++?
Java and C++ are both object-oriented programming languages, but they differ in design, memory management, and platform dependency.
FeatureJavaC++Platform DependencyPlatform-independent (Write Once, Run Anywhere)Platform-dependentMemory ManagementAutomatic through Garbage CollectionManual using new and deleteMultiple InheritanceNot supported through classes (uses interfaces)Supported directlyPointersDoes not support explicit pointersSupports pointersCompilationCompiles to bytecode and runs on JVMCompiles directly to machine codeOperator OverloadingNot supported (except + for strings)Supported
9. Explain public static void main(String args[]) in Java.
The public static void main(String args[]) method is the entry point of a Java application. The JVM looks for this method and starts program execution from here.

public: Makes the method accessible from anywhere so that the JVM can invoke it.
static: Allows the method to be called without creating an object of the class.
void: Specifies that the method does not return any value.
main: The predefined method name that acts as the starting point of program execution.
String args[]: Stores command-line arguments passed to the Java program as an array of strings.
10. What is Java String Pool?
The Java String Pool (JSP) is a special memory area inside the heap where Java stores string literals. It helps save memory by reusing existing string objects instead of creating duplicate ones.
String literals are stored in the String Pool to optimize memory usage.
If a string already exists in the pool, Java returns the existing reference instead of creating a new object.

Java String Pool
Example:
plaintextString str1="Hello"; // "Hello" will be stored in String Pool // str1 will be stored in stack memory11. What will happen if we don't declare the main as static?
We can declare the main method without using static and without getting any errors. But, the main method will not be treated as the entry point to the application or the program.
12. What are Packages in Java?
Packages in Java can be defined as the grouping of related types of classes, interfaces, etc providing access to protection and namespace management.
13. Why Packages are used?
Packages are used in Java in order to prevent naming conflicts, control access, and make searching/locating and usage of classes, interfaces, etc easier.
14. What are the advantages of Packages in Java?
There are various advantages of defining packages in Java.
Packages avoid name clashes.
The Package provides easier access control.
We can also have the hidden classes that are not visible outside and are used by the package.
It is easier to locate the related classes.
15. How many types of packages are there in Java?
There are two types of packages in Java

User-defined packages
Build In packages
16. Explain different data types in Java.
There are 2 types of data types in Java as mentioned below:

Primitive Data Type: Primitive data are single values with no special capabilities. There are 8 primitive data types:
boolean: stores value true or false
byte: stores an 8-bit signed two's complement integer
char: stores a single 16-bit Unicode character
short: stores a 16-bit signed two’s complement integer
int: stores a 32-bit signed two’s complement integer
long: stores a 64-bit two’s complement integer
float: stores a single-precision 32-bit IEEE 754 floating-point
double: stores a double-precision 64-bit IEEE 754 floating-point
Non-Primitive Data Type: Reference Data types will contain a memory address of the variable's values because it is not able to directly store the values in the memory. Types of Non-Primitive are mentioned below:
String: Stores a sequence of characters.
Array: Stores multiple values of the same type.
Class: Blueprint for creating objects.
Object: Instance of a class.
Interface: Defines a contract that classes can implement.
17. When a byte datatype is used?
A byte is an 8-bit signed two-complement integer. The minimum value supported by bytes is -128 and 127 is the maximum value. It is used in conditions where we need to save memory and the limit of numbers needed is between -128 to 127.
18. Can we declare Pointer in Java?
No, Java doesn't provide the support of Pointer. As Java needed to be more secure because which feature of the pointer is not provided in Java.
19. What is the default value of byte datatype in Java?
The default value of the byte datatype in Java is 0.
20. What is the default value of float and double datatype in Java?
The default value of the float is 0.0f and of double is 0.0d in Java.



