Sei sulla pagina 1di 2

# Fundamentals I (Introduction in Java Programming Language), COSC-1336

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

## final int SIZE = 20;

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 4. Show the values of the following remainders:

58 % 6 =4
78 % -4 =2
-34 % 5 = -4
-34 % -5 = -4
5 % 1 =0
1 % 5 =1

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.

## System.out.println("25 / 4 is " + 25 / 4); yes. 6

System.out.println("25 / 4.0 is " + 25 / 4.0); yes 6.25
System.out.println("3 * 2 / 4 is " + 3 * 2 / 4); yes 1
System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4); yes 1.5
System.out.println("3f * 2 / 4 is " + 3f * 2 / 4); yes 1.5

Exercise 8. Suppose m and r are integers. Write a Java expression for mr2 to obtain a floating-point result.

## Exercise 9. Which of the following statements are true?

a) Any expression can be used as a statement;
b) The expression x++ can be used as a statement;
c) The statement x = x + 5 can be used as an expression;
d) The statement x = y = z = 0; is illegal.

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);
}
}