OCA Java SE 7: Using Operators and Decision Constructs

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

What are the operators for logical "and" and "exclusive or"?

"&" and "^"

What are the unary operators?

++ and --.

True of False: The case value can be the literal value null.

False.

True or False: The default switch statement must be the last condition in a switch statement.

False.

True or False: The case value should be a compile-time constant, assignable to the argument passed to the switch statement.

True.

True or False: The if statement enables you to execute a set of statements in your code based on the result of a condition, which must evaluate to a boolean or Boolean value.

True.

How to do bitwise and operation on hex 000F and 2222?

"0x2222 & 0x000F". First convert each number to binary, then do the bitwise operation to get decimal 2 as the result.

How to do exclusive or operation on 24 and 2

"24 ^ 2". First convert each number to binary, then do the bitwise operation to get decimal 26 as the result.

What argument types are valid for a switch statement?

Char , byte, short, int, and String . It also accepts arguments of wrapper classes: Character , Byte, Short, Integer , and Enum.

True or False: You can use the concatenation operators + and += and comparison operators != and == with String objects. The Java language provides special support for concatenating String objects by using the operators + and +=.

True.

True or false: By default, unary operators have a higher precedence than multiplication operators and addition operators.

True.

True or false: Unary operators can be used in prefix or postfix notation.

True.

True or False: An if construct may or may not define its else part.

True.

True or False: If you don't assign a value to a final variable with its declaration, it isn't considered a compile-time constant as a switch argument.

True.

True or False: You can pass an expression as an argument to a switch statement, as long as the type of the expression is one of the acceptable data types.

True.

How is the code block for a switch condition exited, once it has been executed?

A break statement is used to exit a switch construct. In absence of the break statement, control will fall through all the remaining case values in a switch statement until the first break statement is found.

When would a switch statement be useful?

A switch statement is used to compare the value of a variable with multiple pre-defined values.

Discuss why the logical operators && and || are also called short-circuit operators.

If these operators can determine the output of the expression with the evaluation of the first operand, they don't evaluate the second operand. The && operator returns true only if both of the operands are true. If the first operand to this operator evaluates to false, the result can never be true. Therefore, && does not evaluate the second operand. Similarly, the || operator returns true if any of the operands is true. If the first operand to this operator evaluates to true, the result can never be false. Therefore, || does not evaluate the second operator.

How does the comparison operator (==) behave when applied to Strings?

It compares String references, whereas the equals method compares the String values.

What does the pipe operator do?

It does an OR. Returns 1 for all pairs except 0 | 0.

can void methods be used for results in tertiary operator "?"

No.

Does this compile? (!b++ > 100 && a++ == 10)

No. Explanation: Although it may seem that the negation unary operator (!) is being applied to the expression b++ > 100, it's actually being applied to the variable b of type int. Because a unary negation operator (!) can't be applied to a variable of type int, the code fails to compile. The correct if condition would be as follows: (!(b++ > 100) && a++ == 10)

What are the relational operators?

Relational operators are used to compare values for equality (==) and non-equality (!=). They are also used to determine whether two numeric values are greater than (>, >=) or less than (<, <=) each other. The operators equal to (==) and not equal to (!=) can be used to compare all types of primitives: char , byte, short, int, long, float, double, and boolean. The operator == returns true if the primitive values being compared are equal. The operator != returns true if the primitive values being compared are not equal. The result of the relational operator is always a boolean value.

What are the three types of if statement formats?

if, if-else, and if -else-if-else.

What are the type requirements on the switch expression and case constants?

Every case constant must be assignable to the type of the switch expression. So if the swtich(x) and x is a byte, then every case constant literal must be assignable to x: case 'a': //ok case 256: // out of 127 max range, not ok case 100: // in range, ok

What is the basic outline an if construct?

The if construct doesn't use the keyword then to define code to execute when an if condition evaluates to true. The then part of the if construct follows the if condition.

True or False: The right technique for comparing two String values for equality is to use the method equals defined in the String class.

True. This method returns a true value if the object being compared isn't null and is a String object that represents the same sequence of characters as the object to which it's being compared. The comparison operator == determines whether both the reference variables are referring to the same String objects. Hence, it's not the right operator for comparing String values.

Is this a valid expression: boolean b1, b2; b2 = b1 != b2;

Yes, boolean operators take prescendence over =. b1 != b2 evaluates to false, resulting in b2 = false as the expression.

Does the instanceof operator have a compilation error when used with orthogonal class hierarchies?

Yes.

Does every expression have a return value?

Yes. And the value is the LHS: int i = 10; // return value is 10 boolean b = true // return value is 10 boolean b1 = false, boolean b2 = false; b2 = b1 != b2; // returns false So if statements given an expression will evaluate to the LHS. Compilation for the if will fail if the expression does not return a boolean.

What are the logical operators?

You can use the logical operators to determine whether a set of conditions is true or false and proceed accordingly. Logical AND ( && ) evaluates to true if all operands are true, and false otherwise. Logical OR (||) evaluates to true if any or all of the operands is true. Logical negation (!) negates the boolean value. It evaluates to true for false, and vice versa. The result of a logical operation is always a boolean value.


Ensembles d'études connexes

TAMUCC MGMT 5350 Entrepreneurship Byus FINAL - Chap 01-12

View Set

Chapter 11: Reproductive Behaviors

View Set

AFJROTC // Chapter 8 Lesson 3 Seeking Feedback and Promotions

View Set

Accounting Test: Unit 13.12 (multiple choice)

View Set

Electricity (3rd Unit) Lesson: Physics B

View Set

WORLD HISTORY SVHS unit 2 quizzes

View Set