Crossword Solution

Crossword Solution

Chapter 2

Crossword Solution

Short Answers

  1. True or False. If false, give an explanation.
  1. Integer and decimal numbers can be mixed in an expression.

True

  1. Integers can be added to character data.

True

  1. Boolean data can be cast to integer data.

False – Boolean data is not 0 or 1, and cannot be cast to int.

  1. The relational operators cannot be used with character data.

False – They can. Characters are compared by ASCII value, which is alphabetically ordered.

  1. If only one decimal number is used with the / (division) operator, the result is an integer.

False – The result is float or double depending on the type of the decimal number. Two integers are needed to result in an integer.

  1. It makes no difference whether one uses 5 or 5.0 in a numerical expression.

False – It makes a great difference. The value 5 is int and the value 5.0 is double, so the type of the expression may depend on which is used.

  1. The decimal form of 23.00E6 is 2300.00.

False – No. It is 23000000.00.

  1. The argument to println must be a string.

False. Java specifies that the print and println methods accept a single argument of any type.

  1. Two boolean expressions may be compared using <.

False. The < operator cannot be applied to boolean operands

  1. The plus operator may be used with two strings.

True – the operation is concatenation.

  1. Playing Compiler

Evaluate each of the following expressions or determine that the expression is ill formed.

  1. 3 + 4.5 * 2 + 27 / 8

15.0

  1. true || false & 3 < 4 || !(5 == 7)

true

  1. true || (3 < 5 & 6 >= 2)

true

  1. !true > ‘A’

Ill formed

  1. 7 % 4 + 3 – 2 / 6 * ‘Z’

6

  1. ‘D’ + 1 + ‘M’ % 2 / 3

69

  1. 5.0 / 3 + 3/3

2.666666666666667

  1. 53 % 21 < 45 /18

false

  1. (4 < 6) || true & false || false & (2 > 3)

true

  1. 7 – (3 + 8 * 6 + 3) – (2 + 5 * 2)

-59

  1. Playing Compiler

Determine which of the following Java statements/segments are incorrect. If a statement is correct, give the output. If incorrect, explain why.

a.System.out.print ("May 13, 1988 fell on day number ");

May 13, 1988 fell on day number

b.System.out.println( ((13 + (13 * 3 - 1) / 5

+1988 % 100

+ 1988 % 100 / 4

+1988 / 400

- 2 * (1988 / 100)) % 7 + 7) % 7 );

5

c.System.out.print ("Check out this line ");

Check out this line

d.System.out.println( "//hello there " + '9' + 7 );

//hello there97

e.System.out.print( 'H' + 'I' + " is " + 1 + "more example");

145 is 1more example

f.System.out.print( 'H' + 6.5 + 'I' + " is " + 1 + "more example");

151.5 is 1more example

g.System.out.print("Print both of us", "Me too");

Incorrect: the “print” method in System.out expects one string, not two.

h.System.out.print( "Reverse " + 'I' + 'T' );

Reverse IT

i.System.out.print("No! Here is" + 1 + "more example");

No! Here is1more example

j.System.out.println ("Here is " + 10*10)) // that’s 100 ;

Incorrect: semi-colon should appear before the comment in place of the last close paren.

k.System.out.println("Not x is " + true); // that’s true.

Not x is true

l.System.out.print();

Incorrect: illegal number of arguments (must have a string argument).

m.System.out.println;

Incorrect: “println” is a method of System.out, not a field, and so needs () following it. Note, “println”, unlike “print”, may have no arguments, so System.out.println(); is correct..

n.System.out.print("How about this one" + '?' + 'Huh? ' );

Incorrect: ‘Huh?’ needs double quotes around it because it is a string. Single quotes cause the compiler to complain of an unexpected symbol.

  1. Playing Compiler

Find and correct the errors in the following program:

public class LeapYear;

{

public static void main(String args)

{

System.out.print("The year 2300 is a leap year? " + "True or false: ");

// (divisible by 4 and not by 100) or (divisible by 400) //

System.out.println( 2300 % 4 = 0 & 1800 % 100 !=0 || 1800 % 400 == 0);

}

Line 1: Extraneous semicolon at the end of “public class LeapYear”

Line 3: [] is needed after String

Line 7: “2300%4 =0” should be “2300%4 == 0”

The two “1800”s should each be “2300”.

Line 9:Missing the closing bracket at end of the program.

  1. Parentheses and Operator Precedence

Fully parenthesize each of the following expressions to reflect operator precedence.

a. 2+3+4+5

(((2+3)+4)+5)

  1. 3*4+5/6-7

(((3*4) + (5/6)) – 7)

  1. 2+3*4*5

(2+ ((3*4)*5))

  1. 9%2/2*3

(((9%2)/2)* 3)

  1. 7+6*4%2+3*5

((7 + ((6*4)%2)) + (3*5))

  1. true || false ||true & false

((true || false) || (true & true))

  1. Boolean Expressions

Compute the value of each of the following boolean expressions. Recall that Java uses short circuit evaluation. For each expression determine how much of the expression Java must evaluate to determine a value.

a. true & false &true || true

true (the second & is never evaluated)

b. true || true & true & false

true (the || is not evaluated)

c. (true & false) || (true & ! false) || (false & !false)

true (the second || and third & are not evaluated)

d. (2 < 3) || (5 > 2) & !(4 == 4) || 9 != 4

true (the first and second || are not evaluated)

e. 6 == 9 || 5 < 6 & 8 < 4 || 4 > 3

true (the whole expression is evaluated)

  1. Boolean Expressions

Write a Java boolean expression that models each of the following circuit diagrams. See Example 2.9.

(x || !y) & (!x & y || !x & !y)

(x & !y || !x) & (!x & y || x || !y)

  1. Playing Compiler

Determine which of the following expressions are valid. For each valid expression give the data type of the resulting value.

a. 27/13 + 4

6

b. 27/13 + 4.0

6.0

  1. 42.7%3 + 18

18.70000000000003

  1. (3<4) & 5/8

Invalid – incompatible types.

  1. 23/5 + 23/5.0

8.6

  1. 2.0 + ‘a’

99.0

  1. 2 + ‘a’

99

  1. ‘a’ + ‘b’

195

  1. ‘a’/’b’

0

  1. ‘a’ & !‘b’

Invalid - & operates only on boolean types

  1. (double)‘a’/’b’

0.9897959183673469

  1. DeMorgan’s Law

DeMorgan’s Laws for boolean expressions state that

!(a & b) is equivalent to !a || ! b, and

!(a || b) is equivalent !a & ! b

Use DeMorgan’s Laws to simplify the following boolean expressions:

a. !(a || !b)

!a & b

  1. !(!a & !b)

a || b

  1. !(!a || !b)

a & b

  1. !((a&b) || (!a & !b))

(!a || !b) & (a || b)

  1. What’s the Ourput?

Determine the output of the following program:

public class Memory

{

public static void main(String[] args)

{

System.out.print ("There once was a girl named Elaine\n");

System.out.print ("With a microchip lodged in her brain\n");

System.out.print ("\tHer friends were amazed\n");

System.out.print ("\tBedazzled and dazed\n");

System.out.print ("By the facts that Elaine could retain\n");

}

}

There once was a girl named Elaine

With a microchip lodged in her brain

Her friends were amazed

Bedazzled and dazed

By the facts that Elaine could retain

  1. What’s the Output?

Determine the values of each of the following Java expressions:

a. 7/3*2

4

b. 7/(3*2)

1

  1. 7.0/3*2

4.66666666666667

  1. 7/3*2.0

4.0

  1. 7/(3*2.0)

1.16666666666667

  1. 7.0/3.0*2.0

4.66666666666667

  1. (7/3)*2

4

  1. (7.0/3)*2

4.66666666666667

  1. Comments

Debugging a program can be a long and intricate process. Can you think of how you might use Java comments as an aid to debugging? What are some other ways that you might use Java comments?

Obviously it is helpful to use comments to clarify your intended meaning of a segment of code. The notes you leave will help others, and they will likely help you too after a number of months pass before you have to find a bug.

Also, comments can be used to “comment out” lines of code that you suspect are not relevant to the bug at hand, allowing you to run the program concentrating only on the offending lines. The “commented out” lines will not execute. After the bug is found the “comments” can be taken out, and the code reestablished

The Bigger Picture

Binary Encoding – ASCII

1. How many binary numbers are there with 4 bits? 10 bits? n bits?

16, 1024, 2n.

2. How many different characters are included in the ASCII coding scheme?

128

3. Many electronic devices, such as calculators and digital clocks, display no symbols other than the digits 0 through 9. With only ten possible symbols, the 7-bit ASCII code is overkill. We can use fewer than 7 bits per digit. How many bits can be used to encode a digit in a calculator or clock? Using this number ofbits per digit, how many digits can be stored in one byte? This type of encoding is called BCD, or binary coded decimal, and it is twice as compact as ASCII encoding.

4 bits per digit, and therefore two digits per byte.

4. How many different characters can be represented using the 16-bit Unicode system? Explain your answer.

2^16 = 65,536

5. Considering Hebrew, Arabic, Cyrillic, Greek, Sanskrit, and Kanji symbols,

conjecture whether the number of Unicode values is reasonably large enough to

include all the characters that humans use for written communication. Can you

think of any reasons for converting to a 64-bit character code?

60,000 is plenty for alphabets of all current languages except for Kanji symbols used in Chinese and Japanese writing. A complete catalog of all Kanji includes up to 80,000 synbols. Nevertheless, a typical person’s Kanji “vocabulary” is far less than this, and less than 7000 Kanji symbols are enough for any practical use. Hence, 16 bits will suffice for the indefinite future.

2^64 is such a huge number, that it is inconceivable we would ever need to store that many unique characaters or symbols.

6. Determine the ASCII code for the four-digit string "1026". Use one byte for each digit.

00110001 00110000 00110010 00110110

7. Determine the binary equivalent of the decimal number 1026.

10000000010

8. What are the advantages and disadvantages of storing a string of digits using ASCII rather than its binary number encoding?

ASCII requires more space, but is easier to decode for printing digits. Binary number encoding is more compact and allows for efficient arithmetic calculation and manipulation.

9. How do you think digits in a Java program are stored? For example, are digits that

are part of a class name stored in the same way as the digits that comprise an integer in an arithmetic expression? Use the following example to explain your answer.

The first two occurrences of 23478 are stored in 5 bytes using ASCII, the third occurrence is stored as the binary number equivalent of decimal 23478.

Binary Encoding – Decimal

1. Give the decimal equivalents of 1 011100, 1111111 , and 0 000000001011100.

92, 127, and 92.

2. Which characters are encoded by the ASCII and Unicode values in exercise 1?

backslash, DEL key, and the backslash.

3. Determine the 7-bit binary equivalent of 64.

1000000

4. What is the 16-bit binary equivalent of 10,000?

0010011100010000

Boolean Types

1. Is it true that if a, b , and c are boolean then

a || (b & c) is equivalent to (a || b) & (a || c)?

Yes. You can try all 8 true/false combinations for a,b, and c to confirm that the two expressions are equivalent.

2. Is it true that if a, b , and c are integers then

a + (b * c) is equivalent to (a + b) * (a + c)?

No, not in general. 3+(7*2) = 17 does not equal (3+7)*(3+2) = 50, for example.

3. The exclusive-or (XOR) operation on two Boolean operands is defined to be true whenever exactly one of the two operands is true . Write a Java boolean expression that calculates the exclusive-or of two boolean operands x and y .

(x || y) & !(x & y), or equivalently, (!x & y) || (x & !y)