The team at GoApt has curated a list of 5 Java interview question and answers for your preparation.
Java certifications are still popular. And every day, you get to see new Java tutorials uploaded on different websites. All of this shows that Java is here to stay and the demand for Java developers won’t decline anytime soon.
1. What is the difference between String Buffer and String Builder in Java?
Here are a few notable differences between String Buffer and String Builder:
- StringBuilder is a non-synchronized version of the StringBuffer class. Methods in StringBuilder (for example, the overloaded versions of append() method) are not synchronized.
- StringBuilder is faster than StringBuffer because of no overhead of acquiring and releasing locks associated with synchronized methods.
- StringBuffer is considered a thread-safe while StringBuilder is not. If synchronization is required, it is better to use the StringBuffer class. StringBuilder class instances cannot be shared between multiple threads.
- StringBuffer is an old class; it’s incorporated in JDK from the very first release. StringBuilder is a relatively newer class
- When String concatenation is done using the + operator, Java internally converts that call to the corresponding StringBuilder append() method class. For example: “one” + “two” + “three” will be converted to new StringBuilder ().append (“one”).append (“two”).append (“three”).
2. How do you write a Java program to find the largest and smallest number in an integer array?
It can be done in 5 steps:
- 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)” (you can give any name) 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 to Integer.MIN_VALUE and smallest is initialized with Integer.MAX_VALUE.
- Create a loop to iterate over all the elements of the loop. In all the iterations of the loop, compare the current number with the largest and smallest variables, 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 the else part will only execute if the first condition is not true.
Let us move to the next Java Interview Question and Answers:
3. What is the difference between C++ & Java?
C++ and Java are only similar in syntactical comparisons. The major differences between C++ and Java are:
- 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
Do visit the Official Documentation of Java at Oracle’s Website
4. What is the difference between Abstract class and Interface?
- Multiple Inheritance: An abstract class can inherit only one abstract class; however, in the interface, a class may implement several interfaces. This proves Interface supports Multiple Inheritance and Abstract class does not.
- Implementation: An abstract class can provide default code as it contains both incomplete and complete members, whereas an interface cannot provide any code as it contains an only incomplete member
- Fields: We can define fields and constraints in an Abstract class whereas no fields can be defined in an interface
- Speed: An abstract class is fast when compared to an interface as the latter requires more time to find the method to its corresponding class
- Usage: 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. What is the difference between private, protected, public, and package modifiers in Java?
Java has four access modifiers namely private, protected, public, and package. 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 restrict the accessibility of a class, method, or variable on which it applies.
Private keyword in Java
- The private modifier can be applied to member field, method, or nested class in Java.
- One cannot use the private modifier in a top-level class.
- Private variables, methods, and classes are only accessible in the class on which they are declared.
- Private is the highest form of encapsulation Java API provides and should be used as much as possible. It’s a good 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 keyword 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 in the package it belongs to. The plus point about the default modifier is that 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 a protected method, variable or nested class is not only accessible to a class inside the package but also outside the package to subclasses. If you declare a variable protected, anyone can use it if they extend your class. The top-level class cannot be made protected.
Public keyword in Java
- The public is the least restrictive access modifier in Java programming language and it is considered bad practice to declare a field, method, or class by default public because once you make it public it’s very difficult to make any change to the internal structure of the class as it affects all clients using that class.
- Making a class or instance variable public also violates the principle of encapsulation; this in turn affects code maintenance.
Are you aware of other Java interview questions? Have you cleared some Java certification such as OCJP? Would you like to share any Java tutorial links? Do comment. And if you know Java tricks for competitive programming, that would be awesome!