Ch 2 - Operators and Statements Flashcards
Name the three types of Java operators
unary
binary
ternary
All arithmetic operators may be applied to any java primitives, except which types?
boolean and string
Only which operators can be applied to String values?
+ and +=
Name the two short circuit logical operators
|| and &&
Name the logical operators and what they are used for
& - 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
What is the resulting data type of the equation x * y where:
int x = 1;
long y = 33;
since one of the values is long and the other is int, the int value is promoted to a long.
What is the data type of x + y, where
double x = 39.21;
float y = 2.1;
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.
What is the data type of x / y, where
short x = 10;
short y = 3;
x and y will both be promoted to int before the operation, resulting in an output type of int.
What is the data type of x * y / Z, where
short x = 14;
float y = 13;
double z = 30;
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.
Name the four numeric promotion rules:
- if two values have different data types, java will automatically promote one of the values to the larger of the two data types.
- 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.
- 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.
- 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.
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);
x = 2 y = 7
How do you fix the following lines of code: int x = 1.0; short y = 1921222; int z = 9f; long t = 192301398193810323;
int x = (int) 1.0;
short y = (short)1921222; // Stored as 20678
int z = (int)9f;
long t = 192301398193810323L;
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);
x = 3 y = 3
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"); }
its true
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"); }
its not true
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"); }
Compiler error on this line:
if (x1) {
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"); }
Compiler error on this line:
if (x1 = 5) {
The short-circuit operators are && and ||. They are nearly identical to the & and | logical operators except…
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.
What is the output of the following code?
int x = 6;
boolean y = (x >= 6) || (++x <= 7);
System.out.println(x);
Because x >= 6 is true, the increment operator on the right-hand side of the expression is never evaluated, so the output is 6.
true/false: the following code compiles:
boolean x = true == 3;
false
true/false: the following code compiles:
boolean y = false != “Giraffe”;
false
true/false: the following code compiles:
boolean z = 3 == “Kangaroo”;
false
What does the following code output:
boolean y = false;
boolean x = (y = true);
System.out.println(x);
true
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);
false
true
Describe a ternary operator
booleanExpression ? expression1 : expression2;
What is the value of x?
int y = 10;
int x = (y > 5) ? (2 * y) : (3 * y);
20
true/false, the following code compiles:
System.out.println((y > 5) ? 21 : “Zebra”);
true
true/false, the following code compiles:
int animal = (y < 91) ? 9 : “Horse”;
false
What does the following code output: int y = 1; int z = 1; final int x = y<10 ? y++ : z++; System.out.println(y + "," + z);
2,1
What are the data types supported by switch statements?
- byte and Byte
- short and Short
- int and Integer
- char and Character
- String
- enum values
What two primitive types are not supported when used by the switch statement?
boolean and long (and their wrapper classes)
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; }
Weekday
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; }
Weekday
Saturday
The case statement from a switch statement must have what attributes?
must be a literal, enum constant, or final constant variable.
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; }
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; }
true/false, A while loop may terminate after its first evaulation of the boolean expression
true
true/false, a do-while loop guarantees that the statement or block will be executed at least once
true
What would the following code result in:
for ( ; ; ) {
System.out.println(“Hello World”);
}
continuous loop outputting: Hello World Hello World Hello World Hello World Hello World ...
true/false, each of the components of the for loop are optional. Example:
for (int i = 0; i<10; i++) {
System.out.println(i);
}
true
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 + “”);
}
0 1 2 3 4
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 + “”);
}
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.
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);
}
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.
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);
Compiler error:
System.out.println(x);
x is out of scope here.
What would the output of the following code be: String[] names = new String[3]; for (int name : names) { System.out.print(name + " "); }
Compiler error:
for (int name : names) {
Data types don’t match.
Which operators can have optional labels?
if-then, switch, and loops
What does a break statement do?
A break statement transfers the flow of control out to the enclosing statement.
What does a continue statement do?
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.
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); } } }
1a 3a 4a
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); } } }
1a 1c 3a 3c 4a 4c
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); } } }
1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c
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++); }
2
Remember that a continue statement cannot be used with an if/then, unless the if/then is included in a loop.