Top Java Coding Interview Questions

Q1. How to reverse a String in Java?

To reverse a string in Java, you can use several methods. One common approach is to convert the string into a character array, then iterate over the array and swap the characters from the beginning to the end. Another way is to use the StringBuilder class, which provides a reverse() method that can be applied to the string. Additionally, you can use the StringBuffer class, which is similar to StringBuilder but is thread-safe.

Whichever method you choose, make sure to handle null or empty strings appropriately and test your code thoroughly to ensure that it works as expected. Reversing a string may seem like a simple task, but it requires attention to detail and careful implementation to avoid errors.

The following example code shows a few approaches to reverse a String :

Approach 1: By Reverse Iteration of the String

Output: tnioPgnidoCavaJ

Approach 2: By using reverse() method of StringBuilder

Output: tnioPgnidoCavaJ

Q2. How to swap two numbers without using a third variable in Java?

Another approach is to add and subtract the values of the two variables. By subtracting the value of one variable from the sum of the two variables, the value of the other variable can be obtained. This value can then be subtracted from the sum to obtain the original value of the first variable. Using this technique, it is possible to swap two variables in Java without using a third variable.

The following example code shows a way to swap two numbers without using a third variable in Java:

Output:

Before swapping value of x is 20 and y is 30

After swapping value of x is 30 and y is 20

Q3. Write a Java program to check given number is a prime number.

If the number is only divisible by 1 and itself, then it is a prime number. The below program displays the result, indicating whether the given number is prime or not.

Q4. Write a Java program to calculate the discount and amount to be paid based on the price range.

This program can take inputs from the original prices and apply the discount percentage according to the specified price range. The discount percentage can be different for different price ranges. Once the discount is applied, the program can calculate the final amount to be paid by subtracting the discount amount from the original price.

Below chart data are the examples of total shopping amount and applicable discount :

Q5. Write a Java program to display Floyd's Triangle.

Floyd's Triangle is a pattern of numbers that starts with 1 and increases by 1 each time. It is a right-angled triangle where each row represents a line of numbers. The first row has only one number, the second row has two numbers, and so on.

To print Floyd's Triangle in Java, you can use a nested loop. The outer loop controls the number of rows, and the inner loop controls the number of elements in each row. By using the appropriate loop conditions and incrementing the number value, you can easily print Floyd's Triangle in Java. This pattern is often used in programming exercises to practice nested loops and pattern printing.

Q6. Write a Java program to print the following pattern.

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

Q7. Write a Java program to print the following pattern.

5 5 5 5 5 5 5 5 5

4 4 4 4 4 4 4

3 3 3 3 3

2 2 2

1

Q8. Write a Java program to print the following pattern.

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

5 5 5 5 5