Top 20 Most Common Java Interview Questions You Need To Know

Here are 20 most common Java Interview Questions you should know and how to Answer them :

1. What is the difference between ArrayList and LinkedList in Java?
2. How to implement inheritance in Java?
3. How to override a method in Java?
4. How to convert a String to an int in Java?
5. How to sort an ArrayList in Java?
6. How to check if an object is null in Java?
7. How to compare two strings in Java?
8. How to create a thread in Java?
9. How to parse a JSON object in Java?
10. How to print an array in Java?
11. How to create a class in Java?
12. How to generate a random number in Java?
13. How to convert a List to an Array in Java?
14. How to split a string in Java?
15. How to check if a string is numeric in Java?
16. How to implement an interface in Java?
17. How to reverse a string in Java?
18. How to remove duplicates from an ArrayList in Java?
19. How to declare a two-dimensional array in Java?
20. How to check if a number is prime in Java?

1. What is the difference between ArrayList and LinkedList in Java?
ArrayList is an implementation of the List interface that uses an array to store its elements, while LinkedList is an implementation of the List interface that uses a linked list to store its elements. ArrayList is generally faster for read operations, but slower for write operations, while LinkedList is generally faster for write operations, but slower for read operations.

2. How to implement inheritance in Java?
Inheritance in Java is implemented using the “extends” keyword.
For example:
public class ChildClass extends ParentClass {

3. How to override a method in Java?
To override a method in Java, you need to use the “override” annotation and ensure that the method has the same name, parameters, and return type as the method being overridden.
For example:
@Override
public void someMethod() {

4. How to convert a String to an int in Java?
To convert a String to an int in Java, you can use the Integer.parseInt() method.
For example:
int x = Integer.parseInt(“123”);

5. How to sort an ArrayList in Java?
To sort an ArrayList in Java, you can use the Collections.sort() method.
For example:
Collections.sort(myArrayList);

6. How to check if an object is null in Java?
To check if an object is null in Java, you can use the “==” operator. For example:
if (myObject == null) {

7. How to compare two strings in Java?
To compare two strings in Java, you can use the equals() method. For example:
if (string1.equals(string2)) {

8. How to create a thread in Java?
To create a thread in Java, you can extend the Thread class and override the run() method, or you can implement the Runnable interface and pass an instance of it to the Thread constructor. For example:
public class MyThread extends Thread {

9. How to parse a JSON object in Java?
To parse a JSON object in Java, you can use a library such as Gson or Jackson.
For example:
JsonObject obj = new JsonParser().parse(jsonString).getAsJsonObject();

10. How to print an array in Java?
To print an array in Java, you can use the Arrays.toString() method.
For example:
System.out.println(Arrays.toString(myArray));

11. How to create a class in Java?
To create a class in Java, use the “class” keyword followed by the class name and a set of curly braces. Inside the curly braces, you can define fields, methods, and constructors for the class.
For example:
public class MyClass { }

12. How to generate a random number in Java?
To generate a random number in Java, you can use the Math.random() method or the Random class.
For example:
int x = (int)(Math.random() * 100);
Random r = new Random();
int x = r.nextInt(100);

13. How to convert a List to an Array in Java?
To convert a List to an Array in Java, you can use the toArray() method of the List interface.
For example:
String[] array = myList.toArray(new String[myList.size()]);

14. How to split a string in Java?
To split a string in Java, you can use the split() method of the String class.
For example:
String[] words = myString.split(” “);

15. How to check if a string is numeric in Java?
To check if a string is numeric in Java, you can use the isDigit() method of the Character class or a regular expression. For example:
boolean isNumeric = true;
for (char c : myString.toCharArray()) {
if (!Character.isDigit(c)) {
isNumeric = false;
break;
}
}
boolean isNumeric = myString.matches(“\d+”);

16. How to implement an interface in Java?
To implement an interface in Java, use the “implements” keyword followed by the interface name.
For example:
public class MyClass implements MyInterface { }

17. How to reverse a string in Java?
To reverse a string in Java, you can use a loop to iterate through the string and append each character to a new string in reverse order. Alternatively, you can use the StringBuilder class and its reverse() method.
For example:
String reversed = “”;
for (int i = myString.length() – 1; i >= 0; i–) {
reversed += myString.charAt(i);
}
String reversed = new StringBuilder(myString).reverse().toString();

18. How to remove duplicates from an ArrayList in Java?
To remove duplicates from an ArrayList in Java, you can use the removeIf() method and a lambda expression.
For example:
myArrayList.removeIf(i -> Collections.frequency(myArrayList, i) > 1);

19. How to declare a two-dimensional array in Java?
To declare a two-dimensional array in Java, use the following syntax:
int[][] myArray = new int[rows][columns];

20. How to check if a number is prime in Java?
To check if a number is prime in Java, you can use a loop to check if the number is divisible by any number between 2 and the square root of the number.
For example:
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}

Refer below for more Java interview questions.

© 2023, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags