Operator Precedence Questions with Answers

50 practice questions on Operator Precedence (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 50 free in the AptiNest app.

Sample questions

Question 1Medium

Which of these is returned by greater than, <, and equal to, ==, operator?

  1. Integers
  2. Floating - point numbers
  3. Boolean
  4. None of the mentioned

Answer: C. Boolean

Explanation

All relational operators return a boolean value i:e true and false.

Step-by-step solution

  1. Greater than, less than, and equal to are relational operators.
  2. Relational operators compare values and return true or false.
  3. True and false are boolean values in programming.
  4. So the result of these operators is a boolean.
  5. The correct answer is Boolean.

Why the other options are wrong

  • A. Relational operators do not return integers; they return true or false.
  • B. The result is a true or false value, not a floating-point number.
  • D. None is wrong because relational operators return Boolean values.

Tip: Remember: All relational operators return a boolean value in programming.

Question 2Medium

Which of these is not a bitwise operator?

  1. &
  2. &=
  3. |=
  4. <=

Answer: D. <=

Explanation

<= is a relational operator, while &, &=, and |= are bitwise operators.

Step-by-step solution

  1. &, &= and |= are bitwise operators: & works on bits, and &= and |= assign the bitwise result.
  2. <= is a relational operator that compares two values.
  3. So <= is not a bitwise operator, which is option D.

Why the other options are wrong

  • A. & is the bitwise AND operator.
  • B. &= is the bitwise AND assignment operator.
  • C. |= is the bitwise OR assignment operator.

Tip: Bitwise operators are & | ^ ~ << >> >>> and their assignment forms; relational operators are < > <= >=.

Question 3Medium

What is the output of this program?

    class operators {
        public static void main(String args[])
        {
             int x = 8;
             System.out.println(++x * 3 + " " + x);
        }
    }
  1. 24 8
  2. 24 9
  3. 27 8
  4. 27 9

Answer: D. 27 9

Explanation

Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27.
output:

$ javac operators.java
$ java operators
27 9

Step-by-step solution

  1. X starts at 8.
  2. ++x raises x to 9 first, then 9 × 3 = 27.
  3. The next x in the string is now 9.
  4. So the output is 27 9 and option D is correct.

Why the other options are wrong

  • A. It ignores the pre-increment.
  • B. It multiplies the old x, 8, by 3.
  • C. It shows the product 27 but the old x, 8.

Tip: Pre-increment changes the value before it is used.

Question 4Medium

Which of these have highest precedence?

  1. ()
  2. ++
  3. *
  4. >>

Answer: A. ()

Explanation

Order of precedence is (highest to lowest) a -> b -> c -> d.

Step-by-step solution

  1. Operator precedence ranks operators, and parentheses have the highest precedence.
  2. The ++, * and >> operators all rank below parentheses.
  3. So () is the correct answer, option A.

Why the other options are wrong

  • B. ++ is a unary operator and ranks below parentheses.
  • C. Ranks below parentheses in precedence.
  • D. >> is a shift operator and ranks below parentheses.

Tip: Parentheses are evaluated first in any expression.

Question 5Medium

import java.awt.Button;
class CompareReference
{
    public static void main(String [] args)
    {
        float f = 42.0f;
        float [] f1 = new float[2];
        float [] f2 = new float[2];
        float [] f3 = f1;
        long x = 42;
        f1[0] = 42.0f;
    }
}

which three statements are true?

1. f1 == f2
2. f1 == f3
3. f2 == f1[1]
4. x == f1[0]
5. f == f1[0]

  1. 1, 2 and 3
  2. 2, 4 and 5
  3. 3, 4 and 5
  4. 1, 4 and 5

Answer: B. 2, 4 and 5

Explanation

(2) is correct because the reference variables f1 and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types (5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.

Step-by-step solution

  1. Statement 1: f1 and f2 are different array objects, so f1 == f2 is false.
  2. Statement 2: f1 and f3 refer to the same array, so it is true.
  3. Statement 3: comparing an array reference with a float element is not legal Java, so it is not true.
  4. Statements 4 and 5: long 42 and float 42.0 can be compared, and f and f1[0] are both 42.0, so both are true.
  5. The true statements are 2, 4 and 5, which is option B.

Why the other options are wrong

  • A. Statement 1 is false, since f1 and f2 are different objects.
  • C. Statement 3 is not true, since it compares an array with a float.
  • D. Statement 1 is false, since f1 and f2 are different objects.

Tip: Two array references are equal only when they point to the same array object; compare elements with numbers.

Question 6Medium

What will be the output of the program?

class Equals
{
    public static void main(String [] args)
    {
        int x = 100;
        double y = 100.1;
        boolean b = (x = y); /* Line 7 */
        System.out.println(b);
    }
}
  1. true
  2. false
  3. Compilation fails
  4. An exception is thrown at runtime

Answer: C. Compilation fails

Explanation

The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.

A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.

Step-by-step solution

  1. Line 7 uses = to assign a double to an int, which is invalid in Java.
  2. Assignment operator = cannot be used for comparison; == is required for boolean result.
  3. This causes a compilation error as the code is invalid syntax.
  4. No runtime exception or output occurs because the code fails to compile.
  5. The correct answer is option C: Compilation fails.

Why the other options are wrong

  • A. The code does not compile, so no output is printed and the program never reaches a true value.
  • B. The code does not compile, so no output is printed; false is never produced by running it.
  • D. No exception happens at runtime; the error is at compile time because int = double is not allowed.

Tip: Always use == for comparisons and = for assignments in Java.

Question 7Medium

What will be the output of the program?

class Test
{
    public static void main(String [] args)
    {
        int x=20;
        String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
        System.out.println(sup);
    }
}
  1. small
  2. tiny
  3. huge
  4. Compilation fails

Answer: B. tiny

Explanation

This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.

Step-by-step solution

  1. X is initialized to 20.
  2. First condition x < 15 is false.
  3. Second condition x < 22 is true, so 'tiny' is chosen.
  4. String sup is assigned 'tiny'.
  5. Program prints 'tiny'.
  6. Final answer: tiny.

Why the other options are wrong

  • A. small needs x less than 15, but x is 20, so the first condition is false.
  • C. huge is chosen only if both conditions are false, but x less than 22 is true, so tiny is chosen.
  • D. The nested ternary is valid Java, so the code compiles and prints tiny.

Tip: Check nested conditions step by step.

Question 8Medium

What is the output of this program?

    class ternary_operator {
        public static void main(String args[])
        {
             int x = 3;
             int y = ~ x;
             int z;
             z = x > y ? x : y;
             System.out.print(z);
        }
    }
  1. 0
  2. 1
  3. 3
  4. -4

Answer: C. 3

Explanation

~x flips the bits of 3, giving -4. Since 3 > -4 is true, z takes the value of x, which is 3. The output is 3.

3

Step-by-step solution

  1. X is 3.
  2. Y is ~x, the bitwise NOT of 3, which is -4.
  3. z = x > y? x: y. Since 3 > -4 is true, z takes x.
  4. So z is 3, and the program prints 3, which is option C.

Why the other options are wrong

  • A. 0 is not produced: the program prints z, and z is 3.
  • B. 1 is not produced: the program never computes 1.
  • D. -4 is y, the value in the false branch, which is not chosen.

Tip: In Java, ~x equals -x - 1.

Question 9Medium

What will be the output of the program?

class Bitwise
{
    public static void main(String [] args)
    {
        int x = 11 & 9;
        int y = x ^ 3;
        System.out.println( y | 12 );
    }
}
  1. 0
  2. 7
  3. 8
  4. 14

Answer: D. 14

Explanation

The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

Step-by-step solution

  1. 11 & 9: 1011 & 1001 = 1001, which is 9.
  2. 9 ^ 3: 1001 ^ 0011 = 1010, which is 10, so y = 10.
  3. 10 | 12: 1010 | 1100 = 1110, which is 14.
  4. The program prints 14. Option D is the answer.

Why the other options are wrong

  • A. The result is not 0; the OR of 10 and 12 is 14.
  • B. 7 is 0111, but 1010 | 1100 is 1110.
  • C. 8 is 1000, which does not match 1010 | 1100 = 1110.

Tip: In binary, & keeps bits set in both, ^ keeps bits set in one, and | keeps bits set in either.

Question 10Medium

On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?

  1. 1
  2. 32
  3. 33
  4. 31

Answer: D. 31

Explanation

The left shift operator shifts all of the bite in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

Step-by-step solution

  1. Left shift moves bits to the left, losing those beyond the highest position.
  2. Integer has 32 bits, so highest is position 31.
  3. Bits lost when shifted past position 31.
  4. So answer is 31.
  5. Final answer: 31.

Why the other options are wrong

  • A. Left shifts push bits upward, so position 1 is not where bits are lost.
  • B. A 32-bit int has positions 0 to 31, so 32 is not a bit position to lose bits past.
  • C. 33 is beyond the 32 bits of an int, so it cannot be the limit.

Tip: Remember: Left shift loses bits beyond position 31 in integers.

Practise all 50 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics