ch02Sections1-5

# ch02Sections1-5 - the result of 7.5 3 is a double 36...

This preview shows pages 1–2. Sign up to view the full content.

2.7 Exercises, Problems, and Projects 2.7.1 Multiple Choice Exercises 1. int a; 2. a, sales, inter, doubleSales, TAX_RATE 3. a = b; a = 67; c = a – b; a /= 4; a += c; 2.7.2 Reading and Understanding Code 4. 12.5 5. 6 6. 13.0 7. 2.0 8. 2 9. 3 10. 4 11. 1 12. 7 13. 5 14. 2.4 15. 2 16. 2.4 17. 2.0 18. 6 19. 5 20. 4 21. 7 22. 0 2.7.3 Fill in the Code 23. float a; a = 34.2f; 24. a = 10; 25. boolean a; a = false; 26. char a; a = 'B'; 27. int sum = a + b + c; System.out.println( "The sum is " + sum ); 28. double avg = ( double ) ( a + b ) / 2; AF_CH02 Page 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
System.out.println( "The average is " + avg ); 29. int rem = a % b; System.out.println( "The remainder is " + rem ); 30. a++; 31. a *= 3; 32. b = a + 3 – 7; b = a * 4; a = b * b; a = b * 3 * 5; b = a / 2; b = a % 3; 2.7.4 Identifying Errors in Code 33. cannot assign a double to an int variable (possible loss of precision) 34. cannot assign a double to a float variable (possible loss of precision) 35. cannot assign a double to an int variable (possible loss of precision);
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the result of 7.5 % 3 is a double 36. ArithmeticException division by 0 (run time error) 37. there should not be a space between – and = 38. no error. A constant decimal value between 0 and 65535 can be assigned to a char. 39. yes; cannot assign an int value to a boolean variable. 2.7.5 Debugging Area – Using Messages from the Java Compiler and the Java JVM 40 - cannot assign a double to an int variable (possible loss of precision). Change to: double a = 26.4; 41. Missing semicolon at the end of the statement. Change to int a = 3; 42. we are performing integer division, then typecasting to a double. That is why the result is 3.0 and not 3.2. In order to obtain 3.2, change the third statement to double c = ( double ) a / b; 43. =+ is different from += (shortcut operator). Here, a is assigned the value + 3. To add 3 to a, change the second statement to: a += 3; AF_CH02 Page 2...
View Full Document

## This note was uploaded on 10/05/2011 for the course CS 38 taught by Professor Stevehouseman during the Summer '09 term at Irvine Valley College.

### Page1 / 2

ch02Sections1-5 - the result of 7.5 3 is a double 36...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online