0
0
Android Kotlinmobile~20 mins

Passing arguments in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Argument Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Passing arguments between Android fragments
You want to pass a string argument from FragmentA to FragmentB using a Bundle. Which code snippet correctly sets the argument in FragmentA before navigation?
Android Kotlin
val bundle = Bundle()
bundle.putString("userName", "Alice")
val fragmentB = FragmentB()
fragmentB.arguments = bundle
// Navigate to fragmentB
A
val bundle = Bundle()
bundle.putString("userName")
val fragmentB = FragmentB()
fragmentB.arguments = bundle
B
val fragmentB = FragmentB()
fragmentB.arguments?.putString("userName", "Alice")
C
val bundle = Bundle()
bundle.putInt("userName", 123)
val fragmentB = FragmentB()
fragmentB.arguments = bundle
D
val bundle = Bundle()
bundle.putString("userName", "Alice")
val fragmentB = FragmentB()
fragmentB.arguments = bundle
Attempts:
2 left
💡 Hint
Remember to use putString with both key and value, and assign the bundle to the fragment's arguments.
📝 Syntax
intermediate
1:30remaining
Correct syntax for passing arguments in Kotlin function
Which option shows the correct way to define a Kotlin function that takes a string argument and prints it?
Android Kotlin
fun greet(name: String) {
    println("Hello, $name")
}
A
fun greet(name) {
    println("Hello, $name")
}
B
fun greet(name: String) {
    println("Hello, $name")
}
C
fun greet(name: String) -> Unit {
    println("Hello, $name")
}
D
fun greet(String name) {
    println("Hello, $name")
}
Attempts:
2 left
💡 Hint
In Kotlin, parameter types come after the name separated by a colon.
lifecycle
advanced
2:00remaining
Accessing passed arguments in Fragment lifecycle
Where is the best place to safely retrieve arguments passed to a Fragment in Android?
AInside the onCreate() method using requireArguments()
BInside the onCreateView() method using getArguments() without null check
CInside the onAttach() method using arguments!!
DInside the onDestroyView() method using arguments
Attempts:
2 left
💡 Hint
Arguments are available early in the fragment lifecycle but before view creation.
navigation
advanced
2:30remaining
Passing arguments using Safe Args in Navigation Component
Using Android Navigation Component with Safe Args, how do you pass an integer argument named userId from FragmentA to FragmentB?
A
val action = FragmentBDirections.actionFragmentBToFragmentA(userId = 42)
findNavController().navigate(action)
B
val bundle = Bundle()
bundle.putInt("userId", 42)
findNavController().navigate(R.id.fragmentB, bundle)
C
val action = FragmentADirections.actionFragmentAToFragmentB(userId = 42)
findNavController().navigate(action)
DfindNavController().navigate(R.id.fragmentB, bundleOf("userId" to 42))
Attempts:
2 left
💡 Hint
Safe Args generates directions classes with methods to pass arguments safely.
🔧 Debug
expert
3:00remaining
Why does this argument passing code crash?
Given this code in FragmentB, why does the app crash with a NullPointerException? val userName = arguments?.getString("userName")!! textView.text = userName
ABecause arguments is null and the !! operator forces a crash
BBecause getString("userName") returns an empty string which cannot be assigned
CBecause textView is not initialized before setting text
DBecause getString("userName") returns an integer, not a string
Attempts:
2 left
💡 Hint
Check if arguments Bundle is actually set before accessing it with !!