java interview questions
java interview questions
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.
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.
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.
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
• 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.
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.
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.
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.
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.
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.
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
We use type casting in Java to convert one data type into another. The casting operator can perform typecasting while designing a Java program.
• 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
• 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?
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.
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.
1. Arithmetic operators
2. Logical operators
3. Assignment operators
4. Relational operators
5. Unary operators
6. Bitwise operators
8. Instanceof operator.
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
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:
29. What are the differences between JVM, JDK, and JRE?
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.
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
}
Code:
Output:
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.
Code:
Output:
• 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 ( <<<)
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.
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.
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.