Java OCA 1Z0-808 - Operators and Statements

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

____________ ____________ is required any time you are going from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.

Casting primitives

When was enum added to Java?

Java 5

When was the enhanced for loop added to Java?

Java 5.

When applying operators to two data types in which one is an integral and the other floating-point, what does Java do?

Java will automatically promote the integral value to the floating-points value's data type.

For object comparison, what is the equality operator applied to?

The references to the objects, not the objects they point to. Two references are equal if and only if they point to the same object, or both point to null.

After all promotions have occurred and operands have the same data type, what type does the resulting value have?

The same data type as the promoted operands.

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 == z);

True

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

True

*True or False:* Modulus operations can be applied to positive, negative, and floating-point integers.

True.

What operator is useful for determining whether an arbitrary object is a member of a particular class or interface?

the instanceof operator *Example:* moto instanceof MotorCycle

What is the structure of a while loop?

while(booleanExpression) { // body }

___ is the inclusive OR logical operator.

|

What data types are supported by the Java switch statement?

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

What are the most common operators in the Java language?

binary operators

A ___________ of code in Java is a group of zero or more statements between balanced braces, ({}), and be used anywhere a single statement is allowed.

block

The ___________ and ___________, and their associated wrapper classes, are not supported by Java switch statements.

boolean, long

The ____________ ____________ terminates a switch statement and returns flow control to the enclosing statement.

break statement

Trying to set a short variable to an int results in a ____________ error, as Java thinks you are trying to implicitly convert from a larger data type to a smaller one.

compilation

What is the structure of a do-while loop?

do { // body } while(booleanExpression);

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

double

What is the structure of the basic for loop?

for(initialization; boolExpr; updateStmt) { // body }

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

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

The ___, ___, ___, and ___ relational operators only apply to numeric primitive types.

<, <=, >, >=

What are the assignment operators in Java?

=, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=

___ is the equality operator and ___ is the non-equality operator.

==, !=

Assignment operator

A binary operator that modifies the variable on the left side of the operator, with the result of the value on the right-hand side of the equation.

assignment operator

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.

*repetition control structure*

A statement, referred to as a loop, that can execute a block of code multiple times in succession.

logical complement operator

!, flips the value of a boolean expression. *Example:* boolean x = false; x = !x; // Makes x = true.

The logical operators, ___, ___, and ___, may be applied to both numeric and boolean data types.

&, |, and ^ When applied to numeric types, they're referred to as bitwise operators.

Arithmetic operators

+, -, *, /, %, --, ++

Unary operators

+, -, ++, --, !

What are the unary operators?

+, -, --, ++, !

negation operator

-, reverses the sign of a numeric expression.

How many times may a while loops body execute?

0 or more times.

When are equality operators used?

1. Comparing two numeric primitive types. 2. Comparing two boolean values. 3. Comparing two objects, including null and String values.

What are the numeric promotion rules in Java?

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 (excluding unary operators), 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.

What is the order of operator precedence in Java?

1. Postfix-unary operators (++, --) 2. Prefix-unary operators (++, --) 3. Other unary operators (+, -, !) 4. Multiplicative operators (*, /, %) 5. Additive operators (+, -) 6. Shift operators (<<, >>, >>>) 7. Relational operators (<, >, <=, >=, instanceof) 8. Equality operators (==, !=) 9. Bitwise Logical operators (&, ^, |) 10. Short-circuit logical operators (&&, ||) 11. Ternary operators (?) 12. Assignment operators (=, +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=)

statement

A complete unit of execution in Java, terminated with a semicolon (;).

What type of loop will execute at least once?

A do-while loop.

What is the enhanced for loop also known as?

A for-each loop.

*loop*

A sequence of instructions that is continually repeated until a certain condition is reached.

operator

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

*switch statement*

A switch statement 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.

What rules help you remember how to apply the logical operators &, |, and ^?

AND is only true if both operands are true. Inclusive OR (|) is only false if both operands are false. Exclusive OR (^) is only true if the operands are different.

What can you do to make the following code compile? long x = 10; int y = 5; y = y * x;

Add an explicit cast to (int) on the last line. When using compound assignment operators this is done automatically.

What is the result of the following code? for( ; ; ) { System.out.println("Hello World"); }

An infinite loop.

unary operator

An operator that requires exactly one operand, or variable, to function.

When is casting required?

Any time you go from a larger numerical data type to a smaller numerical data type, or convert from a floating-point number to an integral value

*relational operators*

Binary operators that compare two expressions and return a boolean value.

What must the values in each case statement must be?

Compile-time constant values of the same data type as the switch value. This means you can use only literals, enum constants, or final constant variables of the same data type.

___________ ___________ statements break up the flow of execution by using decision making, looping, and branching, allowing the application to selectively execute particular segments of code.

Control flow

*True or False:* You can apply a logical complement operator, !, to a numeric expression.

False

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

False

*True or False:* The initialization and update sections of a basic for loop may not contain multiple statements.

False. The initialization and update sections of a basic for loop may not contain multiple statements, separated by commas.

If there is no case statement that matches the value passed to a switch statement, what occurs?

If available, an optional default statement will be called. If no such default option is available, the entire switch statement will be skipped.

How are prefix unary operators and postfix unary operators applied differently?

If the operator is placed before the operand (pre-increment/decrement operator), then the operator is applied first and the value return is the new value of the expression. Alternatively, if the operator is placed after the operand (post-increment/decrement operator), then the original value of the expression is returned, with operator applied after the value is returned.

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

Long

What components of the for loop signature are required?

Only the for keyword and separating semicolons. All other elements are optional.

equality operators

Operators that compare two operands and return a boolean value about whether the expressions or values are equal, or not equal, respectively.

*compound assignment operators*

Operators that provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. +=, -=, *=, /=, %=, &=, ^=, !=, <<=, >>=, >>>=

What is the result of integer division?

The floor value of the nearest integer that fulfills the operation.

short-circuit operators

The logical operators (&& and ||) that nearly identical to the logical operators, & and |, respectively, except that 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 operator takes three operands?

The ternary operator. boolExpression ? expression1 : expression2

What is the simplest Java loop?

The while loop.

Where does the default statement belong in a switch statement?

There is no requirement that the case or default statements be in a particular order, unless you are going to have pathways that reach multiple sections of the switch block in a single execution.

When smaller data types (byte, short, char) are used with a binary arithmetic operator, what is automatically done?

They are first promoted to int, even if neither operand is an int.

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

This code will not compile because floating-point literals are assumed to be double, unless postfixed with an f (2.1f).

*True of False:* When using a compound assignment operator, the compiler will automatically cast the resulting value to the data type of the value on the left-hand side of the compound operator.

True

*True or False:* Curly braces are not required in control flow statements that branch to a single statement.

True

*True or False:* If one of the two right-hand expressions in a ternary operator performs a side effect, then it may not be applied at runtime.

True

*True or False:* The data type for case statements must all match the data type of the switch variable.

True

*True or False:* When applying operators to data types, if the two values have different data types then the smaller type is promoted to the larger type

True

*True or False:* You cannot apply a negation operator, -, to a boolean expression.

True

What three types of operators are available in Java?

Unary Binary Ternary

What is the scope of variables declared in the initialization block of a for loop?

Variables declared in the initialization block of a for loop have limited scope and are only accessible within the for loop.

Overflow

When a number is so large that it will no longer fit within the data type, so the system "wraps" to the next lowest value and counts up from there.

Underflow

When a number is so small that it will no longer fit within the data type, so the system "wraps" to the next highest value and counts down from there.

What is the primary difference between the logical operators &, |, and ^ and the logical (short circuit) operators && and ||?

When using &, |, and ^ both sides of the expression are evaluated whereas, when using && and || the right-hand side of the expression may never be reached.

When should you use a do-while loop?

When you need to execute the loop body at least once.

How can you change the order of precedence explicitly?

With parentheses.

Is the following line legal? long y = (x=3);

Yes, because assignment is an expression in and of itself, equal to the value of the assignment.

Why do the following lines not compile? boolean x = true == 3; boolean y = false != "Giraffe"; boolean z = 3 == "Kangaroo";

You cannot mix and match types when using equality operators.

What happens if you don't include the break statement in each case of a switch statement?

You will have fall-through, a condition where switch statements don't end when a case is satisfied and the switch handles all remaining cases.

___ is the exclusive OR logical operator.

^

What does an if-then statement look like?

if(booleanExpression) { // Branch if true }

What does an if-then-else statement look like?

if(booleanExpression) { // Branch if true } else { // Branch if false }

The ___________ statement, allows our application to execute a particular block of code if and only if a boolean expression evaluates to true at runtime.

if-then

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

int

A ____________ statement, like a break statement, can be used to exit the switch statement early.

return

What is the structure of a switch statement?

switch(variableToTest) { case constantExpression1: // Branch for case1; case constantExpression2: // Branch for case2; ... default: // Branch for default }

The ____________ ____________ is really a condensed form of an if-then-else statement that returns a value.

ternary operation


Ensembles d'études connexes

Vocab Unit 8B Remote Learning, Vocab Unit 8A - Remote Learning 4/20/20

View Set

PE: Principles of Training (SPORT & FITT)

View Set

7G1 Math Exam Review First Semester

View Set

HSC4501: EXAM 3 STUDY GUIDE, iCLICKERS, AND HW ASSIGNMENTS

View Set

Properties of Numbers & Fractions

View Set

Sherpath - Chapter 21: Managing Patient Care

View Set