Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1. What is Java?

Java is a high-level, object-oriented programming language that is


designed to be platform-independent and write once, run anywhere. It is widely used for
developing a variety of applications, including web, desktop, mobile, and enterprise
systems.
2. What are the main features of Java? The main features of Java include platform
independence, object-oriented programming, automatic memory management
(garbage collection), strong type checking, exception handling, and built-in support for
multithreading.
3. Explain the differences between JDK, JRE, and JVM.
 JDK (Java Development Kit): It is a software development kit that provides the
tools, libraries, and documentation needed to develop Java applications.
 JRE (Java Runtime Environment): It is a runtime environment that contains the
necessary components to run Java applications, including the JVM and core
libraries.
 JVM (Java Virtual Machine): It is an abstract machine that provides the runtime
environment for executing Java bytecode. It converts bytecode into machine-
specific instructions.
4. What are the different data types in Java? Java has two categories of data types:
primitive and reference types. Primitive types include boolean, byte, short, int, long,
float, double, and char. Reference types include classes, interfaces, arrays, and
enumerated types.
5. What is the difference between an object and a class? A class is a blueprint or template
that defines the properties and behaviors of objects. An object, on the other hand, is an
instance of a class. It represents a specific entity in memory and can have its own state
and behavior.
6. What is inheritance in Java? Explain the types of inheritance. Inheritance is a mechanism
in Java that allows a class to inherit the properties and methods of another class. It
promotes code reusability and supports the concept of parent-child relationships. There
are five types of inheritance: single inheritance, multilevel inheritance, hierarchical
inheritance, multiple inheritance (through interfaces), and hybrid inheritance
(combination of multiple and multilevel inheritance).
7. What is the difference between method overloading and method overriding? Method
overloading refers to having multiple methods in the same class with the same name
but different parameters. Method overriding, on the other hand, occurs when a subclass
provides its own implementation of a method that is already defined in its superclass.
Overriding is achieved by using the same method signature.
8. What is the difference between abstract classes and interfaces? An abstract class is a
class that cannot be instantiated and may contain abstract methods. It can also have
non-abstract methods and instance variables. An interface, on the other hand, is a
reference type that defines a contract of methods that implementing classes must
follow. It can only have abstract methods and constants.
9. Explain the concept of multithreading in Java. Multithreading is the concurrent
execution of two or more threads in a single program. Threads are lightweight units of
execution that allow for parallel or asynchronous processing. Multithreading in Java
enables the execution of multiple tasks simultaneously, improving performance and
responsiveness.
What is the use of the synchronized keyword in Java? The synchronized keyword in Java is used
to create mutually exclusive blocks of code or methods. It ensures that only one thread can access the
synchronized block or method at a time, preventing multiple threads from interfering with each other
and maintaining data integrity.

These are just the answers to a few of the Core Java interview questions. Remember, it's
important to understand these concepts in depth and be able to explain them further
during an interview.

11. What is the difference between an abstract class and an interface in Java?

Answer:

 An abstract class is a class that cannot be instantiated and may contain both abstract
and concrete methods. It can have instance variables, constructors, and methods with
different access modifiers. Subclasses of an abstract class must implement or override all
abstract methods.
 An interface, on the other hand, is a collection of abstract methods. It cannot have
instance variables or constructors, and all methods declared in an interface are implicitly
public and abstract. Classes implement interfaces by providing implementations for all
the methods declared in the interface.
12. What is method overloading in Java?

Answer: Method overloading refers to the ability to define multiple methods with the
same name but different parameters in a class. It allows a class to have multiple
methods with the same name but different argument lists. The compiler determines
which method to call based on the arguments provided at the time of method
invocation.

13. What is method overriding in Java?

Answer: Method overriding occurs when a subclass provides its own implementation of
a method that is already defined in its superclass. The method in the subclass must have
the same name, return type, and parameters as the method in the superclass. Method
overriding is used to achieve runtime polymorphism, where the appropriate method
implementation is determined at runtime based on the actual type of the object.

14. What is the difference between final, finally, and finalize in Java?

Answer:

 The final keyword is used to declare a variable, method, or class that cannot be
modified or extended. If a variable is declared as final, its value cannot be changed. If a
method is declared as final, it cannot be overridden in subclasses. If a class is declared as
final, it cannot be subclassed.
 The finally block is used in exception handling to define a code block that will be
executed regardless of whether an exception occurs or not. It is often used to release
resources or perform cleanup operations.
 The finalize() method is a method defined in the Object class. It is called by the
garbage collector before an object is garbage collected. The finalize() method can be
overridden in a class to define custom cleanup operations before an object is destroyed.
15. What are access modifiers in Java?

Answer: Access modifiers are keywords used to specify the accessibility or visibility of
classes, methods, variables, and constructors in Java. There are four access modifiers in
Java:

 public: Accessible from anywhere.


 protected: Accessible within the same package and subclasses.
 default (no modifier): Accessible within the same package only.
 private: Accessible within the same class only.
16. What is the static keyword in Java?

Answer: The static keyword in Java is used to define members (variables or methods)
that belong to the class itself, rather than to individual instances (objects) of the class.
Static members are shared among all instances of the class and can be accessed using
the class name without creating an object. Static variables retain their values across
multiple instances, while static methods can be called without creating an object of the
class.

17. What is the this keyword in Java?


Answer: The this keyword in Java is a reference to the current object. It is used to refer
to the instance variables and methods of the current class. It is often used to
disambiguate between instance variables and method parameters with the same name.
The this keyword can also be used to invoke one constructor from another constructor
in the same class.

18. What is the difference between == and .equals() in Java?

Answer:

 The == operator in Java is used to compare the equality of two objects' references. It
checks if two object references point to the same memory location.
 The .equals() method in Java is used to compare the equality of the contents or values
of two objects. It compares the actual data within the objects, as defined by the
implementation of the .equals() method in the class. By default, the .equals()
method compares the memory addresses, but it can be overridden to compare the
object's contents.
19. What is the difference between a StringBuilder and a StringBuffer?

Answer: Both StringBuilder and StringBuffer are used to manipulate strings, but
there are some differences:

 StringBuilder is not thread-safe, while StringBuffer is thread-safe.


 StringBuilder is more efficient in terms of performance, as it is not synchronized.
 StringBuffer is slower than StringBuilder but provides thread safety through
synchronized methods.
20. What is the difference between checked and unchecked exceptions in Java?

Answer:

 Checked exceptions are exceptions that must be declared in a method's signature using
the throws keyword or caught using a try-catch block. The compiler enforces the
handling of checked exceptions during compilation.
 Unchecked exceptions (runtime exceptions) are exceptions that do not need to be
declared in a method's signature or caught explicitly. They occur at runtime and are
typically caused by programming errors or exceptional conditions that are difficult to
recover from.

21. Write a Java program to swap two numbers without using a temporary variable.
public class NumberSwap {

public static void main(String[] args) {

int a = 10;

int b = 20;

System.out.println("Before swapping: a = " + a + ", b = " + b);

a = a + b;

b = a - b;

a = a - b;

System.out.println("After swapping: a = " + a + ", b = " + b);

Write a Java program to find the factorial of a number using recursion.

public class Factorial {

public static void main(String[] args) {

int number = 5;

int factorial = calculateFactorial(number);

System.out.println("Factorial of " + number + " is " + factorial);

public static int calculateFactorial(int n) {

if (n == 0) {

return 1;

} else {

return n * calculateFactorial(n - 1);


}

Write a Java program to reverse a string.

public class StringReverse {

public static void main(String[] args) {

String input = "Hello, World!";

String reversed = reverseString(input);

System.out.println("Reversed string: " + reversed);

public static String reverseString(String str) {

StringBuilder sb = new StringBuilder(str);

return sb.reverse().toString();

public class SecondLargestNumber {

public static void main(String[] args) {

int[] numbers = {10, 5, 8, 20, 15};

int secondLargest = findSecondLargest(numbers);

System.out.println("Second largest number: " + secondLargest);

Write a Java program to find the second largest number in an array.

public static int findSecondLargest(int[] arr) {

int largest = Integer.MIN_VALUE;

int secondLargest = Integer.MIN_VALUE;


for (int num : arr) {

if (num > largest) {

secondLargest = largest;

largest = num;

} else if (num > secondLargest && num != largest) {

secondLargest = num;

return secondLargest;

You might also like