Task 1.1.3 Practice Exercises – Variables, types and casting
- Give appropriate declarations (type and name) for variables used to store the following values:
- a.An employee’s taxable salary e) The average rainfall for the past month
- The month number within a year f) A letter to hold an answer (‘y’ or ‘n’)
- An employee identification number g) The constant string “Hello”
- The capacity of a tank in cubic inches h) The conversion rate from $AUS to $US
- Assuming that the types of the variables x, y, and z are int, float and double respectively, which of the following statement(s) are likely to result in error ?
- a) x = y; b) x = z; c) y = x;
- d) y = z; e) z = x; f) z = y;
- The following code segment prints an incorrect result (instead of the expected result of sqr = 10000000000 ).
int num1 = 100000; int sqr = num1 * num1;
System.out.println(“sqr = ” + sqr);
Describe the problem / error that has occurred and suggest a solution or work-around to the problem which will allow the correct result to be stored and printed.