0% found this document useful (0 votes)
11 views16 pages

Essential Java Interview Questions

The document contains a comprehensive list of Java interview questions and answers covering various topics such as Java's platform independence, JVM, JRE, JDK, data types, memory management, and differences between Java and C++. It explains key concepts like the main method, packages, wrapper classes, and input/output streams. Additionally, it highlights the advantages of using packages and provides examples for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

Essential Java Interview Questions

The document contains a comprehensive list of Java interview questions and answers covering various topics such as Java's platform independence, JVM, JRE, JDK, data types, memory management, and differences between Java and C++. It explains key concepts like the main method, packages, wrapper classes, and input/output streams. Additionally, it highlights the advantages of using packages and provides examples for clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA INTERVIEW QUESTIONS

1. Is Java Platform Independent if then how?

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.

2. What are the top Java Features?

Java is one the most famous and most used language in the real world, there are many
features in Java that makes it better than any other language some of them are mentioned
below:

 Simple: Java is quite simple to understand and the syntax

 Platform Independent: Java is platform independent means we can run the same
program in any software and hardware and will get the same result.

 Interpreted: Java is interpreted as well as a compiler-based language.

 Robust: features like Garbage collection, exception handling, etc that make the
language robust.

 Object-Oriented: Java is an object-oriented language that supports the concepts of


class, objects, four pillars of OOPS, etc.
 Secured: As we can directly share an application with the user without sharing the
actual program makes Java a secure language.

 High Performance: faster than other traditional interpreted programming languages.

 Dynamic: supports dynamic loading of classes and interfaces.

 Distributed: feature of Java makes us able to access files by calling the methods from
any machine connected.

 Multithreaded: deal with multiple tasks at once by defining multiple threads

 Architecture Neutral: it is not dependent on the architecture.

3. What is JVM?

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.

To know more about the topic refer to JVM in Java.

4. What is JIT?
JIT stands for (Just-in-Time) compiler is a part of JRE(Java Runtime Environment), it is used
for better performance of the Java applications during run-time. The use of JIT is mentioned
in step by step process mentioned below:

1. Source code is compiled with javac to form bytecode

2. Bytecode is further passed on to JVM

3. JIT is a part of JVM, JIT is responsible for compiling bytecode into native machine
code at run time.

4. The JIT compiler is enabled throughout, while it gets activated when a method is
invoked. For a compiled method, the JVM directly calls the compiled code, instead of
interpreting it.

5. As JVM calls the compiled code that increases the performance and speed of the
execution.

To know more about the topic refer to JIT in Java.

5. What are Memory storages available with JVM?


JVM consists of a few memory storages as mentioned below:

1. Class(Method) Area: stores class-level data of every class such as the runtime
constant pool, field, and method data, and the code for methods.

2. Heap: Objects are created or objects are stored. It is used to allocate memory to
objects during run time.

3. Stack: stores data and partial results which will be needed while returning value for
method and performing dynamic linking

4. Program Counter Register: stores the address of the Java virtual machine instruction
currently being executed.

5. Native Method Stack: stores all the native methods used in the application.

To know more about the topic refer to JVM Memory Storages.

6. What is a classloader?

Classloader is the part of JRE(Java Runtime Environment), during the execution of the
bytecode or created .class file classloader is responsible for dynamically loading the java
classes and interfaces to JVM(Java Virtual Machine). Because of classloaders Java run time
system does not need to know about files and file systems.

To know more about the topic refer to ClassLoader in Java.

7. Difference between JVM, JRE, and JDK.

JVM: JVM also known as Java Virtual Machine is a part of JRE. JVM is a type of interpreter
responsible for converting bytecode into machine-readable code. JVM itself is platform
dependent but it interprets the bytecode which is the platform-independent reason why
Java is platform-independent.
JRE: JRE stands for Java Runtime Environment, it is an installation package that provides an
environment to run the Java program or application on any machine.

JDK: JDK stands for Java Development Kit which provides the environment to develop and
execute Java programs. JDK is a package that includes two things Development Tools to
provide an environment to develop your Java programs and, JRE to execute Java programs or
applications.

To know more about the topic refer to the Differences between JVM, JRE, and JDK.

8. What are the differences between Java and C++?

Basis C++ Java

Java is Platform
C++ is Platform Dependent
Platform Independent

C++ is mainly used for Java is Mainly used for


Application System Programming Application Programming

Java is not so interactive


C++ is nearer to hardware
Hardware with hardware

C++ supports global and Java doesn’t support


Global Scope namespace scope. global scope.

Not Supporting Functionality supported in Functionality supported in


Java but not in C++ are: C++ but not in Java are:

 thread support  goto

 documentation  Pointers
comment
 Call by reference
 unsigned right
 Structures and
shift(>>>)
Unions

 Multiple
Inheritance
Basis C++ Java

 Virtual Functions

Java is also an object-


oriented language. It is a
C++ is an object-oriented
single root hierarchy as
language. It is not a single
everything gets derived
root hierarchy .
from a single class
OOPS ([Link]).

Java uses a Single


C++ always creates a new inheritance tree as classes
inheritance tree. in Java are the child of
Inheritance Tree object classes in Java.

9. Explain public static void main(String args[]) in Java.

Unlike any other programming language like C, C++, etc. In Java, we declared the main
function as a public static void main (String args[]). The meanings of the terms are
mentioned below:

1. public: the public is the access modifier responsible for mentioning who can access
the element or the method and what is the limit. It is responsible for making the
main function globally available. It is made public so that JVM can invoke it from
outside the class as it is not present in the current class.
2. static: static is a keyword used so that we can use the element without initiating the
class so to avoid the unnecessary allocation of the memory.

3. void: void is a keyword and is used to specify that a method doesn’t return anything.
As the main function doesn’t return anything we use void.

4. main: main represents that the function declared is the main function. It helps JVM
to identify that the declared function is the main function.

5. String args[]: It stores Java command-line arguments and is an array of type


[Link] class.

10. What is Java String Pool?

A Java String Pool is a place in heap memory where all the strings defined in the program are
stored. A separate place in a stack is there where the variable storing the string is stored.
Whenever we create a new string object, JVM checks for the presence of the object in the
String pool, If String is available in the pool, the same object reference is shared with the
variable, else a new object is created.

Example:

String str1="Hello";
// "Hello" will be stored in String Pool
// str1 will be stored in stack memory

11. What will happen if we declare don’t declare the main as static?

We can declare the main method without using static and without getting any errors. But,
the main method will not be treated as the entry point to the application or the program.

12. What are Packages in Java?


Packages in Java can be defined as the grouping of related types of classes, interfaces, etc
providing access to protection and namespace management.

13. Why Packages are used?

Packages are used in Java in order to prevent naming conflicts, control access, and make
searching/locating and usage of classes, interfaces, etc easier.

14. What are the advantages of Packages in Java?

There are various advantages of defining packages in Java.

 Packages avoid name clashes.

 The Package provides easier access control.

 We can also have the hidden classes that are not visible outside and are used by the
package.

 It is easier to locate the related classes.

15. How many types of packages are there in Java?

There are two types of packages in Java

 User-defined packages

 Build In packages

16. Explain different data types in Java.

There are 2 types of data types in Java as mentioned below:

1. Primitive Data Type

2. Non-Primitive Data Type or Object Data type

Primitive Data Type: Primitive data are single values with no special capabilities. There are 8
primitive data types:

 boolean: stores value true or false

 byte: stores an 8-bit signed two’s complement integer

 char: stores a single 16-bit Unicode character

 short: stores a 16-bit signed two’s complement integer

 int: stores a 32-bit signed two’s complement integer

 long: stores a 64-bit two’s complement integer

 float: stores a single-precision 32-bit IEEE 754 floating-point


 double: stores a double-precision 64-bit IEEE 754 floating-point

Non-Primitive Data Type: Reference Data types will contain a memory address of the
variable’s values because it is not able to directly store the values in the memory. Types of
Non-Primitive are mentioned below:

 Strings

 Array

 Class

 Object

 Interface

17. When a byte datatype is used?

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.

18. Can we declare Pointer in Java?

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.

19. What is the default value of byte datatype in Java?

The default value of the byte datatype in Java is 0.

20. What is the default value of float and double datatype in Java?

The default value of the float is 0.0f and of double is 0.0d in Java.

21. What is the Wrapper class in Java?

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.

22. Why do we need wrapper classes?


The wrapper class is an object class that encapsulates the primitive data types, and we need
them for the following reasons:

1. Wrapper classes are final and immutable

2. Provides methods like valueOf(), parseInt(), etc.

3. It provides the feature of autoboxing and unboxing.

23. Differentiate between instance and local variables.

Instance Variable Local Variable

Declared outside the method, directly


Declared within the method.
invoked by the method.

Has a default value. No default value

It can be used throughout the class. The scope is limited to the method.

24. What are the default values assigned to variables and instances in Java?

In Java When we haven’t initialized the instance variables then the compiler initializes them
with default values. The default values for instances and variables depend on their data
types. Some common types of default data types are:

 The default value for numeric types (byte, short, int, long, float, and double) is 0.

 The default value for the boolean type is false.

 The default value for object types (classes, interfaces, and arrays) is null.

 The null character, “u0000, ” is the default value for the char type.

Example:

// Java Program to demonstrate use of default values

import [Link].*;

class GFG {
4

// static values

static byte b;

static int i;

static long l;

static short s;

static boolean bool;

10

static char c;

11

static String str;

12

static Object object;

13

static float f;

14

static double d;

15

static int[] Arr;

16

public static void main(String[] args)

17

{
18

// byte value

19

[Link]("byte value" + b);

20

// short value

21

[Link]("short value" + s);

22

// int value

23

[Link]("int value" + i);

24

// long value

25

[Link]("long value" + l);

26

[Link]("boolean value" + bool);

27

[Link]("char value" + c);

28

[Link]("float value" + f);

29

[Link]("double value" + d);

30

[Link]("string value" + str);

31

[Link]("object value" + object);


32

[Link]("Array value" + Arr);

33

34

What do you understand by an IO stream?

Java brings various Streams with its I/O package that helps the user to perform all the input-
output operations. These streams support all types of objects, data types, characters, files,
etc to fully execute the I/O operations.

What is the difference between the Reader/Writer class hierarchy and the
InputStream/OutputStream class hierarchy?

The key difference between them is that byte stream data is read and written by
input/output stream classes. Characters are handled by the Reader and Writer classes. In
contrast to Reader/Writer classes, which accept character arrays as parameters,
input/output stream class methods accept byte arrays. In comparison to input/output
streams, the Reader/Writer classes are more efficient, handle all Unicode characters, and are
useful for internalization. Use Reader/Writer classes instead of binary data, such as pictures,
unless you do so.

Example:

// Java Program to demonstrate Reading Writing Binary Data

// with InputStream/OutputStream

3
import [Link].*;

class GFG {

public static void main(String[] args) {

try {

// Writing binary data to a file using OutputStream

byte[] data = {(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea};

10

OutputStream os = new FileOutputStream("[Link]");

11

[Link](data);

12

[Link]();

13

14

// Reading binary data from a file using InputStream

15

InputStream is = new FileInputStream("[Link]");

16

byte[] newData = new byte[5];

17
18

[Link](newData);

19

[Link]();

20

21

// Printing the read data

22

for (byte b : newData) {

23

[Link](b+" ");

24

25

} catch (IOException e) {

26

[Link]();

27

28

29

Output

-32 79 -48 32 -22

You might also like