A.2 Chapter 2: Working with Java data types

Chapter 2 includes four Twist in the Tale exercises. Twist in the Tale 2.1 has two parts.

A.2.1 Twist in the Tale 2.1 (part 1)

Purpose: By default, System.out.println() will print out a number in its decimal base. It does so regardless of the base number system that you use to initialize a number.

Answer: The code prints the following output:

534
534

Explanation: Often programmers are tricked by similar questions. If a variable is assigned a value using 0b100001011 (a number in the binary number system), a programmer might believe that System.out.println() will print out numbers in the binary number system, which is incorrect. By default, System.out.println() will print out a number in its decimal base. All four variables baseDecimal, octVal, hexVal, and binVal represent the decimal value 267 in the decimal, octal, hexadecimal, and binary number systems. The addition operation adds these values and prints 534 twice.

You can use a method from the class Integer to print out a value in the binary number system as follows:

System.out.println(Integer.toBinaryString(0b100001011));

Note that the class Integer isn’t on this exam and you won’t be asked any questions on it. This class is mentioned only for your reference.

A.2.2 Twist in the Tale 2.1 (part 2)

Purpose: A new Java 7 language feature is the use of the underscore in literal number values. This exercise’s purpose is to help you get familiar with this feature if you haven’t worked with underscores in literal number values before.

Answer: Only var1, var6, and var7 correctly define a literal integer value.

Explanation: The literal value 0_x_4_13 defined by var2 is incorrect because it uses underscores after the starting 0 and after the letter x, neither of which is allowed. The correct value is 0x4_13.

The literal value 0b_x10_BA_75 defined by var3 is incorrect. You can’t place an underscore right after the prefixes 0b and 0B that are used to define binary literal values. Also, a binary value can contain only the digits 1 and 0.

The literal value 0b_10000_10_11 defined by value var4 is incorrect. You can’t place an underscore right after the prefixes 0b and 0B used to define binary literal values. The correct value is 0b10000_10_11.

The literal value 0xa10_AG_75 defined by var5 is incorrect because it uses the letter G, which isn’t allowed in a hexadecimal number system. A correct value is 0xa10_A_75.

The literal integer defined by var1 is valid. But 0 (for octal literals) is an exception to the rule stating that a radix prefix can’t be isolated by an underscore (for example, 0x_100_267_760 and 0b_100_110 are invalid expressions).

A.2.3 Twist in the Tale 2.2

Purpose: To reinforce the following concepts:

  • Multiple variables of the same type can be defined on the same line of code.
  • Variable assignment rule: if multiple variables of similar types are assigned values on the same line, assignment starts from right to left. Also, unlike other programming languages such as C, the literal value 0 can’t be assigned to a variable of type boolean.
  • Questions that ask you to select incorrect answers or code can be confusing. It’s common to start by determining the incorrect options and then selecting the correct options. Make note of such questions.

Answer: a, b, c, e

Explanation: Options (a) and (b) are incorrect statements. You can define multiple variables of the same type on the same line. Also, you can assign values to variables of compatible types on the same line of code. Assignment starts from right to left. For proof, the following lines of code will compile:

int int1;
long long2;
long2 = int1 = 10;

But the following lines of code won’t compile:

int i1;
long l2;
int1 = long2 = 10;

In the final line of the preceding code, a literal value 10 is assigned to the variable long2 of type long, which is acceptable. An attempt to assign the value of the variable long2 to int1 fails because it would need an explicit cast.

Option (c) is an incorrect statement because a literal value 0 can’t be assigned to a variable of type boolean.

Option (d) is a correct statement.

Option (e) is an incorrect statement. The code doesn’t define a variable with the name yes and thus seems to treat it like a literal value. Java doesn’t define a literal value yes, so the code doesn’t compile.

A.2.4 Twist in the Tale 2.3

Purpose: The exercise encourages you to

  • Try code with increment and decrement postfix and prefix unary operators
  • Get the hang of how variables are evaluated in an expression that has multiple occurrences of unary operators in postfix and prefix notation

Answer: 32

Explanation: The actual task is to evaluate the following expression:

int a = 10;
a = ++a + a + --a - --a + a++;
System.out.println(a);

This is the actual task because the question asks you to replace all occurrences of ++a with a++, --a with a--, and vice versa. This expression is depicted in figure A.1:

Figure A.1. Evaluation of an expression that has multiple unary operators in postfix and prefix notation

A.2.5 Twist in the Tale 2.4

Purpose: To determine whether the operands of an expression that uses the short-circuit operators && and || will evaluate.

Answer: The operands that will execute are circled and the ones that won’t are enclosed in rectangles in figure A.2.

Figure A.2. In an expression that uses the short-circuit operators && and ||, the operands that are evaluated are circled and the ones that aren’t evaluated are enclosed in rectangles.

Explanation: Both of the short-circuit operators, && and ||, will evaluate their first operand. For the short-circuit operator &&, if the first operand evaluates to false, it won’t evaluate the second operator. For the short-circuit operator ||, if the first operand evaluates to true, it won’t evaluate the second operator.

For the expression (a++ > 10 || ++b < 30), because a++ > 10 evaluates to false, both operands will evaluate.

For the expression (a > 90 && ++b < 30), because a > 90 evaluates to false, the second operand won’t execute.

For expression (!(c > 20) && a == 10), because !(c > 20) evaluates to false, the second operand won’t execute.

The expression (a >= 99 || a <= 33 && b == 10) has three operands together with the OR (||) and AND (&&) short-circuit operators. Because the short-circuit operator AND has higher operator precedence than the short-circuit operator OR, the expression is evaluated as follows:

(a >= 99 || (a <= 33 && b == 10))

Evaluation of the preceding expression starts with the evaluation of (a <= 33 && b == 10). Because a <= 33 evaluates to true, the operator && evaluates the second operand (b == 10) to determine whether (a <= 33 && b == 10) will return true or false. a <= 33 returns true and b == 10 returns false, so the expression (a <= 33 && b == 10) returns false.

The original expression—(a >= 99 || (a <= 33 && b == 10))—is now reduced to the following expression:

(a >= 99 || false)

The short-circuit operator OR (||) executes its first operand (even if the value of the second operand is known), evaluating a >= 99. So for this expression, all three operands are evaluated.

The expression (a >= 99 && a <= 33 || b == 10) also has three operands, together with OR and AND short-circuit operators. Because the short-circuit operator AND has a higher operator precedence than the short-circuit operator OR, this expression is evaluated as follows:

((a >= 99 && a <= 33) || b == 10 )

a >= 99 evaluates to false, so the next operand (a <= 33) isn’t evaluated. Because the first operand to operator ||, a >= 99 && a <= 33), evaluates to false, b == 10 is evaluated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.126