Chapter 2 Operators And Statements Flashcards

Using operators and decision constructs Using loop Constructs

1
Q

A Java operator is

A

a special symbol that can be applied to a set of variables, values or
literals—referred to as operands—and that returns a result.

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

Name the three flavors of operators in Java

A

unary
binary
ternary

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

Java operators follow

A

order of operation, by decreasing order of operator precedence

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

Java guarantees left-to-right evaluation if

A

two operators have the same level of precedence

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

What type of operators: expression++, expression–

A

Post-unary operators

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

What type of operators: ++expression, –expression

A

Pre-unary operators

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

What type of operators: +, -, !

A

Other unary operators

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

What type of operators: *, /, %

A

Multiplication/Division/Modulus

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

What type of operators: +, -

A

Addition/Subtraction

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

What type of operators: <>,&raquo_space;>

A

Shift operators

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

What type of operators: , <=, >=, instanceof

A

Relational operators

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

What type of operators: ==, !=

A

Equal to/not equal to

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

What type of operators: &, ^, |

A

Logical operators

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

What type of operators: &&, ||

A

Short-circuit logical operators

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

What type of operators: boolean expression ? expression1 : expression2

A

Ternary operators

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

What type of operators: =, +=, -=, *=, /=, %=, &=, ^=, !=, «=,&raquo_space;=,&raquo_space;>=

A

Assignment operators

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

binary operators can be used to

A

perform mathematical operations on variables,

create logical expressions, as well as perform basic variable assignments.

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

Can you use modulus operations on negative integers and floating point integers?

A

Yes

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

Numeric promotion

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
20
Q

What is the data type of x * y?
int x = 1;
long y = 33;

A

If we follow the fi rst rule, since one of the values is long and the other is int, and long is larger than int, then the int value is promoted to a long, and the resulting value is long.

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

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

A

This is actually a trick question, as this code will not compile! Floating-point literals are assumed to be double, unless postfixed with an f, as in 2.1f. If the value was set properly to 2.1f, then the promotion would be
similar to the last example, with both operands being promoted to a double, and the result would be a double value.

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

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

A

In this case, we must apply the third rule, namely that x and y will both be promoted
to int before the operation, resulting in an output of type int. Pay close attention to
the fact that the resulting output is not a short

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

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

A

First, x will automatically be promoted to
int solely because it is a short and it 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 multiplied 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
24
Q

By definition, a unary operator is one that requires

A

exactly one operand, or variable, to function.

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

[Blank} Unary operator
Indicates a number is positive, although numbers are assumed to be positive in Java unless accompanied by a negative unary operator

A

+

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

[Blank] Unary operator

Indicates a literal number is negative or negates an expression

A

-

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

[Blank] Unary operator

Increments a value by 1

A

++

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

[Blank] Unary operator

Decrements a value by 1

A

29
Q

[Blank] Unary operator

Inverts a Boolean’s logical value

A

!

30
Q

The [Blank] flips the value of a boolean expression.

A

logical complement operator, !,

31
Q

Likewise, [Blank] reverses the sign of a numeric expression

A

the negation operator, -

32
Q

An assignment operator

A

is a binary operator that modifies, or assigns, the variable on the left-hand side of the operator, with the result of the value on the right-hand side of the equation.

33
Q

[Blank] is when a number is so large that it will no longer fit within the data type, so the system “wraps around” to the next lowest value and counts up from there.

A

Overflow

34
Q

[Blank] when the number is too low to fit in the data type.

A

Underflow

35
Q

System.out.print(2147483647+1);

A

-2147483648 (Overflow)

36
Q

[Blank] compare two expressions and return a

boolean value.

A

Relational Operators

37
Q

Name of relational operator:

A

Strictly Less Than

38
Q

Name of relational operator: <=

A

Less than or equal to

39
Q

Name of relational operator: >

A

Strictly Greater Than

40
Q

Name of relational operator: >=

A

Greater Than or Equal To

41
Q

Logical operators: & AND is only true if

A

both operands are true

42
Q

Logical operators: | Inclusive OR is only false if

A

both operands are false

43
Q

Logical operators: ^ Exclusive OR is only true if

A

the operands are different

44
Q

[Blank] is a complex decision-making structure

in which a single value is evaluated and flow is redirected to the first matching branch, known as a case statement.

A

A switch statement

45
Q

Data types supported by the switch statement are:

A
■ int and Integer
■ byte and Byte
■ short and Short
■ char and Character
■ int and Integer
■ String
■ enum values
46
Q
What is the 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

The code will jump to the default block and then execute all of the proceeding case statements in order until it finds a break statement or finishes the structure:
Weekday
Saturday

47
Q

A [Blank] transfers the flow of control out to the enclosing statement.

A

break statement

48
Q

A [Blank] causes fl ow to finish the execution of the current loop.

A

continue statement

49
Q
Which of the following Java operators can be used with boolean variables? (Choose all that
apply)
A. ==
B. +
C. --
D. !
E. %
F. <=
A

A, D. Option A is the equality operator and can be used on numeric primitives, boolean
values, and object references. Options B and C are both arithmetic operators and
cannot be applied to a boolean value. Option D is the logical complement operator
and is used exclusively with boolean values. Option E is the modulus operator, which
can only be used with numeric primitives. Finally, option F is a relational operator that
compares the values of two numbers.

50
Q
What data type (or types) will allow the following code snippet to compile? (Choose all that
apply)
byte x = 5;
byte y = 10;
\_\_\_\_\_ z = x + y;
A. int
B. long
C. boolean
D. double
E. short
F. byte
A

A, B, D. The value x + y is automatically promoted to int, so int and data types that
can be promoted automatically from int will work. Options A, B, D are such data
types. Option C will not work because boolean is not a numeric data type. Options E
and F will not work without an explicit cast to a smaller data type.

51
Q

What is the output of the following application?
1: public class CompareValues {
2: public static void main(String[] args) {
3: int x = 0;
4: while(x++ < 10) {}
5: String message = x > 10 ? “Greater than” : false;
6: System.out.println(message+”,”+x);
7: }
8: }
A. Greater than,10
B. false,10
C. Greater than,11
D. false,11
E. The code will not compile because of line 4.
F. The code will not compile because of line 5.

A

F. In this example, the ternary operator has two expressions, one of them a String and
the other a boolean value. The ternary operator is permitted to have expressions that
don’t have matching types, but the key here is the assignment to the String reference.
The compiler knows how to assign the first expression value as a String, but the second
boolean expression cannot be set as a String; therefore, this line will not compile.

52
Q

What change would allow the following code snippet to compile? (Choose all that apply)
3: long x = 10;
4: int y = 2 * x;
A. No change; it compiles as is.
B. Cast x on line 4 to int.
C. Change the data type of x on line 3 to short.
D. Cast 2 * x on line 4 to int.
E. Change the data type of y on line 4 to short.
F. Change the data type of y on line 4 to long.

A

B, C, D, F. The code will not compile as is, so option A is not correct. The value 2 * x
is automatically promoted to long and cannot be automatically stored in y, which is
in an int value. Options B, C, and D solve this problem by reducing the long value to
int. Option E does not solve the problem and actually makes it worse by attempting
to place the value in a smaller data type. Option F solves the problem by increasing the
data type of the assignment so that long is allowed.

53
Q

What is the output of the following code snippet?
3: java.util.List list = new java.util.ArrayList();
4: list.add(10);
5: list.add(14);
6: for(int x : list) {
7: System.out.print(x + “, “);
8: break;
9: }
A. 10, 14,
B. 10, 14
C. 10,
D. The code will not compile because of line 7.
E. The code will not compile because of line 8.
F. The code contains an infinite loop and does not terminate.

A

C. This code does not contain any compilation errors or an infinite loop, so options D,
E, and F are incorrect. The break statement on line 8 causes the loop to execute once
and finish, so option C is the correct answer.

54
Q

What is the output of the following code snippet?
3: int x = 4;
4: long y = x * 4 - x++;
5: if(y<10) System.out.println(“Too Low”);
6: else System.out.println(“Just right”);
7: else System.out.println(“Too High”);
A. Too Low
B. Just Right
C. Too High
D. Compiles but throws a NullPointerException.
E. The code will not compile because of line 6.
F. The code will not compile because of line 7.

A

F. The code does not compile because two else statements cannot be chained together
without additional if-then statements, so the correct answer is option F. Option E is
incorrect as Line 6 by itself does not cause a problem, only when it is paired with Line
7. One way to fix this code so it compiles would be to add an if-then statement on
line 6. The other solution would be to remove line 7.

55
Q

What is the output of the following code?
1: public class TernaryTester {
2: public static void main(String[] args) {
3: int x = 5;
4: System.out.println(x > 2 ? x < 4 ? 10 : 8 : 7);
5: }}
A. 5
B. 4
C. 10
D. 8
E. 7
F. The code will not compile because of line 4.

A

D. As you learned in the section “Ternary Operator,” although parentheses are not
required, they do greatly increase code readability, such as the following equivalent
statement:
System.out.println((x > 2) ? ((x < 4) ? 10 : 8) : 7)
We apply the outside ternary operator fi rst, as it is possible the inner ternary expression
may never be evaluated. Since (x>2) is true, this reduces the problem to:
System.out.println((x < 4) ? 10 : 8)
Since x is greater than 2, the answer is 8, or option D in this case.

56
Q
What is the output of the following code snippet?
3: boolean x = true, z = true;
4: int y = 20;
5: x = (y != 10) ^ (z=false);
6: System.out.println(x+", "+y+", "+z);
A. true, 10, true
B. true, 20, false
C. false, 20, true
D. false, 20, false
E. false, 20, true
F. The code will not compile because of line 5.
A

B. This example is tricky because of the second assignment operator embedded in line
5. The expression (z=false) assigns the value false to z and returns false for the
entire expression. Since y does not equal 10, the left-hand side returns true; therefore,
the exclusive or (^) of the entire expression assigned to x is true. The output reflects
these assignments, with no change to y, so option B is the only correct answer. The
code compiles and runs without issue, so option F is not correct.

57
Q

How many times will the following code print “Hello World”?
3: for(int i=0; i<10 ; ) {
4: i = i++;
5: System.out.println(“Hello World”);
6: }
A. 9
B. 10
C. 11
D. The code will not compile because of line 3.
E. The code will not compile because of line 5.
F. The code contains an infinite loop and does not terminate.

A

F. In this example, the update statement of the for loop is missing, which is fine as the
statement is optional, so option D is incorrect. The expression inside the loop increments
i but then assigns i the old value. Therefore, i ends the loop with the same value that it starts with: 0. The loop will repeat infinitely, outputting the same statement over
and over again because i remains 0 after every iteration of the loop.

58
Q
What is the output of the following code?
3: byte a = 40, b = 50;
4: byte sum = (byte) a + b;
5: System.out.println(sum);
A. 40
B. 50
C. 90
D. The code will not compile because of line 4.
E. An undefined value.
A

D. Line 4 generates a possible loss of precision compiler error. The cast operator has
the highest precedence, so it is evaluated first, casting a to a byte. Then, the addition is
evaluated, causing both a and b to be promoted to int values. The value 90 is an int
and cannot be assigned to the byte sum without an explicit cast, so the code does not
compile. The code could be corrected with parentheses around (a + b), in which case
option C would be the correct answer.

59
Q

What is the output of the following code?
1: public class ArithmeticSample {
2: public static void main(String[] args) {
3: int x = 5 * 4 % 3;
4: System.out.println(x);
5: }}
A. 2
B. 3
C. 5
D. 6
E. The code will not compile because of line 3.

A

A. The * and % have the same operator precedence, so the expression is evaluated
from left-to-right. The result of 5 * 4 is 20, and 20 % 3 is 2 (20 divided by 3 is 18, the
remainder is 2). The output is 2 and option A is the correct answer.

60
Q

What is the output of the following code snippet?
3: int x = 0;
4: String s = null;
5: if(x == s) System.out.println(“Success”);
6: else System.out.println(“Failure”);
A. Success
B. Failure
C. The code will not compile because of line 4.
D. The code will not compile because of line 5.

A

D. The variable x is an int and s is a reference to a String object. The two data types
are incomparable because neither variable can be converted to the other variable’s type.
The compiler error occurs on line 5 when the comparison is attempted, so the answer
is option D.

61
Q

What is the output of the following code snippet?
3: int x1 = 50, x2 = 75;
4: boolean b = x1 >= x2;
5: if(b = true) System.out.println(“Success”);
6: else System.out.println(“Failure”);
A. Success
B. Failure
C. The code will not compile because of line 4.
D. The code will not compile because of line 5.

A

A. The code compiles successfully, so options C and D are incorrect. The value of b
after line 4 is false. However, the if-then statement on line 5 contains an assignment,
not a comparison. The variable b is assigned true on line 3, and the assignment operator
returns true, so line 5 executes and displays Success, so the answer is option A.

62
Q
What is the output of the following code snippet?
3: int c = 7;
4: int result = 4;
5: result += ++c;
6: System.out.println(result);
A. 8
B. 11
C. 12
D. 15
E. 16
F. The code will not compile because of line 5.
A

C. The code compiles successfully, so option F is incorrect. On line 5, the pre-increment
operator is used, so c is incremented to 4 and the new value is returned to the
expression. The value of result is computed by adding 4 to the original value of 8,
resulting in a new value of 12, which is output on line 6. Therefore, option C is the
correct answer.

63
Q

What is the output of the following code snippet?
3: int x = 1, y = 15;
4: while x < 10
5: y––;
6: x++;
7: System.out.println(x+”, “+y);
A. 10, 5
B. 10, 6
C. 11, 5
D. The code will not compile because of line 3.
E. The code will not compile because of line 4.
F. The code contains an infinite loop and does not terminate.

A

E. This is actually a much simpler problem than it appears to be. The while statement
on line 4 is missing parentheses, so the code will not compile, and option E is the correct
answer. If the parentheses were added, though, option F would be the correct
answer since the loop does not use curly braces to include x++ and the boolean expression
never changes. Finally, if curly braces were added around both expressions, the
output would be 10, 6 and option B would be correct.

64
Q

What is the output of the following code snippet?
3: do {
4: int y = 1;
5: System.out.print(y++ + “ “);
6: } while(y <= 10);
A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5 6 7 8 9 10 11
D. The code will not compile because of line 6.
E. The code contains an infinite loop and does not terminate.

A

D. The variable y is declared within the body of the do-while statement, so it is out of
scope on line 6. Line 6 generates a compiler error, so option D is the correct answer.

65
Q
What is the output of the following code snippet?
3: boolean keepGoing = true;
4: int result = 15, i = 10;
5: do {
6: i--;
7: if(i==8) keepGoing = false;
8: result -= 2;
9: } while(keepGoing);
10: System.out.println(result);
A. 7
B. 9
C. 10
D. 11
E. 15
F. The code will not compile because of line 8.
A

D. The code compiles without issue, so option F is incorrect. After the first execution
of the loop, i is decremented to 9 and result to 13. Since i is not 8, keepGoing is
false, and the loop continues. On the next iteration, i is decremented to 8 and result
to 11. On the second execution, i does equal 8, so keepGoing is set to false. At the
conclusion of the loop, the loop terminates since keepGoing is no longer true. The
value of result is 11, and the correct answer is option D.

66
Q

What is the output of the following code snippet?
3: int count = 0;
4: ROW_LOOP: for(int row = 1; row <=3; row++)
5: for(int col = 1; col <=2 ; col++) {
6: if(row * col % 2 == 0) continue ROW_LOOP;
7: count++;
8: }
9: System.out.println(count);
A. 1
B. 2
C. 3
D. 4
E. 6
F. The code will not compile because of line 6.

A

A. The expression on line 5 is true when row * col is an even number. On the first
iteration, row = 1 and col = 1, so the expression on line 6 is false, the continue is
skipped, and count is incremented to 1. On the second iteration, row = 1 and col = 2, so the expression on line 6 is true and the continue ends the outer loop with
count still at 1. On the third iteration, row = 2 and col = 1, so the expression on line
6 is true and the continue ends the outer loop with count still at 1. On the fourth
iteration, row = 3 and col = 1, so the expression on line 6 is false, the continue is
skipped, and count is incremented to 2. Finally, on the fifth and final iteration, row
= 3 and col = 2, so the expression on line 6 is true and the continue ends the outer
loop with count still at 2. The result of 2 is displayed, so the answer is option B.

67
Q
What is the result of the following code snippet?
3: int m = 9, n = 1, x = 0;
4: while(m > n) {
5: m--;
6: n += 2;
7: x += m + n;
8: }
9: System.out.println(x);
A. 11
B. 13
C. 23
D. 36
E. 50
F. The code will not compile because of line 7.
A

D. Prior to the first iteration, m = 9, n = 1, and x = 0. After the iteration of the first
loop, m is updated to 8, n to 3, and x to the sum of the new values for m + n, 0 + 11 =
11. After the iteration of the second loop, m is updated to 7, n to 5, and x to the sum
of the new values for m + n, 11 + 12 = 23. After the iteration of the third loop, m is
updated to 6, n to 7, and x to the sum of the new values for m + n, 23 + 13 = 36. On
the fourth iteration of the loop, m > n evaluates to false, as 6 < 7 is not true. The
loop ends and the most recent value of x, 36, is output, so the correct answer is option
D.

68
Q

What is the result of the following code snippet?
3: final char a = ‘A’, d = ‘D’;
4: char grade = ‘B’;
5: switch(grade) {
6: case a:
7: case ‘B’: System.out.print(“great”);
8: case ‘C’: System.out.print(“good”); break;
9: case d:
10: case ‘F’: System.out.print(“not good”);
11: }
A. great
B. greatgood
C. The code will not compile because of line 3.
D. The code will not compile because of line 6.
E. The code will not compile because of lines 6 and 9.

A

B. The code compiles and runs without issue, so options C, D, and E are not correct. The
value of grade is ‘B’ and there is a matching case statement that will cause “great” to
be printed. There is no break statement after the case, though, so the next case statement
will be reached, and “good” will be printed. There is a break after this case statement,
though, so the switch statement will end. The correct answer is thus option B.