212 Summary for 9/18/03

Lecture Summary for 212 (Barat) on 9/18/03

 

Review Questions

  1. How many bytes do these datatypes have?
    byte, short, float, double, char
    Answer: 1, 2, 4, 8, 2

  2. What is wrong with this Java initialization?
    long population = 6000000000;
    Answer: The constant should have the long suffix L:
    long population = 6000000000L;

  3. What is the difference between short circuit operators and complete evaluation operators?
    Answer: In short circuit evaluation, if the first argument of logical and or logical or is sufficient to determine the answer, the second term is not evaluated. If complete evaluation, both terms of logical and or logical or are always evaluated.

    Here are two examples of a java statements that use short circuit evaluation:
    if (p != null && p.getGender() == 'F')

    if (i > 0 && i < a.length && a[i] == 0)

  4. What is the output?
    int x = 3;
    int y = 7;
    int z;
    z = (++x) + (y++);
    System.out.println(x + " " + y + " " + z);
    Answer: 4 8 11

  5. What is the output?
    int x = 3;
    int y = 7;
    System.out.println(x + " " + y + " " + (x++ > 3 && y-- > 7));
    Answer: 4 7 false

  6. What happens if the && is changed to & in the preceding problem?
    Answer: The output then becomes 4 6 false

  7. What is 13 in binary and hex?
    1101 and d.

  8. What is 1010 in hex and decimal?
    a and 10.

  9. What is f in binary and decimal?
    1111 and 15.

  10. Convert the byte variable 10101101 to decimal and hex.
    Answer: It is 23 + 22 + 20 = 8 + 4 + 1 = 13 and 0d in hex.

  11. Convert the short variable 0000000100010101 to decimal and hex.
    Answer: It is 24 + 22 + 20 = 16 + 4 + 1 = 21 in decimal and 0015 in hex.

  12. Is this int variable odd or even?
    00011110011001101110010010111010
    Answer: It ends with a 0 so it is even.

  13. Convert 109 to one byte binary and hex.
    Answer: 109 - 64 = 45, 45 - 32 = 13 --> 109 = 01101101 binary = 6d hex.

  14. Convert 263 to two byte binary and hex.
    Answer: 263 - 256 = 7 --> 0000000100000111 binary = 0107 hex.

  15. Carry out these bitwise operations:
    0x7d & 0x36     0x7d | 0x36     0x7d ^ 0x36
    Answers:
    0x7d & 0x36 = 01111101 & 00110110 = 00110100 = 0x34
    0x7d | 0x36 = 01111101 | 00110110 = 01111111 = 0x7f
    0x7d ^ 0x36 = 01111101 ^ 00110110 = 01011011 = 0x4b

  16. How do you print the int variable x in hex?
    Answer: System.out.println(Integer.toHexString(x));
  17. How do you display the message "Hello." in an input dialog?
    Answer: JOptionPane.showMessageDialog(null, "Hello.");

  18. How do you find an input dialog or message dialog when it is hidden behind other windows?
    Answer: Use Alt-Tab.

  19. How does a do loop differ from a while loop?
    Answer: A do loop checks the condition at the end of the loop so the loop always executes at least once.

  20. How does an if statement differ from a switch statement?
    Answer: A switch statement compares an expression to a sequence of constant values. An if..else statement checks a sequence of conditions, which may be more general than the checks in the switch statement.

  21. What are amicable numbers?
    Answer: n and m are amicable numbers if the sum of the factors of n is m and the sum of the factors of m is n.

 

Motivation for Bitwise Operations

 

Review of Arrays

 

Review of Classes

 

UML (Unified Modeling Language) Diagrams

 

The DecimalFormat Class

 

Arrays of Objects

 

Collection Classes

 

The QuickPick Project