0% found this document useful (0 votes)
83 views4 pages

Java Operators Explained

Comp !0 ICSE CHP 3

Uploaded by

memeruler28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

Java Operators Explained

Comp !0 ICSE CHP 3

Uploaded by

memeruler28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 5

Operators in Java
Section 3: Assignment Questions

1. What are logical operators? Give an example of each.


Ans. Logical operators operate only on Boolean operands and are used to construct complex decision making
expressions. Examples of logical operators – Logical AND - &&, Logical OR - || and Logical NOT - !.

2. What is an assignment operator? Give an example.


Ans. Assignment operator is used to assign the value of an expression to a variable.
For example, age = 15; The value of age after assignment will be 15.

3. Explain the shorthand assignment operator with an example.


Ans. Java offers several variations of the assignment operator that are referred to as shorthand assignment
operators.
For example: z = z + 3; can be rewritten as z += 3;
This version of the statement uses ‘+=’ shorthand assignment operator. Both perform the same action, i.e.,
incrementing the value of variable z by 3. Assuming that the initial value of z was 5, the new value of z will be 8
after the execution of the above statement.

4. What is the use and syntax of a ternary operator?


Ans. A ternary (only three) operator requires three operands. An example of a ternary operator is ? :

Its syntax is: boolean-expression ? expression1 : expression2 ;

First of all, the computer evaluates the boolean-expression which basically is a conditional statement: if it
evaluates to true, then the value of the entire expression is expression1; otherwise, the value of the entire
expression is expression2.

5. State the difference between = and ==.


Ans. The operator = is an assignment operator. It sets a variable equal to some value.
For example, z = 8 sets the variable named z equal to 8.
The == operator is a comparison operator. We can use it to check if something equals something else.
For example, z == 8 checks if the value that z holds is equal to 8.
www.bhuvantechs.com
6. If a = 5, b = 9, Calculate the value of: a += a++ - ++b + a
Ans. Here, a = a + a++ - ++b + a
a = 5 + 5 – 10 + 6
a=6

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


Ans. Here, x = x – (x++ - ++y)
x = 3 – (3 – 8)
x = 3-(-5)
x=8

Operators in Java ~1~


8. What will be the output of the following if x=5?

(i) 5 * ++x (ii) 5 * x++


Ans. 30 Ans. 25.

9. What is type conversion? How is an implicit type conversion different from explicit type conversion?
Ans. Type conversion is a process that converts the 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.

10. What do you understand by type conversion?


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

11. What is type casting in Java? Give an example.


Ans. To create a conversion between two incompatible data types, a cast must be used. A cast is simply an
explicit type conversion. Its syntax is: (data-type) expression;
Here the data-type is any primitive data type. The expression may be a constant, variable or any expression.
Following is an example of a double value that is type cast into an integer:
int a = (int) 7.45;
The above will cast the double value 7.45 into an int. The value of variable a will be 7.

12. What are precedence and associativity?


Ans. Precedence is the priority of an operator according to which it is evaluated. Each operator has a
precedence associated with it. This precedence is used to determine the order of evaluation of an expression
involving more than one operator.

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


Ans. – 4 Ans. 2

14. Write the Java expressions for the following:

i. (a + b)2 + b Ans. (a + b) * (a + b) + b www.bhuvantechs.com

ii. a2+ b2 Ans. a * a + b * b

iii. z = x3+ y3+ xy / 3 Ans. z = x * x * x + y * y * y + (x * y) / 3

iv. f = (a2 + b2 )/ (a2 – b2) Ans. f = (a * a + b * b) /( a * a – b * b)

v. z = (ab + bc + ca) / (3abc) Ans. z = (a * b + b * c + c * a) / (3 * a * b * c)

vi. 0 ≤ x ≤ 50 Ans. x >= 0 && x <= 50

Operators in Java ~2~


15. Rewrite the following statements without using shorthand operators.

i. p /= q Ans. p = p / q

ii. p -= 1 Ans. p = p – 1

iii. p *= q + r Ans. p = p * (q + r)

iv. p -= q – r Ans. p = p – (q – r)

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("Output 1 : " + a + b);
System.out.println("Output 2 : " + (a + b));
}
}

Ans. Output 1 : 12
Output 2 : 3

17. What is the difference in the behaviour of the following two statements? Explain the results.
x -= 5;
x =- 5;
Ans. Statement x -= 5 is the shorthand version of x = x -5 which means subtract 5 from the value of x.
Whereas, Statement x =- 5 is an assignment that assigns a value of -5 to variable x.

18. What is concatenation? On what data type is concatenation performed?


Ans. Concatenation is the process of joining two strings. It is performed on String data type.

19. Determine the output of the following program.

public class PredictOutput1


{ www.bhuvantechs.com
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)));
}
}

Ans. Output 1 : 6
Output 2 : 6

Operators in Java ~3~


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

Ans. Output 1 : true


Output 2 : true

21. Determine the output of the following program.

public class PredictOutput3


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

Ans. Output 1 : false


Output 2 : false

www.bhuvantechs.com

Operators in Java ~4~

You might also like