Java Interview Questions and Answers for Freshers, Quizzes of Computer science

A comprehensive set of java interview questions and answers tailored for freshers. It covers fundamental concepts such as platform independence, key java features, jvm, jit, memory storages, classloaders, and differences between java and c++. Additionally, it explains the components of the main method, java string pool, packages, and data types, offering valuable insights for beginners preparing for java interviews. This resource is designed to help new developers understand core java principles and enhance their interview readiness, making it an excellent study aid for those entering the field of java programming.

Typology: Quizzes

2023/2024

Available from 09/04/2025

fabriceish
fabriceish 🇷🇼

11 documents

1 / 152

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Interview questions for Freshers
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Java Interview Questions and Answers for Freshers and more Quizzes Computer science in PDF only on Docsity!

Java Interview questions for Freshers

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.

  1. What is JVM?
  1. 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.

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

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

and JDK.

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

Basis C++ Java

Platform

C++ is Platform

Dependent

Java is Platform

Independent

Application

C++ is mainly used for

System Programming

Java is Mainly used for

Application

Programming

Hardware

C++ is nearer to

hardware

Java is not so

interactive with

hardware

Global Scope

C++ supports global

and namespace scope.

Java doesn’t support

global scope.

Not Supporting

Functionality

supported in Java but

not in C++ are:

● thread

support

● documentati

on comment

● unsigned

right

shift(>>>)

Functionality

supported in C++ but

not in Java are:

● goto

● Pointers

● Call by

reference

● Structures

and Unions

● Multiple

Inheritance

● Virtual

Functions

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.

  1. 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";

● It is easier to locate the related classes.

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

There are two types of packages in Java

● User-defined packages

● Build In packages

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

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

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

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

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

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

Instance Variable Local Variable

Declared outside the method,

directly invoked by the method.

Declared within the method.

Has a default value. No default value

It can be used throughout the

class.

The scope is limited to the

method.

  1. 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 java.io.; class GFG {* // static values static byte b; static int i; static long l; static short s; static boolean bool; static char c; static String str; static Object object; static float f; static double d; static int[] Arr; public static void main(String[] args) { // byte value System.out.println("byte value" + b); // short value System.out.println("short value" + s); // int value System.out.println("int value" + i); // long value System.out.println("long value" + l); System.out.println("boolean value" + bool); System.out.println("char value" + c); System.out.println("float value" + f); System.out.println("double value" + d); System.out.println("string value" + str); System.out.println("object value" + object); System.out.println("Array value" + Arr); } }

+ GFG.ctr); } }

Output

Number of objects created are 3

  1. What is the default value stored in Local Variables?

There is no default value stored with local variables. Also, primitive

variables and objects don’t have any default values.

  1. Explain the difference between instance variable and a class variable.

Instance Variable: A class variable without a static modifier known as an

instance variable is typically shared by all instances of the class. These

variables can have distinct values among several objects. The contents of

an instance variable are completely independent of one object instance

from another because they are related to a specific object instance of the

class.

Example:

// Java Program to demonstrate Instance Variable import java.io.; class GFG { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public static void main(String[] args) { GFG obj = new GFG(); obj.setName("John"); System.out.println("Name " + obj.getName()); }*

Output

Name John

Class Variable: Class Variable variable can be declared anywhere at the

class level using the keyword static. These variables can only have one

value when applied to various objects. These variables can be shared by

all class members since they are not connected to any specific object of

the class.

Example:

// Java Program to demonstrate Class Variable import java.io.; class GFG {* // class variable **private static final double PI = 3.14159; private double radius; public GFG(double radius) { this.radius = radius; } public double getArea() { return PI * radius * radius; } public static void main(String[] args) { GFG obj = new GFG(5.0); System.out.println("Area of circle: "

  • obj.getArea()); } }**

Output

Area of circle: 78.

  1. What is a static variable?

The static keyword is used to share the same variable or method of a

given class. Static variables are the variables that once declared then a