# Chapter 4

I. Multiple Choice Questions:

1. The statement n+=4 is equivalent to

a) ++n                                                       b) n=n+4                          c) n+1                            d)none

2. What will be the output of a & b in the following, if    int a,b;a=10;b=a++;

a) 10,10                            b)10,11                             c)11,10                          d) 11,11

3. What will be the output of a++ when int a= -1;

a) 1                                    b) -1                                  c) 0                               d) none

4. If int a=25, b=5,c=0; what value is stored in c? when c=a%b;

a)5.                                    b)  5                                  c)   0                              d) none

5. What is the result of the following in Java statement?

When int m=8; m*=8; System.out.println(“The output=”+m);

a)8                                      b) 64                                c) 16                              d) 88

6. double c; int x,y,z; x=5; y=10; z=11;

c= x*y +z / 2;

The value stored in c is :

a) 55.0                                b) 55.5                              c)  55                            d) none

7. int m,p; m=5; p=0; p=m – –  +  – – m; The output will be

a) 11                                   b) 10                                 c) 8                               d) 12

8. int a=7, p=0, q=0 ; p=++a  +  – -a;  q – = p; The output of q will be :

a) 13                                   b)  14                                c)  15                            d) -15

Ans.

1. b) n=n+4

2. a) 10,10

3. b) -1

4. c)   0

5. b) 64

6. a) 55.0

7. c) 8

8. d) -15

II. Write the Java expressions for the following:

1. p=a2+bc                  Ans.  p=a*a + b*c     [or]    Math.pow(a,2) +b*c

2. m= a2 – b2                     Ans.  m= (a*a – b*b) /(a*b)

ab

3. s=ut+ 1 at2                Ans. s=u*t + (1/2.0 ) *a*t*t

2

4. f= uv                       Ans. u*v/(u+v)

u+v

5. (a+b)2 +b                 Ans. Math.pow((a+b),2)+b

6. y= 2(lb + bh + lh)     Ans. 2* (l*b +b*h+l*h)

7. a2 + b2                     Ans. a*a +b*b    [or]     Math.pow(a,2) + Math.pow(b,2)

8. z= x3 + y3xy         Ans. z=Math.pow(x,3) + Math.pow(y,3) – (x*y)/3.0

3

1. What is an operator?

Ans. An operator is basically a symbol or token, which performs arithmetic or logical operations and gives meaningful result.

2. Name the different types of operators.

Ans. The three main types of operators in Java are Arithmetic operators, Relational operators and Logical operators.

3. Explain the following:

a) Arithmetical operator                                             b) Relational operator

c) Logical operator                                                     d) Unary operator

e) New operator                              f) Binary operator

Ans.

a) Arithmetical operator

The operators which are used to perform arithmetical calculations in a program are known as arithmetical operators. Example +, -, *, / , % are the arithmetical operators used to perform addition, subtraction, multiplication, division and modulus ( to get the remainder of division) respectively.

b) Relational operator

These operators are used to show the relationship between the operands. Relational operators like <,<=,>,>=,==,!= compare the values of the variables and result in true or false.

c) Logical Operator

Java uses logical operators (&&, ||, ! ) to yield true or false depending on the outcome of different expressions.

• AND operator (symbol:  &&) results in true when all the conditions joined by AND are true. Example : if(a>b && a>c && a!=0)
• OR operator (symbol:  ||) results in true when atleast one of the conditions joined by OR is true.

Example:  if( a>0 || a<100)

• NOT operator (symbol:  !) is used when the reverse of the result of the expression. It is a unary operator since it uses a single operand.  Example:   if(!a==10)

The above condition results in true when a is not equal to 10.

d) Unary operator

If an arithmetic operator is applied to a single operand it is known as a Unary operator (+, – , ++, — etc)

Examples: a= – 36 ;  b=++a;

e) new operator

The keyword new is used to allocate space in the dynamic memory for the storage of data and functions belonging to an object in Java programming.

f) Binary operator

An arithmetic operator applied to two operands is known as a Binary operator (+, -, *. /, % etc)

Examples: int y=5%3;  double p=5/2.0; double k=y+p;

4. What is a Ternary operator? Explain with the help of an example.

Ans. Ternary operators deal with three operands. It is also called conditional assignment statement because the value assigned to a variable depends on a logical expression.

Syntax:

variable = (test expression)?Expression1 : Expression2;

The variable contains the expression1 if the test condition is true, otherwise it contains expression2.(i.e. if the condition is false)

For example:

int x=20, y=25;

int small= (x<y)? x: y;

In the above example if the condition (x < y) is true, then the value of small=x otherwise the value of small=y.

5. Differentiate between the following:

a) Arithmetical operator and Logical operator

b) Binary operator and Ternary operator

c) Logical AND(&&) and Logical OR(||)

d) Prefix operator and Postfix operator

e) System.out.print() and System.out.println()

Ans.

a) Arithmetical operator and Logical operator

• In an expression when arithmetic operators are used, it usually results in some numeric value whereas when logical operators are used, it results in either true or false.
• Arithmetical shorthand operators (+=, – =, *=, /=, %=) can be used to write short hand expressions whereas logical operators cannot be used to write such shorthand expressions.

b) Binary operator and Ternary operator

• A binary operator deals with two operands (For example a +b) whereas a Ternary operator deals with three operands (For example String x= “equal”, y= “unequal”; String c= (a= =b) ? x : y;
• The result of a binary operator does not always depend on the outcome of another expression. whereas the outcome of a ternary operator always depends on the expression given before the question mark.

c) Logical AND(&&) and Logical OR(||)

• && operator results in true when all the conditions joined by AND are true whereas an || operator results in true when any one of the conditions joined by OR is true.
• Example of &&:

if(a>b && a>c && a!=0) .

The above expression results in true only when all three conditions result in true.

Example of ||:

if( a>0 || a<100).

The above expression results in true when any one of the above conditions result in true.

d) Prefix operator and Postfix operatora

• Although prefix and postfix operators are unary operators, prefix operators are applied before the operand (for example ++a, – -b) whereas postfix operator are applied after the operand ( for example a- -, m++)
• Prefix operator works on the principle ‘change before action’ and the value of the variable changes before the operation whereas Postfix operator works on the principle ‘change after action’ and the value of the variable changes after the operation.

e) System.out.print() and System.out.println()

• System.out.print( ) prints the statement and the cursor waits in the same line of the screen whereas System.out.println( ) prints the statement and the cursor jumps to the next line and waits there.
• System.out.print(“Hello”);

System.out.print(“ world”);

The above statements produces the output as follows:

Hello world

• System.out.println(“Hello”);

System.out.print(“ world”);

The above statements produces the output as follows:

Hello

world

6. Differentiate between an operator and an expression.

Ans.

• An operator is basically a symbol or token, which performs arithmetical or logical operations to give meaningful result whereas an expression is a set of variables, constants and arithmetical operators. In other words, an expression is a combination of operators and operands.
• Example:

% is an operator whereas y=x%2 is an expression.

7. if m=5 and n=2 then what will be the value of m and n after execution that will be stored in (a) and (b)?

a) m – =n                  b) n = m+ m/n;

Ans.

Output

3

7

Working

a) m=m-n

m= 5 – 2

m=3

b) n= 5 + 5/2

n= 5 + 2

n=7

8. State the difference between = and = = .

Ans.

• = is an assignment operator whereas = = is a relational operator.
• Example: c=3 is used for assigning the value of 3 to c whereas if(c= =3) is used to check whether the value of c is equal to 3.

9. What will be the output for the following program segment?

int a=0, b=10, c=40;

a= –b + c++ +b;

System.out.println(“a=”+a);

Ans.

Output

a=58

Working

a= 9 + 40 + 9

=58

10. What will be the output of the following?

if x=5

(a) 5 * ++x;

(b) 5 * x++;

Ans.

Output

30

25

Working

5*6=30

5*5=25

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

a=2, b=3 and c=9

a) a – (b++) * (- -c);

b) a * (++b) %c

Ans.

Output

-22

8

Working

2 – 3*8= 2 – 24 = – 22

2 * (4)%9  = 8%9 = 8

[ Since * and % have the same precedence, left to right is followed]

12. If a=5 and b=9 calculate the value of:

a+= a++ – ++b  +a;

Ans.

Output

6

Working

a= a +( a++ – ++b  +a)

a= 5 + ( 5 – 10 + 6)

a= 5+ (1)

a= 6

13. Give the output of the program snippet.

int a=10, b=12;

if(a>=10)

a++;

else

++b;

System.out.println(“a=”+a+ “and b=”+b);

Ans.

Output

a=11and b=12

Working

if(10>=10)     //true

10++;

else

++12;

System.out.println(“a=”+11+ “and b=”+12);

//b value 12 is printed because the control does’nt enter the else part.

14. Rewrite the following using ternary operator.

if(income<=100000)

tax=0;

else

tax=(0.1 * income);

Ans.

tax= (income <=100000)? 0: (0.1 * income);

15. Rewrite the following using ternary operator.

if( p>5000)

d=p*5/100;

else

d=2*p/10;

Ans.

d= (p>5000)? p*5/100 : 2*p/10;

**************