Java was first released in 1995 yet still remains to be one of the most popular programming languages. The PYPL PopularitY of Programming Language Index done in February 2022 ranked Java language the second most popular language in the world. Java language is today used by at least 10 million developers in the world who develop applications for more than 15 billion devices that support Java. Java language also supports the latest technologies like big data, IoT, machine learning, and more. Java is among the top ten most demanded languages in 2022 among the 200+ programming languages in the world today. Thus, knowledge of Java Basics is a valuable skill for any programmer or developer, especially those aspiring to go into full-stack development.
Java interview questions for freshers
An interview for a developer or programmer role depends on the job level, i.e., entry-level, mid-level, and senior-level. Also, interview questions may depend on the level of experience a recruiter is looking for. When preparing for an entry-level role, the Java basics for interview should cover the following fundamentals:
- General Java language fundamentals
- Data types
- OOP concepts
- Variables
- Operators
- Methods, methods overriding, methods overloading
- Arrays
- Classes and class inheritance
- Multithreading, concurrency, and thread basics
- Strings
- Collections framework, interface, classes, and collection algorithms
Here are some common Java interview questions for freshers.
- Outline core Java features
The following are core features of the Java language
- It is an object-oriented programming language
- Java is platform-independent and operates on the WORA (write once run anywhere) principle
- Java supports multithreading allowing multiple executions of tasks simultaneously
- Java syntax is simple, clean, and easy to understand
- Java is a highly secure language
- It is a distributed language and supports the development of distributed applications
- What is object-oriented programming?
Object-oriented programming (OOP) is a programming approach that organizes programs or software design around objects rather than functions and logic. This approach is ideal for writing and managing large complex codes that require frequent updating and maintenance.
- What are the principles of object-oriented programming?
Object-oriented programming is founded on the following core principles:
- Abstraction is a process by which the implementation details of an object are hidden from the end-user, and only the functionality, i.e., the properties (attributes), behavior (methods), and interfaces, are exposed.
- Encapsulation is a mechanism that wraps up data and code as a single unit.
- Polymorphism is the ability of an object, variable, or function to take multiple forms.
- Inheritance is where one class acquires the properties of another class
- What is an object in Java? How is it created?
An object is an instance of a Java class. In other words, a real-world entity created from a Java class and has an identity, state, and behavior. A class is considered an object constructor or a template, blueprint, or guideline for creating objects. An object is created using the ‘new’ keyword
- Is Java 100% object-oriented?
No. Java is not a pure object-oriented language. This is because it uses 8 primitive data types, including float, byte, double, short, long, boolean, int, and char which are not objects.
- What is the difference between JDK, JRE, and JVM in Java?
- JDK, Java Development Kit, is a development environment that includes development tools like compilers and debuggers and test programs for developing Java applications, components, applets, and others using the Java language.
- JRE, Java runtime environment in which Java bytecode can be executed. It comprises the Java Virtual Machine (JVM), class libraries, and class loader.
- JVM, Java virtual machine, is the platform component that loads, verifies, and executes bytecodes.
- What is inheritance in Java?
Inheritance in Java is the concept where the properties and methods of one class can be inherited by another class. This is important as it facilitates the reuse of code and establishes a relationship between different classes. A class that is derived from another class is known as a subclass. Also, a class that is derived from a subclass is known as a superclass.
The class from which properties and methods are derived is known as a parent class, and the class that inherits the same is known as the child class. Thus a parent class can be a base class or superclass, while a child class can be a subclass or a superclass.
- Explain different types of inheritance in Java
Four types of inheritance exist in Java. These are
- Single inheritance is where only one class inherits the properties and methods of the other thus, only one parent and one child class exists in this type of inheritance.
- Multilevel inheritance is when a class inherits the methods and properties of another class which in turn has its properties and methods inherited by another class. Therefore, this class contains multiple parent and child classes at different levels.
- Hierarchical inheritance is where more than one class inherits the methods and properties of one class. In this case, there will be multiple child classes inheriting properties from one parent class.
- Hybrid inheritance, as the name suggests, combines two or more types of inheritance.
- What is multiple inheritance and does Java support this type of inheritance?
Multiple inheritance is where one child class inherits the properties and methods of multiple classes. Java classes do not support multiple inheritance but can acquire multiple inheritance from other languages like C++ and Python through an interface.
Java does not support multiple inheritance as this creates ambiguity where multiple parent classes share the same method name. This is referred to as the diamond problem.
- What is a constructor?
A constructor is a special method of a class in OOP that is used to initialize a newly created object of the same name as the class. Whenever an object of a class is created, a constructor is called automatically.
- Differentiate between constructor and method
A constructor is a special method of a class that is used to initialize newly created objects of the same name as the class and no return type.
A method, also known as a function, is used to implement specific behavior of a class. Unlike the constructor, a method has its own name and return type. Thus while the constructor is called during the initialization of an object, a method is called to implement its functionality.
- Explain constructor chaining in Java
In Java, constructor chaining is the process where a constructor of a class is called from inside another constructor. Constructor chaining can work with multiple classes but basically follows the inheritance process.
Constructor chaining can happen in two ways:
- Within the same class, using the () keyword
- From the base class using super()
- What is an array? Can you declare an array without an array size?
An array refers to a collection of similar data types.
It is not possible to declare an array without an array size. This will result in a compile-time error.
- What is the difference between an ArrayList and a vector?
While both the ArrayList and Vector use an Array as an internal data structure and are dynamically resizable, the difference between the two is in the way they are resized. An ArrayList increments 50% of its current size, while a Vector increments 100% of its current size if an element is added. Thus, ArrayList methods are unsynchronized, and Vector methods are synchronized.
- Explain a Java String Pool
A Java String Pool is a collection of strings stored in Java Heap Memory. During the creation of an object, the String pool checks to verify that the object does not already exist in the pool. If it does, the reference of the object is returned to the variable. If not, the new object is created in the string pool, and its reference is returned.
Conclusion
As an aspiring Java developer or programmer, having a working knowledge of Java basics does set you apart. We have listed the core Java topics/concepts that should guide your preparation for an interview. If you have been doing a lot of practicals, then the entry-level interview should not be a hard nut to crack. Still, there is no shortcut for adequate preparation. We hope that these frequently asked interview questions and answers will help you impress the recruiter on your big day.