Operators in Java

ICSE Class 10 Computer Applications · Logix Computer Applications · 56 solved questions

Exercise
Marks
11 Mark · Multiple Choice Questions

If a = 8 and b = 4, the value of a %= b is ...........

Show SolutionHide Solution↓
  • 0 ✓
  • 21 Mark · Multiple Choice Questions

    An operator taking only single operand for its operation is called ...........

    Show SolutionHide Solution↓
  • A unary operator ✓
  • 31 Mark · Multiple Choice Questions

    Which one of the following is not a binary operator?

    Show SolutionHide Solution↓
  • ! ✓
  • 41 Mark · Multiple Choice Questions

    Which one of the following is not a valid operator in Java?

    Show SolutionHide Solution↓
  • !== ✓
  • 51 Mark · Multiple Choice Questions

    The statement i = i + 1 is equivalent to ...........

    Show SolutionHide Solution↓
  • All of these ✓
  • 61 Mark · Multiple Choice Questions

    For x = 5, the statement sum = ++x + 8 evaluates to ...........

    Show SolutionHide Solution↓
  • sum = 14 ✓
  • 71 Mark · Multiple Choice Questions

    Assuming x = 1 with the following code snippet:
        int y = --x;
    Which one of the following is true?

    Show SolutionHide Solution↓
  • x=0, y=0 ✓
  • 81 Mark · Multiple Choice Questions

    The statement (1>0) && (1<0) evaluates to ...........

    Show SolutionHide Solution↓
  • false ✓
  • 91 Mark · Multiple Choice Questions

    The statement (1>0) || (1<0) evaluates to ...........

    Show SolutionHide Solution↓
  • true ✓
  • 101 Mark · Multiple Choice Questions

    The statement (1==1)? 1: 0 evaluates to ...........

    Show SolutionHide Solution↓
  • 1 ✓
  • 111 Mark · Multiple Choice Questions

    The expression 17 % 4 gives the output ...........

    Show SolutionHide Solution↓
  • 1 ✓
  • 121 Mark · Multiple Choice Questions

    Consider the following code snippet:

    float x = 8.25F;
    int y;
    y = (int) x;

    What are the values of x and y?

    Show SolutionHide Solution↓
  • x= 8.25, y = 8 ✓
  • 131 Mark · Multiple Choice Questions

    The expression 13 / 3 gives the output ...........

    Show SolutionHide Solution↓
  • 4 ✓
  • 141 Mark · Multiple Choice Questions

    The statement System.out.println("six " + 3 + 3); gives the output ...........

    Show SolutionHide Solution↓
  • six 33 ✓
  • 151 Mark · Multiple Choice Questions

    The expression 4 + 8 % 2 gives the output ...........

    Show SolutionHide Solution↓
  • 4 ✓
  • 161 Mark · Multiple Choice Questions

    Implicit type conversion is also known as ...........

    Show SolutionHide Solution↓
  • All of these ✓
  • 1State whether the given statements are True or…

    There is only one ternary operator in Java.
    True

    Solution not available in the source paper.

    2State whether the given statements are True or…

    Arithmetic operators + and - also have a unary form.
    True

    Solution not available in the source paper.

    3State whether the given statements are True or…

    Operators = and == perform the same operation in Java.
    False

    Solution not available in the source paper.

    4State whether the given statements are True or…

    The expression 14 % 2 evaluates to 0.
    True

    Solution not available in the source paper.

    5State whether the given statements are True or…

    The expression 7 / 13 evaluates to 0.
    True

    Solution not available in the source paper.

    6State whether the given statements are True or…

    The output of System.out.println(!true); is false.
    True

    Solution not available in the source paper.

    7State whether the given statements are True or…

    The expressions 6 + 7 and "6" + "7" evaluate to the same value.
    False

    Solution not available in the source paper.

    8State whether the given statements are True or…

    The expression m = m + 2 is same as m =+ 2.
    False

    Solution not available in the source paper.

    9State whether the given statements are True or…

    The new operator allocates memory during runtime.
    True

    Solution not available in the source paper.

    10State whether the given statements are True or…

    The statements n = 25 and n == 25 are same.
    False

    Solution not available in the source paper.

    11State whether the given statements are True or…

    The expression p =- 9 is same as p = p-9.
    False

    Solution not available in the source paper.

    12State whether the given statements are True or…

    The assignment operator (=) is a binary operator.
    True

    Solution not available in the source paper.

    13State whether the given statements are True or…

    The output of System.out.println(1==1); is true.
    True

    Solution not available in the source paper.

    14State whether the given statements are True or…

    Explicit type conversion is also known as coercion.
    False

    Solution not available in the source paper.

    1Assignment Questions

    We have two variables x and y. Write Java statements to calculate the result of division of y by x and calculate the remainder of the division.

    Show SolutionHide Solution↓
    int quotient = y / x;
    int remainder = y % x;
    2Assignment Questions

    Assign the value of pi (i.e., 3.142) to a variable with requisite data type.

    Show SolutionHide Solution↓
    float pi = 3.142f;
    3Assignment Questions

    What are logical operators? Give an example of each.

    Show SolutionHide Solution↓

    Logical operators operate only on boolean operands and are used to construct complex decision-making expressions. Some of the logical operators are given below:

    OperatorSymbol
    Logical AND&&
    Logical OR||
    Logical NOT!
    4Assignment Questions

    What is an assignment operator? Give an example.

    Show SolutionHide Solution↓

    Assignment operator is used to assign the value of an expression to a variable. It has the following syntax:

    variable = expression;

    For example,

    totalMarks = 780;

    The above statement assigns the value of 780 to the variable totalMarks. Any previous value stored in totalMarks variable is overwritten by this new value.

    5Assignment Questions

    Explain the shorthand assignment operator with an example.

    Show SolutionHide Solution↓

    Java provides shorthand assignment operators for all the arithmetic binary operators. Shorthand assignment operators follow the below syntax:

    variable = variable operation expression;

    Taking the example of shorthand addition operator, the expression x = x + 3 can be rewritten as x += 3;

    6Assignment Questions

    What is the purpose of the new operator?

    Show SolutionHide Solution↓

    The new operator is used to allocate memory for the object.

    7Assignment Questions

    What is the use and syntax of a ternary operator?

    Show SolutionHide Solution↓

    Ternary operator is used to check if a condition is true and false. Depending on whether the condition tests true or false, expression 1 or expression 2 is evaluated. Its syntax is:

    boolean-expression ? expression1 : expression2;
    8Assignment Questions

    State the difference between = and ==.

    Show SolutionHide Solution↓
    ===
    It is the assignment operator used for assigning a value to a variable.It is the equality operator used to check if a variable is equal to another variable or literal.
    E.g. int a = 10; assigns 10 to variable a.E.g. if (a == 10) checks if variable a is equal to 10 or not.
    9Assignment Questions

    If a = 5, b = 9, calculate the value of:
        a += a++ - ++b + a

    Show SolutionHide Solution↓

        a += a++ - ++b + a
    ⇒ a = a + (a++ - ++b + a)
    ⇒ a = 5 + (5 - 10 + 6)
    ⇒ a = 5 + 1
    ⇒ a = 6

    a++ first uses the current value of a (which is 5) in the expression and then increments it to 6. ++b first increment the current value of b to 10 and uses this incremented value in the expression.

    10Assignment Questions

    Distinguish between the following:

    a. Prefix and Postfix Increment

    Show SolutionHide Solution↓
    Prefix IncrementPostfix Increment
    It works on the principle of first increment, then use.It works on the principle of first use, then increment.
    It (++) is written before the operand.It (++) is written after the operand.
    Example:
    int a = 99;
    int b = ++a;
    After the execution of these two statements, both a and b will have the value of 100.
    Example:
    int a = 99;
    int b = a++;
    After the execution of these two statements, a will have the value of 100 and b will have the value of 99.

    b. Prefix and Postfix Decrement

    Prefix DecrementPostfix Decrement
    It works on the principle of first decrement, then use.It works on the principle of first use, then decrement.
    It (--) is written before the operand.It (--) is written after the operand.
    Example:
    int a = 100;
    int b = --a;
    After the execution of these two statements, both a and b will have the value of 99.
    Example:
    int a = 100;
    int b = a--;
    After the execution of these two statements, a will have the value of 99 and b will have the value of 100.

    c. Unary and Binary Operators

    Unary OperatorsBinary Operators
    It operates on a single operandIt operates on two operands
    Increment (++) and Decrement (--) operators are examples of Unary Arithmetic OperatorsMultiplication (*) and Division (/) are examples of Binary Arithmetic Operators

    d. Increment and Decrement Operator

    Increment OperatorDecrement Operator
    Increment operators increment the value of their operand by 1.Decrement operators decrement the value of their operand by 1.
    Increment operators are represented by ++ symbol.Decrement operators are represented by -- symbol.

    e. / and % operator

    /%
    It is the Division operatorIt is the Modulus operator
    Returns the quotient of division operationReturns the remainder of division operation
    Example: int a = 5 / 2; Here a will get the value of 2 which is the quotient of this division operationExample: int b = 5 % 2; Here b will get the value of 1 which is the remainder of this division operation
    11Assignment Questions

    If m=5, n=2; what will be the output of m and n after execution?
    i. m -= n       ii. n = m + m/n

    Show SolutionHide Solution↓

    i.  m -= n
    ⇒ m = m - n
    ⇒ m = 5 - 2
    ⇒ m = 3

    ii. n = m + m/n
    ⇒ n = 5 + 5/2
    ⇒ n = 5 + 2
    ⇒ n = 7

    12Assignment Questions

    If x = 3, y = 7, calculate the value of:
    x -= x++ - ++y

    Show SolutionHide Solution↓

        x -= x++ - ++y
    ⇒ x = x - (x++ - ++y)
    ⇒ x = 3 - (3 - 8)
    ⇒ x = 3 - (-5)
    ⇒ x = 3 + 5
    ⇒ x = 8

    13Assignment Questions

    What will be the output of the following if x=5?
    i. 5 * ++x
    ii. 5 * x++

    Show SolutionHide Solution↓

    i.  5 * ++x
    ⇒ 5 * 6
    ⇒ 30

    ii.  5 * x++
    ⇒ 5 * 5
    ⇒ 25

    14Assignment Questions

    What is type conversion? How is an implicit type conversion different from explicit type conversion?

    Show SolutionHide Solution↓

    Type conversion is a process that converts a value of one data type to another data type.

    In an implicit conversion, the result of a mixed mode expression is obtained in the higher most data type of the variables without any intervention by the user. For example:

    int a = 10;
    float b = 25.5f, c;
    c = a + b;

    In case of explicit type conversion, the data gets converted to a type as specified by the programmer. For example:

    int a = 10;
    double b = 25.5;
    float c = (float)(a + b);
    15Assignment Questions

    What do you understand by type conversion?

    Show SolutionHide Solution↓

    Type conversion is a process that converts a value of one data type to another data type.

    16Assignment Questions

    Explain the term 'typecasting'.

    Show SolutionHide Solution↓

    The process of converting a value of one data type to another data type is called typecasting.

    17Assignment Questions

    What are precedence and associativity?

    Show SolutionHide Solution↓

    Precedence of operators refers to the order in which the operators are applied to the operands in an expression.
    Associativity of operators refers to the direction of execution of operators ("Left to Right" or "Right to Left") when operators in an expression have the same precedence.

    18Assignment Questions

    Evaluate the following expressions, if the values of the variables are a = 2, b = 3 and c = 3

    i. a - (b++) * (--c)

    ii. a * (++b) %c

    Show SolutionHide Solution↓

    i.  a - (b++) * (--c)
    ⇒ 2 - 3 * 2
    ⇒ 2 - 6
    ⇒ -4

    ii. a * (++b) %c
    ⇒ 2 * 4 % 3
    ⇒ 8 % 3
    ⇒ 2

    19Assignment Questions

    Write the Java expressions for the following:

    i. (a + b)2 + b

    Show SolutionHide Solution↓

    Math.pow(a+b, 2) + b

    ii. a2 + b2

    a * a + b * b

    iii. z = x3 + y3 + (xy) / 3

    z = Math.pow(x, 3) + Math.pow(y, 3) + x * y / 3

    iv. f = a2 + b2 / a2 - b2

    f = (a * a + b * b) / (a * a - b * b)

    v. z = ab + bc + ca / 3abc

    z = (a * b + b * c + c * a) / (3 * a * b * c)

    vi. 0 <= x <= 50

    x >= 0 && x <= 50

    vii. a = (0.05 - 2y3) / x - y

    a = (0.05 - 2 * y * y * y) / (x - y)

    viii. (a + b)n / √3 + b

    Math.pow(a+b, n) / (Math.sqrt(3) + b)

    ix. ax + by / ∛x + ∛y

    (Math.pow(a, x) + Math.pow(b, y)) / (Math.cbrt(x) + Math.cbrt(y))

    20Assignment Questions

    Rewrite the following statements without using shorthand operators.

    a. p /= q
    b. p -= 1
    c. p *= q + r
    d. p -= q - r

    Show SolutionHide Solution↓

    a. p = p / q
    b. p = p - 1
    c. p = p * (q + r)
    d. p = p - (q - r)

    21Assignment Questions

    Determine the output of the following program.

    public class Test
    {
        public static void main(String[] args) 
        {
            int a = 1, b = 2;
            System.out.println("Output1: " + a + b);
            System.out.println("Output2: " + (a + b));
        }
    }
    Output1: 12
    Output2: 3
    

    Solution not available in the source paper.

    22Assignment Questions

    What is the difference between the following two statements in terms of execution? Explain the results.
    x -= 5;
    x =- 5;

    Show SolutionHide Solution↓

    The first statement, x -= 5; subtracts 5 from x and assigns the result back to x. It is equivalent to x = x - 5;
    The second statement, x =- 5; assigns the value of -5 to x.

    23Assignment Questions

    What is concatenation? On which data type is concatenation performed?

    Show SolutionHide Solution↓

    Concatenation means joining two strings together. It is performed on String data type.

    24Assignment Questions

    Determine the output of the following program.

    public class PredictOutput1
    {
      public static void main(String args[])
      {
           int a = 4, b = 2, c = 3;
           System.out.println("Output 1: " + (a = b * c));
           System.out.println("Output 2: " + (a = (b * c)));
      }
    }
    Output 1: 6
    Output 2: 6
    

    Solution not available in the source paper.

    25Assignment Questions

    Determine the output of the following program.

    public class PredictOutput2
    {
      public static void main(String args[])
      {
           int a = 6, b = 2, c = 3;
           System.out.println("Output 1: " + (a == b * c)); 
           System.out.println("Output 2: " + (a == (b * c)));
      }
    }
    Output 1: true
    Output 2: true
    

    Solution not available in the source paper.

    26Assignment Questions

    Determine the output of the following program.

    public class PredictOutput3
    {
      public static void main(String args[])
      {
           int a = 2, b = 2, c = 2;
           System.out.println("Output 1: " + (a + 2 < b * c)); 
           System.out.println("Output 2: " + (a + 2 < (b * c)));
      }
    }
    Output 1: false
    Output 2: false
    

    Solution not available in the source paper.

    Press ↓ to reveal a solution and move to the next question. Back to Logix Computer Applications.

    Free sample bank