Frequently Asked Java Interview Question and Answers

Basic Questions for Interview


Java is a multi-platform, object-oriented, and network-centric language that can be used as a platform in itself. It is a fast, secure, reliable programming language for coding everything from mobile apps and enterprise software to big data applications and server-side technologies.

1. Why is Java a platform independent language?

Ans:

Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

  • The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it

2. Is Java Platform Independent if then how?

Ans:

Yes, Java is a Platform Independent language. Unlike many programming languages javac compiler 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.

3. Explain in brief the history of Java?

Ans:

In the year 1991, a small group of engineers called ‘Green Team’ led by James Gosling, worked a lot and introduced a new programming language called “Java”. This language is created in such a way that it is going to revolutionize the world.

In today’s World, Java is not only invading the internet, but also it is an invisible force behind many of the operations, devices, and applications.

4. Why is Java not a pure object oriented language?

Ans:

Java supports primitive data types – byte, Boolean, char, short, int, float, long, and double and hence it is not a pure Object Oriented Language.

5. What is the difference between C and Java?

Ans:

The differences between C and Java are as follows:

C- LanguageJava
C language was developed by Dennis M. Ritchie in the year 1972.Java was developed by James Gosling in the year 1995.
C is Procedural-OrientedJava is Object-Oriented
Functions play a major role in the C language.Objects play a major role in the Java language
C language does not support OOPS ConceptsJava supports OOPS concepts, in which Inheritance the main property used for code reusability.
It is a middle-level languageIt is a high-level language.
It is not portableIt is portable
Default members of C are publicDefault members of Java are private
In C, memory is allocated using MallocIn Java, memory is allocated using a new keyword.
Storage classes are supported by CStorage classes are not supported in Java
6. What are the top Java Features?

Ans:

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.

7. 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.

8. What is JIT?

Ans:

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 compiler 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.

9. What is a ClassLoader?

Ans:

ClassLoader: class loader is a subschema of JVM which is used to load class files. Whenever we run Java programs, the data will be first loaded from the classloader. There are mainly three in-built classloaders in JVM, they are:

  • Application Classloader
  • Bootstrap Classloader
  • Extension Classloader

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 programmer. 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. 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

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 java.lang.String class.
13. What is the default value stored in Local Variables?

Ans:

Neither the Local Variables nor any primitives and Object references have any default value stored in them.

14. How to write a basic Hello World program in Java?

Ans:

class AllIn1

{

public static void main(String args[ ]) {

System.out.println(“Hello World”);

}

}

15. What is JDK?

Ans:

Java Development Kit is one of the three prominent technology packages used in Java programming. JDK is used as a standalone component to run the Java programs by JVM and JRE. This kit is used to implement Java platform specifications, including class libraries and compiler.

16. What do you mean by JRE?

Ans:

Java Runtime Environment (JRE) is a collection of software tools that are designed for the development of Java applications. This is a part of JDK, but it can be downloaded separately. JRE is mainly responsible for orchestrating the component activities. 

17. What is the significant difference between  JVM, JRE, and JDK?

Ans:

We can understand the difference between JVM, JDK, and JRE by the following diagram:

  • JVM: Java Virtual Machine is the main part of Java programming, which provides platform independence. JRE and JDK both contain JVM in order to run our Java programs.
  • JDK: This development kit is mainly used for developing programs. 
  • JRE: Java Runtime Environment is mainly used for running Java programs.
18. What is Java String Pool?

Ans:

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

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

Ans:

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.

20. What are Packages in Java?

Ans:

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

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. What are the advantages of Packages in Java?

Ans:

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.
23. How many types of packages are there in Java?

Ans:

There are two types of packages in Java

  • User-defined packages
  • Build In packages
24. Define variables in Java and explain with example?

Ans:

Variables in Java can be defined as a basic storage unit of a program. It is a storage unit that holds the value during the program execution. Always the variable is assigned with a datatype.

For Example:int a = 10;

25. What are the different types of variables in Java?

Ans:

There are mainly three different types of variables available in Java, and they are:

  • Static Variables
  • Local Variables
  • Instance Variables

Static Variables: A variable that is declared with the static keyword is called a static variable. A static variable cannot be a local variable, and the memory is allocated only once for these variables. 

Local Variables: A variable that is declared inside the body of the method within the class is called a local variable. A local variable cannot be declared using the static keyword.

Instance Variables: The variable declared inside the class but outside the body of the method is called the instance variable. This variable cannot be declared as static and its value is instance-specific and cannot be shared among others.

Example:

class A{ 

int num=30;//instance variable 

static char name=pranaya;//static variable 

void method(){ 

int n=90;//local variable 

}//end of class

26. What is Typecasting?

Ans:

Typecasting in Java is done explicitly by the programmer; this is done to convert one data type into another data type. 

Widening (automatically) – conversion of a smaller data type to a larger data type size.   byte -> short -> char -> int -> long -> float -> double

Narrowing (manually) – converting a larger type to a smaller size typedouble -> float -> long -> int -> char -> short -> byte

Fresco Play Course ID: 7408 iSecurity Quiz:

Website: https://allin1solutions000.com

Spread the love

Leave a comment