📖 Scenario: You are building a simple Kotlin program that stores different types of items in a list. Since the items are of different types, you will use Kotlin's Any type, which can hold any kind of value.
🎯 Goal: Create a Kotlin program that uses the Any type as a universal base to store and print different types of values.
📋 What You'll Learn
Create a list called items that can hold values of any type
Add at least three different types of values to the items list
Create a variable called count to store the number of items
Use a for loop to print each item in the items list
💡 Why This Matters
🌍 Real World
In real apps, you often need to store or handle data of different types together. Kotlin's <code>Any</code> type lets you do this safely and easily.
💼 Career
Understanding <code>Any</code> helps you work with generic data, APIs, and collections that hold mixed types, which is common in software development.
Progress0 / 4 steps
1
Create a list with different types using Any
Create a list called items that holds these exact values: 42 (an integer), "Hello" (a string), and true (a boolean). Use the Any type for the list.
Kotlin
Hint
Use val items: List<Any> = listOf(42, "Hello", true) to create the list.
2
Add a variable to count the number of items
Create a variable called count and set it to the size of the items list.
Kotlin
Hint
Use val count = items.size to get the number of items.
3
Use a for loop to print each item
Use a for loop with the variable item to go through items and print each item.
Kotlin
Hint
Use for (item in items) { println(item) } to print each item.
4
Print the total count of items
Print the text "Total items: " followed by the value of count using a single println statement.
Kotlin
Hint
Use println("Total items: $count") to print the count.
Practice
(1/5)
1. In Kotlin, which type is the universal base type that can hold any kind of value?
easy
A. Universal
B. Object
C. Base
D. Any
Solution
Step 1: Understand Kotlin's type hierarchy
Kotlin uses Any as the root of its type system, similar to Object in Java.
Step 2: Identify the universal base type
Any can hold any non-null value, making it the universal base type in Kotlin.
Final Answer:
Any -> Option D
Quick Check:
Universal base type = Any [OK]
Hint: Remember: Kotlin's root type is called Any [OK]
Common Mistakes:
Confusing Any with Object (Java's base type)
Thinking Base or Universal are Kotlin types
Assuming Any can hold null (it cannot)
2. Which of the following is the correct way to declare a variable of type Any in Kotlin?
easy
A. val x: Any = 10
B. val x = Any(10)
C. val x: Any() = 10
D. val x: any = 10
Solution
Step 1: Review Kotlin variable declaration syntax
Variables are declared with val or var, followed by a colon and the type.
Step 2: Check correct usage of Any type
Any is a type, not a constructor, so you assign values directly without parentheses.
Final Answer:
val x: Any = 10 -> Option A
Quick Check:
Declare with colon and type name = val x: Any = 10 [OK]
Hint: Use colon for type, no parentheses for Any [OK]
Common Mistakes:
Trying to call Any as a constructor
Using lowercase 'any' instead of 'Any'
Adding parentheses after type name
3. What will be the output of this Kotlin code?
fun main() {
val a: Any = "Hello"
val b: Any = 123
println(a)
println(b)
}
medium
A. Hello
123
B. Any
Any
C. Compilation error
D. null
null
Solution
Step 1: Understand variable assignments
Variable a holds a string "Hello" and b holds an integer 123, both typed as Any.
Step 2: Analyze the print statements
Printing a and b calls their toString() method, outputting "Hello" and "123" respectively.
Final Answer:
Hello
123 -> Option A
Quick Check:
Any holds values, prints their string form [OK]
Hint: Any prints actual value's string, not type name [OK]
Common Mistakes:
Expecting type name 'Any' to print
Thinking it causes compile error
Assuming null prints for Any
4. Identify the error in this Kotlin code snippet:
fun main() {
val x: Any = null
println(x)
}
medium
A. Missing semicolon after println
B. println cannot print Any type
C. Cannot assign null to a variable of type Any
D. Variable x must be declared as var, not val
Solution
Step 1: Check type nullability
Any in Kotlin is non-nullable by default, so it cannot hold null.
Step 2: Identify assignment error
Assigning null to x of type Any causes a compile-time error.
Final Answer:
Cannot assign null to a variable of type Any -> Option C
Quick Check:
Any is non-nullable, null assignment fails [OK]
Hint: Any disallows null; use Any? for nullable [OK]
Common Mistakes:
Thinking println can't print Any
Expecting semicolons in Kotlin
Confusing val and var for nullability
5. Given a list of mixed types, how can you create a new list containing only the string representations of elements using Any as the base type?
val mixedList: List<Any> = listOf(1, "two", 3.0, true)
Which Kotlin code correctly produces List<String> with string forms of all elements?
hard
A. val stringList = mixedList.filterIsInstance<String>()
B. val stringList = mixedList.map { it.toString() }
C. val stringList = mixedList.map { it as String }
D. val stringList = mixedList.filter { it is String }
Solution
Step 1: Understand the goal
We want a list of strings representing all elements, not just the strings themselves.
Step 2: Analyze each option
val stringList = mixedList.map { it.toString() } maps each element to its string form, producing List<String>. val stringList = mixedList.filterIsInstance<String>() filters only strings, losing other elements. val stringList = mixedList.map { it as String } tries unsafe cast causing runtime error. val stringList = mixedList.filter { it is String } filters strings but returns List<Any>, not List<String>.
Final Answer:
val stringList = mixedList.map { it.toString() } -> Option B
Quick Check:
Map all to string form for List<String> [OK]
Hint: Use map with toString() to convert all elements [OK]
Common Mistakes:
Using filterIsInstance<String> loses non-string elements
Casting with 'as String' causes runtime errors
Filtering returns List<Any>, not List<String>
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.