CHAPTER4
CHAPTER4
Operators in Java
Section 3: Assignment Questions
1. 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. [ICSE-2005]
Ans. q=y/x;
r=y % x;
2. Assign the value of pi (i.e., 3.142) to a variable with requisite data type. [ICSE-2007]
Ans. double pi=3.142
When you run the above code, the output will be:
-5.5 is not positive
8. State the difference between = and == operators. [ICSE 2007]
Ans. The operator = is an assignment operator. It assigns a variable to some value. For example, z = 8
sets the value of the variable z to 8.
The == operator is an equality operator. We can use it to check if something equals something else. For
example, if (z == 8) checks if the value that z holds is equal to 8.
e. / and % operator
www.bhuvantechs.com
Ans. ‘/’ is the division operator and returns the quotient value after division.
‘%’ is the modulus operator and returns the remainder value after division.
For example,
x=10/2;
y=10%2;
Here, the value of x will be 5 and the value of y will be 0.
12. If x = 3, y = 7, what will be the output of x and y after execution? x -= x++ - ++y
Ans. Output : X=8, y=8
14. What is type conversion? How is an implicit type conversion different from explicit type
conversion?
Ans. Type conversion is a process that converts a value of one data type to another data type. Type
conversions automatically performed by the Java compiler are known as implicit type conversions.
Explicit type conversions are performed by the programmer using the cast operator.
18. Evaluate the following expressions, if the values of the variables are: a = 2, b = 3 and c = 3
a. p /= q Ans. p=p/q
b. p -= 1 Ans. p=p-1
c. p *= q + r Ans. p=p*(q+r)
d. p -= q – r Ans. p=p-(q-r)
Output1: 12
Output2: 3 www.bhuvantechs.com
22. What is the difference between the following two statements in terms of execution? Explain the
results.
x -= 5;
x =- 5;
Ans. x-=5 is x=x-5 (shorthand operator).
x=-5 is -5 is assigned to variable x.
Output 1: 6
Output 2: 6
Output 1: true
Output 2: true
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)));
}
}
Ans. Output :-
Output 1: false
Output 2: false