Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Escape Sequence

What are escape sequences in Java? Give three examples.

ANSWER

An escape sequence is a set of characters that has a special meaning to


the Java compiler. In the escape sequence, a character is preceded by a
backslash (\).

Some examples of escape sequences are \n, \' and \t.

Short hand in java

What are short hand operators? Explain with an example.


Ans. Java provides special operators that can be used to combine an
arithmetic operation with an assignment. For example, the expression,
a = a+4;
can be written as,
a += 4;
As this operator is a shorter way of expressing an expression it is also
sometimes called Short
hand operators

What are precedence and associativity?

Answer

Precedence of operators refers to the order in which the operators are applied to
the operands in an expression.
Associativity of operators refers to the direction of execution of operators ("Left
to Right" or "Right to Left") when operators in an expression have the same
precedence.
Home Work

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

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


a = −−b + c++ + b;
System.out.println(“a =” + a);
2. if m = 5 and n = 2 output the values of m and n after execution in (i)
and (ii):-
i. m − = n;
ii. n = m + m/n;
3. What will be the output of the following, if x = 5 initially?
i. 5 *++x
ii. 5*x++
4. Evaluate the following expressions,
if the values of the variables are a = 2, b=3 and c=9
i. a − (b++) * (− −c)
ii.a * (++b) % c
5. If a = 5, b = 9 calculate the value of:
a+ = a++ - ++b + a
6. What is the result stored in x, after evaluating the following
expression?
int x = 5;
x = x++ *2 + 3 * –x;
7. State the value a, b and c after the execution of each of the following
statements where a=12, b=13 and c=11:
i. a = a++ + – –b + c++;
ii. b= b++ + ++a * 2;
iii. c= c++ + a++ * (++b);
iv. b= b++ + ++b + c++ + ++a;
v. b+= --c + ++c + a++ + (b++);
Find out the output
class Output1
{
static void main()
{
int a=5,b=6,c;
c=a+b%2;
b=a+2*c;
System.out.println(c);
System.out.println(b);
}
}
class Output2
{
static void main()
{
int a=12,b=13,c=6;
c+=a+b%2;
b-=a+2*c;
System.out.println(c);
System.out.println(b);
}
}
class Output3
{
static void main()
{
int a=12,b=13,c=0,d=1;
c+=a;
c+=b;
d*=a;
d*=b;
System.out.println(c);
System.out.println(d);
}
}
class Output4
{
static void main()
{
int a=12,b=13,c;
c=a++ + ++a/7 + b++;
System.out.println(a+“\t”+b+“\t”+c);
c=++a + a++/3 + ++b;
System.out.println(a+“\t”+b+“\t”+c);
}
}

You might also like