If you have an interview coming up that would test your understanding of Java, you are on the right page. Java is one of the most important programming languages that are in high demand these days. If you wish to excel in the field of programming, you must have a thorough knowledge of Java. Therefore, here are 15 Java interview questions that will help you get through the test and interview with flying colours.
How inner class is different from subclass?
A class that has been nested in a different class is known as an inner class. All outer class methods and variables are accessible by the inner class in which it is nested.
A subclass is a class that inherits from a different class, which is known as super-class. Sub-class has access to all protected as well as public methods and fields of the super-class.
Can you name the several access specifiers for Java classes?
Access specifiers are keywords in Java that are used before the name of a class, which describes the access scope of the class. There are different types of specifiers for Java classes such as:
- Public: Class, field, method, all can be accessed from anywhere.
- Protected: Access to method and field is granted from the class where it is located, or from sub-classes. Outside access is not granted.
- Private: Access to method and field is only possible from the same class. Access from outside or sub-classes is not possible.
- Default: Method, field, class can only be accessed from the same package without any access from outside of the location.
What is the purpose of static methods and static variables?
A static method and static variable are useful where the project necessitates multiple objects of a class to have a shared method or variable as opposed to creating separate copies of objects. It can create a method and variable which all objects share. This is one of the popular Java interview questions for freshers that is often asked.
4) Describe Data Encapsulation and its importance?
Combining the various methods and properties in a unit in Object-oriented Programming is called Encapsulation. It can be used for the purpose of hiding data.
Encapsulation allows for a modular approach while developing software by the programmers as each object serves its own function independent of other objects with a separate set of methods and variables.
5) What are Loops?
When you need to repeatedly execute a statement or statement set in Java programming, you can use looping.
6) What are the different types of Loops in Java?
There are three different types of looping as described below:
A) While Loops: If you need a statement to be repeatedly executed till a condition is successfully met, you can use the While loop. Once the condition has been checked, the statement is executed.
B) For Loops: When you need to execute a statement or set of statements in Java for a set number of times, the For loop is used. The programmer needs to know and specify the number of loops while programming the loop.
C) Do While Loops: When the statement is repeatedly executed; however, the condition to be met is checked after execution of each loop, the Do While Loop is used. In practice, it is very similar to While loops.
7) What is an Infinite Loop?
An infinite loop runs infinitely without any condition. A breaking logic can be defined in the statement body to break the loop.
8) How are break and continue statements different from each other?
The break keyword breaks the loop instantly, while with the continue keyword the current iteration is broken and the loop moves on to the subsequent iteration.
9) What are double and float variables in Java programming?
Float variable occupies memory equal to 4 bytes, while the Double variable occupies 8 bytes of memory. Float is a single-precision decimal number while double is a double-precision floating point decimal number.
10) Where is Final keyword used in Java?
The keyword Final is used in Java to declare a constant value. The constant value can not be modified after it is assigned.
11) What is the method of generating Random numbers?
You can use the Random class in java.util package or use math.random()for generating numbers between 0.1 and 1.0.
12) All the classes in Java are derived from which base class?
The base class from which all the classes of Java are derived is java.lang.object.
13) Which method in Java cannot return any data?
The main() method in Java cannot return any data.
14) What are the packages in Java and what is their importance?
Packages refer to a set of interfaces and classes that are related to one another and that have been bundled together. Java developers can use these packages to make the codes modular, as well as code can be reused through grouping it. It also allows the packaged codes to be exported to other classes for being utilized.
15) In Java, can you import sub-packages by importing its package?
Importing sub-packages needs to be done separately by the developer since importing the package does not import the sub-packages automatically.
Apart from these core Java interview questions, there is a plethora of knowledge about Java that you can explore and develop your knowledge and skills into this essential programming language. The Java courses at Great Learning Academy are an excellent resource while learning Java that you can rely on wholeheartedly.