0% found this document useful (0 votes)
62 views

Operators and Assignments

The document contains examples of Java code snippets and questions about the output of each code. It covers topics like operators, assignments, references, methods, conditionals, loops, bitwise operations, and operator precedence. The correct answers are provided with explanations of the output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Operators and Assignments

The document contains examples of Java code snippets and questions about the output of each code. It covers topics like operators, assignments, references, methods, conditionals, loops, bitwise operations, and operator precedence. The correct answers are provided with explanations of the output.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Operators and Assignments

Finding the output


1.  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
345375
.

D
375375
.
Answer: Option B
Explanation:
Output: 15 15
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.
So Output: 3+7+5+" "3+7+5
Output: 15 15 Because Numeric values will be added

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: Option B
Explanation:
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 PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}

void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}

String fix(String s1)


{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}

A
slip stream
.

B
slipstream stream
.

C
stream slip stream
.

D
slipstream slip stream
.
Answer: Option D
Explanation:
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of
"slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of
"slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".

4.  What will be the output of the program?


class BitShift
{
public static void main(String [] args)
{
int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}

A
-2147483648 and 1
.

B
0x80000000 and 0x00000001
.

C
-2147483648 and -1
.

D 1 and -2147483648
.
Answer: Option A
Explanation:
Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:
Before: 1000 0000 0000 0000 0000 0000 0000 0000
After: 0000 0000 0000 0000 0000 0000 0000 0001
Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.
Option B is incorrect because the output method print() always displays integers in base 10.
Option D is incorrect because this is the reverse order of the two output numbers.

5.  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
Compilation fails
.

D
An exception is thrown at runtime
.
Answer: Option C
Explanation:
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.

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: Option B
Explanation:
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.  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
52
.

B
53
.

C
63
.

D
64
.
Answer: Option C
Explanation:
In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are
each incremented, and in the fifth iteration x is doubly incremented and y is incremented.

8.  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
53
.

B
82
.

C
83
.

D
85
.
Answer: Option B
Explanation:
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.

9.  What will be the output of the program?


class Bitwise
{
public static void main(String [] args)
{
int x = 11 & 9;
int y = x ^ 3;
System.out.println( y | 12 );
}
}

A
0
.
B
7
.

C
8
.

D
14
.
Answer: Option D
Explanation:
The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1;
the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.

10.  What will be the output of the program?


class SSBool
{
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}

A
ok
.

B
dokey
.

C
ok dokey
.

D
No output is produced
.

E. Compilation error
Answer: Option B
Explanation:
The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line
10 is what causes that if test to be true. Hence it prints "dokey".

11.  What will be the output of the program?


class SC2
{
public static void main(String [] args)
{
SC2 s = new SC2();
s.start();
}

void start()
{
int a = 3;
int b = 4;
System.out.print(" " + 7 + 2 + " ");
System.out.print(a + b);
System.out.print(" " + a + b + " ");
System.out.print(foo() + a + b + " ");
System.out.println(a + b + foo());
}

String foo()
{
return "foo";
}
}

A
9 7 7 foo 7 7foo
.

B
72 34 34 foo34 34foo
.
C
9 7 7 foo34 34foo
.

D
72 7 34 foo34 7foo
.
Answer: Option D
Explanation:
Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to
right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator
will add the two operands.

12.  What will be the output of the program?


class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}

void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}

void twice(int x)
{
x = x*2;
s = x;
}
}

A
77
.

B
7 14
.

C
14 0
.

D
14 14
.
Answer: Option B
Explanation:
The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The
instance variable s is updated by twice()'s x, which is 14.

13.  What will be the output of the program?


class Two
{
byte x;
}

class PassO
{
public static void main(String [] args)
{
PassO p = new PassO();
p.start();
}

void start()
{
Two t = new Two();
System.out.print(t.x + " ");
Two t2 = fix(t);
System.out.println(t.x + " " + t2.x);
}

Two fix(Two tt)


{
tt.x = 42;
return tt;
}
}

A
null null 42
.

B
0 0 42
.

C
0 42 42
.

D
000
.
Answer: Option C
Explanation:
In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in
the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.

14.  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: Option C
Explanation:
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.

15.  What will be the output of the program?


public class Test
{
public static void leftshift(int i, int j)
{
i <<= j;
}
public static void main(String args[])
{
int i = 4, j = 2;
leftshift(i, j);
System.out.printIn(i);
}
}

A
2
.

B
4
.

C
8
.

D
16
.
Answer: Option B
Explanation:
Java only ever passes arguments to a method by value (i.e. a copy of the variable) and never by reference. Therefore the value of the
variable i remains unchanged in the main method.
If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If
you had 128 right shifted by 2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting to binary to do the left and
right bit shifts.

Pointing out the correct statements

1. 

import java.awt.*;
class Ticker extends Component
{
public static void main (String [] args)
{
Ticker t = new Ticker();
/* Missing Statements ? */
}
}

which two of the following statements, inserted independently, could legally be inserted into missing section of this code?
1. boolean test = (Component instanceof t);
2. boolean test = (t instanceof Ticker);
3. boolean test = t.instanceof(Ticker);
4. boolean test = (t instanceof Component);
A
1 and 4
.

B
2 and 3
.

C
1 and 3
.

D
2 and 4
.
Answer: Option D
Explanation:
(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct
because Component is part of the hierarchy of t, because Ticker extends Component.
(1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is
incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.
2.  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: Option D
Explanation:
Statements (1), (2), (3), and (4) are correct. (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.

3.  Which two statements are equivalent?


1. 16*4
2. 16>>2
3. 16/2^2
4. 16>>>2
A
1 and 2
.

B
2 and 4
.

C
3 and 4
.

D
1 and 3
.
Answer: Option B
Explanation:
(2) is correct. 16 >> 2 = 4
(4) is correct. 16 >>> 2 = 4
(1) is wrong. 16 * 4 = 64
(3) is wrong. 16/2 ^ 2 = 10

4.  Which two statements are equivalent?


1. 3/2
2. 3<2
3. 3*4
4. 3<<2
A
1 and 2
.

B
2 and 3
.

C
3 and 4
.

D
1 and 4
.
Answer: Option C
Explanation:
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2 = false.
(3) is correct. 3 * 4 = 12.
(4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12 in binary (3*2*2).

5. 
import java.awt.Button;
class CompareReference
{
public static void main(String [] args)
{
float f = 42.0f;
float [] f1 = new float[2];
float [] f2 = new float[2];
float [] f3 = f1;
long x = 42;
f1[0] = 42.0f;
}
}

which three statements are true?


1. f1 == f2
2. f1 == f3
3. f2 == f1[1]
4. x == f1[0]
5. f == f1[0]
A
1, 2 and 3
.

B
2, 4 and 5
.

C
3, 4 and 5
.

D
1, 4 and 5
.
Answer: Option B
Explanation:
(2) is correct because the reference variables f1 and f3 refer to the same array object.
(4) is correct because it is legal to compare integer and floating-point types.
(5) is correct because it is legal to compare a variable with an array element.
(3) is incorrect because f2 is an array object and f1[1] is an array element.

6.  Which two are equal?


1. 32/4
2. (8 >> 2) << 4
3. 2^5
4. 128 >>> 2
5. 2 >> 5
A
1 and 2
.

B
2 and 4
.

C
1 and 3
.

D
2 and 3
.
Answer: Option B
Explanation:
(2) and (4) are correct. (2) and (4) both evaluate to 32. (2) is shifting bits right then left using the signed bit shifters >> and <<. (4) is shifting bits
using the unsigned operator >>>, but since the beginning number is positive the sign is maintained.
(1) evaluates to 8, (3) looks like 2 to the 5th power, but ^ is the Exclusive OR operator so (3) evaluates to 7. (5) evaluates to 0 (2 >> 5 is not 2 to the
5th).

You might also like