Java Operators Practice Questions with Solutions

Operators are one of the most fundamental concepts in Java programming. They are special symbols used to perform operations on variables and values. Whether you’re adding numbers, comparing values, making decisions, or manipulating bits, operators play a crucial role in every Java program.

Java provides several types of operators, including Arithmetic Operators, Relational Operators, Logical Operators, Assignment Operators, Unary Operators, Ternary Operators, and Bitwise Operators. Understanding these operators is essential for writing efficient programs and solving coding problems.

Java operators are widely used in:

  • Mathematical Calculations
  • Decision Making
  • Conditional Statements
  • Loops
  • Object-Oriented Programming
  • Data Processing
  • Game Development
  • Android Applications
  • Enterprise Software Development

In this chapter, you’ll solve beginner-friendly and interview-oriented Java practice questions based on different types of operators. Each question includes a complete Java solution, sample input/output, explanation, and concepts covered to strengthen your programming skills. Java Operators practice questions with solutions help to understand the concepts.


1. Java Program to Perform Addition Using Arithmetic Operator

Problem Statement

Write a Java program to add two numbers using the Arithmetic Addition (+) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        int sum = firstNumber + secondNumber;

        System.out.println("Sum = " + sum);

        scanner.close();
    }

}

Sample Input

Enter First Number: 15
Enter Second Number: 20

Sample Output

Sum = 35

Explanation

The addition operator (+) calculates the sum of two integer values entered by the user.

Formula:

Sum = First Number + Second Number

Concepts Covered

  • Arithmetic Operator
  • Addition
  • Scanner Class
  • User Input
  • Variables

2. Java Program to Perform Subtraction Using Arithmetic Operator

Problem Statement

Write a Java program to subtract two numbers using the Arithmetic Subtraction (-) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        int difference = firstNumber - secondNumber;

        System.out.println("Difference = " + difference);

        scanner.close();
    }

}

Sample Input

Enter First Number: 50
Enter Second Number: 18

Sample Output

Difference = 32

Explanation

The subtraction operator (-) subtracts the second number from the first number.

Formula:

Difference = First Number − Second Number

Concepts Covered

  • Arithmetic Operator
  • Subtraction
  • Variables
  • Scanner Class

3. Java Program to Perform Multiplication Using Arithmetic Operator

Problem Statement

Write a Java program to multiply two numbers using the Arithmetic Multiplication (*) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        int product = firstNumber * secondNumber;

        System.out.println("Product = " + product);

        scanner.close();
    }

}

Sample Input

Enter First Number: 12
Enter Second Number: 8

Sample Output

Product = 96

Explanation

The multiplication operator (*) multiplies two numbers and returns the product.

Formula:

Product = First Number × Second Number

Concepts Covered

  • Arithmetic Operator
  • Multiplication
  • User Input
  • Variables

4. Java Program to Perform Division Using Arithmetic Operator

Problem Statement

Write a Java program to divide two numbers using the Arithmetic Division (/) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        double firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextDouble();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextDouble();

        if (secondNumber == 0) {
            System.out.println("Division by zero is not allowed.");
        } else {

            double result = firstNumber / secondNumber;

            System.out.println("Division = " + result);
        }

        scanner.close();

    }

}

Sample Input

Enter First Number: 20
Enter Second Number: 5

Sample Output

Division = 4.0

Explanation

The division operator (/) divides the first number by the second number.

The program also checks whether the divisor is zero to prevent an arithmetic error.

Formula:

Division = First Number / Second Number

Concepts Covered

  • Arithmetic Division Operator
  • if Statement
  • Scanner Class
  • User Input
  • Exception Prevention

5. Java Program to Find the Remainder Using Modulus Operator

Problem Statement

Write a Java program to find the remainder after dividing two numbers using the Modulus (%) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        int remainder = firstNumber % secondNumber;

        System.out.println("Remainder = " + remainder);

        scanner.close();

    }

}

Sample Input

Enter First Number: 17
Enter Second Number: 5

Sample Output

Remainder = 2

Explanation

The modulus operator (%) returns the remainder left after division.

Example:

17 ÷ 5

Quotient = 3

Remainder = 2

Concepts Covered

  • Modulus Operator
  • Arithmetic Operators
  • Variables
  • User Input

6. Java Program to Check Which Number is Greater Using Relational Operator

Problem Statement

Write a Java program to determine which of the two numbers is greater using relational operators.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        if (firstNumber > secondNumber) {
            System.out.println(firstNumber + " is Greater.");
        } else if (secondNumber > firstNumber) {
            System.out.println(secondNumber + " is Greater.");
        } else {
            System.out.println("Both Numbers are Equal.");
        }

        scanner.close();
    }

}

Sample Input

Enter First Number: 50
Enter Second Number: 35

Sample Output

50 is Greater.

Explanation

The relational operator (>) compares two numbers and determines which value is larger.

Concepts Covered

  • Relational Operator
  • if-else
  • Comparison
  • Scanner Class

7. Java Program to Compare Two Numbers

Problem Statement

Write a Java program to compare two numbers using relational operators.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        if (firstNumber == secondNumber) {
            System.out.println("Both Numbers are Equal.");
        } else {
            System.out.println("Numbers are Different.");
        }

        scanner.close();
    }

}

Sample Input

Enter First Number: 20
Enter Second Number: 20

Sample Output

Both Numbers are Equal.

Explanation

The equality operator (==) checks whether two values are equal.

Concepts Covered

  • Equality Operator
  • Relational Operators
  • Comparison
  • Conditional Statements

8. Java Program to Check Equality of Two Numbers

Problem Statement

Write a Java program to determine whether two numbers are equal or not.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int numberOne, numberTwo;

        System.out.print("Enter First Number: ");
        numberOne = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        numberTwo = scanner.nextInt();

        boolean result = (numberOne == numberTwo);

        System.out.println("Result = " + result);

        scanner.close();

    }

}

Sample Input

Enter First Number: 12
Enter Second Number: 12

Sample Output

Result = true

Explanation

The comparison returns a Boolean value:

  • true if both numbers are equal.
  • false otherwise.

Concepts Covered

  • Boolean Data Type
  • Equality Operator
  • Relational Operators

9. Java Program to Demonstrate Logical AND (&&)

Problem Statement

Write a Java program to demonstrate the Logical AND (&&) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int age;
        double percentage;

        System.out.print("Enter Age: ");
        age = scanner.nextInt();

        System.out.print("Enter Percentage: ");
        percentage = scanner.nextDouble();

        if (age >= 18 && percentage >= 60) {
            System.out.println("Eligible");
        } else {
            System.out.println("Not Eligible");
        }

        scanner.close();
    }

}

Sample Input

Enter Age: 20
Enter Percentage: 72

Sample Output

Eligible

Explanation

The Logical AND operator (&&) returns true only when both conditions are true.

Concepts Covered

  • Logical AND
  • Boolean Expressions
  • Conditional Statements

10. Java Program to Demonstrate Logical OR (||)

Problem Statement

Write a Java program to demonstrate the Logical OR (||) operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int marks;

        System.out.print("Enter Marks: ");
        marks = scanner.nextInt();

        if (marks >= 90 || marks == 100) {
            System.out.println("Excellent Performance");
        } else {
            System.out.println("Good Performance");
        }

        scanner.close();
    }

}

Sample Input

Enter Marks: 95

Sample Output

Excellent Performance

Explanation

The Logical OR operator (||) returns true if at least one condition is true.

Concepts Covered

  • Logical OR
  • Conditional Statements
  • Boolean Operators
  • Decision Making

11. Java Program to Demonstrate Logical NOT (!) Operator

Problem Statement

Write a Java program to demonstrate the Logical NOT (!) operator.

Java Solution

public class Main {

    public static void main(String[] args) {

        boolean isJavaEasy = true;

        System.out.println("Original Value : " + isJavaEasy);
        System.out.println("After NOT Operator : " + !isJavaEasy);

    }

}

Sample Output

Original Value : true
After NOT Operator : false

Explanation

The Logical NOT (!) operator reverses the Boolean value.

  • true becomes false
  • false becomes true

Concepts Covered

  • Logical NOT Operator
  • Boolean Data Type
  • Unary Operator

12. Java Program to Demonstrate Assignment Operators

Problem Statement

Write a Java program to demonstrate different assignment operators.

Java Solution

public class Main {

    public static void main(String[] args) {

        int number = 20;

        number += 10;
        System.out.println("After += : " + number);

        number -= 5;
        System.out.println("After -= : " + number);

        number *= 2;
        System.out.println("After *= : " + number);

        number /= 5;
        System.out.println("After /= : " + number);

    }

}

Sample Output

After += : 30
After -= : 25
After *= : 50
After /= : 10

Explanation

Assignment operators combine assignment with arithmetic operations.

Examples:

+=
-=
*=
/=
%=

These operators reduce the amount of code required for common calculations.

Concepts Covered

  • Assignment Operators
  • Arithmetic Operators
  • Variables

13. Java Program to Demonstrate Unary Increment and Decrement Operators

Problem Statement

Write a Java program to demonstrate increment (++) and decrement (--) operators.

Java Solution

public class Main {

    public static void main(String[] args) {

        int number = 10;

        System.out.println("Original Value = " + number);

        number++;

        System.out.println("After Increment = " + number);

        number--;

        System.out.println("After Decrement = " + number);

    }

}

Sample Output

Original Value = 10
After Increment = 11
After Decrement = 10

Explanation

Unary operators modify the value by one.

  • ++ increases the value by one.
  • -- decreases the value by one.

Concepts Covered

  • Unary Operators
  • Increment Operator
  • Decrement Operator

14. Java Program to Find the Largest Number Using the Ternary Operator

Problem Statement

Write a Java program to find the larger of two numbers using the ternary operator.

Java Solution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int firstNumber, secondNumber, largest;

        System.out.print("Enter First Number: ");
        firstNumber = scanner.nextInt();

        System.out.print("Enter Second Number: ");
        secondNumber = scanner.nextInt();

        largest = (firstNumber > secondNumber)
                ? firstNumber
                : secondNumber;

        System.out.println("Largest Number = " + largest);

        scanner.close();

    }

}

Sample Input

Enter First Number: 18
Enter Second Number: 42

Sample Output

Largest Number = 42

Explanation

The ternary operator is a compact alternative to an if-else statement.

Syntax:

condition ? value1 : value2

Concepts Covered

  • Ternary Operator
  • Conditional Expressions
  • Relational Operators

15. Java Program to Demonstrate Bitwise Operators

Problem Statement

Write a Java program to demonstrate commonly used Bitwise Operators.

Java Solution

public class Main {

    public static void main(String[] args) {

        int firstNumber = 12;
        int secondNumber = 10;

        System.out.println("AND = " + (firstNumber & secondNumber));

        System.out.println("OR = " + (firstNumber | secondNumber));

        System.out.println("XOR = " + (firstNumber ^ secondNumber));

        System.out.println("LEFT SHIFT = " + (firstNumber << 1));

        System.out.println("RIGHT SHIFT = " + (firstNumber >> 1));

    }

}

Sample Output

AND = 8
OR = 14
XOR = 6
LEFT SHIFT = 24
RIGHT SHIFT = 6

Explanation

Bitwise operators work directly on the binary representation of numbers.

OperatorPurpose
&Bitwise AND
``
^Bitwise XOR
<<Left Shift
>>Right Shift

Bitwise operators are commonly used in low-level programming, embedded systems, networking, and performance optimization.

Concepts Covered

  • Bitwise Operators
  • Binary Operations
  • Left Shift
  • Right Shift

Chapter Summary

In this chapter, you learned how Java Operators perform different types of operations on variables and values. You practiced arithmetic calculations, comparisons using relational operators, logical decision-making, assignment operations, increment and decrement operators, the ternary operator, and bitwise operations.

Operators are the building blocks of Java programming and are used in almost every Java application. Whether you’re developing desktop applications, Android apps, enterprise software, or preparing for coding interviews, understanding operators is essential for writing efficient and logical programs.

By completing these practice questions, you’ve strengthened your understanding of Java operators and built a solid foundation for upcoming topics like conditional statements, loops, methods, arrays, and object-oriented programming.


Key Takeaways

  • Java provides different categories of operators for different purposes.
  • Arithmetic operators perform mathematical calculations.
  • Relational operators compare two values and return a Boolean result.
  • Logical operators combine multiple conditions.
  • Assignment operators simplify arithmetic assignments.
  • Unary operators increment or decrement variable values.
  • The ternary operator provides a concise alternative to if-else.
  • Bitwise operators manipulate binary values directly.
  • Operators are widely used in every Java program.
  • Mastering operators improves coding efficiency and problem-solving skills.

Frequently Asked Questions (FAQs)

1. What are operators in Java?

Operators are special symbols that perform operations on variables, constants, and expressions.

Example:

int sum = a + b;

2. How many types of operators are available in Java?

Java mainly provides:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Unary Operators
  • Bitwise Operators
  • Shift Operators
  • Ternary Operator

3. What is the difference between == and = in Java?

  • = assigns a value to a variable.
  • == compares two values for equality.

Example:

int a = 10;

if(a == 10)
{
    System.out.println("Equal");
}

4. What is the ternary operator?

The ternary operator is a shorthand alternative to if-else.

Syntax:

condition ? value1 : value2;

5. What are logical operators?

Logical operators combine multiple Boolean conditions.

Examples:

&&
||
!

6. What are bitwise operators used for?

Bitwise operators manipulate the binary representation of integers and are commonly used in:

  • Embedded Systems
  • Networking
  • Cryptography
  • Device Drivers
  • Performance Optimization

7. Which operator returns the remainder?

The modulus operator (%) returns the remainder after division.

Example:

17 % 5 = 2

8. Why are Java operators important in coding interviews?

Java operators are fundamental to programming logic and are frequently used in:

  • Problem Solving
  • Algorithms
  • Conditional Statements
  • Loops
  • Competitive Programming
  • Technical Interviews

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top