Java Interview Questions for Freshers
Java remains one of the most in-demand programming languages. Whether you are a fresher or an experienced in the IT field looking to switch, you must crack an interview, which consists of Java questions for sure. In this article, I have gathered 40 Java Interview Questions for Freshers, which will help you to prepare well for the interview. This will be our Part 1 of Java Interview Questions, Many other questions will be provided in another article as well.
I suggest you go through all the questions and answers provided here in this article to get an idea on how to answer and to know what the questions might encounter in the interview.
1. What is Java?
Ans: Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.
2. What do you understand by Java virtual machine?
Ans: Java Virtual Machine is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine which calls the main method present in the Java code. JVM is the specification which must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.
3. List the features of the Java Programming language?
Ans: A few of the significant features of Java Programming Language are:
Easy: Java is a language that is considered easy to learn. One fundamental concept of OOP Java has a catch to understand.
Secured Feature: Java has a secured feature that helps develop a virus-free and tamper-free system for the users.
OOP: OOP stands for Object-Oriented Programming language. OOP signifies that, in Java, everything is considered an object.
Independent Platform: Java is not compiled into a platform-specific machine; instead, it is compiled into platform-independent bytecode. This code is interpreted by the Virtual Machine on which the platform runs.
4. Is Java Platform Independent if then how?
Ans: Yes, Java is a Platform Independent language. Unlike many programming languages javac compiles the program to form a bytecode or .class file. This file is independent of the software or hardware running but needs a JVM (Java Virtual Machine) file preinstalled in the operating system for further execution of the bytecode. Although JVM is platform dependent, the bytecode can be created on any System and can be executed in any other system despite hardware or software being used which makes Java platform independent.
5. What is JVM?
Ans: JVM stands for Java Virtual Machine it is a Java interpreter. It is responsible for loading, verifying, and executing the bytecode created in Java. Although it is platform dependent which means the software of JVM is different for different Operating Systems it plays a vital role in making Java platform Independent.
6. Is Java a completely object-oriented programming language?
Ans: Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don’t directly belong to the Integer classes.
7. How is Java different from C++?
Ans: C++ is only a compiled language, whereas Java is compiled as well as an interpreted language.
• Java programs are machine-independent whereas a c++ program can run only in the machine in which it is compiled.
• C++ allows users to use pointers in the program. Whereas java doesn’t allow it. Java internally uses pointers.
• C++ supports the concept of Multiple inheritances whereas Java doesn’t support this. And it is due to avoiding the complexity of name ambiguity that causes the diamond problem.
8. What is the difference between JDK, JRE, and JVM?
Ans: JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the runtime environment in which Java bytecode can be executed. It is a specification which specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE.
JVMs are available for many hardware and software platforms (so JVM is platform dependent). It is a runtime instance which is created when we run the Java class. There are three notions of the JVM: specification, implementation, and instance.
JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
JDK is an acronym for Java Development Kit. It is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation
9. What is a ClassLoader?
Ans: A classloader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; ClassLoader is the first to load the executable file. Java has Bootstrap, Extension, and Application classloaders.
10. What are the Memory Allocations available in Java?
Ans: Java has five significant types of memory allocations.
- Class Memory
- Heap Memory
- Stack Memory
- Program Counter-Memory
- Native Method Stack Memory
11. What are the differences between Heap and Stack Memory in Java?
Ans: Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program’s runtime.
12. How many types of memory areas are allocated by JVM?
Ans: Many types, they are:
- Class (Method) Area: Class Area stores per-class structures such as the runtime constant pool, field, method data, and the code for methods.
- Heap: It is the runtime data area in which the memory is allocated to the objects
- Stack: Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
- Program Counter Register: PC (program counter) register contains the address of the Java virtual machine instruction currently being executed.
- Native Method Stack: It contains all the native methods used in the application.
13. What is JIT compiler?
Ans: Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
14. What are the main differences between the Java platform and other platforms?
Ans: There are the following differences between the Java platform and other platforms.
- Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms.
- Java is executed on the top of other hardware platforms whereas other platforms can only have the hardware components.
15. What do you understand by an instance variable and a local variable?
Ans: Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost. All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.
Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.
16. What gives Java its ‘write once and run anywhere’ nature?
Ans: The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform specific and can be executed on any computer.
17. Will the program run if we write static public void main?
Ans: Yes, the program will successfully execute if written so. Because, in Java, there is no specific rule for the order of specifiers
18. What are the various access specifiers in Java?
Ans: In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
- Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
- Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
- Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
- Private The private class, methods, or variables defined as private can be accessed within the class only.
19. What are the default values assigned to variables and instances in java?
Ans: There are no default values assigned to the variables in java. We need to initialize the value before using it. Otherwise, it will throw a compilation error of (Variable might not be initialized).
- But for instance, if we create the object, then the default value will be initialized by the default constructor depending on the data type.
- If it is a reference, then it will be assigned to null.
- If it is numeric, then it will assign to 0.
- If it is a boolean, then it will be assigned to false. Etc.
20. What do you mean by data encapsulation?
Ans: Data Encapsulation is an Object-Oriented Programming concept of hiding the data attributes and their behaviours in a single unit.
- It helps developers to follow modularity while developing software by ensuring that each object is independent of other objects by having its own methods, attributes, and functionalities.
- It is used for the security of the private properties of an object and hence serves the purpose of data hiding.
21. Why Packages are used?
Ans: Packages are used in Java in order to prevent naming conflicts, control access, and make searching/locating and usage of classes, interfaces, etc easier.
22. When a byte datatype is used?
Ans: A byte is an 8-bit signed two-complement integer. The minimum value supported by bytes is -128 and 127 is the maximum value. It is used in conditions where we need to save memory and the limit of numbers needed is between -128 to 127.
23. Can we declare Pointer in Java?
Ans: No, Java doesn’t provide the support of Pointer. As Java needed to be more secure because which feature of the pointer is not provided in Java.
24. What is the Wrapper class in Java?
Ans: Wrapper, in general, is referred to a larger entity that encapsulates a smaller entity. Here in Java, the wrapper class is an object class that encapsulates the primitive data types.
The primitive data types are the ones from which further data types could be created. For example, integers can further lead to the construction of long, byte, short, etc. On the other hand, the string cannot, hence it is not primitive. Getting back to the wrapper class, Java contains 8 wrapper classes. They are Boolean, Byte, Short, Integer, Character, Long, Float, and Double. Further, custom wrapper classes can also be created in Java which is similar to the concept of Structure in the C programming language. We create our own wrapper class with the required data types.
25. Can you tell the difference between equals () method and equality operator (==) in Java?
Ans: We are already aware of the (==) equals operator. That we have used this to compare the equality of the values. But when we talk about the terms of object-oriented programming, we deal with the values in the form of objects. And this object may contain multiple types of data. So using the (==) operator does not work in this case. So we need to go with the .equals() method.
Both [(==) and .equals ()] primary functionalities are to compare the values, but the secondary functionality is different.
26. What do you mean by aggregation?
Ans: The term aggregation refers to the relationship between two classes best described as a “whole/part” and “has-a” relationship. This kind is the most specialized version of an association relationship. It contains the reference to another class and is said to have ownership of that class.
27. What is Object Cloning?
Ans: An ability to recreate an object entirely similar to an existing object is known as Object Cloning in Java. Java provides a clone() method to clone a current object offering the same functionality as the original object.
28. Explain Java String Pool.
Ans: A collection of strings in Java’s Heap memory is referred to as Java String Pool. In case you try to create a new string object, JVM first checks for the presence of the object in the pool. If available, the same object reference is shared with the variable, else a new object is created.
29. What is the final keyword in Java?
Ans: The term final is a predefined word in Java that is used while declaring values to variables. When a value is declared using the final keyword, then the variable’s value remains constant throughout the program’s execution.
30. What part of memory – Stack or Heap – is cleaned in the garbage collection process?
Ans: On Heap memory, garbage collection is employed to release the memory used by objects with no references. Every object created in the Heap space has access to the entire application and may be referred to from anywhere.
31. What is object-oriented paradigm?
Ans: It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.
- Follows the bottom-up approach in program design.
- Focus on data with methods to operate upon the object’s data
- Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
- Implements the real-time approach like inheritance, abstraction, etc.
- The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.
32. What is an object?
Ans: The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables as the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword.
33. Why do we need wrapper classes?
Ans: The wrapper class is an object class that encapsulates the primitive data types, and we need them for the following reasons:
- Wrapper classes are final and immutable
- Provides methods like valueOf(), parseInt(), etc.
- It provides the feature of autoboxing and unboxing.
34. What is a Class Variable?
Ans: In Java, a class variable (also known as a static variable) is a variable that is declared within a class but outside of any method, constructor, or block. Class variables are declared with the static keyword, and they are shared by all instances (objects) of the class as well as by the class itself. No matter how many objects are derived from a class, each class variable would only exist once.
35. What is the difference between System.out, System.err, and System.in?
- System.out – It is a PrintStream that is used for writing characters or can be said it can output the data we want to write on the Command Line Interface console/terminal.
- System.err – It is used to display error messages.
- System.in – It is an InputStream used to read input from the terminal Window. We can’t use the System.in directly so we use Scanner class for taking input with the system.in.
36. What are the super most classes for all the streams?
Ans: All the stream classes can be divided into two types of classes that are ByteStream classes and CharacterStream Classes. The ByteStream classes are further divided into InputStream classes and OutputStream classes. CharacterStream classes are also divided into Reader classes and Writer classes. The SuperMost classes for all the InputStream classes is java.io.InputStream and for all the output stream classes is java.io.OutPutStream. Similarly, for all the reader classes, the super-most class is java.io.Reader, and for all the writer classes, it is java.io.Writer.
37. What are the FileInputStream and FileOutputStream?
Ans: To read and write data, Java offers I/O Streams. A Stream represents an input source or an output destination, which could be a file, an i/o device, another program, etc. FileInputStream in Java is used to read data from a file as a stream of bytes. It is mostly used for reading binary data such as images, audio files, or serialized objects.
In Java, the FileOutputStream function is used to write data byte by byte into a given file or file descriptor. Usually, raw byte data, such as pictures, is written into a file using FileOutputStream.
38. What is the purpose of using BufferedInputStream and BufferedOutputStream classes?
Ans: When we are working with the files or stream then to increase the Input/Output performance of the program we need to use the BufferedInputStream and BufferedOutputStream classes. These both classes provide the capability of buffering which means that the data will be stored in a buffer before writing to a file or reading it from a stream. It also reduces the number of times our OS needs to interact with the network or the disk. Buffering allows programs to write a big amount of data instead of writing it in small chunks. This also reduces the overhead of accessing the network or the disk.
39. Briefly explain the concept of constructor overloading.
Ans: Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.
40. Explain the use of final keyword in variable, method and class.
Ans: In Java, the final keyword is used as defining something as constant /final and represents the non-access modifier.
- final variable: When a variable is declared as final in Java, the value can’t be modified once it has been assigned. If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class.
- final method: A method declared as final cannot be overridden by its children’s classes. A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn’t make sense. Java throws compilation error saying – modifier final not allowed here
- final class: No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage.
I Hope these questions will help you in your preparation. Not only just these questions there are lot many Java Interview Questions for Freshers that are getting ready for you. All the questions will be posted in upcoming articles. Feel free to check all the latest blogs/articles that we posted here.
Refer our page and website with your friends who need this. Let’s learn together and grow together.
All the Best
-Vinay Neeradi (techy_miki)