Operators in Java

Solutions for Computer Applications, Class 9, ICSE

Assignment Questions

21 questions

Question 1

What are logical operators? Give an example of each.

Assignment Questions

Answer:

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!

Question 2

What is an assignment operator? Give an example.

Assignment Questions

Answer:

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.

Question 3

Explain the shorthand assignment operator with an example.

Assignment Questions

Answer:

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;

Question 4

What is the use and syntax of a ternary operator?

Assignment Questions

Answer:

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;

Question 5

State the difference between = and ==.

Assignment Questions

Answer:

===
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.

Question 6

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

Assignment Questions

Answer:

    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.

Question 7

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

Assignment Questions

Answer:

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

Question 8

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

Assignment Questions

Answer:

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

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

Question 9

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

Assignment Questions

Answer:

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);

Question 10

What do you understand by type conversion?

Assignment Questions

Answer:

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

Question 11

What is typecasting in Java? Give an example.

Assignment Questions

Answer:

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

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

Question 12

What are precedence and associativity?

Assignment Questions

Answer:

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.

Question 13

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

Assignment Questions

Answer:

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

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

Question 14

Write the Java expressions for the following:

i. (a + b)2 + b

Assignment Questions

Answer:

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

Question 15

Rewrite the following statements without using shorthand operators.

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

Assignment Questions

Answer:

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

Question 16

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));
    }
}
Assignment Questions

Answer:

Question 17

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

Assignment Questions

Answer:

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.

Question 18

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

Assignment Questions

Answer:

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

Question 19

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)));
  }
}
Assignment Questions

Answer:

Question 20

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)));
  }
}
Assignment Questions

Answer:

Question 21

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)));
  }
}
Assignment Questions

Answer:

Multiple Choice Questions

12 questions

Question 1

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

  1. A unary operator ✓
  2. A binary operator
  3. A ternary operator
  4. None of these
Multiple Choice Questions

Answer:

Question 2

Which one of the following is not a binary operator?

  1. AND
  2. %
  3. ==
  4. ! ✓
Multiple Choice Questions

Answer:

Question 3

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

  1. <=
  2. !== ✓
  3. !=
  4. ==
Multiple Choice Questions

Answer:

Question 4

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

  1. i++
  2. i += 1
  3. ++i
  4. All of these ✓
Multiple Choice Questions

Answer:

Question 5

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

  1. sum = 13
  2. sum = 14 ✓
  3. sum = 15
  4. sum = 16
Multiple Choice Questions

Answer:

Question 6

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

  1. 0
  2. 1
  3. false ✓
  4. true
Multiple Choice Questions

Answer:

Question 7

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

  1. 0
  2. 1
  3. false
  4. true ✓
Multiple Choice Questions

Answer:

Question 8

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

  1. 0
  2. 1 ✓
  3. false
  4. true
Multiple Choice Questions

Answer:

Question 9

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

  1. 4
  2. 3
  3. 2
  4. 1 ✓
Multiple Choice Questions

Answer:

Question 10

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

  1. 4 ✓
  2. 3
  3. 0
  4. 1
Multiple Choice Questions

Answer:

Question 11

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

  1. six 33 ✓
  2. six 6
  3. 33 six
  4. 6 six
Multiple Choice Questions

Answer:

Question 12

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

  1. 6
  2. 8
  3. 4 ✓
  4. None of these
Multiple Choice Questions

Answer:

State whether the given statements are True or False

10 questions

Question 1

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

State whether the given statements are True or False

Answer:

Question 2

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

State whether the given statements are True or False

Answer:

Question 3

The expression 10 % 4 evaluates to 2.
True

State whether the given statements are True or False

Answer:

Question 4

The expression 3 / 4 evaluates to 0.
True

State whether the given statements are True or False

Answer:

Question 5

The expressions 3 + 4 and "3" + "4" evaluate to the same value.
False

State whether the given statements are True or False

Answer:

Question 6

The expression x = x + 7 is same as x =+ 7.
False

State whether the given statements are True or False

Answer:

Question 7

The new operator allocates memory during runtime.
True

State whether the given statements are True or False

Answer:

Question 8

The statements x = 7 and x == 7 are same.
False

State whether the given statements are True or False

Answer:

Question 9

The expression z =- 7 is same as z = z-7.
False

State whether the given statements are True or False

Answer:

Question 10

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

State whether the given statements are True or False

Answer: