What is Java and what are its features?
Answer: Java is an object-oriented programming language that is widely used for developing applications. Its features include:
  • Platform independent
  • Object-oriented
  • Automatic memory management
  • Multithreaded
  • Robust
  • Simple
  • Secure


What is the difference between JDK, JRE, and JVM?
Answer: JDK (Java Development Kit) is a software development kit used for developing Java applications. It includes a compiler, debugger, and other tools for development. JRE (Java Runtime Environment) is a software package that provides the environment for running Java applications. It includes the JVM (Java Virtual Machine) and the Java class libraries. JVM is a virtual machine that is responsible for running the Java bytecode.

What is the difference between an abstract class and an interface?
Answer: An abstract class is a class that cannot be instantiated, but can have abstract methods that are implemented by its subclasses. An interface is a collection of abstract methods that do not have an implementation. A class can implement multiple interfaces, but can only inherit from one abstract class.

What is a constructor and what is its purpose?
Answer: A constructor is a special method in a class that is used to initialize the object of the class. It is called when an object is created and can be used to set the initial values of the instance variables.

What is the difference between a static and non-static method?
Answer: A static method belongs to the class and can be called without creating an object of the class. A non-static method belongs to an instance of the class and can only be called on an object of the class.

What is the difference between an ArrayList and a LinkedList?
Answer: An ArrayList is implemented as a dynamic array and is good for random access to elements. A LinkedList is implemented as a linked list and is good for adding and removing elements from the beginning or end of the list.

What is the difference between an abstract class and an interface?
Answer: An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract class can have instance variables, while an interface cannot. A class can inherit from only one abstract class, but can implement multiple interfaces.



What is the difference between a stack and a queue?
Answer: A stack is a data structure that uses a last-in, first-out (LIFO) approach for adding and removing elements. A queue is a data structure that uses a first-in, first-out (FIFO) approach for adding and removing elements.

What is a thread and what is its purpose?
Answer: A thread is a lightweight process that runs concurrently with other threads within a program. It is used to perform multiple tasks simultaneously within a single application.

What is the difference between a checked and an unchecked exception?
  • A checked exception is an exception that must be declared in a method or a constructor using the "throws" keyword, and must be caught or propagated to the calling method.  Examples of checked exceptions include IOException and SQLException,
  • An unchecked exception is an exception that does not need to be declared or caught, and can be thrown at runtime. Examples of unchecked exceptions include NullPointerException and ArithmeticException.

What is the difference between an abstract class and an interface in Java?
  • An abstract class is a class that can't be instantiated, and is typically used as a base class for other classes to inherit from. It may have one or more abstract methods, which must be implemented by any concrete subclass.
  • An interface is similar to an abstract class in that it can't be instantiated, but it is used to define a set of methods that a class must implement. An interface can't have any instance variables, and all methods defined in an interface are implicitly abstract.

What is the difference between a class and an object in Java?
Answer: A class is a blueprint or template for creating objects. It defines the properties and methods that objects of that class will have. An object is an instance of a class, and represents a specific thing or concept.

What is a constructor in Java?
Answer: A constructor is a special method that is used to create and initialize objects of a class. It has the same name as the class, and is called when a new object of that class is created. A constructor can take arguments to set initial values for the object's properties.

What is the difference between static and non-static methods in Java?
  • A static method is a method that is associated with the class itself, rather than with any particular instance of the class. It can be called directly on the class, without creating an instance of the class.
  • A non-static method, on the other hand, is associated with a specific instance of the class, and can only be called on an object that has been created from that class.

What is the difference between a stack and a queue in Java?
  • A stack is a data structure that works on the "last in, first out" (LIFO) principle. Elements are added to and removed from the top of the stack.
  • A queue is a data structure that works on the "first in, first out" (FIFO) principle. Elements are added to the back of the queue and removed from the front.

What is the purpose of the "static" keyword in Java?

Answer: The "static" keyword in Java is used to create class-level variables and methods that can be accessed directly on the class, rather than on an instance of the class. Static methods and variables are shared across all instances of a class.

What is the difference between "==" and ".equals()" in Java?
Answer: In Java, "==" is used to compare the reference values of two objects, while ".equals()" is used to compare the contents of two objects. In other words, "==" checks if two objects are the same object in memory, while ".equals()" checks if two objects have the same values.

What is a thread in Java?
Answer: A thread in Java is a lightweight process that runs concurrently with other threads in the same program. Each thread has its own call stack, but shares the same heap memory with other threads. Threads can be used to perform multiple tasks simultaneously, such as handling user input and updating the user interface.

What is an exception in Java?
Answer: An exception in Java is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions. Exceptions can be caused by a variety of factors, such as incorrect user input, a system error, or a programming error.

What is the difference between JDK, JRE, and JVM?
  • JDK stands for Java Development Kit, which is a software development kit used to develop Java applications. It includes a compiler, debugger, and other tools necessary for development.
  • JRE stands for Java Runtime Environment, which is a software environment that allows Java programs to run on any machine that has it installed. It includes the JVM and necessary libraries.
  • JVM stands for Java Virtual Machine, which is an abstract machine that provides the runtime environment in which Java bytecode can be executed.

What is the difference between abstract class and interface in Java?
  • An abstract class is a class that cannot be instantiated and is used as a base for subclasses. It can have both abstract and concrete methods, and can also have instance variables.
  • An interface is a collection of abstract methods that must be implemented by any class that implements the interface. It cannot have instance variables, constructors, or concrete methods.

What is the purpose of the static keyword in Java?
  • The static keyword can be used to create class variables and methods that belong to the class rather than to an instance of the class.
  • Class variables can be accessed by any instance of the class, while class methods can be called without creating an instance of the class.

What is the difference between checked and unchecked exceptions in Java?
  • Checked exceptions are exceptions that must be caught or declared in a method's throws clause. Examples include IOException and SQLException.
  • Unchecked exceptions are exceptions that do not need to be declared or caught. Examples include NullPointerException and ArrayIndexOutOfBoundsException.

What is the difference between an interface and an abstract class?
  • An interface is a collection of abstract methods that must be implemented by any class that implements the interface. It cannot have instance variables or concrete methods.
  • An abstract class is a class that cannot be instantiated and is used as a base for subclasses. It can have both abstract and concrete methods, and can also have instance variables.

What is the difference between final, finally, and finalize in Java?
  • final is a keyword that can be used to make a variable constant or prevent a method from being overridden.
  • finally is a code block that is used in a try-catch-finally statement to execute code after the try and catch blocks are finished, regardless of whether an exception was thrown or not.
  • finalize is a method that is called by the garbage collector to release resources before an object is destroyed.

What is the difference between a checked and unchecked exception in Java?
  • A checked exception is a type of exception that must be declared in a method or handled using a try-catch block. Examples include IOException and SQLException.
  • An unchecked exception is a type of exception that does not need to be declared or handled. Examples include NullPointerException and ArrayIndexOutOfBoundsException.

What is the difference between an abstract class and an interface in Java?
  • An abstract class is a class that cannot be instantiated and can have both concrete and abstract methods. It can also have instance variables, constructors, and static methods. A subclass of an abstract class must either implement all the abstract methods or be declared as an abstract class itself.
  • An interface is a collection of abstract methods that is used to define a contract between a class and the outside world. It cannot have instance variables, constructors, or concrete methods. A class can implement multiple interfaces.


What is polymorphism in Java?
Answer: Polymorphism is the ability of an object to take on many forms. In Java, this is achieved through method overloading and method overriding. Method overloading is when a class has multiple methods with the same name but different parameters. Method overriding is when a subclass has a method with the same name and signature as a method in the superclass, and the subclass implementation is used instead of the superclass implementation.

What is a static method in Java?
Answer: A static method is a method that belongs to the class and not to any specific instance of the class. It can be called using the class name instead of an instance of the class. Static methods cannot access non-static variables or methods of the class, as they do not have a specific instance to work with.

What is the difference between a HashSet and a TreeSet in Java?
Answer: Both HashSet and TreeSet are implementations of the Set interface in Java.
  • HashSet is an unordered collection that uses a hash table to store its elements. It allows for fast access and retrieval of elements, but the order of elements is not guaranteed.
  • TreeSet is a sorted collection that uses a red-black tree to store its elements. It maintains elements in ascending order based on their natural order or a specified comparator. Access and retrieval of elements are slower than HashSet, but the order of elements is guaranteed.

What is an abstract class?
Answer: An abstract class is a class that is declared abstract and cannot be instantiated. It is used as a base class for other classes to extend from, and can have abstract and non-abstract methods.

What is the difference between static and non-static methods?
Answer: A static method belongs to the class and can be called without an instance of the class being created. A non-static method belongs to an instance of the class and can only be called on an instance of the class.

What is the difference between an interface and an abstract class?
Answer: An interface is a contract that specifies a set of methods that a class implementing the interface must implement. An abstract class is a class that cannot be instantiated and serves as a base class for other classes to extend from.

What is the difference between a stack and a queue?
Answer: A stack is a data structure that follows the Last-In-First-Out (LIFO) principle, where the last element added to the stack is the first element removed. A queue is a data structure that follows the First-In-First-Out (FIFO) principle, where the first element added to the queue is the first element removed.

What is the purpose of the "this" keyword in Java?
Answer: The "this" keyword refers to the current object instance and is used to access its instance variables and methods.

What is the difference between an interface and an abstract class in Java?
Answer: An abstract class is a class that can have both abstract and non-abstract methods, and it cannot be instantiated. An interface is a collection of abstract methods and cannot contain any implementation. Classes can implement multiple interfaces but can only inherit from one abstract class.

What is the difference between a checked and unchecked exception?
Answer: Checked exceptions are checked at compile time and must be either handled with a try-catch block or declared in the method signature with the throws keyword. Unchecked exceptions are not checked at compile time and do not have to be handled or declared.

What is the difference between a class variable and an instance variable in Java?
Answer: A class variable, also known as a static variable, is associated with the class itself rather than any specific instance of the class. An instance variable is associated with a specific instance of the class.

What is the difference between method overloading and method overriding in Java?
Answer: Method overloading is when two or more methods in a class have the same name but different parameters. Method overriding is when a subclass provides its own implementation of a method that is already provided by its parent class.

What is the difference between a constructor and a method in Java?
Answer: A constructor is a special method that is used to initialize objects, while a method is used to perform some action on an object. Constructors have the same name as the class and do not have a return type, while methods have a return type.

What is the purpose of the final keyword in Java?
Answer: The final keyword can be used to create constants, which cannot be changed after they are initialized. It can also be used to prevent a class from being subclassed or a method from being overridden.

What is the difference between a public, private, and protected modifier in Java?
Answer: Public variables and methods can be accessed from anywhere in the program. Private variables and methods can only be accessed within the same class. Protected variables and methods can be accessed within the same class, subclasses, and classes in the same package.

What is method overloading in Java?
Answer:Method overloading is when a class has two or more methods with the same name, but with different parameters. The methods are differentiated by the number or types of arguments passed in.

What is method overriding in Java?
Answer:Method overriding is when a subclass provides its own implementation for a method that is already defined in its superclass. The subclass method must have the same name, return type, and parameters as the superclass method.

What is the difference between final, finally, and finalize in Java?
Answer:Final is a keyword used to define a constant variable, a method that cannot be overridden, or a class that cannot be extended. Finally is a block of code that is always executed after a try block, regardless of whether an exception is thrown or not. Finalize is a method that is called by the garbage collector before an object is destroyed.

What is the difference between a constructor and a method in Java?
Answer:A constructor is a special method that is used to initialize an object of a class. It is called when an object is created, and has the same name as the class. A method is a regular function that performs some action on an object, and can be called multiple times with different arguments.

What is a package in Java?
Answer:A package is a collection of related classes and interfaces that are grouped together for organizational purposes. It helps to avoid naming conflicts and makes it easier to manage large programs.

What is the difference between an ArrayList and a LinkedList in Java?
Answer:An ArrayList is a resizable array implementation of the List interface, while a LinkedList is a doubly-linked list implementation of the same interface. ArrayLists are generally faster for random access of elements, while LinkedLists are faster for adding or removing elements from the middle of the list.

What is the difference between a checked and an unchecked exception in Java?
Answer:A checked exception is a type of exception that is checked at compile time, and must be either caught or declared in the method signature. Examples include IOException and ClassNotFoundException. An unchecked exception is not checked at compile time, and can be thrown at runtime without being caught or declared. Examples include NullPointerException and ArrayIndexOutOfBoundsException.

What is the difference between a thread and a process in Java?
Answer: A process is an instance of a running program, while a thread is a unit of execution within a process. A process can contain multiple threads, which can run concurrently and share memory and other resources. Threads are lighter weight than processes, and can be created and destroyed more quickly.

Java Coding Round Interview Questions and Answers

1. Write a program to check if a given string is a palindrome or not.

import java.util.Scanner;

public class Palindrome {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String input = scanner.nextLine();

        String reversedInput = new StringBuilder(input).reverse().toString();

        if (input.equals(reversedInput)) {
            System.out.println("The string is a palindrome.");
        } else {
            System.out.println("The string is not a palindrome.");
        }
    }
}

2. Write a program to find the factorial of a given number.

import java.util.Scanner;

public class Factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int input = scanner.nextInt();

        int factorial = 1;
        for (int i = 1; i <= input; i++) {
            factorial *= i;
        }

        System.out.println("The factorial of " + input + " is " + factorial);
    }
}


3. Write a program to find the sum of all elements in an array.

public class ArraySum {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int sum = 0;

        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }

        System.out.println("The sum of the array elements is " + sum);
    }
}

4. Write a program to find the maximum element in an array.

public class MaxArray {
    public static void main(String[] args) {
        int[] array = {10, 20, 30, 40, 50};
        int max = array[0];

        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }

        System.out.println("The maximum element in the array is " + max);
    }
}

5. Write a program to reverse an array.

import java.util.Arrays;

public class ReverseArray {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int startIndex = 0;
        int endIndex = array.length - 1;

        while (startIndex < endIndex) {
            int temp = array[startIndex];
            array[startIndex] = array[endIndex];
            array[endIndex] = temp;

            startIndex++;
            endIndex--;
        }

        System.out.println("The reversed array is " + Arrays.toString(array));
    }
}

6. Write a program to print the first 10 numbers in the Fibonacci sequence.

public class Fibonacci {
    public static void main(String[] args) {
        int n = 10, t1 = 0, t2 = 1;
        System.out.print("First " + n + " terms: ");

        for (int i = 1; i <= n; ++i) {
            System.out.print(t1 + " + ");

            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

7. Write a program to find the largest and smallest elements in an array.

import java.util.Arrays;

public class ArrayMinMax {
    public static void main(String[] args) {
        int[] arr = {5, 2, 9, 1, 7};
        int min = Arrays.stream(arr).min().getAsInt();
        int max = Arrays.stream(arr).max().getAsInt();
        System.out.println("Minimum element: " + min);
        System.out.println("Maximum element: " + max);
    }
}

8. Write a program to sort an array of strings in alphabetical order.

import java.util.Arrays;

public class SortStrings {
    public static void main(String[] args) {
        String[] arr = {"apple", "banana", "orange", "grape"};
        Arrays.sort(arr);
        System.out.println("Sorted array: " + Arrays.toString(arr));
    }
}

9. Write a program to check if a given number is prime or not.

import java.util.Scanner;

public class PrimeNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        boolean isPrime = true;

        if (num <= 1) {
            isPrime = false;
        } else {
            for (int i = 2; i <= num / 2; ++i) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }

        if (isPrime)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

10. Write a program to reverse a string.

import java.util.Scanner;

public class ReverseString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        StringBuilder sb = new StringBuilder(str);
        sb.reverse();
        System.out.println("Reversed string: " + sb.toString());
    }
}

11. Write a Java program to find the largest element in an array.

public class ArrayMax {
  public static void main(String[] args) {
    int[] arr = { 3, 5, 2, 7, 1 };
    int max = arr[0];
    for (int i = 1; i < arr.length; i++) {
      if (arr[i] > max) {
        max = arr[i];
      }
    }
    System.out.println("Maximum element: " + max);
  }
}

12. Write a Java program to check if a given string is a palindrome.

public class Palindrome {
  public static void main(String[] args) {
    String str = "racecar";
    boolean isPalindrome = true;
    for (int i = 0; i < str.length() / 2; i++) {
      if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
        isPalindrome = false;
        break;
      }
    }
    if (isPalindrome) {
      System.out.println("The string is a palindrome.");
    } else {
      System.out.println("The string is not a palindrome.");
    }
  }
}

13. Write a Java program to sort an array in ascending order.

public class ArraySort {
  public static void main(String[] args) {
    int[] arr = { 3, 5, 2, 7, 1 };
    Arrays.sort(arr);
    for (int i = 0; i < arr.length; i++) {
      System.out.println(arr[i]);
    }
  }
}

14. Write a Java program to calculate the factorial of a number.

public class Factorial {
  public static void main(String[] args) {
    int n = 5;
    int factorial = 1;
    for (int i = 1; i <= n; i++) {
      factorial *= i;
    }
    System.out.println("Factorial of " + n + " is " + factorial);
  }
}

15. Write a Java program to check if a given string is a palindrome or not.

public class Palindrome {
    public static boolean isPalindrome(String str) {
        int left = 0, right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right))
                return false;
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String str = "racecar";
        if (isPalindrome(str))
            System.out.println(str + " is a palindrome");
        else
            System.out.println(str + " is not a palindrome");
    }
}


16. Write a Java program to find the sum of all prime numbers between 1 and n.

public class PrimeSum {
    public static boolean isPrime(int num) {
        if (num <= 1)
            return false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }

    public static int primeSum(int n) {
        int sum = 0;
        for (int i = 2; i <= n; i++) {
            if (isPrime(i))
                sum += i;
        }
        return sum;
    }

    public static void main(String[] args) {
        int n = 10;
        System.out.println("Sum of prime numbers between 1 and " + n + " is " + primeSum(n));
    }
}

17. Write a Java program to reverse a given integer without using any built-in functions.

public class ReverseInt {
    public static int reverse(int num) {
        int rev = 0;
        while (num != 0) {
            int digit = num % 10;
            rev = rev * 10 + digit;
            num /= 10;
        }
        return rev;
    }

    public static void main(String[] args) {
        int num = 12345;
        System.out.println("Reverse of " + num + " is " + reverse(num));
    }
}

18. Write a Java program to find the second largest element in an array of integers.

public class SecondLargest {
    public static int secondLargest(int[] arr) {
        int max1 = arr[0], max2 = Integer.MIN_VALUE;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max1) {
                max2 = max1;
                max1 = arr[i];
            } else if (arr[i] > max2 && arr[i] != max1) {
                max2 = arr[i];
            }
        }
        return max2;
    }

    public static void main(String[] args) {
        int[] arr = {3, 5, 1, 7, 4, 8};
        System.out.println("Second largest element in the array is " + secondLargest(arr));
    }
}

19. Write a program to reverse a string without using the "StringBuilder" class.

public static String reverseString(String str) {
    char[] charArray = str.toCharArray();
    int left = 0;
    int right = charArray.length-1;
    while(left < right) {
        char temp = charArray[left];
        charArray[left] = charArray[right];
        charArray[right] = temp;
        left++;
        right--;
    }
    return new String(charArray);
}

20. Write a program to find the sum of digits of a number recursively.

public static int sumOfDigits(int num) {
    if(num == 0) {
        return 0;
    }
    return (num%10) + sumOfDigits(num/10);
}

21. Write a program to find if a number is a palindrome or not.

public static boolean isPalindrome(int num) {
    int reversed = 0;
    int temp = num;
    while(temp != 0) {
        int remainder = temp%10;
        reversed = (reversed*10) + remainder;
        temp = temp/10;
    }
    return num == reversed;
}

22. Write a program to reverse a string without using the built-in reverse function.

public static String reverseString(String str) {
    StringBuilder sb = new StringBuilder();
    for (int i = str.length() - 1; i >= 0; i--) {
        sb.append(str.charAt(i));
    }
    return sb.toString();
}

23. Write a program to find the first non-repeating character in a string.

public static char findFirstNonRepeatingChar(String str) {
    int[] charCounts = new int[256];
    for (int i = 0; i < str.length(); i++) {
        charCounts[str.charAt(i)]++;
    }
    for (int i = 0; i < str.length(); i++) {
        if (charCounts[str.charAt(i)] == 1) {
            return str.charAt(i);
        }
    }
    return '\0';
}

24. Write a Java program to reverse a string without using any built-in functions or libraries.

public static String reverseString(String str) {
    char[] arr = str.toCharArray();
    int i = 0, j = arr.length - 1;
    while (i < j) {
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        i++;
        j--;
    }
    return new String(arr);
}


25. Write a Java program to find the sum of all even numbers in an array of integers.

public static int sumOfEvens(int[] arr) {
    int sum = 0;
    for (int num : arr) {
        if (num % 2 == 0) {
            sum += num;
        }
    }
    return sum;
}

26. Write a program to find the Fibonacci series of a given number in Java.

public static int fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    int fib = 1, prevFib = 1;
    for (int i = 2; i < n; i++) {
        int temp = fib;
        fib += prevFib;
        prevFib = temp;
    }
    return fib;
}

27. Write a program to find the largest and smallest number in an array in Java

public static void findMinMax(int[] arr) {
    int min = arr[0];
    int max = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    System.out.println("Min = " + min);
    System.out.println("Max = " + max);
}

27. Write a Java program to check if a number is prime or not.

public class PrimeNumber {
  public static boolean isPrime(int n) {
    if (n <= 1) {
      return false;
    }
    for (int i = 2; i <= Math.sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
  
  public static void main(String[] args) {
    int n = 17;
    if (isPrime(n)) {
      System.out.println(n + " is a prime number.");
    } else {
      System.out.println(n + " is not a prime number.");
    }
  }
}


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

public static int secondLargest(int[] arr) {
    int largest = Integer.MIN_VALUE;
    int secondLargest = Integer.MIN_VALUE;
    for(int i=0; i largest) {
            secondLargest = largest;
            largest = arr[i];
        }
        else if(arr[i] > secondLargest && arr[i] != largest) {
            secondLargest = arr[i];
        }
    }
    return secondLargest;
}