Ch 2 - Operators and Statements Flashcards

1
Q

Name the three types of Java operators

A

unary
binary
ternary

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

All arithmetic operators may be applied to any java primitives, except which types?

A

boolean and string

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

Only which operators can be applied to String values?

A

+ and +=

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

Name the two short circuit logical operators

A

|| and &&

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

Name the logical operators and what they are used for

A

& - AND - only true if both operands are true
| - INCLUSIVE OR - only false if both operands are false
^ - EXCLUSIVE OR - only true if the operands are different

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

What is the resulting data type of the equation x * y where:
int x = 1;
long y = 33;

A

since one of the values is long and the other is int, the int value is promoted to a long.

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

What is the data type of x + y, where
double x = 39.21;
float y = 2.1;

A

This is a trick questions because y is not properly formatted. Floating point literals are assumed to be double, unless postfixed with an f, as in 2.1f.

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

What is the data type of x / y, where
short x = 10;
short y = 3;

A

x and y will both be promoted to int before the operation, resulting in an output type of int.

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

What is the data type of x * y / Z, where
short x = 14;
float y = 13;
double z = 30;

A

x will automatically be promoted to int solely because it is a short and is being used in an arithmetic binary operation. The promoted x value will then be automatically promoted to a float so that it can be divided with y. The result of x * y will then be automatically promoted to a double, so that it can be multiplied with z, resulting in a double value.

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

Name the four numeric promotion rules:

A
  1. if two values have different data types, java will automatically promote one of the values to the larger of the two data types.
  2. If one of the values is integral and the other is floating-point, java will automatically promote the integral value to the floating-point value’s data type.
  3. Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a java binary arithmetic operator, even if neither of the operands is int.
  4. After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Given the following code, provide the values for x and y:
int x = 3;
int y = ++x * 5 / x-- + --x;
System.out.println("x = " + x);
System.out.println("y = " + y);
A
x = 2
y = 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
How do you fix the following lines of code:
int x = 1.0;
short y = 1921222;
int z = 9f;
long t = 192301398193810323;
A

int x = (int) 1.0;
short y = (short)1921222; // Stored as 20678
int z = (int)9f;
long t = 192301398193810323L;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Given the following code, provide the values for x and y:
long x = 5;
long y = (x=3);
System.out.println(x);
System.out.println(y);
A
x = 3
y = 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What is the output of the following code:
boolean b = false;
if (b = true) {
	System.out.println("its true");
} else {
	System.out.println("its not true");
}
A

its true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What is the output of the following code:
boolean b = true;
if (b = false) {
	System.out.println("its true");
} else {
	System.out.println("its not true");
}
A

its not true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is the output of the following code:
int x1 = 1;
if (x1) {
	System.out.println("its true");
} else {
	System.out.println("its not true");
}
A

Compiler error on this line:

if (x1) {

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
What is the output of the following code:
int x1 = 1;
if (x1 = 5) {
	System.out.println("its true");
} else {
	System.out.println("its not true");
}
A

Compiler error on this line:

if (x1 = 5) {

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

The short-circuit operators are && and ||. They are nearly identical to the & and | logical operators except…

A

the right hand side of the expression may never be evaluated if the final result can be determined by the left hand side of the expression.

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

What is the output of the following code?
int x = 6;
boolean y = (x >= 6) || (++x <= 7);
System.out.println(x);

A

Because x >= 6 is true, the increment operator on the right-hand side of the expression is never evaluated, so the output is 6.

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

true/false: the following code compiles:

boolean x = true == 3;

A

false

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

true/false: the following code compiles:

boolean y = false != “Giraffe”;

A

false

22
Q

true/false: the following code compiles:

boolean z = 3 == “Kangaroo”;

A

false

23
Q

What does the following code output:
boolean y = false;
boolean x = (y = true);
System.out.println(x);

A

true

24
Q
What does the following code output:
File x = new File("myFile.txt");
File y = new File("myFile.txt");
File z = x;
System.out.println(x == y);
System.out.println(x == z);
A

false

true

25
Q

Describe a ternary operator

A

booleanExpression ? expression1 : expression2;

26
Q

What is the value of x?
int y = 10;
int x = (y > 5) ? (2 * y) : (3 * y);

A

20

27
Q

true/false, the following code compiles:

System.out.println((y > 5) ? 21 : “Zebra”);

A

true

28
Q

true/false, the following code compiles:

int animal = (y < 91) ? 9 : “Horse”;

A

false

29
Q
What does the following code output:
int y = 1;
int z = 1;
final int x = y<10 ? y++ : z++;
System.out.println(y + "," + z);
A

2,1

30
Q

What are the data types supported by switch statements?

A
  • byte and Byte
  • short and Short
  • int and Integer
  • char and Character
  • String
  • enum values
31
Q

What two primitive types are not supported when used by the switch statement?

A

boolean and long (and their wrapper classes)

32
Q
What does the following code output?
int dayOfWeek = 5;
switch(dayOfWeek) {
	default: 
		System.out.println("Weekday");
		break;
	case 0:
		System.out.println("Sunday");
		break;
	case 6:
		System.out.println("Saturday");
		break;
}
A

Weekday

33
Q
What does the following code output?
int dayOfWeek = 5;
switch(dayOfWeek) {
	case 0:
		System.out.println("Sunday");
	default: 
		System.out.println("Weekday");
	case 6:
		System.out.println("Saturday");
		break;
}
A

Weekday

Saturday

34
Q

The case statement from a switch statement must have what attributes?

A

must be a literal, enum constant, or final constant variable.

35
Q
In the following switch/case statement, which lines do not compile?
private int getSortOrder(String firstName, final String lastName) {
	String middleName = "Patricia";
	final String suffix = "JR";
	int id = 0;
	switch(firstName) {
		case "Test":
			return 52;
		case middleName:			
id = 5;
			break;
		case suffix:
			id = 0;
			break;
		case lastName:
			id = 8;
			break;
		case 5:
			id = 7;
			break;
		case 'J':
			id = 10;
			break;
		case java.time.DayOfWeek.SUNDAY:
			id = 15;
			break;
	}
	return id;
}
A
private int getSortOrder(String firstName, final String lastName) {
	String middleName = "Patricia";
	final String suffix = "JR";
	int id = 0;
	switch(firstName) {
		case "Test":
			return 52;
		case middleName:		// DOES NOT COMPILE. not a final variable
			id = 5;
			break;
		case suffix:
			id = 0;
			break;
		case lastName:			// DOES NOT COMPILE. although it is final, it is not constant because it is passed into the method.
			id = 8;
			break;
		case 5:					// DOES NOT COMPILE. Not a string
			id = 7;
			break;
		case 'J':				// DOES NOT COMPILE. Not a string
			id = 10;
			break;
		case java.time.DayOfWeek.SUNDAY:	// DOES NOT COMPILE. not a string.
			id = 15;
			break;
	}
	return id;
}
36
Q

true/false, A while loop may terminate after its first evaulation of the boolean expression

A

true

37
Q

true/false, a do-while loop guarantees that the statement or block will be executed at least once

A

true

38
Q

What would the following code result in:
for ( ; ; ) {
System.out.println(“Hello World”);
}

A
continuous loop outputting:
Hello World
Hello World
Hello World
Hello World
Hello World
...
39
Q

true/false, each of the components of the for loop are optional. Example:
for (int i = 0; i<10; i++) {
System.out.println(i);
}

A

true

40
Q

What would the output of the following code be:
int x = 0;
for (long y = 0, z = 4; x < 5 && y < 10; x++, y++) {
System.out.println(y + “”);
}

A

0 1 2 3 4

41
Q

What would the output of the following code be:
int x = 0;
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.println(x + “”);
}

A

Compiler error in the for line:
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {

You cannot redeclare variable in the initialization block.

42
Q

What would the output of the following code be:
for (long y = 0, int x = 4; x < 5 && y < 10; x++, y++) {
System.out.println(x + “, “ + y);
}

A

Compiler error:
for (long y = 0, int x = 4; x < 5 && y < 10; x++, y++) {

Can’t have multiple data types in the initialization block.

43
Q

What would the output of the following code be:
for (long y = 0, x = 4; x < 5 && y < 10; x++, y++) {
System.out.println(y + “”);
}
System.out.println(x);

A

Compiler error:
System.out.println(x);

x is out of scope here.

44
Q
What would the output of the following code be:
String[] names = new String[3];
for (int name : names) {
	System.out.print(name + " ");
}
A

Compiler error:
for (int name : names) {

Data types don’t match.

45
Q

Which operators can have optional labels?

A

if-then, switch, and loops

46
Q

What does a break statement do?

A

A break statement transfers the flow of control out to the enclosing statement.

47
Q

What does a continue statement do?

A

The continue statement transfers control to the boolean expression that determines if the loop should continue. In other words, it ends the current iteration of the loop.

48
Q
What does the following code output:
	public static void main(String[] args) {
		FIRST_CHAR_LOOP: for (int a = 1; a <= 4; a++) {
			for (char x = 'a'; x <= 'c'; x++) {
				if (a==2 || x == 'b') 
					continue FIRST_CHAR_LOOP;
				System.out.print(" " + a + x);
			}
		}
	}
A

1a 3a 4a

49
Q
What does the following code output:
	public static void main(String[] args) {
		for (int a = 1; a <= 4; a++) {
			for (char x = 'a'; x <= 'c'; x++) {
				if (a==2 || x == 'b') 
					continue;
				System.out.print(" " + a + x);
			}
		}
	}
A

1a 1c 3a 3c 4a 4c

50
Q
What does the following code output:
	public static void main(String[] args) {
		for (int a = 1; a <= 4; a++) {
			for (char x = 'a'; x <= 'c'; x++) {
				System.out.print(" " + a + x);
			}
		}
	}
A

1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c

51
Q

What does the following code output:
public static void main(String[] args) {

int count = 0;
ROW_LOOP: for(int row = 1; row <= 3; row++) {
	for(int col=1; col<=2; col++) {
		if (row * col % 2 == 0) continue ROW_LOOP;
		count++;
	}

}
System.out.println(count++); }
A

2

Remember that a continue statement cannot be used with an if/then, unless the if/then is included in a loop.