Introduction To Java Interview Questions For Fresher Applicants
If you are looking for a job that is related to Java, you need to prepare for the Java Interview Questions For Fresher Applicants. Though every interview is different and the scope of a job is also different, we can help you out with the top Java Interview Questions and Answers, which will help you take the leap and get you success in your Java Interview.
Part 1 – Java Interview Questions (Basic)
This first part covers basic interview questions and answers.
1. Difference between String Buffer and String Builder in Java
This is one among the favorite questions to interviewers; however, frequently answered miserably by the candidates.
Here are few notable differences between String Buffer and String Builder for a better understanding.
1)StringBuilder is a non-synchronized version of the StringBuffer class. Methods in StringBuilder
E.g. all overloaded version of append () method is not synchronized.
2)StringBuilder is working faster than StringBuffer because of no overhead of acquiring and releasing locks associated with synchronized methods.
3)StringBuffer is considered thread-safe, and StringBuilder is not because if such synchronization is required, it is better to use the StringBuffer class. StringBuilder class Instances cannot be shared between multiple threads
4)StringBuffer is the old class; it’s incorporated in JDK from its very first release, while StringBuilder is a relatively newer class
5)Most important fact about StringBuffer and StringBuilder is that, when String concatenation is done using + operator, Java internally convert that call to corresponding StringBuilder append() method class. For example “one” + “two” + “three” will be converted to new StringBuilder ().append (“one”).append (“two”).append (“three”).
2. Java program to find the largest and smallest number in an integer array
A simple 5 pointer to answer this question
- Create a Java source file with the name MaximumMinimumArrayDemo.java and copy the code to compile and execute in your favorite IDE
- Create a method called largest and smallest (int  numbers) to print the largest and smallest number of int array passed to the program.
- Use two variables, largest and smallest, to store the maximum and minimum values from the array. Initially, the largest is initialized withInteger.MIN_VALUE and smallest is initialized with Integer.MAX_VALUE.
- To all the iterations of the loop, compare the current number with largest to smallest and update them accordingly.
- If a number is larger than the largest, it can’t be smaller than the smallest, which means you don’t need to check if the first condition is true; that’s why we have used an if-else code block, where else part will only execute if the first condition is not true.
The logic to find the largest element from an array in Java is given below.
Instead of assigning a variable with Integer.MAX_VALUE, we have assigned the first element from the array.
3. What is the difference between C++ & Java?
C++ and Java are only similar in syntactical comparisons. Stated below are differences:
- Java is multithreaded
- Java has no pointers
- Java has automatic memory management (garbage collection)
- Java is platform-independent
- Java has built-in support for comment documentation
- Java has no operator overloading
Java doesn’t provide multiple inheritances
- There are no destructors in Java
Part 2 – Java Interview Questions (Advanced)
Let us now have a look at the advanced Interview Questions.
4. Difference between Abstract class and Interface?
An abstract class can inherit only one abstract class; however, a class may implement several interfaces in the interface. This proves Interface supports Multiple Inheritance, whereas Abstract class does not.
An abstract class can provide default code as it contains incomplete and complete members, whereas an interface cannot provide any code as it contains an incomplete member.
We can define fields and constraints in an Abstract class, whereas no fields can be defined in an interface.
An abstract class is fast when compared to an interface as the latter requires more time to find the method to its corresponding class.
An abstract class comes into the picture when we want to share a common functionality in a parent-child relationship, whereas Interface is used to define and enforce polymorphism, decoupling, and standardization
5. Difference between private, protected, public, and package modifier or keyword in Java
private vs public vs protected vs package in Java
package-level access is considered to be the default access level offered by Java if in case no access modifier is identified
These access modifiers function to restrict the accessibility of a class, method or variable on which it applies
Described below are the functionalities of each access modifier and how different they are from one another
private keyword in Java
- the private modifier in java can be applied to the member field, method or nested class in Java.
- One cannot use the private modifier on top-level class.
- private variables, methods, and class are only accessible on the class on which they are declared.
- privates the highest form of Encapsulation Java API provides and should be used as much as possible.
It’s the best coding practice in Java to declare variable private by default. A private method can only be called from the class where it has declared
package or default access level in Java
- the package is a keyword that is used to declare a package in Java; a package is a directory to which a class in Java belongs.
- package or default access level is the second-highest restrictive access modifier after private, and any variable, method or class declared as package-private is only accessible on the package it belongs to.
The good thing about the default modifier is that the top-level class can also be package-private if there is no class-level access modifier.
protected keyword in Java
The difference between private and protected keyword is that protected method, variable or nested class accessible inside a class, inside the package, and outside of the package on a subclass.
If you declare a variable protected means, anyone can use it if they extend your class. The top-level class cannot be making protected as well.
public keyword in Java
The public is the least restrictive access modifier in the Java programming language, and its bad practice to declare field, method or class by default public because once you make it public, it’s very difficult to make any change on the internal structure of the class as it affects all clients using it.
Making class or instance variable public also violated the principle of Encapsulation, which is not good at all and affects maintenance badly.
This has been a guide to List Of Java Interview Questions and Answers so that the candidate can crackdown these Java Interview Questions easily. You may also look at the following articles to learn more –