Chapter 2 Flashcards

1
Q

When is type casting required? (Two conditions with numbers)

A

1.Any time you go from larger numerical data types to smaller types 2. When you convert a floating point number to an integral value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Would this run? for( ; ;){ System.out.println(“HelloWorld”); }
A

This will run without issue. This example reinforces the fact that the pieces of a for loop are each optional. But the semi colons are retired.

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

What two things can you do a for-each method call on?

A

a built in java array or an object whose class implements java.lang.Iterable.

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

What is overflow?

A

When a number is so large that it no longer fits within the data type. The system in these cases wraps around to the next lowest value and counts up from there.

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

What’s the structure for using a continue statement?

A

while(booleanExpression)

{ //body //somewhere in loop continue; }

For example:

public class Main {

public static void main(String… strings) {

int x = 9;

while (x < 12) {

System.out.println(x);

x++;

if (x == 11) {

System.out.println(“continued”);

continue;

}

System.out.println(“didn’t continue”);

}

}

}

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

Are floating-point literals automatically set to float or double unless postfixed?

A

Double. Must have an f on the end if you want it to be a float.

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

Which two primitives can arithmetic operators not be applied to?

A

boolean and char

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

What three scenarios could equality operators be used in?

A
  1. Comparing two numeric primitive types. 2. Comparing two boolean values. 3. Comparing two objects, including null and String values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why won’t this compile? String names = “Lisa”; for (String name : names){ System.out.println(name); }

A

Because names is not an array. pg. 84

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

What can logical operators be applied to?

A

numeric and boolean data types

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

Are labels required for break and continue statements?

A

No

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

Why doesn’t this compile? int x =!5;

A

Because ! is reserved for boolean values or expressions. If you want 5 to be negative simply put the negation operator (-) in front of 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. For loop examples to know - Adding Multiple Terms to the for Statement: int x = 0; for (long y=0, z = 4; x <5 && y < 10; x++, y++) { System.out.print(y + “ “); }
A

This demonstrates a few things: you can declare a variable before the loop begins and use it after it completes. Second, your initialization block, boolean expression, and update statements can have include references that do not talk to each other. Third, the update statement, can modify multiple variables.

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

What is the output? short x = 10; short y = 3; short z = x/y; System.out.println(z);

A

This won’t compile. The result of x/y would be an int as data types smaller than int are promoted to int when used with arithmetic operators.

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

What’s a java statement?

A

Anything that ends with ;

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

What are logical operators called when they refer to boolean data types?

A

Logical operators

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

What is the logical complement operator?

A

! inverts the value of a boolean expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. For loop examples to know - Using incompatible data types in the initialization block: for (long y = 0, int x = 4; x < 5 && y < 10; x++, y++){ // why won’t this compile? System.out.print(x + “ “ ); }
A

The variables in the initialization block must all be of the same type.

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

What are the three flavors of operators available to java?

A

Unary, binary, and ternary. One, two, or three operands

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

Why won’t this compile? String[] names = new String[3]; for (int name : names0{ System.out.println(name); }

A

Because name is a string array and the for each loop indicates name is an int. Also, keep in mind that this array is initialized with three null pointer values.

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

What is the simplest assignment operator?

A

= as in int x = 1;

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

What is the ternary operator a condensed form of?

A

The if, else, then statement

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

What is a label with respect to loops?

A

An optional pointer to the head of a statement that allows the application flow to jump to it or break from it. such as OUTER_LOOP: and INNER_LOOP:

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

What is the structure of the for-each statement?

A

for(datatype instance : collection){ // body } curly braces optional for single statement blocks but required for multi-statement blocks.

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

Why doesn’t this compile and how would you fix it? short x = 10; short y = 3; short z = 10*3;

A

It doesn’t compile because anything smaller than int is promoted to int during arithmetic operations. So you have a type problem. You could fix it with casting: short z = (short)10*3;

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

What is the structure of a switch statement?

A

switch(variableToTest){ caseOne constantExpressionOne: code to run for caseOne; optionalBreak; caseTwo constantExpressionTwo: code to run for case2; default: (optional)

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

What is the operator precedence of the third three operators?

A
  1. Relational operators: ,<=,>=, instanceof 8. Equal to/not equal to: ==,!= 9. Logical operators &,^,|
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the structure of a ternary operator?

A

booleanExpression ? expressionOne : expressionTwo

for example: int y = x==2 ? 3 : 4;

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

Which of this code will run if false? if(hourOfDay<11) System.out.println(“Good Morning”); morningGreetingCount++;

A

Only morningGreetingCount++. When there are no brackets, it only runs the next statement/block of code. Whitespace means nothing to java.

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

What is a compile time constant value?

A

These are variables that have been marked with the final keyword and initialized with a literal value in the same expression in which it is declared. Variables passed into methods, even if marked final, are not considered compile time constant. Switch statements require it’s case statement variable to be this. So, like this: final int b =4;

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

Why does the break matter in a switch statement?

A

If you don’t give a case a break statement the program will run all the proceeding statements until the switch statement ends or it finds a break;

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

What is a nested loop?

A

A loop containing another loop

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

What are the arithmetic operators?(7)

A

+,-,*,/,% and include the unary operators: ++,–

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

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

A

Trick! This will not compile. 2.1 must have an f, and if you had that then both of these would be promoted to double.

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

Why doesn’t this compile? int animal = (y<91) ? 9 : “Horse”;

A

If y is greater than or equal to 91, the return type of the expression would be a string. And, as animal is an int, we have a type problem.

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

What is this doing? int x = (int)1.0;

A

Converting the double 1.0 to an int

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

What is an operand?

A

The thing an operator is evaluating.

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

What’s the structure for using a break statement?

A

optionalLabel: while(booleanExpression){ //body //somwhere in loop break optionalLabel; }

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

When is && true? When is it false?

A

only true when both operands (expressions being evaluated) are true. Returns false if either operand is false

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

What do break statements do?

A

Terminate switch statements or other statements to return control back to the program.

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

!=

A

Compares two operands and returns a boolean value whether or not the two objects are not equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q
  1. For loop examples to know - Using Variables Outside the Loop: for ( long y = 0, x = 4; x < 5 &&; y < 10; x++, y++){ System.out.print( y + “ “ ); } System.out.println(x); // why doesn’t this compile

(Are the variables defined in an initiation block of a for loop available outside the for loop?)

A

Scope variable problem. x (and all other variables defined in the initialization block of a for loop) are only in scope for the duration of the loop. After the loop is over the variable is donezo.

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

What is the structure of a do-while statement?

A

do { //body }while (booleanExpression); curly braces required for block of multiple statements. Optional for one line bodies. So this works:

String hello = “hello”;

do System.out.println(hello);

while(1>0);

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

What is the operator precedence of the second three operators?

A
  1. Multiplication/Division/Modulus: *,/,% 5. Addition/subtraction 6. shift operators: <>,>>>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Why does this work? long x=10; int y = 5; y *=x;

A

Because the compound operator will take care of casting.

46
Q

What are logical operators called when they refer to numerical data types?

A

bitwise operators: they compare the bits that compose the numbers

47
Q

How does java handle this situation? x = 2; y = 2.0; x == y;

A

It would return a true by promoting to a double.

48
Q

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

A

In this case, both numbers will be promoted to int before the operation resulting in an output type of int.

49
Q

When is | true? When is | false?

A

(Inclusive Or) If either operand is true it will return true. It will only be false if both operands are false.

50
Q

If a switch statement doesn’t find a matching case, what happens?

A

Either a provided default statement runs or the switch statement is skipped entirely.

51
Q

What are pre-increment/decrement and post-increment/decrement operators?

A

++–value and value++– These can change how a program runs substantially.

52
Q
  1. For loop examples to know - Redeclaring a Variable in the initialization block: int x = 0; for(long y=0, x=4; x < 5 && y < 10; x++, y++){ //why doesn’t this compile? System.out.print(x + “ “); }
A

This doesn’t compile because the initialization block is trying to reinitialize an already existing value.

53
Q

What is the operator precedence of the last three operators? (9-12)

A
  1. Short-circuit logical operators: &&, || 11. ternary operators: boolean expression ? expression1 : expression 2 12. Assignment operators: =,+=,-=,
54
Q

Numeric Promotion Rules: 3 rules

A
  1. If two values have different data types in an equation, Java promotes the smaller of the two to match the size (bit wise) of the larger number. 2. Smaller data types like byte, short, and char are first promoted to int anytime they are used with a Java binary arithmetic operator, even if neither of the operands are integer. Unary operators are excluded from this rule. (adding ++ to a short value results in a short value) 3. After all the promotion required to be used with the binary operator, the result will match the value in the data type to which everything was promoted to. Result type will equal the type of the hence promoted operands.
55
Q

What is an operator?

A

A special symbol that can be applied to variables, values, or literals.

56
Q

Can you use a return statement in a switch block?

A

Yes. And it will act like a break; and exit the switch block.

57
Q

Do ternary operators behave like logical operators or short circuit/conditional operators?

A

Like short circuit operators. Which means if you have something in the last (else) place, it may not run.

58
Q

What do relational operators do? What do they return?

A

They compare two expressions and return a boolean value.

59
Q

What are the compound assignment operators necessary to know for the exam?

A

+= and -=

60
Q

What is the structure of a basic for-loop?

A

for(initialization; booleanExpression; updateStatement){ // body } curly braces optional for one line bodies, required for more than one line bodies to work.

61
Q

What’s a java statement?

A

Anything that ends with ;

62
Q

What do java operators do if two operators have the same level of precedence?

A

Goes through them from left to right

63
Q

What is a unary operator?

A

By definition, it is an operator that only requires one operand.

64
Q

What are the three logical operators? (Not short circuit)

A

& | ^

65
Q

Why doesn’t this compile and how would you fix it? short x = 10; short y = 3; short z = 10*3;

A

It doesn’t compile because anything smaller than int is promoted to int during arithmetic operations. So you have a type problem. You could fix it with casting: short z = (short)(10*3);

66
Q

How many times will this code continue before it goes into the nested loop that has the break in it?

public static void main(String[] args) {

int[] listOne = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int searchValue = 5;

PARENT_LOOP: for (int i = 0; i < listOne.length; i++) {

System.out.println(“In parent loop”);

FIRST_NESTED_LOOP: for (int j = 0; j < listOne[i]; j++) {

System.out.println(“In nested loop”);

if (listOne[i] != searchValue) {

System.out.println(“about to continue”);

continue PARENT_LOOP; }

if (listOne[i] == searchValue) {

System.out.println(“about to break”);

System.out.println(listOne[i]);

break PARENT_LOOP; } }

System.out.println(“Did we really break out of parent? “); } }

A

In parent loop In nested loop about to continue In parent loop In nested loop about to continue In parent loop In nested loop about to continue In parent loop In nested loop about to continue In parent loop In nested loop about to break 5

67
Q

What are the four relational operators applied to primitive data types (only)

A

< <= > >=

68
Q

What are compound assignment operators?

A

assignment operators with arithmetic or logical operations built in.

69
Q

What are the increment and decrement operators?

A

++ and –

70
Q

When is ^ true? When is ^ false?

A

(Exclusive Or) This will only return true if one of them is true and the other is false. If both are true, it returns false. If both are false. It returns false. The operands must be different from each other.

71
Q

what is underflow?

A

when a number is too low to fit in the data type

72
Q

How do equality operators handle object comparison?

A

By comparing the references to objects. not to the objects themselves.

73
Q

What are the 5 unary operators in Java?

A

+ to indicate a number is positive - indicates a literal number is negative or negates an expression ++ increments a value by 1 – decrements a value by 1 ! Inverts a booleans logical value

74
Q

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

A

The int will be promoted to a long and the returned value will be a long.

75
Q

Are these equivalent? x = x*z; x*=z;

A

Yes. This is a compound assignment variable.

76
Q

In switch statements, does it really matter where/if you put the default statement?

A

no, but there are consequences. If the default is in the middle of the cases, it will run all the proceeding cases until it finds a break;

77
Q

==

A

Compares two operands and returns a boolean value indicated if the two operands do or do not equal each other.

78
Q

Under what conditions will instanceof return true?

A

If the reference that objectA points to is an instance of a class, subclass, or class that implements a particular interface, as objectB.

79
Q

What is the structure of a while statement?

A

while(booleanExpression){ // body } Curley braces optional for a single statement but required for bodies that have more than one statement.

80
Q

What are the resulting values of the variables? int x = 3; int y = ++x * 5 / x– + –x;

A

x = 2; y = 7; Remember, the value and the variable are not always the same given the time that the increment occurs in context with the expression.

81
Q

What does the modulus operator do?

A

It’s also called the remainder operator and tells the user the remainder of two number numbers when divided by each other.

82
Q

What are pre and post unary operators evaluated in the order of precedence?

A

Very first

83
Q

When does && return true? When is it false?

A

This looks for a reason to fail. As soon as it finds a false operand it exits the expression.

84
Q

What are the two types of for statements?

A
  1. The basic for loop. 2. The enhanced for loop. AKA for-each statement
85
Q

Which relational operator can be applied to object references and classes or interfaces?

A

a instanceof b

86
Q

What is the for-each statement specifically designed for?

A

To iterate over arrays and Collection objects.

87
Q

What is the output of x? int x = 6; boolean y = (x>=6) || (++x<=7);

A

Because x>=6 the increment operator is never evaluated so the output would be 6.

88
Q

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

A

First, x is promoted to long, then to float to be multiplied with y, then the result of that is promoted to double for division with z. Result being double.

89
Q

Can you use a break statement in loops like for, while, and do-while loops?

A

Yes. And it will end the loop as soon at it is hit.

90
Q

What are the five steps of a basic for statement?

A
  1. Initialization statement executes (int j = 0…) 2. If booleanExpression is true, continue to block, else exit loop. (…; j<10…) 3. Body executes 4. Execute updateStatements (…..j++) 5. Return to step 2
91
Q

Do the data types in switch statement variables and cases matter?

A

Yes, they must match.

92
Q

When is || true? When is it false?

A

This looks for a reason to succeed. As soon as it finds true it considers it a success.

93
Q

What is the structure of an if-then-else statement?

A

if(booleanExpression){ //code to run if true } else { // code to run if false }

94
Q

When are references equal?

A

If, and only if, they point to the same object, or if they both point to null.

95
Q

Which data types are supported by switch statements? 10 types

A

byte and Byte short and Short char and Character int and Integer String enum values

96
Q

What are two principle ways for advanced loop control?

A

Break and Continue statements.

97
Q

What do labels allow you to do with labels on break statements?

A

You are able to break out of parent loops from within a nested loop.

98
Q

What is a compile time constant value?

A

These are variables that have been marked with the final keyword and initialized with a literal value in the same expression in which it is declared. Variables passed into methods, even if marked final, are not considered compile time constant. Switch statements require it’s case statement variable to be this.

99
Q

What is the difference between a break statement and a continue statement?

A

Break statements transfer control to the enclosing statement, continue statements transfer control to the to the boolean station

100
Q
  1. For loop examples to know - Creating an infinite loop: for ( ; ;){ System.out.println(“HelloWorld”); }
A

This will run without issue. This example reinforces the fact that the pieces of a for loop are each optional. But the semi colons are retired.

101
Q

What is an assignment operator?

A

It is a binary operator that modifies or assigned the variable on the left-hand side of the operator with the value or expression

102
Q

What is a switch statement?

A

A decision making structure evaluating a single value watching for cases.

103
Q

What’s wrong with this? 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 = 1; 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”: // compiles fine as test is a string literal return 52; case middleName: //Does not compile. middle name is not a final variable id = 5; break; case suffix: //compiles fine as suffix is a final constant variable id = 0; break; case lastName: //does not compile. even though it is final because it is passed to the function it is not constant id = 8; break; case 5: //does not compile because the type doesn’t match the switch variable type id = 7; break; case ‘J’://does not compile because the type doesn’t match the switch variable type id = 1; break; case java.time.DayOfWeek.SUNDAY://does not compile because the type doesn’t match the switch variable type id=15; break; } return id; }

104
Q

What is the key difference between a while-statement and a do-while statement?

A

The do while statement will always execute at least one time.

105
Q

What are the conditional/short circuit operators?

A

&& and ||. Why is it called short circuited? Because the right hand Sind of the expression may never be evaluated. It may know the final result of the expression before the right side gets evaluated.

106
Q

What is a block of code?

A

A group of zero or more statements between braces.

107
Q

Does this work? long x =5; long y = (x=3); What would the output be?

A

Yes this works. The assignment operator is an expression in an of itself, equal to the value of the assignment. It returns a value. So if you printed both of these the out put would be three.

108
Q

What is the structure of an if-then statement?

A

if(booleanExpression){ //code to run if true }

109
Q

What is the operator precedence of first three operators? (Unary

A
  1. Post-unary operators: expression++,expression– 2. Pre-unary: ++expression, –expression 3. Other unary operators: ~, +, -, !
110
Q

Why doesn’t this compile? boolean y = -true;

A

Because - is reserved for numerical values. If you want to invert a boolean expression or value use the logical complement operator (!)

111
Q

Why would someone prefer a traditions for loop over a for each loop?

A

For more, fine grained control of what happens.

112
Q

Does this compile?

int i = 0;

do System.out.println(1+1); while (i<4);

A

Yes. But it’s not a good idea. We aren’t incrementing i, so the program will run indefinitely.