Q1.
Evaluate the following java expressions –
(a) int a, mb = 2, k = 4; (b) int x, y = 6; (c) int x = 2, y = 9;
A = mb * 3 / 4 + k / 4 + 8 – mb + 5 / 8; x = ++y + 2y; x += y -= -4;
(d) int x = 8, y = 6, z = 4;
(i) x>y (ii) ( y + z ) >= (x / z) (iii) (x + y) > z && (z + y) > x
(iv) (y > x) || (z < y) (v) x || y && z (vi) x
(vii) -y (viii) x <= !y && z (ix) (x > y) && (!y < z)
(e) int j = 5;
(i) (5 * ++j) % 6
(ii) (5 * j++) % 6
Q2. Write the corresponding Java expressions for the following mathematical expressions –
(a) a2 + b2 + c2 (b) 2 – ye2y + 4y (c) p + q / (r + s)4 (d) | ex – x |
Q3. Predict the Output –
(a) What is the value of the variable ‘n’ after the code in these questions –
Assume : int i = 3, j = 7, n = 0;
(i) if (j > 4) (iv) if ( i > 2) {
n = 2; n = 1;
(ii) if (j > 4) { if (j > 4)
n = 2; n = 2;
} else { else
n = 3; n = 3;
} } else {
(iii) if ( 3 > 4 || 5 > 4) { n = 4;
n = 1; if (j % 2 >= 2)
} n = 5;
else { else
n = 2; n = 6; }
} System.out.println(n);
(b) What will be the output of following code fragment if the value of ‘ch’ is –
(i) a (ii) c (iii) d (iv) h (v) b
switch (ch)
{ case ‘a’ : System.out.println(“It is a”);
case ‘b’ : System.out.println(“It is b”);
case ‘c’ : System.out.println(“It is c”);
break;
case ‘d’ : System.out.println(“It is d”);
break;
default : System.out.println(“Not a, b, c, d”); }
(c) int m = 100;
(i) while (m > 0) { (ii) while (true) {
if (m < 10) if (m < 10)
break; continue;
m = m – 10; } System.out.println(“m is” + m); m = m – 10; } System.out.println(“m is” + m);
(d) (i) int s = 14; (ii) int s = 14
if ( s < 20) if ( s < 20)
System.out.println(“under”); System.out.println(“under”);
else else {
System.out.println(“over”); System.out.println(“over”);
System.out.println(“the limit”); System.out.println(“the limit”); }
(iii) int s = 94;
if ( s < 20) {System.out.println(“under”); }
else {System.out.println(“over”);}
System.out.println(“the limit”);
(e) int a = 10, b = 5;
if ( a > b) { if (b > 5) System.out.println(“b is “ + b);}
else System.out.println(“a is “ + a);
Q4. Find the Errors –
(a) int x=0, n1, n2, n3, n4; (b) int i = 5, j = 10;
switch (x) if (i < j) || ( i = 10)
{ case 1 : n1 = 10; System.out.println(“OK”);
n2 = 20; System.out.println(“Not OK”);
case 2 : n3 = 30;
break;
n4 = 40;
Q5. Rewrite the following fragment using ‘Switch-case’ statement –
(a) if (a == 0) (b) if (ch == ‘E’)
System.out.println(“Zero”); System.out.println(eastern++);
if (a == 1) if (ch == ‘W’)
System.out.println(“One”); System.out.println(western++);
if (a == 2) if (ch == ‘N’)
System.out.println(“Two”); System.out.println(northern++);
if (a == 3) if (ch == ‘S’)
System.out.println(“Three”); System.out.println(southern++);
else else
System.out.println(“Unknown”); System.out.println(unknown++);
(c) if (code == ‘A’) (d) if (num == 5) {
System.out.println(“Accountant”); val = num * 25 – 20;
else if (code == ‘C’ || code == ‘G’) System.out.println(num + val); }
System.out.println(“Grade IV”); else if (num == 10) {
else if (code == ‘F’) val = num * 25 – 20;
System.out.println(“Financial Advisor”); System.out.println(num - val); }