# Review Exercises (Set A1)

1. Name the 6 numerical primitive data types in Java, and identify those whose values are stored only approximately in memory.

2. Which of the following are legal names for variables in Java?

1. 1stNum
2. num1
3. myChar_
4. $tempBoolean 5. while 3. The expression (4.2 % 0.25) + (42 % 5) evaluates to what value? 4. What does the following code print? public class Main { public static void main(String[] args) { int x = 5; int y = 3; System.out.println(x+y+"stuff"); System.out.println("stuff"+x+y); } }  5. (Fill in each blank with the appropriate word.) The following contains an example of a _______________ (widening or narrowing) conversion. The conversion shown is accomplished by ________________ (promotion, assignment, or casting). double d = 1.23; float f = (float) d;  6. Convert -39 to it’s 8-bit two’s complement form. 7. What does the following code print? public class Main { public static void main(String[] args) { int x = 3; int y = 5; int z = x++; y += --x; System.out.println(z); System.out.println(y); } }  8. If a character has value 76 when treated as an integer, how should it be referred to when using the ‘\u###’ form? 9. Convert the binary number 11100101101000010000111101110001 to hexadecimal. 10. What does the following code print? public class Main { public static void main(String[] args) { char c = 'W'; c = (char) ((( c - 'A' + 5) % 26) + 'A'); System.out.println(c); } }  11. Write a truth table for the following expression: (P ^ Q) && (! (P || R)) 12. What does the following code print? public class Main { public static void main(String[] args) { int x = 1; int y = 2; int z = 3; z = y += 1; System.out.println(z); System.out.println( (true || false) ? x : y); } }  13. What does the following code print? public class Main { public static void main(String[] args) { int x = 1 + 2 * 3; switch (x) { case 6: System.out.println("A"); case 7: System.out.println("B"); case 8: System.out.println("C"); case 9: System.out.println("D"); default: System.out.println("E"); } if (1 < 0) if (0 < 1) System.out.println("F"); else System.out.println("G"); System.out.println("H"); } }  14. What does the following print? public class Main { public static void main(String[] args) { boolean found = false; int i = 65; while (! found) { found = (i % 31 == 0); i++; } System.out.println(i); } }  15. What does the following print? public class Main { public static void main(String[] args) { int f = 1; double d = 2.345; System.out.printf("%.1f + %d \" %.2f",d,f,d); } }  16. What does the following print? public class Main { public static void main(String[] args) { for (int i = 0; i < 6; i++) { for (int j = i; j > 0; j--) System.out.print(j); System.out.println(); } } }  17. What does the following print? public class Main { public static void main(String[] args) { int c = 0; for (int i = 0; i < 100; i++) { if (i % 5 == 0) c++; } System.out.println(c); } }  18. How many times will the following print “buzz lightyear”? public class Main { public static void main(String[] args) { int x = 0; do { x++; System.out.println(“buzz lightyear”); } while (x > 0); } }  19. Suppose you generate a random double, x, and display it to the console. You then ask the user to type in the value he thinks will sum with this number to equal 1. Storing his response in a double, y, what is wrong with using the following code snippet as a check to see if he was correct? if (y == 1 - x) { System.out.println("You were correct!"); } else { System.out.println("Sorry. You were incorrect."); }  20. Who designed and attempted to build the “Difference Engine” and the “Analytic Engine” (the latter being the device the first programmer, Ada Byron, wrote her first program for…) ### Answers 1. numerical types: byte, short, int, long, float, double stored approximately: float, double 2. (b), (c), and (d); (the first choice is illegal as you can't start a variable name with a digit, the last is illegal as "while" is a reserved word) 3. 2.2 4. 8stuff stuff53  5. narrowing; casting 6.$11011001$7. 3 8  8. \u$\textrm{004C}$9.$\textrm{E5A10F71}$(16) 10. B  11. P Q R P^Q P||R !(P||R) (P^Q)&&(!(P||R)) T T T F T F F T T F F T F F T F T T T F F T F F T T F F F T T T T F F F T F T F T T F F T F T F F F F F F F T F  12. 3 1  13. B C D E H  14. 94  15. 2.3 + 1 " 2.35  16.  1 21 321 4321 54321  17. 20  18. While tempting to say "To infinity and beyond!", it actually prints exactly$2147483648\$ times
19. One has compared two doubles using the equality operator, which is inherently dangerous, as doubles are stored only approximately.
20. Charles Babbage