[**
Java Interview Questions for Freshers
**](https://bit.ly/43FAd5j)
If you are planning to learn Core Java or any interview is scheduled in the coming days. As core java plays an important role in any Java interview, Java interview questions for freshers help you to prepare for Java-based interviews. In any Java-based interview, core Java is the favorite area for most interviewers and always considered to be the deciding factor of the interview.
- What is aggregation in Java?
Answer: Aggregation is best defined as the entity reference where it represents the relationship between two classes where the aggregate class contains a reference to the class which it owns. Aggregation represents a has-a and whole/part relationship.
For example consider an aggregate class Employee stores information such as name, age, salary, and the Address class stores information such as city, state, and pin-code. Now, if the Employee class is defined to contain an Address object then it will be said that the Employee object has-a Address object. The Address object also makes up part-of Employee object – there is no employee without any address to live. Therefore, the Employee object owns the Address object.
- Explain the JVM architecture?
Answer: Java Virtual Machine is the abstract machine or specification that provides a runtime environment to execute the bytecode. JVM supports Java and many other languages known as JVM languages, the program written in these languages is compiled into the bytecode and then executed by the JVM. contains key components which are classloader, memory area, execution engine etc.
a) Classloader
It is a subsystem of JVM which load class files. Whenever a Java program is run, it is loaded by the classloader.
b) Class Area
Class Area holds class-level data of each class file such as metadata, constant run pool, and static variables.
c) Heap
It is the runtime data which is used for allocating objects.
d) Stack
The stack is used for storing temporary variable. This component has a stack frame which is allocated one frame to each thread and when the execution of the thread is completed then that frame is also gets destroyed.
e) Registers
This component contains the address of JVM instruction which currently being executed.
f) Native Method Stack
All the native method stack used in the application are stored in this.
g) Execution Engine
It contains:
A virtual processor
An interpreter that executes the instructions after reading the bytecode.
JIT compiler, used for improving the performance due to the slow execution. It compiles the similar part of the bytecode at the same time which reduces the total time needed for compilation.
h) Java Native Interface
It provides an interface which is needed for communicating with another application developed in another language like C, C++, C# etc.
- What is the use of Classloader in Java?
Answer: A Java program is made up of a different number of custom classes and pre-defined classes. When a program is executed, JVM is used to load all the content of that needed class and through the use of Classloader JVM, it finds that class.
There are three types of Classloaders:
System Class Loader
It loads all the classes from the classpath.
Extension ClassLoader
It loads all the classes from the extension directory.
Bootstrap Class Loader
It loads all the pre-defined java classes.
- Which class is a superclass of all classes?
Answer: Java.lang.The object is the root class for all the java classes and we don’t need to extend it. Every other java classes fall back under the object. All the different non-primitive types including arrays are inherited directly or indirectly from this class.
- What is the static keyword?
Answer: The static keyword is used with a class level variable to make it global so all the objects will be able to share the same variable. It can also be used with methods. A static method can access only static variables of the class and invoke only a static method of the class.
The interview generally asks this question in the Java interview questions for freshers. Even if you are a fresher, you should have a good knowledge of the keywords in Java.
- What are finally and finalize in Java?
Answer: Finally block is used with a try-catch block to put the code that you always want to get executed even the execution is thrown by the try-catch block. Finally is just used for releasing the resources which were created by the try block.
Finalize() is a special method in Object class that we can override in our classes. Finalize() is called by the Garbage collector to collect the garbage value when the object is getting it. This method is generally overridden to release the system resources when garbage value is collected from the object.
- What is Type casting in Java?
Answer: Casting in Java is one of the top topics from where you can get questions in your interview. When we assign a value of one data type to a different data type then these two data types might not be compatible with each other and needs conversion. If data types are compatible with each other like, in case of the conversion of int value to long then automatic conversion is done by Java and doesn’t require typecasting. But if data types are not compatible with each other then they need to be cast for conversion.
Syntax
dataType variablename = (dataType) variableToConvert;
Also Read: Top 5 Java Frameworks
- What is the inner and anonymous inner class?
Answer: In Java, we can define a class inside a class and they are called nested classes. Any nested class which is non-static are known as inner class. Inner classes are associated with objects of the class and they can access all the variables and methods of the outer class.
Any local inner class without any name is known as an anonymous inner class. It is defined and instantiated in a single statement. Anonymous inner class always extend a class or implement an interface. Since an anonymous inner class doesn’t have any name, it is not possible to create its constructor.
- What is break and continue statement?
Answer: In a while or do-while loop, we use break for a statement to terminate the loop. We use a break statement in a switch statement to exit the switch case. We can also use break statement for terminating the nested loop.
The continue statement is used for skipping the current iteration of a for, while or do-while loop. We can use the break statement with a label to skip the current iteration of the outermost loop.
The most basic programming question, not only related to the Java. If you have some knowledge of programming languages, you should know the answer to this question as it is among frequently asked Java interview questions for freshers.
- What is an interface?
Answer: Interfaces are the core part of Java programming language used a lot in JDK, java design patterns, and most of the frameworks and tools. The interface provides a way to achieve abstraction in Java and used to define the contract for the subclasses to implement.
The interface is a good starting point to define the type and create a top-level hierarchy in your code. In Java, a class can implement multiple interfaces, it’s better to use interfaces as a superclass in most of the cases.
Read More: https://bit.ly/43FAd5j
Top comments (0)