A. What are reserved words? What are identifiers? What are some of the basic conventions of identifiers? Answer:
1. Words such as class, public, static, void , and main have specific meanings in Java, they called reserved words or keywords.
2. An identifier is a name for a variable, parameter, constant, user-defined method etc. In Java an identifier is any sequence of letters, digits, and the underscore
3. Identifiers may not begin with a digit; Identifiers are case-sensitive; Wherever possible identifiers should be concise and self-documenting. A variable called "area" is more illuminating than one called "a".
4. By convention identifiers for variables and methods are lowercase. Uppercase letters are used to separate these into multiple words, for example getName, findSurfaceArea, preTaxTotal, and so on. Note that a class name starts with a capital letter. Reserved words are entirely lowercase and may not be used as identifiers. B. What are primitive types? How are integers stored in computer? Answer:
1. Java has 8 primitive types: byte, short, int, long, float, double, boolean and char
2. Integer values in Java are stored exactly , as a string of bits (binary digits 0 or 1). One of the bits stores the sign of the integer, 0 for positive, 1 for negative.
3. The primitive type byte uses one byte (eight bits) of storage; int uses 4 bytes, short uses 2 bytes and long uses 8 bytes. C. How are floating-point number stored? What is mantissa? What are compounded round-off errors? Answer:
1. A floating-point number is stored in two parts using scientific notation: a mantissa, which specifies the digits of the number, and an exponent.
2. In type double, there are 64 bits in total. 11 bits are allocated for the exponent, and (typically) 52 bits for the mantissa. 1 bit is allocated for the sign. This is a double-precision number.
3. When floating-point numbers are converted to binary, most cannot be represented exactly, leading to round-off error . These errors are compounded by arithmetic operations. D. What is the best way to compare two double floating-point numbers? Answer:
Because of round-off errors, when comparing floating-point numbers such as x and y, you should check the relative difference instead of x==y or x!=y. Traditionally, if |x-y| is "small enough" (typically |x-y| <= 10^ -16 * max(|x|, |y|)), it is considered x==y. E. What is the difference among NaN, Infinity and -Infinity? Answer:
1. NaN, “not a number.” means undefined result. Examples of operations that produce NaN are: taking the square root of a negative number, or 0.0 divided by 0.0.
2. An operation that gives an infinitely large or infinitely small number, like division by zero, produces a result of Infinity or -Infinity F. What are final variables? Answer
1. A final variable identified by the keyword final , is used to name a quantity whose value will not change once assigned.
2. Here are some examples of final declarations:
final double TAX_RATE = 0.08;
final int CLASS_SIZE = 35;
final double TAX_RATE;
if (< some condition >)
TAX_RATE = 0.08;
TAX_RATE = 0.0;
// TAX_RATE can be assigned a value just once : its value is final! G. What are prefix for binary, hexadecimal and octal literals? How to convert them ? Answer:
1. 0b or 0B is the prefix to indicate binary literals; 0x or 0X is the prefix to indicate hexadecimal literals; 0 is the prefix for octal literals. (Literals are constants)
2. To convert between base 2 and base 10, see here
3. To convert among base 2, base 8 and base 16, see here H. Practice of the Week: 1. 2. 3. Q18-20 of Chapter 2 in Barron's (edition 8)