0% found this document useful (0 votes)
15 views5 pages

CHAPTER4

This document covers various operators in Java, including assignment, logical, and arithmetic operators, along with their definitions and examples. It also discusses concepts like type conversion, precedence, and associativity, providing sample code and expected outputs for various expressions. Additionally, it includes questions and answers related to operator usage and behavior in Java programming.

Uploaded by

Borra ram kumar
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)
15 views5 pages

CHAPTER4

This document covers various operators in Java, including assignment, logical, and arithmetic operators, along with their definitions and examples. It also discusses concepts like type conversion, precedence, and associativity, providing sample code and expected outputs for various expressions. Additionally, it includes questions and answers related to operator usage and behavior in Java programming.

Uploaded by

Borra ram kumar
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/ 5

CHAPTER 4

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

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


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

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

5. 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 rewrite as z += 3. The version of the statement uses
+= shorthand assignment operator. Both perform the same action i.e. increment 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.

6. What is the purpose of the new operator? [ICSE-2006]


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

7. What is the use and syntax of a ternary operator? [ICSE 2009]


Ans. A ternary (only three) operator requires three operands. The ternary operator consists of a
condition that evaluates to either true or false, plus a value that is returned if the condition is true and
another value that is returned if the condition is false. An example of a ternary operator is ?.
www.bhuvantechs.com

Example: class Operator {


public static void main(String[] args) {
Double number = -5.5;
String result;
result = (number > 0.0) ? "positive" : "not positive";
System.out.println(number + " is " + result); } }

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.

9. If a = 5, b =9, calculate the value of: a += a++ - ++b + a [ICSE 2008]


Ans. 6

10. Distinguish between the following:

a. Prefix and Postfix Increment


Ans. Prefix increment: when the increment operator (++) is put before its operand like, ++x.
Postfix increment: when the increment operator (++) is put after its operand like, x++.
The difference between the two lies in their return values. The prefix increment returns the value of a
variable after it has been incremented. On the other hand, the postfix increment returns the value of a
variable before it has been incremented.

b. Prefix and Postfix Decrement


Ans. Prefix decrement: when the decrement operator (--) is put before its operand like, --x.
Postfix decrement: when the decrement operator (--) is put after its operand like, x--.
The difference between the two lies in their return values. The prefix decrement returns the value of a
variable after it has been decremented. On the other hand, the postfix decrement returns the value of a
variable before it has been decremented.

c. Unary and Binary Operators


Ans. Unary operators are those operators which operate on one operand, for example, ++a here ++ is
an unary operator.
Binary operators are those operators which operate on two operands, for example, a+b here + is a
binary operator.

d. Increment and Decrement Operator


Ans. Increment operator increases the value of the variable by one. ‘++’ is the increment operator.
Decrement operator decreases the value of the variable by one. ‘--’ is the decrement operator.
In both the cases, the value of the operand is altered to its new value. The operands required to be used
with increment or decrement operator should be a variable.

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.

Operators in Java ~2~


11. If m=5, n=2; what will be the output of m and n after execution?
i. m -= n ii. n = m + m/n [ICSE-2005]
Ans. Output : m=3, n=2 Ans. Output : m=5, n=7

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

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


5 * ++x 5 * x++ [ICSE 2005]
Ans. Output : 30 Ans. Output : 25

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.

15. What do you understand by type conversion? [ICSE-2010]


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

16. Explain the term ‘typecasting’. [ICSE-2007]


Ans. To create a conversion between two incompatible types, a cast must be used. A cast is simply an
explicit type conversion. 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
being typecasted 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.

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

18. 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 [ICSE 2007]


Ans. Output: -4 Ans. Output: 2 www.bhuvantechs.com

19. Write the Java expressions for the following:

i. (a+b)2+b [ICSE 2009] Ans. Math.pow((a+b),2)+b

ii. a2+b2 [ICSE 2006] Ans. Math.pow(a,2)+Math.pow(b,2)

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

Operators in Java ~3~


iv. f=(a2+b2)/(a2-b2) Ans. f=(Math.pow(a,2)+Math.pow(b,2))/(Math.pow(a,2)-Math.pow(b,2))

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

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

vii. a=(0.05-2y3)/(x-y) Ans. a=(0.05-2*(Math.pow(y,3)))/(x-y)

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

ix. (ax+by)/³√x+³√y Ans. (Math.pow(a,x)+Math.pow(b,y))/(Math.cbrt(x)+Math.cbrt(y))

20. Rewrite the following statements without using shorthand operators.

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)

21. 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));
}
}
Ans. Output :-

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.

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


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

Operators in Java ~4~


24. 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)));
}
}
Ans. Output :-

Output 1: 6
Output 2: 6

25. 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 :-

Output 1: true
Output 2: true

26. Determine the output of the following program.

public class PredictOutput3


{
public static void main(String args[])
{
www.bhuvantechs.com

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

Operators in Java ~5~

You might also like