What is the difference between internal and external storage in Android?
Internal storage is private to the app and not accessible by other apps. External storage is shared and can be accessed by other apps and the user. External storage may be removable like an SD card.
Click to reveal answer
beginner
How do you write a text file to internal storage in Kotlin?
Use openFileOutput(filename, Context.MODE_PRIVATE) to get a FileOutputStream, then write bytes or strings to it. This saves the file privately for your app.
Click to reveal answer
intermediate
What permission is required to write to external storage on Android 10 and below?
You need the WRITE_EXTERNAL_STORAGE permission declared in the manifest and requested at runtime for Android 6.0 to 10.
Click to reveal answer
intermediate
What is the recommended way to access shared media files on Android 11 and above?
Use the MediaStore API with scoped storage. Direct file path access is restricted, so apps should use content URIs and system APIs to read/write media.
Click to reveal answer
beginner
How can you check if external storage is available for read and write?
Check Environment.getExternalStorageState() and compare it to Environment.MEDIA_MOUNTED for read/write access.
Click to reveal answer
Which method writes a file to internal storage in Android Kotlin?
2. Which of the following is the correct Kotlin syntax to open a file named "data.txt" for reading in an Android app?
easy
A. val input = openFileOutput("data.txt", Context.MODE_PRIVATE)
B. val input = openFileInput("data.txt")
C. val input = openFile("data.txt", Context.MODE_PRIVATE)
D. val input = readFile("data.txt")
Solution
Step 1: Identify the correct method for reading files
openFileInput is the correct method to open a file for reading in Android Kotlin.
Step 2: Check syntax correctness
openFileInput takes only the filename as a parameter, no mode needed.
Final Answer:
val input = openFileInput("data.txt") -> Option B
Quick Check:
Read file = openFileInput(filename) [OK]
Hint: openFileInput(filename) opens file for reading [OK]
Common Mistakes:
Using openFileOutput for reading
Adding mode parameter to openFileInput
Using non-existent readFile method
3. What will be the output of the following Kotlin code snippet in an Android app?
val filename = "note.txt"
val fileContents = "Hello World"
openFileOutput(filename, Context.MODE_PRIVATE).use {
it.write(fileContents.toByteArray())
}
val input = openFileInput(filename).bufferedReader().use { it.readText() }
println(input)
medium
A. note.txt
B. null
C. Hello World
D. FileNotFoundException
Solution
Step 1: Write text to file
The code writes "Hello World" to "note.txt" using openFileOutput.
Step 2: Read text from the same file
openFileInput reads the file content back as a string.
Final Answer:
Hello World -> Option C
Quick Check:
Write then read file = "Hello World" [OK]
Hint: Write then read file returns the original string [OK]
Common Mistakes:
Expecting filename instead of content
Assuming file is empty or null
Not closing streams causing errors
4. Consider this Kotlin code snippet in an Android app:
val input = openFileInput("config.txt")
val text = input.bufferedReader().readText()
input.close()
What is the main issue with this code?
medium
A. Not using 'use' block to safely close the stream
B. The input stream is closed before reading text
C. bufferedReader() cannot be called on input streams
D. openFileInput should be called with Context.MODE_PRIVATE
Solution
Step 1: Check resource management
The code manually closes the input stream but risks exceptions if readText() fails.
Step 2: Use 'use' block for safety
Using 'use' automatically closes the stream safely even if errors occur.
Final Answer:
Not using 'use' block to safely close the stream -> Option A
Quick Check:
Safe stream closing = use block [OK]
Hint: Always use 'use' to auto-close streams safely [OK]
Common Mistakes:
Adding mode parameter to openFileInput
Closing stream before reading
Believing bufferedReader() is invalid here
5. You want to save user preferences as a JSON string in a private file named "prefs.json" and then read it back safely. Which Kotlin code snippet correctly writes and reads this file in an Android app?
hard
A. openFileOutput("prefs.json", Context.MODE_PRIVATE).use { it.write(jsonString.toByteArray()) }
val json = openFileInput("prefs.json").bufferedReader().use { it.readText() }
B. openFileInput("prefs.json").use { it.write(jsonString.toByteArray()) }
val json = openFileOutput("prefs.json", Context.MODE_PRIVATE).bufferedReader().use { it.readText() }
C. val file = File("prefs.json")
file.writeText(jsonString)
val json = file.readText()
D. val fos = openFileOutput("prefs.json")
fos.write(jsonString.toByteArray())
fos.close()
val fis = openFileInput("prefs.json")
val json = fis.bufferedReader().readText()
fis.close()
Solution
Step 1: Write file using openFileOutput with 'use'
openFileOutput("prefs.json", Context.MODE_PRIVATE).use { it.write(jsonString.toByteArray()) }
val json = openFileInput("prefs.json").bufferedReader().use { it.readText() } correctly writes the JSON string to "prefs.json" using openFileOutput and safely closes the stream with 'use'.
Step 2: Read file using openFileInput with 'use'
It then reads the file back safely using openFileInput and 'use' to auto-close the stream.
Final Answer:
openFileOutput("prefs.json", Context.MODE_PRIVATE).use { it.write(jsonString.toByteArray()) }
val json = openFileInput("prefs.json").bufferedReader().use { it.readText() } -> Option A
Quick Check:
Write/read private file safely = openFileOutput/input + use [OK]
Hint: Use openFileOutput/input with 'use' to safely write/read files [OK]
Common Mistakes:
Mixing up openFileInput and openFileOutput
Using File class for internal storage without context
Not closing streams properly causing leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.