# Q9. State the rules of operator precedence.

Ans. All expressions are evaluated according to an operator precedence hierarchy that establishes the rules that govern the order in which operations are evaluated.

Operator (type). *,  /, and the remainder operator % are performed before + and -.

Any expression in parentheses is evaluated first

The assignment operator has a lower precedence than any of the arithmetic operators.

# Q8. What is casting, when do we need it?

Ans. Casting is a form of conversion, which uses the cast operator to specify by a type name in parenthesis and is placed in front of the value to be converted. For example:

result = ( float ) total / count ;

They are helpful in situations where we temporarily need to treat a value as another type.

# c.) fl + longval / sh

Ans.

a.) int because

b.) long because

c.) float because

# iii.) if the input is 500.

Ans.

i) 400 because the arithmetic operator + has higher precedence than ? : operator thus the condition before ? is taken as (n+val) and (1000+2000)>1750 is true.

ii) 400 the reason is the same as explained above((1000+1000)>1750 is true).

iii) 200 because (1000+500)>1750 is false.

# ii.) using a conditional operator.(Do not use the mathematical function here).

Ans.

i) Math.abs(p)

ii) p > 0 ? p : -p

# ii.) What will be the result of the if the values of val is 5 initially?

Ans.

i.) The expression

a.) is an assignment expression.

b.) is a an relational expression that tests for equality.

ii.) The result of

a.) will be val having value 3 i.e., 3 and the result of

b.) will be boolean false because 5 is not equal to 3.

Ans.

i.) false

ii.) true

# ii.) What is the effect of replacing ++ch with ch+1?

Ans.

i.) 21

21

ii.) ++ch not only replaces itself with ch+1 i.e., 21 but also increments the value of ch i.e., after ++ch the value of ch is 21. Whereas ch+1 will onlu print the incremented value i.e., 20+1=21; it will not increment the value of ch. Therefore , after replacing ++ch with ch+1, the output of the program will be:

21

20

# Q1. Distinguish between a unary, a binary and a ternary operator. Give examples of Java operators for each one of them.

Ans. A unary operator requires a single operand. Unary +, unary -, ++, — etc. are some unary operators in Java.

A binary operator requires two operands.

+ (add), – ( subtract), *,  /, % etc. are some binary operators in Java.

A ternary operator requires three operands. ? : (the conditional operator) is a ternary operator in Java.

# Programming Fundamentals.

Q1. Distinguish between a unary, a binary and a ternary operator. Give examples of Java operators for each one of them.

Q2. Given the following code fragment

int ch=20;

System.out.println(++ch);

System.out.println(ch);

• What output does the above fragment produce?
• What is the effect of replacing ++ch with ch+1?

Q3. What will be the rest of the following two expressions if i=10 initially?

i.) ++i<=10

ii.) i++<=10

Q4. Given the two following expressions:

a.) val=3          b.) val= =3

i.) How are these two different?

ii.) What will be the result of the if the values of val is 5 initially?

Q5. Construct an expression that is  equal to the absolute value of an variable. That is, if a variable p is positive, the value of the expression is just p, but if  p is negative, the value of the expression is -p, which would be positive. Do it in two ways:

i.) using a mathematical function

ii.) using a conditional operator.(Do not use the mathematical function here).

Q6. What output will the following code fragment produce?

int val, res, n=1000;

res = n + val > 1750 ? 400 : 200 ;

System.out.println(res);

i.) if the input is 2000

ii.) if the input is 1000

iii.) if the input is 500.

Q7. Given the following set of identifiers:

byte b;

char ch;

short sh;

int intval;

long longval;

float fl;

Identify the datatype of the following expressions:

a.)’a’-3

b.) intval * longval – ch

c.) fl + longval / sh

Q8. What is casting, when do we need it?

Q9. State the rules of operator precedence.

Q10. Suppose x1 and x2 are two double type variables that you want to add as integers and assign to an integer variable. Construct a Java statement for doing so.

Q11. What is a type, as this term relates to programming?

Q12. One of the primitive types in Java is boolean. What is the boolean type? Where are boolean values used? What are its possible values?

Q13. What is a literal?

Q14. What does the computer do when it executes a variable declaration statement? Give an example.