-
Notifications
You must be signed in to change notification settings - Fork 1
Module 2 Mastery Check
wolvesled edited this page Aug 29, 2013
·
1 revision
- Why does Java strictly specify the range and behavior of its primitive types? Answer: for portability, security, and less programming errors.
- 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.
- 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".
- Given this output, \ One \ Two \ Three \ using a single string, show the println() statement that produced it. Answer: System.out.println("One\nTwo\nThree");
- 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.
- 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.
- Show how a short-circuit AND can be used to prevent a divide-by-zero error. Answer: (d != 0) && (i / d)
- In an expression, what type are byte and short promoted to? Answer: int
- 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.
- Write a program that finds all of the prime numbers between 1 and 100.
- Does the use of redundant parentheses affect program performance? Answer: no, the compiler will take care of it.
- Does a block define a scope? Answer: yes