Challenging Java programs and succeeding in coding interviews-11

Boost your Java coding skills and ace coding interviews with our latest tutorial, “Java Programs and Succeeding in Coding Interviews Part-11”.

In this tutorial, we explore logical testing in programming and how to solve logical errors in Java programs.

Many interviewers often ask candidates to solve logical errors in their programs, so it’s important to sharpen your logical thinking skills.

So, to learn how to correct and fix logical errors in Java programs!

Find and fix errors in the below Java program(1001)

public class JavaTechypid {
   public static void main(String[] args) {
      int num = 5;
      
      if (num > 0) {
         System.out.println(num + " is positive.");
      }
      else if (num == 0) {
         System.out.println(num + " is zero.");
      }
      else {
         System.out.println(num + " is positive.");
      }
   }
}

Find and fix errors in the below Java program(1005)

public class JavaTechypid {
    public static void main(String[] args) {
        int a = 5;
        int b = 7;
        
        if (a + b == 11) {
            System.out.println("The sum is equal to 11.");
        } else {
            System.out.println("The sum is not equal to 11.");
        }
    }
}

Find and fix errors in the below Java program(1036)

public class JavaTechypid {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        int sum = 0;

        if (num1 < num2) {
            for (int i = num1; i <= num2; i++) {
                sum += i;
            }
        } else {
            for (int i = num2; i <= num1; i++) {
                sum += i;
            }
        }

        System.out.println("The sum of numbers between " + num1 + " and " + num2 + " is: " + sum);
    }
}

Find and fix errors in the below Java program(1045)

public class JavaTechypid {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 10;
        int sum = num1 - num2;
        System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
    }
}

Find and fix errors in the below Java program(1114)

// Incorrect program that calculates the factorial of a number
public class JavaTechypid {
    public static int factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n-2);
    }
    
    public static void main(String[] args) {
        int n = 5;
        int result = factorial(n);
        System.out.println("Factorial of " + n + " is " + result);
    }
}

Find and fix errors in the below Java program(1119)

public class JavaTechypid {
    public static void main(String[] args) {
        int a = 5;
        int b = 0;
        int c = a / b; // dividing by zero
        System.out.println("Result: " + c);
    }
}

Errors Solutions(1001)

public class JavaTechypid {
   public static void main(String[] args) {
      int num = 5;
      
      if (num > 0) {
         System.out.println(num + " is positive.");
      }
      else if (num == 0) {
         System.out.println(num + " is zero.");
      }
      else {
         System.out.println(num + " is negative."); // The error is corrected here.
      }
   }
}

The error was in the last condition where it was printing “is positive” instead of “is negative” which is incorrect. So, we need to change the error message to “is negative”.

Errors Solutions(1005)

public class JavaTechypid {
    public static void main(String[] args) {
        int a = 5;
        int b = 7;
        
        if (a + b == 12) {
            System.out.println("The sum is equal to 11.");
        } else {
            System.out.println("The sum is not equal to 11.");
        }
    }
}

To fix this error, we need to change the condition to if (a + b == 11) to if (a + b == 12).

Because the values of a and b are correct, the condition is checking for a sum of 11 instead of 12 (which is the actual sum of a and b).

As a result, the program will always print “The sum is not equal to 11.” regardless of the input values.

Errors Solutions(1036)

public class JavaTechypid {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 20;
        int sum = 0;

        if (num1 <= num2) {
            for (int i = num1; i <= num2; i++) {
                sum += i;
            }
        } else {
            for (int i = num2; i <= num1; i++) {
                sum += i;
            }
        }

        System.out.println("The sum of numbers between " + num1 + " and " + num2 + " is: " + sum);
    }
}

To fix this logical error, we need to change the conditional statement to if (num1 <= num2) instead of if (num1 < num2).

The program is supposed to find the sum of numbers between two given integers, but if num1 is greater than num2, it will actually find the sum of numbers from num2 to num1 instead of num1 to num2.

Errors Solutions(1045)

public class JavaTechypid {
    public static void main(String[] args) {
        int num1 = 5;
        int num2 = 10;
        int sum = num1 + num2;
        System.out.println("The sum of " + num1 + " and " + num2 + " is " + sum);
    }
}

To fix this error, Instead of -, we should use +.

we was trying to calculate the sum of num1 and num2 by subtracting num2 from num1 and storing the result in the sum variable.

Errors Solutions(1114)

// Corrected program that calculates the factorial of a number
public class JavaTechypid {
    public static int factorial(int n) {
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n-1); // corrected error: n-1 instead of n-2
    }
    
    public static void main(String[] args) {
        int n = 5;
        int result = factorial(n);
        System.out.println("Factorial of " + n + " is " + result);
    }
}

The recursive call is now passing n-1 as the argument, which will calculate the correct factorial of the given number.

Errors Solutions(1119)

public class JavaTechypid {
    public static void main(String[] args) {
        int a = 5;
        int b = 0;
        int c;
        if (b != 0) {
            c = a / b;
            System.out.println("Result: " + c);
        } else {
            System.out.println("Division by zero is not allowed.");
        }
    }
}

In this program, we have added an if condition to check if the value of b is zero before performing the division.

If b is not zero, then the division is performed and the result is printed.

Otherwise, the program prints an error message indicating that division by zero is not allowed.

Leave a Reply