Rules of Operator Precedence.

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.

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

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.