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

Chapter 3: Operators and Assignments Java Operators

This document discusses Java operators and assignments. It provides examples of different types of operators like arithmetic, bitwise, logical, assignment and relational operators. It also discusses operator precedence and examples of passing variables into methods. The document contains 12 multiple choice questions with explanations of the answers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Chapter 3: Operators and Assignments Java Operators

This document discusses Java operators and assignments. It provides examples of different types of operators like arithmetic, bitwise, logical, assignment and relational operators. It also discusses operator precedence and examples of passing variables into methods. The document contains 12 multiple choice questions with explanations of the answers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CHAPTER 3: OPERATORS AND ASSIGNMENTS

Java Operators

1. Which two are equal? (Choose two.)

A. 32/ 4;
B. (8 >> 2) << 4;
C. 2^ 5;
D. 128 >>> 2;
E. (2 << 1) * (32 >> 3);
F. 2 >> 5;

ANSWER: B and D

B and D both evaluate to 32. B is shifting bits right then left using the signed bit shifters >> and
<<. D is shifting bits using the unsigned operator >>>, but since the beginning number is positive
the sign is maintained.

A evaluates to 8, C looks like 2 to the 5th power, but ^ is the Exclusive OR operator so C evaluates
to 7. E evaluates to 16, and F evaluates to 0 (2 >> 5 is not 2 to the 5th).

2. Given the following,

1. import java.awt.*;
2. class Ticker extends Component {
3. public static void main (String [] args) {
4. Ticker t = new Ticker();
5.
6. }
7. }

Which two of the following statements, inserted independently, could legally be inserted into
line 5 of this code? (Choose two.)

A. boolean test = (Component instanceof t);


B. boolean test = (t instanceof Ticker);
C. boolean test = t.instanceof(Ticker);
D. boolean test = (t instanceof Component);
E. boolean test = t.instanceof(Object);
F. boolean test = (t instanceof String);

ANSWER: B and D

B is correct because class type Ticker is part of the class hierarchy of t; therefore, it is a legal
use of the instanceof operator. D is also correct because Component is part of the hierarchy of
t, because Ticker extends Component in line 2.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 19


A is incorrect because the syntax is wrong. A variable (or null) always appears before the
instanceof operator, and a type appears after it. C and E are incorrect because the statement is
used as a method, which is illegal. F is incorrect because the String class is not in the hierarchy
of the t object.

3. Given the following,

class Equals {
public static void main(String[] args) {
int x = 100;
double y = 100.1;
boolean b = (x = y);
System.out.println(b);
}
}

What is the result?

A. true
B. false
C. Compilation fails
D. An exception is thrown at runtime

ANSWER: C

The code will not compile because in line 5, 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.

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.

4. Given the following,

1. import java.awt.Button;
2. class CompareReference {
3. public static void main(String [] args) {
4. float f = 42.0f;
5. float [] f1 = new float[2];
6. float [] f2 = new float[2];
7. float [] f3 = f1;
8. long x = 42;
9. f1[0] = 42.0f;
10. }
11. }

Which three statements are true? (Choose three.)

20 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


A. f1 == f2
B. f1 == f3
C. f2 == f1[1]
D. x == f1[0]
E. f == f1[0]

ANSWER: B, D, and E

B is correct because the reference variables f1 and f3 refer to the same array object. D is correct
because it is legal to compare integer and floating-point types. E is correct because it is legal to
compare a variable with an array element.

C is incorrect because f2 is an array object and f1[1] is an array element.

5. Given the following,

class BitShift {
public static void main(String[] args) {

int x = 0x80000000;
System.out.print(x + " and ");
x = x >>> 31;
System.out.println(x);
}
}

What is the output from this program?

A. -2147483648 and 1
B. 0x80000000 and 0x00000001
C. -2147483648 and -1
D. 1 and -2147483648
E. None of the above

ANSWER: A

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

C is incorrect because the >>> operator zero fills the left bits, which in this case changes the
sign of x, as shown. B is incorrect because the output method print() always displays integers
in base 10. D is incorrect because this is the reverse order of the two output numbers. E is
incorrect because there was a correct answer.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 21


6. Given the following,

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

What is the result?

A. 0
B. 7
C. 8
D. 14
E. 15

ANSWER: D

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.

A, B, C, and E, are incorrect based on the program logic described above.

7. Which of the following are legal lines of code? (Choose all that apply.)

A. int w = (int)888.8;
B. byte x = (byte)1000L;
C. long y = (byte)100;
D. byte z = (byte)100L;

ANSWER: A, B, C, and D

A 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. B and D are correct because a long can be cast into a
byte. If the long is over 127, it loses its most significant (leftmost) bits. C actually works, even
though a cast is not necessary, because a long can store a byte.

There are no incorrect answer choices.

22 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


Logical Operators

8. Given the following,

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);
}
}

What is the result?

A. 5 3
B. 8 2
C. 8 3
D. 8 5
E. 10 3
F. 10 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.

A, C, D, E, and F are incorrect based on the program logic described above.

9. Given the following,

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);
}
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 23


What is the result?

A. 5 2
B. 5 3
C. 6 3
D. 6 4
E. 7 5
F. 8 5

ANSWER: C

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.

A, B, D, E, and F are incorrect based on the program logic described above.

10. Given the following,

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

What is the result?

A. ok
B. dokey
C. ok dokey
D. No output is produced
E. Compilation error
F. An exception is thrown at runtime

ANSWER: B

The & operator has a higher precedence than the | operator so that on line 6 b1 and b2 are
evaluated together as are b2 & b3. The final b1 in line 8 is what causes that if test to be true.

A, C, and D are incorrect based on the program logic described above.

24 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


11. Given the following,

class Test {
public static void main(String[] args) {
int x = 20;
String sup = (x < 15) ? "small" : (x < 22) ? "tiny" : "huge";
System.out.println(sup);
}
}

What is the result of compiling and running this code?

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.

A, C, and D are incorrect based on the program logic described above.

12. Given the following,

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);
}
}

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 25


What is the result?

A. count = 0
B. count = 2
C. count = 3
D. count = 4
E. count = 10
F. count = 11

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.

A, B, D, E, and F are incorrect based on the program logic described above.

Passing Variables into Methods

13. Given the following,

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;
}
}

What is the result?

A. 77
B. 7 14
C. 14 0
D. 14 14
E. Compilation fails
F. An exception is thrown at runtime

26 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


ANSWER: B

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.

A, C, and D are incorrect based on the program logic described above.

14. Given the following,

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;
}
}

What is the result?

A. true true
B. false true
C. true false
D. false false
E. Compilation fails
F. An exception is thrown at runtime

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.

A, C, D, E, and F are incorrect based on the program logic described above.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 27


15. Given the following,

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";
}
}

What is the result?

A. slip stream
B. slipstream stream
C. stream slip stream
D. slipstream slip stream
E. Compilation fails
F. An exception is thrown at runtime

ANSWER: D

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”.

A, B, C, and E are incorrect based on the program logic described above.

28 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


16. Given the following,

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";
}
}

What is the result?

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
E. 9 34 34 foo34 34foo

ANSWER: D

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.

A, B, C, and E are incorrect based on the program logic described above.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 29


17. Given the following,

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;
}
}

What is the result?

A. 12 15
B. 15 15
C. 345375
D. 375375
E. Compilation fails
F. An exception is thrown at runtime

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.

A, C, D, E, and F are incorrect based on the program logic described above.

30 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College


18. Given the following,

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;
}
}

What is the result?

A. null null 42
B. 00 42
C. 0 42 42
D. 000
E. Compilation fails
F. An exception is thrown at runtime

ANSWER: C

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.

A, B, D, E, and F are incorrect based on the program logic described above.

B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 31


32 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College

You might also like