1)
What will be the output of the program?
class Equals
{
public static void main(String [] args)
{
int x = 100;
double y = 100.1;
boolean b = (x = y); /* Line 7 */
System.out.println(b);
}
}
A. true
B. false
C. compile-time error
D. Run-time error
Answer: C
The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code,the output would be false.
2)
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
A. true true
B. false true
C. true false
D. false false
Answer: B
The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
3)
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
A. 5 3
B. 8 2
C. 8 3
D. 8 5
Answer: B
The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented,and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations.
4)
What will be the output of the program?
class BoolArray
{ boolean [] b = new boolean[3]; int count = 0;
void set(boolean [] x, int i) { x[i] = true;++count;}
public static void main(String [] args)
{ BoolArray ba = new BoolArray();
ba.set(ba.b, 0);ba.set(ba.b, 2);ba.test();
}
void test()
{ if ( b[0] && b[1] | b[2] ) count++;
if ( b[1] && b[(++count - 2)] ) count += 7;
System.out.println("count = " + count);
}}
A. count =0
B. count =2
C. count =3
D. count =4
Answer: C
The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.
5)
What will be the output of the program?
class PassA
{
public static void main(String [] args)
{
PassA p = new PassA();
p.start();
}
void start()
{
long [] a1 = {3,4,5};
long [] a2 = fix(a1);
System.out.print(a1[0] + a1[1] + a1[2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
a3[1] = 7;
return a3;
}
}
A. 12 15
B. 15 15
C. 3 4 5 3 7 5
D. 3 7 5 3 7 5
Answer: B
The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.
6)
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
A. small
B. tiny
C. huge
D. Compilation fails
Answer: B
This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup.
7)
Which of the following are legal lines of code?
1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A. 1 and 2
B. 2 and 3
C. 3 and 4
D. All statements are correct.
Answer: D
(1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses
the digits after the decimal.(2) and (4) are correct because a long can be cast into a byte. If the long is
over 127, it loses its most significant (leftmost) bits.(3) actually works, even though a cast is not necessary because a long can store a byte.
No comments:
Post a Comment