Beruflich Dokumente
Kultur Dokumente
Assessment of Lesson 2
Exercise 1. Which of the following identifiers are valid? Which are Java keywords? Yellow = identifiers
Blue = keywords
applet, Applet, a++, --a, 4#R, $4, #44, apps, class, public, int, x, y, radius
Exercise 2. What are the benefits of using constants? Declare an int constant SIZE with value 20.
So you don’t have to type a variable time and time again
Exercise 3. Assume that int a = 1 and double d = 1.0, and that each expression is independent. What
are the results of the following expressions:
1. a = 46 / 9; a=5
2. a = 46 % 9 + 4 * 4 – 2; a = 15
3. a = 45 + 43 % 5 * (23 * 3 % 2); a = 48
4. a %= 3 / a + 3; a=1
5. d = 4 + d * d + 4; d = 9.0
6. d += 1.5 * 3 + (++a); d = 7.5, a = 2
7. d -= 1.5 * 3 + a++; d = -4.5, a = 2
Exercise 5. Find the largest and smallest byte, short, int, long, float, and double. Which of
these data types requires the least amount of memory? How many bytes each of these types need?
Byte: -128 to 127. Requires the least amount of memory. 8 bit signed
Short: -32768 to 32767. 16 bit signed
Int: −231 𝑡𝑜 231 − 1 32 bit signed
Long: −263 𝑡𝑜 263 64 bit signed
Float: −3.402823538 𝑡𝑜 3.402823538 32 bit IEEE 754
Double: −1.7976931348623157308 𝑡𝑜 1.7976931348623157308 64 bit IEEE 754
Exercise 6. What is the result of 25/4? How would you rewrite the expression if you wished the result to
be a floating-point number?
6 float a = 25/4
Exercise 7. Are the following statements correct? If so, show the output.
Exercise 8. Suppose m and r are integers. Write a Java expression for mr2 to obtain a floating-point result.
Exercise 10. Identify and fix the syntax errors in the following snippet of Java code:
public class Test {
public void main(string [] args) {
int i; (i isn’t initialized)
int k = 100.0;
int j = i + 1;
Sytem.out.println("j is " + j + " and
k is " + k);
}
}