Operators in Java

Multiple Choice Questions

Tick the correct answer:

  1. The statement n += 4; is equivalent to:
    a. ++n;
    b. n = n + 4;
    c. n + 1;
    d. None
  2. What will be the output of a and b in the following:
    int a, b;
    a = 10;
    b = a++;

    a. 10, 10
    b. 10, 11
    c. 11, 10
    d. 11, 11
  3. What will be the output of a++; when int a = -1;
    a. 1
    b. -1
    c. 0
    d. None
  4. If int a = 25, b = 5, c = 0; What value is stored in c, when c = a % b;?
    a. 5.0
    b. 5
    c. 0
    d. None
  5. What is the result of the following in Java statement, when
    int m = 8;
    m *= 8;
    System.out.println("The output = " + m);

    a. 8
    b. 64
    c. 16
    d. 88
  6. double c;
    int x, y, z;
    x = 5;
    y = 10;
    z = 11;
    c = x * y + z / 2;

    The value stored in c is:
    a. 55.0
    b. 55.5
    c. 55
    d. None
  7. int m, p;
    m = 5;
    p = 0;
    p = m-- + --m;

    The output will be:
    a. 11
    b. 10
    c. 8
    d. 12
  8. int a = 7, p = 0, q = 0;
    p = ++a + --a;
    q -= p;

    The output of q will be:
    a. 13
    b. 14
    c. 15
    d. -15

Write the Java expressions for the following:

  1. p = a2 + bc
    p = a * a + b * c;
  2. m = (a2 – b2) ÷ ab
    m = (a * a - b * b) / (a * b);
  3. s = ut + ½ at2
    s = u * t + 1.0 / 2 * a * t * t;
  4. f = (uv) ÷ (u + v)
    f = (u * v) / (u + v);
  5. (a + b)2 + b
    (a + b) * (a + b) + b;
  6. y = 2(lb + bh + lh)
    y = 2 * (l * b + b * h + l * h);
  7. a2 + b2
    a * a + b * b;
  8. z = x3 + y3 – xy ÷ 3
    z = x * x * x + y * y + y - x * y / 3;

Answer the following questions:

  1. What is an operator?
    An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result.
  2. Name the different types of operators.
    The different types of operators are:
    a. Unary
    b. Binary
    c. Ternary.
  3. Explain the following:
    a. Arithmetical operator: The operators used to perform arithmetical calculations in a program are known as arithmetical operators.
    b. Relational operator: The operators used to show the relationship between the operands by comparing the values of the variables and give the result as true or false, are known as relational operators.
    c. Logical operator: AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.
    d. Unary operator: The operators applied to a single operand is known as unary operator.
    e. new operator: The new operator is used to allocate space in the dynamic memory for the storage of data and functions belonging to an object in Java programming.
    f. Binary operator: The operators that deal with two operands is known as binary operator.
  4. What is a ternary operator? Explain with the help of an example.
    The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.
    Example:
    int a = 5:
    int b = 3;
    int max = (a > b)? a : b;
  5. Differentiate between the following:
    a. Arithmetical operator and logical operator: The operators used to perform arithmetical calculations in a program are known as arithmetical operators. AND, OR and NOT are logical operators, that yield true or false depending upon the outcome of different expressions.
    b. Binary operator and Ternary operator: The operators that deal with two operands is known as binary operator. The ternary operator deals with three operands. It is also known as a conditional operator. In this operator, the value of the variable depends on the logical expression.
    c. Logical AND and Logical OR: Logical AND combines two or more boolean expressions in such a way that it results in true if all the expressions are true, otherwise it returns false. Logical OR combines two or more boolean expressions in such a way that it will return true when any one condition is true, otherwise it returns false.
    d. Prefix operator and Postfix operator: Prefix means that the value of the variable changes before the operation takes place. Postfix means that the operand will change after performing the operation.
    e. System.out.print() and System.out.println(): When we use print(), the cursor remains in the same line after producing the result. But in println(), the cursor shifts to the next line after producing the result.
  6. Differentiate between an operator and an expression.
    An operator is a symbol or token which performs arithmetical or logical operations to give meaningful result. An expression is any meaningful combination of variables, constants and/or operators, that, when calculated, yields a value.
  7. If m = 5 and n = 2, then what will be the output of m and n after execution that will store in (a) and (b)?
    a. m -= n;
    m = m – n;
    = 5 – 2
    = 3
    b. n = m + m / n;
    n = 5 + 5 / 2
    = 5 + 2
    = 7
  8. State the difference between = and ==.
    = is an assignment operator. It assigns a value to a variable. == is a relational operator. It compares two values for equality.
  9. What will be the output of the following program segment?
    int a = 0, b = 10, c = 40;
    a = --b + c++ + b;
    System.out.println("a = " + a);

    a = 9 + 40 + 9
    = 58.
  10. What will be the output of the following, if x = 5;?
    a. 5 * ++x
    = 5 * 6
    = 30.
    b. 5 * x++
    = 5 * 5
    = 25.
  11. Evaluate the following expressions, if the values of the variables are:
    a = 2, b = 3, and c = 9
    a. a - (b++) * (--c)
    = 2 – 3 * 8
    = 2 – 24
    = -22.
    b. a * (++b) % c
    = 2 * 4 % 9
    = 2 * 4
    = 8.
  12. If a = 5, b = 9, calculate the value of:
    a += a++ - ++b + a;
    a = a + a++ - ++b + a;
    = 5 + 5 – 10 + 6
    = 6.
  13. Give the output of the program snippet:
    int a = 10, b = 12;
    if(a >= 10)
    a++;
    else
    ++b;
    System.out.println("a = " + a + " and b = " + b);

    a = 11 and b = 12
  14. Rewrite the following using ternary operator:
    if(income <= 100000)
    tax = 0;
    else
    tax = (0.1 * income);

    tax = (income <= 100000)? 0 : (0.1 * income);
  15. Rewrite the following using ternary operator:
    if(p > 5000)
    d = p * 5 / 100;
    else
    d = 2 * p / 10;

    d = (p > 5000)? p * 5 / 100 : 2 * p / 10;

12 thoughts on “Operators in Java

Leave a Reply