Java was originally developed by Sun Microsystems and is a high-level programming language that has been a major contributor to the software development domain ever since its inception. Windows, Mac OS, and the various versions of UNIX are some of the platforms that support Java. If you wish to master Java and crack the Java Interview questions, it is not only necessary to learn the skills but also to practice them in real-time through implementation. To ace any Java interview, you can go through the following questions that will help you improve your skills.
The following java interview questions cover the basic concepts required for freshers, and also experienced professionals.
Table of Contents
1. List the features of Java Programming Language.
A few common features of java programming language are listed below
- Object oriented programming
- Portable
- Platform independent programming
- Secure
- Robust
- High performance
- Dynamic programming
- Multithreaded
2. Does Java support pointers?
No, Java does not support pointers. This is because it helps make java more secure to use.
3. How many data types does java support, and can you list them?
Java primarily supports eight primitive data types. The data types are byte, short, int, long, float, double, char, boolean.
4. Is it possible to make an array volatile in Java?
Yes, it is possible to make an array volatile in Java. But the catch here is that you can only do so for the reference that is pointing to an array, and not the whole array itself. For example, if one thread changes your reference variable to point towards another array, this provides a volatile guarantee. Suppose multiple threads change individual array elements, they won’t have guarantee provided by the volatile modifier.
5. What is the difference between checked and unchecked exceptions in Java?
The checked exception in java is checked during compilation by the compiler. Declaring them in the throws clause or handling the checked exception is mandatory for a method. It is a subclass of exception but, it does not descend from the Runtime Exception.
An unchecked exception is not checked by the compiler during compilation, and is a descendant of the Runtime Exception. This question is typically asked by interviewers of smaller companies and by investment banks as well.
6. What are the types of constructors that Java supports?
The following types of constructors are supported by java:
– Non-Parameterized or Default Constructors
– Copy constructor
– Parameterized Constructors
7. JDK, JVM, and JRE. What is the difference between these terms?
A software environment that is being used for the development of Java programs is known as JDK. It is essentially a collection of libraries that are used for developing several Java applications. A java program is able to run successfully because of the software environment known as Java Runtime Environment or JRE. Any Java application can run only inside the JRE. When Java programs are converted into bytecode executables, it is done with the help of an environment known as Java virtual machine or JVM. JVM is platform independent, whereas JDK and JRE are platform-dependent.
8. What is a classloader in Java? What are the types?
A subsystem of Java virtual machine or JVM that is used to load class files is known as classloader. When we run any java program, it is first run by the classloader. Java comes with built-in classloaders, these classloaders are listed below.
- Bootstrap classloader
- Extension classloader
- System/Application classloader
A bootstrap classloader is the superclass of the extension classloader and loads the rt.jar file that contains the class files such as java.lang package classes, java.net package classes, java.sql package classes, etc. Extension classloader is the child of bootstrap but the parent of system classloader. It loads files that are located inside $JAVA_HOME/jre/lib/ext directory. System classloader loads files from a classpath.
9. Can an interface in Java be inherited?
Hybrid and hierarchical inheritance is supported by java with the help of inheritable interfaces. So, yes, interfaces can be inherited in java programming.
10. How can we get a string as the user input from the console?
To get a string as the user input from the console, you will first need to instantiate an input reader class. A few options available are BufferedReader, and InputStreamReader Scanner. Once this is done, the relative functionality of a class can be used. The commonly used function is nextLine() of the Scanner class.
11. How do you check the size of a string?
To check the size of a string, you have to use the length() function.
12. Write a program to calculate the factorial of a number.
import java.util.Scanner;
public class star {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int fact=1;
int n=sc.nextInt();
for (int i=1;i<=n;i++)
fact=fact*i;
System.out.println(fact);
}
}
13. What is encapsulation in Java?
The process of wrapping variables and functions together in a single unit is known as encapsulation, This is done to hide the unnecessary details. It is also known as data hiding since it helps us hide the underlying intricacies. Classes in java are the wrapped up entities.
14. Are the constructors of the base class also inherited by the child class if it inherits base class?
Constructors cannot be inherited because they are not properties of a class. If a constructor could be inherited, this would also mean that the child class can be created with the constructor of the parent class. This causes referencing errors if the child class is instantiated. Thus, to avoid any such errors, constructors cannot be inherited. If the child class wants to invoke the parent class constructor, we have to use the super() keyword.
15. What is singleton and what is the benefit of making classes singleton in Java?
A class in java that can have at the most one instance of it in any java application is known as a singleton. In case a new instance is created for the same class, it would have to point to the first instance that is created. Thus, it should have the same value for all properties and attributes. A singleton class is created so that we can create a global point access for objects. They find their primary use in caching, logging, and device drivers that are entities for universal access.
16. How is garbage collection done in Java?
There is an automatic built-in garbage collector in Java. If not for the built-in method, there is a manual function that can be used. The manual initiation can be done using gc() of system class.
Conclusion
This brings us to the end of the blog on Java Interview Questions. We hope that you found this helpful and were able to learn more about the commonly asked interview questions.
Kamran Sharief
Related posts
Sidebar
Recent Posts
The Best Forex Brokers for Beginners
Since COVID-19 first popped up, interest in the forex market has increased dramatically. Knowing how to get involved with minimal…
Maximizing Success: The Symbiosis of Dedicated Software Development Teams and Product Design Services
The Symbiosis of Dedicated Software Development Teams and Product Design Services In the rapidly evolving landscape of technology, businesses aiming…