Here are 10 essential Java interview questions tailored for freshers:
- Is Java Platform Independent? If so, how?
- Yes, Java is a Platform Independent language. The javac compiler compiles Java programs into bytecode (
.class
files). This bytecode is independent of the underlying software or hardware. To execute it, you need a Java Virtual Machine (JVM) installed on the operating system. Despite JVM being platform-dependent, the bytecode can be created on any system and executed on any other system, making Java truly platform independent.
- Yes, Java is a Platform Independent language. The javac compiler compiles Java programs into bytecode (
- What are the top features of Java?
- Java boasts several features that set it apart:
- Simplicity: Java is easy to understand and has a straightforward syntax.
- Platform Independence: You can run the same program on different software and hardware, yielding consistent results.
- Interpreted and Compiled: Java combines both interpreted and compiled approaches.
- Robustness: Features like garbage collection and exception handling enhance its robustness.
- Object-Oriented: Java supports classes, objects, and the four pillars of OOP.
- Security: Applications can be shared without revealing the actual program.
- High Performance: Faster than traditional interpreted languages.
- Dynamic Loading: Supports dynamic loading of classes and interfaces.
- Distributed: Allows method calls across connected machines.
- Multithreaded: Handles multiple tasks concurrently.
- Architecture Neutral: Not dependent on specific architectures.
- Java boasts several features that set it apart:
- What is the JVM (Java Virtual Machine)?
- The JVM acts as a runtime engine for executing Java applications. It loads, verifies, and executes the bytecode generated by the Java compiler. Although the JVM software varies across different operating systems, it plays a crucial role in achieving Java’s platform independence.
- Why is Java not a pure object-oriented language?
- Java supports primitive data types (such as
int
,float
, etc.), which are not objects. Therefore, it is not considered a pure object-oriented language
- Java supports primitive data types (such as
- What is the difference between shallow copy and deep copy in Java?
- Shallow Copy: In a shallow copy, a new object is created, but the contents (fields) of the original object are copied by reference. Changes made to the fields of the copied object affect the original object and vice versa.
- Deep Copy: In a deep copy, a new object is created, and all the fields are copied recursively.
- What are constructors in Java?
- A constructor is a section of code that is used to initialize an object in Java. It has to be given the same name as the class. Additionally, it is automatically invoked when an object is created and has no return type.
- The Java compiler automatically produces a no-argument constructor, often known as the default constructor, if a class does not explicitly define any.
- What’s the purpose of Static methods and static variables?
- We utilize static keywords to make a method or variable shared for all objects when it is necessary to share them across many objects of a class rather than create separate copies for each object.
- Static variable: Another name for a static variable is a class variable.
- What is the Inheritance?
- Inheritance is the mechanism using which one object can inherit all of the characteristics and actions of another object belonging to a different class. It is utilized for code reusability and method overriding.
- What are the various access specifiers in Java?
- In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
- Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
- Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
- Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
- Private The private class, methods, or variables defined as private can be accessed within the class only.
- What is aggregation?
- Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship.
- For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code. In other words, we can say that Employee (class) has an object of Address class.
- What is method overloading?
- Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways.
- By Changing the number of arguments
- By Changing the data type of arguments
- Method overloading increases the readability of the program. Method overloading is performed to figure out the program quickly.