Top Java Programming Interview Questions for Beginners (With Solutions)


If you are preparing for a junior software engineering role or a university exam, mastering basic control flow in Java is essential. Interviewers don't just want to see if you can write the code; they want to know if you understand the logic behind it.

Below are some of the most common foundational Java programming questions, complete with the logic explanation and the working code.

1. Check if a Number is Even or Odd

The Logic: To determine if a number is even or odd, we use the modulo operator (%). The modulo operator returns the remainder of a division operation. If a number divided by 2 leaves a remainder of 0, it is even. Otherwise, it is odd. This is an O(1) constant time operation, meaning it executes instantly regardless of the number's size.

The Solution:

import java.util.Scanner; 


public class Main { 

    public static void main(String[] args) { 

        Scanner input = new Scanner(System.in); 

        System.out.print("Enter a number: "); 

        int num = input.nextInt(); 

        

        // Using modulo operator to check the remainder

        if (num % 2 == 0) { 

            System.out.println(num + " is even."); 

        } else { 

            System.out.println(num + " is odd."); 

        } 

        

        input.close(); // Always close your scanner to prevent memory leaks

    } 

}

2. Find the Largest Among Three Numbers

The Logic: Finding the largest of three numbers requires standard if-else if conditional statements. We compare the first number against the second and third. If it is greater than both, it is the largest. If not, we move to the second number, and so on.

The Solution:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter three numbers: "); int num1 = input.nextInt(); int num2 = input.nextInt(); int num3 = input.nextInt(); if (num1 >= num2 && num1 >= num3) { System.out.println(num1 + " is the largest."); } else if (num2 >= num1 && num2 >= num3) { System.out.println(num2 + " is the largest."); } else { System.out.println(num3 + " is the largest."); } input.close(); } }


Good Luck! 😊