# Chapter 5 Unit 2: Variables and Expressions

**Contents**

- Fill in the blanks
- Explain the meaning of the following short hand operators
- Arrange these operators according to their precedence:
- Write short answers

## Fill in the blanks

#### Question 1

The statement n++ is equivalent to n = n + * 1*.

#### Question 2

If a = -1; then ++a returns * 0*.

#### Question 3

The symbol '=' is also known as an * assignment* operator.

#### Question 4

* An expression* is a set of variables, constants and arithmetical operators.

#### Question 5

An expression, that includes different data types to the variables, is known as * impure or mixed mode* expression.

#### Question 6

The default value of boolean constant is * false*.

#### Question 7

++m is an expression whereas, ++; is a * prefix increment* operator.

#### Question 8

In shift right operator, bits of the number are shifted to the * right* and fills 0 on voids

*.*

**on the left**#### Question 9

A * variable* can possess any combination of letters without space.

#### Question 10

* Tokens* are the smallest unit of a Java program.

#### Question 11

* Ternary* operator is also known as the conditional operator.

#### Question 12

The null literal is represented as * null*.

## Explain the meaning of the following short hand operators

Operators | Meaning |
---|---|

+= | It is 'Add AND assign' operator. It adds right operand with left operand and assigns the result to the left operand. Example: a += b; is equivalent to a = a + b; |

<<= | It is 'Left Shift AND assign' operator. It shifts the binary pattern of the left operand by defined number of bits left and assigns the result to the left operand. Example: a <<= 2 is equivalent to a = a << 2; |

*= | It is 'Multiply AND assign' operator. It multiplies right operand with the left operand and assigns the result to the left operand. Example: a *= b; is equivalent to a = a * b; |

>>= | It is 'Right Shift AND assign' operator. It shifts the binary pattern of the left operand by defined number of bits right and assigns the result to the left operand. Example: a >>= 2 is equivalent to a = a >> 2; |

%= | It is 'Modulus AND assign' operator. It divides the left operand by right operand and assigns remainder to the left operand. Example: a %= b is equivalent to a = a % b |

/= | It is 'Divide AND assign' operator. It divides the left operand by right operand and assigns the result to the left operand. Example: a /= b is equivalent to a = a / b |

!= | It is 'not equal to' operator. It checks if values of its two operands are unequal or not. If its operands are not equal, it returns true otherwise it returns false. Example: a != b ⇒ true if a and b are not equal otherwise false. |

## Arrange these operators according to their precedence:

Operators | Precedence of Operators |
---|---|

Logical AND | Postfix |

Additive | Multiplicative |

Relational | Additive |

Conditional | Relational |

Multiplicative | Logical AND |

Logical OR | Logical OR |

Assignment | Conditional |

Postfix | Assignment |

## Write short answers

#### Question 1

What is meant by Token? Name all the tokens which are used in Java.

**A token is the smallest element of a program that is meaningful to the compiler. The different types of tokens in Java are:**

**Identifiers****Literals****Operators****Separators****Keywords**

#### Question 2

What are reserved words? Name at least five reserved words.

**In Java, a reserved word is a word that has a predefined meaning in the language. Due to this, reserved words can’t be used as names for variables, methods, classes or any other identifier. Reserved words are also known as keywords. Five commonly used Java reserved words are:**

**public****class****int****double****char**

#### Question 3

Explain the following with an example each:

(a) Assignment operator

**Assignment operator (=) is used to assign a literal, value of a variable, the result of any expression or the return value of a method to a variable.****Example:**`int a = 10;`

`int b = a * 4;`

(b) Relational operator

**Relational operators are used to determine the relationship between the operands. Relational operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The result of an operation involving relation operators is a boolean value — true or false.****Example:**`int a = 8;`

`int b = 10;`

`boolean c = a < b;`

**Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.**

(c) Logical operator

**Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value.****Example:**`int a = 7;`

`int b = 10;`

`boolean c = a < b && a % 2 == 0;`

**Here, the result of first boolean expression a < b is true and the result of second boolean expression a % 2 is false. The logical AND operator ( && ) combines these true and false boolean values and gives a resultant boolean value as false. So, boolean variable c becomes false.**

(d) Ternary operator

**Ternary operator operates on three operands. Its syntax is:**`condition ? expression 1 : expression 2`

**Ternary operator evaluates the condition. If the condition is true then result of ternary operator is the value of expression 1. Otherwise the result is the value of expression 2.****Example:**`boolean isLeapYear = true;`

`int febDays = isLeapYear ? 29 : 28;`

**Here, the ternary operator checks if the value of boolean variable isLeapYear is true or false. As it is true, expression 1, which in this example is the value 29, is the result of the ternary operator. So, int variable febDays becomes 29.**

#### Question 4

Why do you need to declare variables? Explain.

**Declaring a variable tells Java how much memory it should reserve for storing the variable. Declaring a variable also helps in preventing errors as the compiler can check and flag illegal operations at compile time itself.**

#### Question 5

What are operators? Name the different types of operators that are used in Java.

**An operator is a symbol or sign used to specify an operation to be performed in Java programming. The three main types of operators are Arithmetical, Logical and Relational.**

#### Question 6

Explain 'Arithmetical Operators' with their types.

**Arithmetical operators are used to perform common mathematical operations. Operands of Arithmetic operators must be of numeric type. There are two types of Arithmetical operators in Java:**

**Unary Arithmetical Operators****These operators work on a single operand. Operators in this category are unary plus (+), unary minus (-), prefix and postfix increment (++) and decrement (--) operators.****Binary Arithmetical Operators****These operators work on two operands. They work in mathematical expressions the same way as they work in algebra. Operators in this category are Addition (+), Subtraction (-), Multiplication (*), Division (/) and Modulus (%).**

#### Question 7

What do you understand by 'Shift Operators'? Explain.

**Shift Operators perform bit manipulations on data by shifting the bits of its first operand right or left. The different types of shift operators available in Java are:**

**Shift Left Operator (<<)**

Its syntax is op1 << op2. It shifts bits of op1 left by op2 number of bits filling the vacated places on the right with zeros.**Signed Shift Right Operator (>>)**

Its syntax is op1 >> op2. It shifts bits of op1 right by op2 number of bits filling the vacated places on the left with the value of its Most Significant Bit. It preserves sign bit of the number.**Unsigned Shift Right Operator (>>>)**

Its syntax is op1 >>> op2. It shifts bits of op1 right by op2 number of bits filling the vacated places on the left with zeros. It doesn't preserve sign bit of the number.

#### Question 8

Distinguish between:

(a) Unary and Binary arithmetic operator

Unary Arithmetic Operator | Binary Arithmetic Operator |
---|---|

It operates on a single operand | It operates on two operands |

Increment (++) and Decrement (--) operators are examples of Unary Arithmetic Operators | Multiplication (*) and Division (/) are examples of Binary Arithmetic Operators |

(b) Prefix and Postfix operator

Prefix Operator | Postfix Operator |
---|---|

It works on the principle of CHANGE-THEN-USE. | It works on the principle of USE-THEN-CHANGE. |

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

(c) Variables and Constants

Variables | Constants |
---|---|

Value of variables can change while program is running. | Value of constant remains the same throughout the execution of the program. |

Variables can be of Primitive or Non-Primitive Data Types. | Constant are categorized as Integer literals, Real literals, Character literals, String literals, Boolean literals, Null literal. |

(d) Assignment and Expression

Assignment | Expression |
---|---|

Assignment is the process in which Assignment operator (=) is used to assign a literal, value of a variable, the result of any expression or the return value of a method to a variable. | Expression is a set of variables, constants and arithmetical operators. |

Example: int a = 10; int b = a * 4; | Example: a++ + ++a x * x + 4 |

#### Question 9

How does a boolean literal differ from a String literal? Explain.

String Literal | Boolean Literal |
---|---|

String literals are written by enclosing a set of characters within a pair of double quotes. | A boolean literal can take only one of the two boolean values represented by the words true or false. |

String literals can be assigned to variables of String data type. | Boolean literals can be assigned to variables of boolean data type. |

Escape Sequences can be used to write string literals | Only true and false values are allowed for boolean literals |

#### Question 10

Evaluate the following expressions, if the values of the variables are:

int p,w,k;

p, w, k = 8; int m = 11, r = 7;

(a) p += m + (--r + k) + 3 * (m++) * m;

##### Answer

**p += m + (--r + k) + 3 * (m++) * m**

⇒ **p = p + (m + (--r + k) + 3 * (m++) * m)**

⇒ **p = 8 + (11 + (6 + 8) + 3 * 11 * 12)**

⇒ **p = 8 + (11 + 14 + 3 * 11 * 12)**

⇒ **p = 8 + (11 + 14 + 33 * 12)**

⇒ **p = 8 + (11 + 14 + 396)**

⇒ **p = 8 + 421**

⇒ **p = 429**

(b) k += --m % 5 + m++ * 10 + r++;

##### Answer

**k += --m % 5 + m++ * 10 + r++**

⇒ **k = k + (--m % 5 + m++ * 10 + r++)**

⇒ **k = 8 + (10 % 5 + 10 * 10 + 7)**

⇒ **k = 8 + (0 + 100 + 7)**

⇒ **k = 8 + 107**

⇒ **k = 115**

(c) w = k * m++ / 3 + k + --r + r++;

##### Answer

**w = k * m++ / 3 + k + --r + r++**

⇒ **w = 8 * 11 / 3 + 8 + 6 + 6**

⇒ **w = 88 / 3 + 8 + 6 + 6**

⇒ **w = 29 + 8 + 6 + 6**

⇒ **w = 49**

(d) p *= r++ % 7 + --m % 5 + k * ++k + 8 * --k;

##### Answer

**p *= r++ % 7 + --m % 5 + k * ++k + 8 * --k**

⇒ **p = p * (r++ % 7 + --m % 5 + k * ++k + 8 * --k)**

⇒ **p = 8 * (7 % 7 + 10 % 5 + 8 * 9 + 8 * 8)**

⇒ **p = 8 * (0 + 0 + 72 + 64)**

⇒ **p = 8 * 136**

⇒ **p = 1088**

(e) k /= m++ * --r + 4 * r++ + --k + m * ++p / 3;

##### Answer

**k /= m++ * --r + 4 * r++ + --k + m * ++p / 3**

⇒ **k = k / (m++ * --r + 4 * r++ + --k + m * ++p / 3)**

⇒ **k = 8 / (11 * 6 + 4 * 6 + 7 + 12 * 9 / 3)**

⇒ **k = 8 / (66 + 24 + 7 + 108 / 3)**

⇒ **k = 8 / (66 + 24 + 7 + 36)**

⇒ **k = 8 / 133**

⇒ **k = 0**