0
0
Javaprogramming~10 mins

Type casting in Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Afloat
Bint
Cdouble
Dlong
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong type in the cast, like int or float.
Forgetting the parentheses around the type.
2fill in blank
medium

Complete 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'
Adouble
Bint
Cfloat
Dlong
Attempts:
3 left
💡 Hint
Common Mistakes
Using (double) instead of (int).
Expecting rounding instead of truncation.
3fill in blank
hard

Fix 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'
Along
Bfloat
Cdouble
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to the same type (long) instead of int.
Forgetting to cast and causing a compile error.
4fill in blank
hard

Fill 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'
AInteger
BString
Cint
DDouble
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.
5fill in blank
hard

Fill 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'
Adouble
C2.0
Dint
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.