Operators Flashcards

1
Q

Are there any overloaded operators in Java?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the order of evaluation for this expression?

x *= 2 + 5;

A

x = x * (2 + 5);

Because the right side of = is always evaluated first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the comparison relational operators in Java?

A
> 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the equality relational operators in Java?

A
== 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???)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result?

5.0 == 5L?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the result?

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

A

b is true

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does == look for?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does equal method in Object class works?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does equal method in String class works?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does equals or == work for enums?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
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"); }
    }
}
A

== works

equals works

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is instanceof comparison used for?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

‘a’ refers to a B

In examples like this, the use of the instanceof operator protects the program from attempting an illegal downcast.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happans when null is compared using instanceof?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is the result?
class Cat { }
class Dog {
    public static void main(String [] args) {
        Dog d = new Dog();
        System.out.println(d instanceof Cat);
    }
}
A

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

17
Q

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

A

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

18
Q
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
A
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
Q
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);
  }
}
A

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

20
Q

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

A

If 3 is equal to 2 OR 3 < 4

x = 3 y = 4

21
Q

What are the logical operators in Java?

A

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

22
Q

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

A

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
Q
What is the result?
byte b1 = 6 &amp; 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3);
A

0 15 1

24
Q

What are the bitshift operators in Java?

A

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
Q
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);
A
4
4
2147483644
-4
8
-8
26
Q
What is the result?
class Logical {
    public static void main(String [] args) {
        boolean b1 = false, b2 = false;
        boolean b3 = (b1 == true) &amp;&amp; (b2 = true); 
    System.out.println(b3 + " " + b2);
    %java Logical
false false}
}
A

false false

27
Q

What is the result?

if (5 && 6) { }

A

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

28
Q

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

A

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

29
Q

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

A

z = 7 after this code

30
Q

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

A

z = 8 after this code

31
Q

What is the result?
boolean t = true;
boolean f = false;
System.out.println(t ^ f);

A

true

32
Q

What is the name of this operator?

x = (boolean expression) ? value to assign if true : value to assign if false

A

The conditional operator (a.k.a. the “ternary operator”)