Skip to content

Module 2 Mastery Check

wolvesled edited this page Aug 29, 2013 · 1 revision
  1. Why does Java strictly specify the range and behavior of its primitive types? Answer: for portability, security, and less programming errors.
  2. What is Java's character type, and how does it differs from the character type used by many other programming languages? Answer: char, unlike in C and c++ which store in byte. In java, char is stored as unicode but compatible with ansi when come to char calculation.
  3. A boolean value can have any value you like because any non-zero value is true. True or False? Answer: False, in Java it can only store keyword "true" or "false".
  4. Given this output, \ One \ Two \ Three \ using a single string, show the println() statement that produced it. Answer: System.out.println("One\nTwo\nThree");
  5. What is wrong with this fragment? for(i = 0; i < 10; i++) {int sum; sum = sum + 1;} System.out.println("Sum is: " + sum); Answer: sum is called outside the scope of the block where it's lifetime ended when exit it.
  6. Explain the difference between the prefix and postfix forms of the increment operator. Answer: prefix increment returns the value after the increment operation, postfix increment returns the value before the increment operation.
  7. Show how a short-circuit AND can be used to prevent a divide-by-zero error. Answer: (d != 0) && (i / d)
  8. In an expression, what type are byte and short promoted to? Answer: int
  9. In general, when is a cast needed? Answer: convert from incompatible types or narrowing conversion from one type to another. e.g. from double to int or from int to char is needed of casting.
  10. Write a program that finds all of the prime numbers between 1 and 100.
  11. Does the use of redundant parentheses affect program performance? Answer: no, the compiler will take care of it.
  12. Does a block define a scope? Answer: yes

Clone this wiki locally