Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert an integer to a double.
Java
int num = 5; double result = ([1]) num; System.out.println(result);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong type in the cast, like int or float.
Forgetting the parentheses around the type.
✗ Incorrect
We use (double) to cast an integer to a double type in Java.
2fill in blank
mediumComplete the code to cast a double to an integer.
Java
double pi = 3.14; int whole = ([1]) pi; System.out.println(whole);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
(double) instead of (int).Expecting rounding instead of truncation.
✗ Incorrect
To convert a double to an int, use (int) cast. This drops the decimal part.
3fill in blank
hardFix the error in casting a long to an int.
Java
long bigNumber = 100000L; int smaller = ([1]) bigNumber; System.out.println(smaller);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to the same type (long) instead of int.
Forgetting to cast and causing a compile error.
✗ Incorrect
To cast a long to an int, use (int). Casting to long again is incorrect.
4fill in blank
hardFill both blanks to create a dictionary with string keys and integer values using casting.
Java
Map<String, Integer> map = new HashMap<>(); String key = "age"; Object value = 30; map.put(key, ([1]) value); int age = ([2]) map.get(key); System.out.println(age);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to String instead of Integer.
Casting to Double which is incompatible.
Using int instead of Integer when putting into the map.
✗ Incorrect
Cast value to Integer when putting in the map, and cast the retrieved value to int when getting it.
5fill in blank
hardFill all three blanks to cast and calculate the average of two numbers.
Java
int a = 5; int b = 8; double average = (([1]) a + ([2]) b) / [3]; System.out.println(average);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by integer 2 causing integer division.
Not casting both numbers to double.
Casting to int which does not help here.
✗ Incorrect
Cast a and b to double to avoid integer division, then divide by 2.0 to get a double average.