Unit 1 Lesson 5 Homework

Part A: Core Understanding

  1. Determine the result of each cast.
    a. (int) 8.7
    b. (int) -6.9
    c. (double) 12



  1. Determine the value of each expression.
    a. (int) 6.8 * 3
    b. (int) (6.8 * 3)



  1. Describe what happens when an int calculation goes above Integer.MAX_VALUE.



  1. Identify why a double calculation such as 0.1 + 0.2 may contain a small precision error.



Part B: Read and Trace

  1. Determine the exact output.
int points = 14;
points += 6;
points /= 4;
points *= 3;

System.out.println(points);



  1. Determine the exact output.
int lives = 5;
lives--;
lives++;
lives++;

System.out.println(lives);



Part C: Find and Fix

  1. Correct the cast placement so result stores 15.
double value = 7.8;
int result = (int) value * 2;




  1. Rewrite both updates using compound assignment operators.
score = score + 8;
score = score % 5;




Part D: Write Code

  1. Write one statement that rounds the positive double variable price to the nearest int and stores the result in roundedPrice. A complete main method is not required.





  1. Write the three assignment statements needed to swap the values of left and right without losing either value.