Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

java interview questions

The document provides a comprehensive overview of Java, including its definition, history, key features, and comparisons with C++. It covers essential concepts such as classes, objects, JVM, JDK, JRE, and various data types, operators, and memory management. Additionally, it discusses the latest Java versions and their differences, as well as practical examples and syntax for object creation and type casting.

Uploaded by

dhaneshsudha996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java interview questions

The document provides a comprehensive overview of Java, including its definition, history, key features, and comparisons with C++. It covers essential concepts such as classes, objects, JVM, JDK, JRE, and various data types, operators, and memory management. Additionally, it discusses the latest Java versions and their differences, as well as practical examples and syntax for object creation and type casting.

Uploaded by

dhaneshsudha996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Interview Questions for Freshers

1. What is Java?

Java is an open-source, general-purpose, platform-independent language. It is an OOP-based language that we can use to develop web, IoT, gaming, big data, AI, mobile, and enterprise
applications. Java was released by the company Sun Microsystems in 1995. Since then, it has been a reliable, secure platform for developers and enterprises.
Furthermore, Java is a fast, multi-platform, and versatile programming language. It is called WORA (Write Once and Run Anywhere) language since we can seamlessly write Java programs on any
platform. That’s why Java is the right choice for developing cloud-based applications. Above all, Java offers a robust ecosystem with a rich set of libraries and in-built functions, allowing the
development of various applications.

2. Explain in brief the history of Java.


A small group of Sun Microsystems engineers, the ' Green Team' led by James Gosling, invented the Java language. They introduced Java to the world in 1995. Java
continues to be one of the top-ranked programming languages. Developers and Enterprises use Java to build a range of applications.

Initially, the language was designed to develop embedded systems for use in electronic appliances. The initial names of Java are ‘Greentalk’ and ‘Oak’. Then, it got the name Java,
which refers to an island in Indonesia where coffee was first invented.

3. What is the latest version of Java?


The latest version of Java is Java 22 or JDK22. This version was released on March 19, 2024. JDK 21 is the latest Long-Term Support (LTS) release of the Java SE platform
on September 19, 2023. JDK 21 replaced JDK 17, the earlier LTS of the Java SE platform.

4. What are the key differences between the JDK 22 and JDK 21?
JDK 22 is the long-term support release, whereas JDK 21 is the short-term support release.

5. Compare the Java and C++ languages.

Java C++
Java is platform-independent C++ is platform-dependent
It supports the Go-to statement It supports the go-to statement
It doesn’t support structures and unions It does support unions and structures
It has built-in support for threading It does not support threading
We use Java for building applications We use this language for system programming
Java uses both an interpreter and a compiler C++ uses compiler only

6. What are the key features of Java?


The following are the key features of Java:

• Dynamic: Java is more dynamic when compared to C++ and C. Java can quickly adapt to any evolving environment.
• Object-oriented: Java is an OOP-based language.
• Simple: Java is straightforward to learn and code.
• Secure: Java is a safe platform for developing applications.
• Platform Independent: While compiling codes in Java, codes change into platform-independent bytecode – not platform-specific machine code.
• Portable: Java is a highly portable language.
• High-Performance: Java provides improved performance with its built-in Just-in-Time compiler.
• Multithreaded: We can perform multiple tasks simultaneously in Java.
• Bug-free: Java eliminates errors even during the runtime check and compile-time check.

7. What is a class in Java?


A class in Java is a blueprint that supports the creation of individual objects. We can use classes to define object data types and methods. We can consider classes as
categories and objects as items of the categories. The important thing is that all the class objects must have the class properties.

8. Define Object in Java.


The Object is the real-time entity having some state and behaviour. In Java, Object is an instance of the class having the instance variables as the state of the object and the
methods as the behaviour of the object. The object of a class can be created by using the new keyword.

An object stores its state in fields and expresses its behaviour through methods. Know that Methods operate on an object’s internal state. It acts as the critical mechanism for
object-to-object communication.

New Section 1 Page 1


9. Define JVM.
JVM stands for the Java Virtual Machine. JVM is a part of JRE (Java Runtime Environment. It is a virtual machine that provides a runtime environment to write Java codes. We
use JVM to execute Java bytecodes.

The JVM acts as an interpreter between Java and the underlying hardware. It allows Java applications to run on different operating systems and platforms. The main thing is that
JVM plays a crucial role in memory management through automatic memory allocation and garbage collection.

10. What is the class loader in JVM?


A class loader dynamically loads Java classes into the JVM during the execution. Data will be loaded from the classloader first whenever we run Java programs. A class loader is
one of the components of the JRE. The JVM doesn’t need underlying files to run Java programs.

There are three class loaders in JVM in total. They are:

• Application or System classloader.


• Bootstrap or primordial classloader.
• Extensions classloader.

11. What is JDK?


Java Development Kit, or JDK, is one of the three technology packages used in Java programming. We use JDK to implement Java platform specifications such as class
libraries and compilers. With JDK, we can develop software applications and applets.

JDK is a platform-specific component that contains the Java interpreter, Java compiler, Java classes, and development tools. JDK is the core package of Java, also known as the
superset of the JRE.

12. What is JRE?


Java Runtime Environment, or JRE, is one of the three Java platform components besides JVM and JDK. JRE is a collection of software tools that support developing high-
performance Java applications. Though JRE is a part of JDK, we can download it separately.

JRE enables communication between Java programs and the operating system. It acts as a facilitator, offering all the required resources to developers. So, developers can run
applications on any operating system without any customisation.

13. What is the JIT compiler?


Just-In-Time Compiler or JIT is one of the critical components of JRE. JIT compiles the bytecodes of a particular method into the native machine code at runtime. In a way, the JIT
compiler helps to improve the performance of Java applications.

When a method is compiled, the compiled code is directly called by the JVM without interpretation. The important thing is that the compilation doesn’t require a processor and
memory, which speeds up program compilation.

14. What are variables in Java programming?


Variables in Java are the basic storage units that hold data values. We can access and modify these values. Variables are the memory locations where we can store values. We
need to declare all variables before using them. Also, we can assign a variable with a data type such as a number, string, character, etc.

The syntax for a variable is given as below:

type variableName = value;

type – It can be an integer or string

variableName – The name of the variable

Value – The value assigned to the variable

An example of a variable is given below.

int mynumber = 10;

15. What are the various types of variables in Java?


There are three main variables available in Java. They are:

• Static Variables or class variables


• Instance Variables or non-static fields
• Local Variables

Static Variables: A variable declared with the static keyword is called a static variable. A static variable cannot be a local variable. We can allocate memory only once for these
variables.

Instance Variables: an instance variable declared inside the class but outside the body of a method. An instance variable is instance-specific. So, we cannot share instance
variables.

Local Variables: A variable declared inside a method's body within a class is called a local variable.

Example:

class A
-{
int num-30;//instance variable
static char name=MindMajix;//static variable
void method()
-{
int n=90;//local variable
}
}//end of class

New Section 1 Page 2


16. What is Type Casting in Java?

We use type casting in Java to convert one data type into another. The casting operator can perform typecasting while designing a Java program.

There are two types of typecasting, as listed below.

• Widening Typecasting or automatic – It is about converting a smaller data type into a larger data type size.
• Narrowing Typecasting or manual – It is about converting a larger data type into a smaller data type

The below flow diagram shows the conversions.

17. What are the data types in Java?


Datatypes in Java specify the values and sizes we can store in variables. The data type informs the compiler how to handle a variable or method.

There are two types of data types in Java. They are:

• Primitive Data Types – integers, characters, floating point numbers, and Booleans
• Non-primitive Data Types – arrays, strings, classes, enum, and interfaces.

18. What are the default values and sizes of primitive data types?

Primitive Data Type Default Size Default Value


Integer 4 bytes 0
Character 2 bytes ‘u0000’
Byte 1 byte 0
Short 2 bytes 0
Long 8 bytes 0L
Floating point numbers 4 bytes 0.0f
Double 8 bytes 0.0d
Boolean 1 bit False

19. Write the syntax for object creation in Java.


We need to use the new keyword to create an object in Java. The syntax for the object creation is given below.

ClassName objectName = new ClassName();

ClassName – The name of the class where the object is created

objectName – The name of the object created.

An example of object creation is given below.

// creating an object for MindMajix


MindMajix m1 = new MindMajix();

20. What is the Unicode system?


Unicode is a Universal International Standard Character Encoding system. All the written programming languages comply with this unicode system worldwide. The hexadecimal
number is used to represent characters in the Unicode system. For example, the value 0x0041 in the Unicode system represents the Latin character A.

Java supports the Unicode system and represents characters with a 16-bit data type. The characters in hexadecimal numbers range from 0x0000 to 0xFFFF. At the same time,
characters that are larger than 16 bits are called supplementary characters. They range from 0x10000 to 0x10FFFF.

21. What makes Java a ‘Run Anywhere’ language?


Java compiler converts source codes into bytecodes. Generally, bytecodes are platform-independent, so we can compile and execute them on any platform.

22. Are null, main, delete, and next the keywords of Java?
No, they are not Java keywords.

23. What happens if you write ‘public static void’ instead of ‘static public void’?
Nothing changes. The compilation and execution of programs will occur properly since the order of specifiers doesn’t matter in Java.

24. Is there any default value for a local variable in Java?


No, there is no default value for a local variable in Java. So, we must assign values for local variables before compilation. Otherwise, the compiler will throw an error.

25. How many types of operators are available in Java?

New Section 1 Page 3


25. How many types of operators are available in Java?
Java supports eight types of operators. They are:

1. Arithmetic operators

2. Logical operators

3. Assignment operators

4. Relational operators

5. Unary operators

6. Bitwise operators

7. The ternary or conditional operator

8. Instanceof operator.

26. What is operator precedence in Java?


Java provides a set of rules and regulations to specify how to execute operators. If an expression has many operators, then the execution of the expression is done based on the
operator precedence. This operator precedence evaluates the operators based on the priority. For example, multiplication has the highest priority over addition and subtraction.

27. What are the different logical operators used in Java?

Operator Description
Logical NOT • We use this operator with logical expressions, Boolean type, and relational variables.
• The symbol denotes the Logical NOT operator is "!"
Logical OR • This logical operator operates only the Boolean variable types
• The symbol for this operator is “||”
Logical AND • We can combine many relational operations using this operator. The output will be of Boolean type.
• The “&&” is the symbol for logical AND

28. What is the role of the unary operator in Java?


The unary operator in Java has only one operand. It performs various operations, including negating an expression, incrementing or decrementing a value by one, etc.

An example of the unary operator is given below:

Code:

Main.java
class UnaryExample
-{
public static void main(String args[])
-{
int x=10;
System.out.println(x++);
System.out.println(++x);
System.out.println(x--);
System.out.println(--x);
}
}

Output:

java -cp /tmp/nRpgsmRLTQ/UnaryExample


=== Code Execution Successful ===

29. What are the differences between JVM, JDK, and JRE?

JVM JDK JRE


It stands for Java Virtual Machine It stands for Java Development Kit It stands for Java Runtime Environment
It is the heart of Java programming. It is a development platform and the superset of the JRE. We use JRE for code execution
It executes programs line by line It has the JVM It has the JVM
It converts bytecodes into machine codes. It provides executables, tools, and binaries It contains Java classes, binaries, and JVM.

30. What is the difference between ++a and a++ increment operators?
++a is a prefix increment operator, whereas a++ is a postfix increment operator. The prefix increment operator returns the value ‘a’ after incrementing it. The postfix increment
operator returns the ‘a’ value before incrementing it.

The below example shows the use of the operators.

Code:

class HelloWorld {
public static void main(String args[ ])
int a, b;
// Pre-increment
a = 1;
b = ++a;
System.out.println( b );// prints 2
// Post-increment
a = 1;
b = a++;
System.out.println( b );// prints 1
}

New Section 1 Page 4


}
}

31. Define left shift and right shift operators in Java.


Left Shift: The left shift is a bitwise operator in Java. In this shift, bits are moved towards the left-hand side, and zeros are placed at the rightmost places.

The example below shows the left shift.

Code:

public class LeftShiftOperator {


public static void main(String[] args)
int a=2;//
int i;
i=a<<1;//4
System.out.println("the value of a before left
shift is: " +a);
System.out.println("the value of a after applying
left shift is: " +i);
}
}

Output:

java -cp /tmp/GIYi18A02D/LeftShiftOperator


the value of a before left shift is: 2
the value of a after applying left shift is: 4
=== Code Execution Successful ===|

Right Shift: The right shift is also the bitwise operator in Java. In this shift, bits are moved towards the right-hand side, and zeros are placed at the leftmost place.

The example below shows the right shift.

Code:

public class RightShiftOperator {


public static void main(String[] args) {
int a=2;
int I;
i=a>>1;
System.out.println("the value of a before right shift is:
" +a);
System.out.println("the value of a after applying right

Output:

java -cp /tmp/xy76CaCCuR/RightShiftOperator


the value of a before right shift is: 2
the value of a after applying right shift is: 1
=== Code Execution Successful ===

32. What are the bitwise operators in Java?


We use bitwise operators in Java to perform bit operations.

The following are the bitwise operators in Java. They are:

• Bitwise OR (A|B)
• Bitwise XOR (A^B)
• Bitwise AND (A&B)
• Bitwise complement (~A)
• Left shift ( A<<2)
• Right shift (A>>2)
• Unsigned right shift (>>>)
• Unsigned left shift ( <<<)

33. What is a ternary operator?

We use the ternary operator in Java to replace the if-else statement. It is also known as the conditional operator that uses three operands.

The syntax for the ternary operator is given as:

variable = (expression)? expression true:expression false

The example below shows the use of the ternary operator.

int age = 21;


String result = (age 18) ? "adult" : "minor";

34. What are various access specifiers in Java?


Java has four access specifiers. They are:

1. Private: We can access the methods or classes declared private within the same class.

2. Public: We can access the methods or classes declared public anywhere in a program. In other words, we can access them within and outside the class.

3. Default: By default, all the variables, classes, and methods are of default scope. The default is accessible only within the package.

4. Protected: We can access the methods, variables, and classes defined as private within the same class of the same package or by the subclass of the same class.

35. What are the uses of packages in Java?


By using packages in Java, we can:

New Section 1 Page 5


By using packages in Java, we can:

• arrange the group of classes into a single API unit


• ensure access protection
• control the naming conflicts
• locate related classes easily
• reuse the existing classes

36. What is the constructor?


The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is
allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the
class name. The constructor must not have an explicit return type.

37.How many types of constructors are used in Java?


Based on the parameters passed in the constructors, there are two types of constructors in Java.
• Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default
values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in
the class.
• Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values. In other words, we can say that the
constructors which can accept the arguments are called parameterized constructors.

38. What is the purpose of a default constructor?


The purpose of the default constructor is to assign the default value to the objects. The java compiler creates a default constructor implicitly if there is no constructor in the class.
1. class Student3{
2. int id;
3. String name;
4.
5. void display(){System.out.println(id+" "+name);}
6.
7. public static void main(String args[]){
8. Student3 s1=new Student3();
9. Student3 s2=new Student3();
10. s1.display();
11. s2.display();
12. }
13. }
Test it Now
Output:
0 null
0 null

39. What are the differences between the constructors and methods?
There are many differences between constructors and methods. They are given below.
Java Constructor Java Method
A constructor is used to initialize the state of an object. A method is used to expose the behaviour of an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default constructor if you don't have any constructor in a class. The method is not provided by the compiler in any case.
The constructor name must be same as the class name. The method name may or may not be same as class name.

New Section 1 Page 6


New Section 1 Page 7

You might also like