0
0
Android Kotlinmobile~20 mins

File access and storage in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Access Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Reading a file from internal storage
What will be the output shown in the TextView after running this code snippet that reads a file named "data.txt" from internal storage?
Android Kotlin
val fileInput = openFileInput("data.txt")
val content = fileInput.bufferedReader().use { it.readText() }
textView.text = content
AThe exact text content stored inside "data.txt" is displayed in the TextView
BThe TextView remains empty because openFileInput returns null
CThe TextView shows the file path of "data.txt"
DThe app crashes with FileNotFoundException if "data.txt" does not exist
Attempts:
2 left
💡 Hint
openFileInput reads the file content as a stream, which you convert to text.
lifecycle
intermediate
2:00remaining
File writing in Activity lifecycle
If you write data to a file in the onPause() method of an Activity, what happens if the app is killed before onPause() is called?
AThe data is saved in onCreate() instead
BThe data will not be saved because onPause() was never called
CThe data is saved only if onStop() is called
DThe data is saved automatically by the system
Attempts:
2 left
💡 Hint
onPause() is called before the Activity goes to background, but not if the app is killed abruptly.
📝 Syntax
advanced
2:00remaining
Correct syntax for writing text to a file
Which Kotlin code snippet correctly writes the string "Hello World" to a file named "output.txt" in internal storage?
AFile("output.txt").writeBytes("Hello World")
BopenFileOutput("output.txt", Context.MODE_PRIVATE).writeText("Hello World")
CopenFileOutput("output.txt", Context.MODE_PRIVATE).use { it.write("Hello World".toByteArray()) }
DopenFileOutput("output.txt").write("Hello World")
Attempts:
2 left
💡 Hint
openFileOutput returns a FileOutputStream which you can write bytes to.
🔧 Debug
advanced
2:00remaining
Diagnosing a FileNotFoundException
An app crashes with FileNotFoundException when trying to read "config.txt" using openFileInput("config.txt"). What is the most likely cause?
A"config.txt" does not exist in the app's internal storage
BThe app lacks permission to read external storage
CThe file path is incorrect because openFileInput requires absolute path
DThe file is open in write mode elsewhere causing a lock
Attempts:
2 left
💡 Hint
openFileInput only reads files previously saved in internal storage by the app.
🧠 Conceptual
expert
2:00remaining
Choosing storage for sensitive data
Which storage option is best for saving sensitive user data like passwords in an Android app?
AExternal storage public directory
BSharedPreferences without encryption
CCache directory accessible by other apps
DInternal storage files with MODE_PRIVATE access
Attempts:
2 left
💡 Hint
Sensitive data should be stored where only your app can access it.