LIWA

Operators in Java

ICSE Class 9 · Computer Applications · Chapter 4

What this chapter covers

Key points

Questions answered

Define an operator and an operand. Identify both in `perimeter = 2 * (l + b);`.
An operator is a symbol that tells the compiler to perform an operation on values; an operand is a value on which the operator acts. The operators are `=`, `` and `+`. The operands of `+` are `l` and `b`; the operands of `` are `2` and the value of `(l + b)`; the operands of `=` are `perimeter` and the value of `2 * (l + b)`.
Classify the following by form: `!`, `%`, `?:`, `--`, `>=`.
`!` and `--` are unary; `%` and `>=` are binary; `?:` is ternary.
If `int m = 47, n = 6;`, find (a) `m / n` (b) `m % n` (c) `m / (double) n` rounded to two decimal places (d) `-m % n`.
(a) 7, by integer division. (b) 5, since 6 × 7 = 42 and 47 − 42 = 5. (c) 7.83, since 47 ÷ 6 = 7.8333… (d) −5, because the sign of the remainder follows the first operand.
Write the Java expression for (a2 + b2)/(2ab).
`(a * a + b * b) / (2 * a * b)`. The brackets around the numerator and the whole denominator are essential.
Distinguish between `=` and `==`.
`=` is the assignment operator; it stores the value on its right in the variable on its left. `==` is a relational operator; it compares two values and gives `true` or `false`.
What is short-circuit evaluation? Why does `(x != 0) && (y / x > 1)` never cause a division-by-zero error?
Short-circuit evaluation means Java stops evaluating a logical expression as soon as its result is known. When `x` is 0, `x != 0` is `false`, so the result of `&&` must be `false` and the division `y / x` is never carried out.
Distinguish between the prefix and postfix forms of the increment operator, with an example.
In prefix form (`++a`) the variable is increased first and the new value is used; in postfix form (`a++`) the current value is used first and then the variable is increased. With `a = 4`, `b = ++a;` gives `b` = 5, while `b = a++;` gives `b` = 4; in both cases `a` ends as 5.
If `int p = 8;`, find the value of `q` and the final value of `p` after `q = p++ * 2 + --p;`.
`p++` uses 8, then `p` becomes 9. `--p` makes `p` equal to 8 and uses 8. `q = 8 * 2 + 8`, which is 24. The final value of `p` is 8.

Past board exam questions

ICSE 2027 sample paper · 1 mark

Evaluate the given expression and identify the result 3*5%7+8/3*9;

  1. 15
  2. 19
  3. 18
  4. 19.0
Show answer
19

ICSE 2027 sample paper · 1 mark

What is the value stored in z: z *= z++ + ++z; where z = 2

  1. 6
  2. 4
  3. 12
  4. 10
Show answer
12

ICSE 2027 sample paper · 1 mark

Match the operators in Column A to the type of operators in Column B: | Column A | Column B | |---|---| | 1. + | (a) Logical | | 2. > | (b) Mathematical | | 3. && | (c) Relational |

  1. 1-(a), 2-(b), 3-(c)
  2. 1-(b), 2-(c), 3-(a)
  3. 1-(c), 2-(a), 3-(b)
  4. 1-(b), 2-(a), 3-(c)
Show answer
1-(a), 2-(b), 3-(c)

ICSE 2027 sample paper · 2 marks

Write the Java expression for the following: (mn + |m|)/(m + n)

Show answer
(m*n + Math.abs(m)) / (m + n)

ICSE 2027 sample paper · 2 marks

Evaluate the following expression: 13*7%6+8/5*7;

Show answer
13*7%6 + 8/5*7 = 91%6 + 1*7 (8/5 = 1 in integer division) = 1 + 7 = 8

ICSE 2027 sample paper · 2 marks

Differentiate between = and = =.

Show answer
= is the assignment operator: it assigns the value on its right to the variable on its left, e.g. a = 5; == is a relational (comparison) operator: it checks whether two values are equal and returns true or false, e.g. a == 5

ICSE 2027 sample paper · 2 marks

Rose wrote the following code to check whether a number is an even number or not, and print an appropriate message: ```java if(n%2==0) System.out.println("even Number"); else System.out.println("Not an even number"); ``` She asked Raj to rewrite the code using the ternary operators. What would be the code written by Raj?

Show answer
```java String s = (n%2==0) ? "even Number" : "Not an even number"; System.out.println(s); ``` (or directly: System.out.println(n%2==0 ? "even Number" : "Not an even number");)

ICSE 2027 sample paper · 2 marks

What is the value of ch? ```java int x = 65; char ch=(char) (x++ + 5); ```

Show answer
x++ uses 65 (then x becomes 66), so x++ + 5 = 70. (char)70 = 'F', so ch = F