Chapter 2: Operator and assignments

Tuesday, March 31, 2009

Chapter 2:
Operator and assignments
2.1) Which statements about the output of the following program are true? [1]
public class Logic {
public static void main(String args[]) {
int i = 0;
int j = 0;

boolean t = true;
boolean r;

r = (t && 0<(i+=1));
r = (t && 0<(i+=2));
r = (t && 0<(j+=1));
r = (t 0<(j+=2));

System.out.println( i + “ ” + j );
}
}
(a) The first digit printed is 1.
(b) The first digit printed is 2.
(c) The first digit printed is 3.
(d) The second digit printed is 1.
(e) The second digit printed is 2.
(f) The second digit printed is 3.

2.2) Which statements about the output of the following program are true? [6]
public static void main(String args[])
{
int i =0;
i = i++;
System.out.println(i);
}
(a) 0 is printed.
(b) 1 is printed.

2.3) Which statements about the output of the following program are true? [1]
public class EqualTest {
public static void main(String args[]) {
String s1 = “YES”;
String s2 = “YES”;
if ( s1 == s2 ) System.out.println(“equal”);
String s3 = new String(“YES”);
String s4 = new String(“YES”);
if ( s3 == s4 ) System.out.println(“s3 eq s4”);
}
}
(a) “equal” is printed, “s3 eq s4” is printed.
(b) “equal” is printed only.
(c) “s3 eq s4” is printed only.
(d) Nothing is printed.

2.4) What happens when you try to compile and run the following code?
public class EqualsTest {
public static void main(String args[]) {
char A = ‘\u0005’;
if ( A == 0x0005L )
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}
(a) The compiler reports “Invalid character in input” in line 3
(b) The program compiles and prints “Not equal”.
(c) The program compiles and prints “Equal”.

2.5) What will happen when you attempt to compile and run the following code? [7]
public class As{
int i = 10;
int j;
char z= 1;
boolean b;

public static void main(String argv[]){
As a = new As();
a.amethod();
}

public void amethod(){
System.out.println(j);
System.out.println(b);
}
}
(a) Compilation succeeds and at run time an output of 0 and false.
(b) Compilation succeeds and at run time an output of 0 and true.
(c) Compile time error b is not initialised.
(d) Compile time error z must be assigned a char value.

2.6) Given the following code what will be the output? [7]
class ValHold {
public int i = 10;
}

public class ObParm {
public static void main(String argv[]) {
ObParm o = new ObParm();
o.amethod();
}

public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
} //End of amethod

public void another(ValHold v, int i){
i = 0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
(a) 10,0,30
(b) 20,0,30
(c) 20,99,30
(d) 10,0,20

2.7) Here are three proposed alternatives to be used in a method to return false if the object reference x has the null value. Which statement will work? [3]
(a) if ( x == null ) return false;
(b) if ( x.equals(null) ) return false;
(c) if ( x instanceof null ) return false;

2.8) .What will be the result of compiling and running the given program? [8]
Select one correct answer.
1 class Q1
2 {
3 public static void main(String arg[])
4 {
5 int a[]={2,2};
6 int b=1;
7 a[b]=b=0;
8 System.out.println(a[0]);
9 System.out.println(a[1]);
10 }
11 }

(a) Compile time error at the line no. 5.
(b) Run time error at the line no. 5.
(c) Program compiles correctly and print 2,0 when executed.
(d) Program compiles correctly and print 0,2 when executed.

2.9) What will be the result of compiling and running the given program? [8]
Select one correct answer.

1 public class Q4
2 {
3 public static void main(String[] args)
4 {
5 boolean t1 = true, t2 = false, t3 = t1;
6 boolean t = false;
7 t &&= (t1 ( t2 && t3));
8 System.out.println(t);
9 }
10 }
(a) Program compiles correctly and print true when executed.
(b) Program compiles correctly and print false when executed.
(c) Compile time error.
(d) Run time error.

2.10) What will be the result of compiling and running the given program? [8]
Select one correct answer.
1 class AA{}
2 class BB extends AA{}
3 class Q6
4 {
5 public static void main(String arg[])
6 {
7 AA a=null;
8 BB b=(BB)a;
9 System.out.println(b);
10 System.out.println(b instanceof BB);
11 System.out.println(b instanceof AA);
12 }
13 }
(a) Program compiles correctly and print null,true,false.
(b) Program compiles correctly and print null,true,true.
(c) Program compiles correctly and print null,false,false.
(d) Program compiles correctly and print null,false,true.
(e) Compile time error at line no.8 as null value cannot be casted.
(f) Run time error at line no. 8 as null value cannot be casted.

2.11) Which one of the following are valid character contants? [8]
Select any two.
(a) char c = '\u000d' ;
(b) char c = '\u000a';
(c) char c = '\u0000';
(d) char c = '\uface' ;










Answers:
2.1) (c), (d)
Unlike & and operators, the && and operators short circuit the evaluation of their operands if the result of the operation can be determined just based on the value of the first operand. The second operand of the operation in the program is never evaluated. Variable i ends up with the value 3 which is the first digit printed, and j ends up with a value of 1 which is the second digit printed.

2.2) (a)
To explain u in a more simple way...

int k = 10;
int j;

Post increment :
j = k++;

In this statement the value of k is 10, & there is a ++ operator also
attached with it, so it will be assigned to j, so now j gets 10, but
k becomes as 11 because the ++ expression comes after the variable,
or if u see k as k++.

Pre-increment :
j = ++k;
In this case, k is having 10 as value, then the pre-increment
operation is being performed, so k becomes 11 in this case, & then it
is being assigned to j, so now j becomes 11 & k also becomes 11.

The thing which u have must have noticed in the post increment & pre-
increment is that the value of k is increased by 1, where as j in the
first case becomes 10 & in second case j becomes 11.

2.3) (b)
The compiler creates one String object for both s1 and s2, thus “equal” appears. But using the new String operator two distinct objects are created so “s3 eq s4” does not appear.

2.4) (c)
The compiler promotes variable A to a long before the comparison. The compiler does not report “Invalid character in input” in line 3 because this is correct form for initializing a char primitive. Therefore, answer (a) is incorrect. Because (c) is correct  (b) cannot possibly be correct.

2.5) (a)
The default value for a boolean declared at class level is false, and integer is 0, and 1 is within the range of the char and don’t need casting.

2.6) (d)
In the call another(v,i);
A COPY of the reference to v is passed and thus any changes will be intact after this call.

2.7) (a)
The ONLY correct way to check a reference for the null value. Answer (b) is incorrect because if x is null, there will not be an object whose equals method can be called. This statement would cause a NullPointerException at runtime when x is null. Answer (c) is incorrect because only a reference type, such as a class, or array, can be the right operand of the instanceof operator.

2.8) (c)
First of all value of b which is 1 is assigned to array index then rest of the code will be executed.

2.9) (c)
Compile time error: There is no operator like &&=

2.10) (c)
As null value can be casted but do remember it is of no use to cast null value that is why it is written in Khalid Mughal's book that we can not cast null value.Secondly,instanceof operator always return false for null value.

2.11) (c), (d)
'\u000d' and '\u000a' are not valid as these value do line break. Note:'\u000a' is not valid even when used behind single line comments (i.e. //.....),but offcourse for multiline comment it is valid. '\uface' is valid because we can use any character from range 0 - 9 or A - F or a - f.

0 comments:

Post a Comment