Chapter 5 Beginner 58 Questions

Practice Questions — Operators in Java

← Back to Notes
9 Easy
10 Medium
9 Hard

Topic-Specific Questions

Question 1
Easy
What is the output?
System.out.println(15 + 3);
System.out.println(15 - 3);
System.out.println(15 * 3);
Basic arithmetic operations.
18
12
45
Question 2
Easy
What is the output?
System.out.println(10 / 3);
System.out.println(10 % 3);
Integer division truncates. Modulus gives the remainder.
3
1
Question 3
Easy
What is the output?
System.out.println(10 == 10);
System.out.println(10 != 5);
System.out.println(10 > 20);
Relational operators return boolean.
true
true
false
Question 4
Easy
What is the output?
System.out.println(true && false);
System.out.println(true || false);
System.out.println(!true);
AND needs both true, OR needs at least one true, NOT reverses.
false
true
false
Question 5
Easy
What is the output?
int x = 10;
x += 5;
x *= 2;
System.out.println(x);
Trace: 10, then +5=15, then *2=30.
30
Question 6
Medium
What is the output?
int a = 5;
System.out.println(a++);
System.out.println(a);
System.out.println(++a);
Post-increment: use then increment. Pre-increment: increment then use.
5
6
7
Question 7
Medium
What is the output?
System.out.println(10.0 / 3);
System.out.println(10 / 3);
System.out.println((double) 10 / 3);
If either operand is double, the result is double.
3.3333333333333335
3
3.3333333333333335
Question 8
Medium
What is the output?
int marks = 45;
String result = (marks >= 40) ? "Pass" : "Fail";
System.out.println(result);
Ternary: condition ? valueIfTrue : valueIfFalse.
Pass
Question 9
Medium
What is the output?
int a = 12, b = 10;
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
12 = 1100, 10 = 1010 in binary.
8
14
6
Question 10
Medium
What is the output?
System.out.println(8 << 1);
System.out.println(8 << 2);
System.out.println(8 >> 1);
System.out.println(8 >> 2);
Left shift doubles per shift. Right shift halves per shift.
16
32
4
2
Question 11
Hard
What is the output?
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
System.out.println(2 + 3 * 4 - 6 / 2 + 10 % 3);
Precedence: *, /, % before +, -.
14
20
12
Question 12
Hard
What is the output?
System.out.println(true || false && false);
System.out.println((true || false) && false);
&& has higher precedence than ||.
true
false
Question 13
Hard
What is the output?
int x = 5;
int y = x++ + ++x;
System.out.println("x = " + x + ", y = " + y);
First x++ uses 5 (then x=6), then ++x increments to 7 before use.
x = 7, y = 12
Question 14
Hard
What is the output?
int a = -7;
System.out.println(a % 3);
System.out.println(a / 3);
In Java, the sign of the modulus result follows the sign of the dividend (left operand).
-1
-2
Question 15
Medium
What is the difference between && and & when used with boolean expressions?
One short-circuits, the other always evaluates both sides.
&& is the short-circuit AND: if the left operand is false, the right operand is not evaluated (because the result is always false). & is the non-short-circuit AND: both operands are always evaluated regardless. && is used for normal boolean logic. & is used when you need the side effects of the right operand to execute.
Question 16
Hard
What is the output?
byte b = 10;
b += 5;  // Works!
System.out.println(b);
// b = b + 5;  // Would this work?
Compound assignment includes an implicit cast. Regular assignment does not.
15
The commented line b = b + 5 would NOT compile because b + 5 is promoted to int, and assigning int to byte requires explicit casting: b = (byte)(b + 5);.
Question 17
Easy
What does the modulus operator (%) return?
It gives the remainder after division.
The modulus operator % returns the remainder after integer division. 10 % 3 = 1 (because 10 = 3*3 + 1). Common uses: checking even/odd (n % 2 == 0), checking divisibility (n % 5 == 0), wrapping values in a range.

Mixed & Application Questions

Question 1
Easy
What is the output?
int a = 20, b = 7;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
Standard arithmetic with a=20, b=7.
27
13
140
2
6
Question 2
Easy
Priya scored 85, 92, 78, 95, and 88 in five subjects. Write a program that calculates and prints the total, average (as double), and whether the average is above 85 (using ternary operator).
Cast to double for average. Use ternary for the message.
int total = 85 + 92 + 78 + 95 + 88;
double average = (double) total / 5;
String status = (average > 85) ? "Above 85" : "Below or equal 85";
System.out.println("Total: " + total);
System.out.println("Average: " + average);
System.out.println("Status: " + status);
Output: Total: 438, Average: 87.6, Status: Above 85
Question 3
Medium
What is the output?
int x = 10;
System.out.println(x > 5 && x < 20);
System.out.println(x > 15 || x < 5);
System.out.println(!(x == 10));
Evaluate each comparison first, then the logical operator.
true
false
false
Question 4
Medium
Aarav wants to check if a number is even or odd using the bitwise AND operator, and also using the modulus operator. Write both checks for the number 42.
num & 1 gives 0 for even, 1 for odd. num % 2 gives 0 for even.
int num = 42;
// Method 1: Bitwise AND
boolean isEvenBitwise = (num & 1) == 0;
System.out.println(num + " is even (bitwise): " + isEvenBitwise);

// Method 2: Modulus
boolean isEvenModulus = (num % 2) == 0;
System.out.println(num + " is even (modulus): " + isEvenModulus);
Question 5
Medium
What is the output?
int a = 5, b = 3;
System.out.println(a > b ? "a is bigger" : "b is bigger or equal");
System.out.println(a > b ? a : b);
int max = (a > b) ? a : b;
System.out.println("Max: " + max);
Ternary operator selects a value based on the condition.
a is bigger
5
Max: 5
Question 6
Hard
What is the output?
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
== compares references for objects. .equals() compares values.
true
false
true
Question 7
Hard
Write a program that swaps two integers without using a temporary variable. Use XOR bitwise operator. Start with a=25, b=40.
XOR swap: a^=b; b^=a; a^=b;
int a = 25, b = 40;
System.out.println("Before: a=" + a + ", b=" + b);
a = a ^ b;  // a = 25 ^ 40
b = a ^ b;  // b = (25 ^ 40) ^ 40 = 25
a = a ^ b;  // a = (25 ^ 40) ^ 25 = 40
System.out.println("After:  a=" + a + ", b=" + b);
Question 8
Hard
What is the output?
int x = 100;
x >>= 2;
System.out.println(x);
x <<= 3;
System.out.println(x);
>>= 2 divides by 4. <<= 3 multiplies by 8.
25
200
Question 9
Medium
What is the output?
int a = 10;
int b = a++;
int c = ++a;
System.out.println("a=" + a + ", b=" + b + ", c=" + c);
Post-increment returns current value then increments. Pre-increment increments then returns.
a=12, b=10, c=12
Question 10
Hard
Rohan writes: if (x > 5 & y / x > 2) and it crashes when x is 0. Why? How should he fix it?
The & operator evaluates both sides. Division by zero when x is 0.
The & operator (non-short-circuit) evaluates both operands. When x is 0, x > 5 is false, but y / x > 2 is still evaluated, causing ArithmeticException: / by zero. Fix: use && (short-circuit AND): if (x > 5 && y / x > 2). With &&, if x > 5 is false (which it is when x=0), the right side is skipped.
Question 11
Easy
What is the output?
boolean a = true, b = false;
System.out.println(a && b);
System.out.println(a || b);
System.out.println(!a && b);
System.out.println(a || !b);
Evaluate NOT first, then AND, then OR.
false
true
false
true

Multiple Choice Questions

MCQ 1
What is the result of 10 / 3 in Java?
  • A. 3.33
  • B. 3
  • C. 3.0
  • D. 4
Answer: B
B is correct. Both 10 and 3 are integers. Integer division truncates the decimal: 10 / 3 = 3 (not 3.33). To get 3.33, use 10.0 / 3 or (double) 10 / 3.
MCQ 2
Which operator checks equality in Java?
  • A. =
  • B. ==
  • C. ===
  • D. equals
Answer: B
B is correct. == checks equality for primitives and reference equality for objects. = (A) is assignment. === (C) does not exist in Java (it is JavaScript). equals (D) is a method, not an operator.
MCQ 3
What does the % operator do?
  • A. Calculates percentage
  • B. Returns the remainder after division
  • C. Divides and rounds up
  • D. Returns the quotient
Answer: B
B is correct. The modulus operator % returns the remainder. 10 % 3 = 1 (10 = 3*3 + 1). It does NOT calculate percentage (A), round up (C), or return the quotient (D).
MCQ 4
What is the output of: System.out.println(true || false)?
  • A. true
  • B. false
  • C. 1
  • D. Error
Answer: A
A is correct. The OR operator returns true if at least one operand is true. true || false = true. Java's boolean operators return boolean values, not integers (C).
MCQ 5
What does x += 5 mean?
  • A. x = 5
  • B. x = x + 5
  • C. x == 5
  • D. x = 5 + 5
Answer: B
B is correct. x += 5 is shorthand for x = x + 5. It adds 5 to the current value of x and stores the result back in x.
MCQ 6
What is the output of: int a = 5; System.out.println(a++)?
  • A. 5
  • B. 6
  • C. 4
  • D. Error
Answer: A
A is correct. Post-increment (a++) returns the current value (5) and then increments a to 6. So println prints 5. After this line, a is 6.
MCQ 7
What is the result of: 5 & 3?
  • A. 8
  • B. 1
  • C. 6
  • D. 7
Answer: B
B is correct. 5 = 101, 3 = 011. Bitwise AND: 101 & 011 = 001 = 1. AND gives 1 only where both bits are 1.
MCQ 8
What is the result of the ternary: (10 > 5) ? 100 : 200?
  • A. 100
  • B. 200
  • C. true
  • D. Error
Answer: A
A is correct. 10 > 5 is true, so the ternary returns the first value (100). The ternary operator evaluates the condition and returns one of two values.
MCQ 9
Which of the following uses short-circuit evaluation?
  • A. & and |
  • B. && and ||
  • C. ^ and ~
  • D. << and >>
Answer: B
B is correct. && and || are short-circuit operators: they skip the right operand when the result is already determined. & and | (A) always evaluate both operands when used as boolean operators. ^ and ~ (C) are bitwise. << and >> (D) are shift operators.
MCQ 10
What is the output of: System.out.println(5 ^ 3)?
  • A. 8
  • B. 15
  • C. 6
  • D. 125
Answer: C
C is correct. In Java, ^ is the bitwise XOR operator, NOT exponentiation. 5 = 101, 3 = 011. XOR: 101 ^ 011 = 110 = 6. For exponentiation, use Math.pow(5, 3) which gives 125 (D).
MCQ 11
What is the output of: System.out.println(2 + 3 * 4)?
  • A. 20
  • B. 14
  • C. 24
  • D. 12
Answer: B
B is correct. Multiplication has higher precedence than addition. 3 * 4 = 12 first, then 2 + 12 = 14. To get 20, use parentheses: (2 + 3) * 4.
MCQ 12
What is the output of: System.out.println(-7 % 3)?
  • A. 2
  • B. -1
  • C. 1
  • D. -2
Answer: B
B is correct. In Java, the sign of the modulus result follows the dividend (left operand). -7 % 3 = -1 because -7 = 3 * (-2) + (-1). This is different from Python where the result follows the divisor's sign.
MCQ 13
Which statement about compound assignment is TRUE?
  • A. b += 5 and b = b + 5 are always identical for byte b
  • B. b += 5 includes an implicit cast; b = b + 5 does not
  • C. Both expressions require explicit casting for byte
  • D. Neither expression compiles for byte variables
Answer: B
B is correct. b += 5 is equivalent to b = (byte)(b + 5) (implicit narrowing cast). But b = b + 5 performs integer promotion on b + 5 (result is int) and cannot assign int to byte without explicit cast. This is a subtle but important distinction.
MCQ 14
What does the >>> operator do in Java?
  • A. Divides by 8
  • B. Logical right shift (fills with 0s)
  • C. Triple comparison
  • D. Unsigned division
Answer: B
B is correct. >>> is the unsigned (logical) right shift operator. It shifts bits right and fills the leftmost positions with 0, regardless of the sign bit. >> (signed shift) preserves the sign bit. This distinction matters for negative numbers.
MCQ 15
What is the output of: System.out.println(true || false && false)?
  • A. true
  • B. false
  • C. Error
  • D. null
Answer: A
A is correct. && has higher precedence than ||. So the expression is true || (false && false) = true || false = true. With parentheses: (true || false) && false = false.
MCQ 16
What is the output of: int a = 5; int b = -a++; System.out.println(b + " " + a);
  • A. -5 6
  • B. -6 6
  • C. -5 5
  • D. -6 5
Answer: A
A is correct. Post-increment has highest precedence but returns the original value. So: a++ returns 5 (a becomes 6), then -5 is assigned to b. Result: b = -5, a = 6.
MCQ 17
Which operator should you use to compare String values in Java?
  • A. ==
  • B. ===
  • C. .equals()
  • D. compareTo() > 0
Answer: C
C is correct. .equals() compares String content (values). == (A) compares references. === (B) does not exist in Java. compareTo() (D) compares lexicographic order, not equality.
MCQ 18
What is the ternary operator in Java?
  • A. if-else
  • B. switch-case
  • C. ? :
  • D. for-each
Answer: C
C is correct. The ternary operator condition ? valueIfTrue : valueIfFalse is the only operator in Java that takes three operands. if-else (A) and switch-case (B) are statements, not operators. for-each (D) is a loop construct.
MCQ 19
What is the output of: System.out.println(~5)?
  • A. -5
  • B. -6
  • C. 4
  • D. -4
Answer: B
B is correct. The bitwise NOT (~) operator flips all bits. For any integer n, ~n = -(n+1). So ~5 = -(5+1) = -6. In binary: 5 is ...00000101, ~5 is ...11111010 which is -6 in two's complement.
MCQ 20
What is the output of: System.out.println(1 << 10)?
  • A. 10
  • B. 100
  • C. 1024
  • D. 1000
Answer: C
C is correct. Left shifting 1 by 10 positions is equivalent to 1 * 2^10 = 1024. Left shift by n multiplies by 2^n. This is a fast way to compute powers of 2 and is commonly used in systems programming.

Coding Challenges

Challenge 1: Simple Interest Calculator

Easy
Write a program to calculate simple interest. Principal = 50000, Rate = 8.5%, Time = 3 years. Formula: SI = (P * R * T) / 100. Print principal, rate, time, and interest.
Sample Input
(No input required)
Sample Output
Principal: 50000 Rate: 8.5% Time: 3 years Simple Interest: 12750.0
Use double for rate and interest. Use int for principal and time.
public class SimpleInterest {
    public static void main(String[] args) {
        int principal = 50000;
        double rate = 8.5;
        int time = 3;
        double interest = (principal * rate * time) / 100;
        System.out.println("Principal: " + principal);
        System.out.println("Rate: " + rate + "%");
        System.out.println("Time: " + time + " years");
        System.out.println("Simple Interest: " + interest);
    }
}

Challenge 2: Even/Odd Checker (Three Methods)

Easy
Write a program that checks if a number is even or odd using three different methods: modulus (%), bitwise AND (&), and ternary operator. Test with the number 42.
Sample Input
(No input required)
Sample Output
Number: 42 Method 1 (modulus): even Method 2 (bitwise): even Method 3 (ternary): even
Use all three methods: n%2==0, (n&1)==0, and ternary.
public class EvenOddChecker {
    public static void main(String[] args) {
        int n = 42;
        System.out.println("Number: " + n);
        System.out.println("Method 1 (modulus): " + (n % 2 == 0 ? "even" : "odd"));
        System.out.println("Method 2 (bitwise): " + ((n & 1) == 0 ? "even" : "odd"));
        String result = (n % 2 == 0) ? "even" : "odd";
        System.out.println("Method 3 (ternary): " + result);
    }
}

Challenge 3: Digit Extractor

Medium
Given a 4-digit number (e.g., 5821), extract and print each digit separately using only arithmetic operators (/ and %). Print thousands, hundreds, tens, and ones digits.
Sample Input
(No input required)
Sample Output
Number: 5821 Thousands: 5 Hundreds: 8 Tens: 2 Ones: 1
Use only / and % operators. Do not convert to String.
public class DigitExtractor {
    public static void main(String[] args) {
        int num = 5821;
        int thousands = num / 1000;
        int hundreds = (num / 100) % 10;
        int tens = (num / 10) % 10;
        int ones = num % 10;
        System.out.println("Number: " + num);
        System.out.println("Thousands: " + thousands);
        System.out.println("Hundreds: " + hundreds);
        System.out.println("Tens: " + tens);
        System.out.println("Ones: " + ones);
    }
}

Challenge 4: Leap Year Checker

Medium
Vikram needs to check if a year is a leap year. Rules: divisible by 4 AND (not divisible by 100 OR divisible by 400). Check years 2024, 2100, and 2000 using logical operators.
Sample Input
(No input required)
Sample Output
2024 is a leap year: true 2100 is a leap year: false 2000 is a leap year: true
Use % for divisibility and &&, || for combining conditions.
public class LeapYear {
    public static void main(String[] args) {
        int y1 = 2024, y2 = 2100, y3 = 2000;
        boolean leap1 = (y1 % 4 == 0) && (y1 % 100 != 0 || y1 % 400 == 0);
        boolean leap2 = (y2 % 4 == 0) && (y2 % 100 != 0 || y2 % 400 == 0);
        boolean leap3 = (y3 % 4 == 0) && (y3 % 100 != 0 || y3 % 400 == 0);
        System.out.println(y1 + " is a leap year: " + leap1);
        System.out.println(y2 + " is a leap year: " + leap2);
        System.out.println(y3 + " is a leap year: " + leap3);
    }
}

Challenge 5: Power of 2 Checker (Bitwise)

Hard
Write a program that checks if numbers are powers of 2 using the bitwise trick: n > 0 && (n & (n-1)) == 0. Test with numbers 1, 2, 4, 6, 8, 16, 15, 32, 100, 1024.
Sample Input
(No input required)
Sample Output
1 is power of 2: true 2 is power of 2: true 4 is power of 2: true 6 is power of 2: false ... 1024 is power of 2: true
Use the bitwise expression n & (n-1). A power of 2 has exactly one bit set.
public class PowerOfTwo {
    public static void main(String[] args) {
        int[] nums = {1, 2, 4, 6, 8, 16, 15, 32, 100, 1024};
        for (int n : nums) {
            boolean isPowerOf2 = n > 0 && (n & (n - 1)) == 0;
            System.out.println(n + " is power of 2: " + isPowerOf2);
        }
    }
}

Challenge 6: Bitwise Set Bit Counter

Hard
Write a program that counts the number of set bits (1s) in the binary representation of a number using bitwise AND and right shift. Test with 42 (binary: 101010, should have 3 set bits) and 255 (binary: 11111111, should have 8 set bits).
Sample Input
(No input required)
Sample Output
42 in binary: 101010 Set bits in 42: 3 255 in binary: 11111111 Set bits in 255: 8
Use a loop with & and >> operators. Also use Integer.toBinaryString() for display.
public class SetBitCounter {
    public static void main(String[] args) {
        int[] nums = {42, 255};
        for (int num : nums) {
            int count = 0;
            int temp = num;
            while (temp > 0) {
                count += temp & 1;
                temp >>= 1;
            }
            System.out.println(num + " in binary: " + Integer.toBinaryString(num));
            System.out.println("Set bits in " + num + ": " + count);
        }
    }
}

Challenge 7: Precedence Puzzle Solver

Hard
Without running the code, predict the output of 5 complex expressions. Then write a program that prints each expression and its result with step-by-step comments. Expressions: (a) 10 + 2 * 3 - 4 / 2, (b) 15 % 4 + 2 * 3, (c) true || false && !true, (d) 5 << 1 + 2, (e) 10 > 5 ? 2 + 3 : 4 * 5.
Sample Input
(No input required)
Sample Output
a) 10 + 2*3 - 4/2 = 14 b) 15%4 + 2*3 = 9 c) true || false && !true = true d) 5 << (1+2) = 40 e) 10>5 ? 2+3 : 4*5 = 5
Include step-by-step evaluation in comments.
public class PrecedencePuzzle {
    public static void main(String[] args) {
        // a) 2*3=6, 4/2=2, 10+6-2=14
        System.out.println("a) 10 + 2*3 - 4/2 = " + (10 + 2 * 3 - 4 / 2));
        // b) 15%4=3, 2*3=6, 3+6=9
        System.out.println("b) 15%4 + 2*3 = " + (15 % 4 + 2 * 3));
        // c) !true=false, false&&false=false, true||false=true
        System.out.println("c) true || false && !true = " + (true || false && !true));
        // d) + has higher precedence than <<! So 1+2=3, then 5<<3=40
        System.out.println("d) 5 << 1+2 = " + (5 << 1 + 2));
        // e) 10>5 is true, so 2+3=5
        System.out.println("e) 10>5 ? 2+3 : 4*5 = " + (10 > 5 ? 2 + 3 : 4 * 5));
    }
}

Challenge 8: Grade Assigner with Nested Ternary

Hard
Neha needs a program that assigns letter grades based on percentage using nested ternary operators. Rules: >=90: A+, >=80: A, >=70: B+, >=60: B, >=50: C, >=40: D, <40: F. Test with marks: 95, 82, 71, 63, 55, 42, 35.
Sample Input
(No input required)
Sample Output
95% -> A+ 82% -> A 71% -> B+ 63% -> B 55% -> C 42% -> D 35% -> F
Use nested ternary operators. Format output clearly.
public class GradeAssigner {
    public static void main(String[] args) {
        int[] marks = {95, 82, 71, 63, 55, 42, 35};
        for (int m : marks) {
            String grade = (m >= 90) ? "A+" :
                           (m >= 80) ? "A" :
                           (m >= 70) ? "B+" :
                           (m >= 60) ? "B" :
                           (m >= 50) ? "C" :
                           (m >= 40) ? "D" : "F";
            System.out.println(m + "% -> " + grade);
        }
    }
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn Java with a live mentor?

Explore our Java Masterclass