Operators Flashcards Preview

JAVA OCA7 > Operators > Flashcards

Flashcards in Operators Deck (32)
Loading flashcards...
1

Are there any overloaded operators in Java?

Yes.
* The + operator can be used to add two numeric primitives together or to perform a concatenation operation if either operand is a String.
* The &, |, and ^ operators can all be used in two different ways.

2

What is the order of evaluation for this expression?

x *= 2 + 5;

x = x * (2 + 5);
Because the right side of = is always evaluated first.

3

What are the comparison relational operators in Java?

> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Comparison can also be between different types e.g. it's also legal to compare a character primitive with any number. When comparing a character with a character or a character with a number, Java will use the Unicode value of the character as the numerical value, for comparison.

4

What are the equality relational operators in Java?

== Equal
!= Not equal
You can't compare incompatible types.
There are four different types of things that can be tested:
* Numbers
* Characters
* Boolean primitives
* Object reference variables
(What is left???)

5

What is the result?

5.0 == 5L?

If a floating-point number is compared with an integer and the values are the same, the == operator usually returns true as expected

6

What is the result?

boolean b = false;
if (b = true) { System.out.println("b is true");
} else { System.out.println("b is false"); }

b is true
The result of any assignment expression is the value of the variable following the assignment.

7

What does == look for?

Remember, the == operator is looking at the bits in the variable, so for reference variables, this means that if the bits in both reference variables are identical, then both refer to the same object.

8

How does equal method in Object class works?

The equals() method in class Object works the same way that the == operator works.

9

How does equal method in String class works?

When the equals() method is used to compare two
strings, it will return true if the strings have the same value, and it will return false if the strings have different values. (??they should be really the same object in string pool - check this)

10

How does equals or == work for enums?

It compares if the two operands refer to same enum constant.

11

What is the result?
public class EnumEqual {
enum Color {RED, BLUE} // ; is optional
public static void main(String[] args) {
Color c1 = Color.RED; Color c2 = Color.RED;
if(c1 == c2) { System.out.println("== works"); }
if(c1.equals(c2)) { System.out.println("equals works"); }
}
}

== works
equals works

12

What is instanceof comparison used for?

To check whether an object is of a particular class or interface.

13

What is the result?

class A { }
class B extends A {
public static void main (String [] args) {
A myA = new B();
m2(myA);
}
public static void m2(A a) {
if (a instanceof B)
((B)a).doBstuff(); // downcasting an A reference
// to a B reference
}
public static void doBstuff() {
System.out.println("'a' refers to a B");
}
}

'a' refers to a B
(In examples like this, the use of the instanceof operator protects the program from attempting an illegal downcast.)

14

Given:
interface Foo { }
class A implements Foo { }
class B extends A { }
...
A a = new A();
B b = new B();

which are true?
a instanceof Foo
b instanceof A
b instanceof Foo

All. last one is implemented indirectly. An object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object's superclasses implement the interface.

15

What happans when null is compared using instanceof?

It is legal to test whether the null reference is an instance of a class. This will always result in false, of course. This example,
class InstanceTest {
public static void main(String [] args) {
String a = null;
boolean b = null instanceof String;
boolean c = a instanceof String;
System.out.println(b + " " + c);
}
}
prints this:
false false

16

What is the result?
class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
}
}

Compilation fails. You can't use the instanceof operator to test across two different class hierarchies.

17

What is the result?
int [] nums = new int[3];
if (nums instanceof Object) { }

Result is true. An array is always an instance of Object. Any array,

18

Given:
interface Face { }
class Bar implements Face{ }
class Foo extends Bar { }
What are the results?
null -- Any class or interface type
Foo instance -- Foo, Bar, Face, Object
Bar instance -- Bar, Face, Object tue
Bar instance -- Foo
Foo [ ] -- Foo, Bar, Face
Foo [ ] -- Object
Foo [ 1 ] -- Foo, Bar, Face, Object

null -- Any class or interface type -> false
Foo instance -- Foo, Bar, Face, Object -> true
Bar instance -- Bar, Face, Object -> true
Bar instance -- Foo -> false
Foo [ ] -- Foo, Bar, Face -> false
Foo [ ] -- Object -> true
Foo [ 1 ] -- Foo, Bar, Face, Object -> true

19

What is the result?
public class MathTest {
static int players = 0;
public static void main (String [] args) {
System.out.println("players online: " + players++);
System.out.println("The value of players is " + players);
System.out.println("The value of players is now " + ++players);
}
}

players online: 0
The value of players is 1
The value of players is now 2

20

How can we read this code and what is the result?
int x = 2; int y = 3;
if ((y == x++) | (x < ++y)) {
System.out.println("x = " + x + " y = " + y);
}

If 3 is equal to 2 OR 3 < 4
x = 3 y = 4

21

What are the logical operators in Java?

&, |, ^, !, &&, and ||

22

What are the bitwise operators in Java and how do they work?

The bitwise & operator performs a bitwise AND operation.
The bitwise ^ operator performs a bitwise exclusive OR operation.
The bitwise | operator performs a bitwise inclusive OR operation.
The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

23

What is the result?
byte b1 = 6 & 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3);

0 15 1

24

What are the bitshift operators in Java?

The signed left shift operator "<>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

25

What is the result?
byte b1 = 8;
byte b2 = -8;
System.out.println(b1>>>1);
System.out.println(b1>>1);
System.out.println(b2>>>1);
System.out.println(b2>>1);
System.out.println(b1);
System.out.println(b2);

4
4
2147483644
-4
8
-8

26

What is the result?
class Logical {
public static void main(String [] args) {
boolean b1 = false, b2 = false;
boolean b3 = (b1 == true) && (b2 = true);
System.out.println(b3 + " " + b2);
%java Logical
false false}
}

false false

27

What is the result?
if (5 && 6) { }

Compile error. !, || and && work only on boolean operands.

28

Whats is the difference between |, & vs ||, &&

|| and && are short circuit operators, the others are non-short circuit.

29

What is the result?
int z = 5;
if(++z > 5 || ++z > 6) z++;

z = 7 after this code

30

What is the result?
int z = 5;
if(++z > 5 | ++z > 6) z++;

z = 8 after this code