MCQs on File Handling and I/O | Kotlin

Kotlin provides robust file handling features that allow efficient file reading, writing, and serialization. Test your knowledge on Kotlin’s I/O operations, file manipulation, and serialization techniques in this quiz!


Reading and Writing Files

  1. Which Kotlin function is used to read all content from a file?
    a) readFile()
    b) readLines()
    c) readText()
    d) getFileContent()
  2. How do you write a string to a file in Kotlin?
    a) file.writeText("text")
    b) file.write("text")
    c) file.append("text")
    d) writeToFile("text")
  3. Which function reads lines from a file in Kotlin?
    a) readFile()
    b) read()
    c) readLines()
    d) getLines()
  4. What does the appendText() function do in Kotlin?
    a) Appends text at the beginning of the file
    b) Overwrites the existing file with new content
    c) Appends text at the end of the file
    d) Reads the content of the file
  5. How do you check if a file exists in Kotlin?
    a) file.exists()
    b) file.isFile()
    c) file.checkExistence()
    d) file.existsFile()
  6. In Kotlin, which class is used for reading a file?
    a) BufferedReader
    b) FileReader
    c) InputStream
    d) FileWriter
  7. Which of the following function is used to create a file in Kotlin if it does not already exist?
    a) create()
    b) createNewFile()
    c) openFile()
    d) createFile()
  8. What happens if you try to write to a file that doesn’t exist in Kotlin?
    a) It throws an error
    b) It creates a new file and writes to it
    c) It appends an empty file
    d) It creates an empty file
  9. What is the function to read the first line of a file in Kotlin?
    a) readLine()
    b) readFirstLine()
    c) getLine(1)
    d) firstLine()
  10. In Kotlin, how can you safely handle potential I/O errors while reading or writing files?
    a) By using try-catch blocks
    b) By using safeFile()
    c) By checking file size
    d) By using File.safeRead()

Using File and BufferedReader

  1. How do you read a file line by line using Kotlin?
    a) FileReader.readLines()
    b) BufferedReader.readLine()
    c) file.forEachLine()
    d) File.readText()
  2. Which method of BufferedReader is used to read the next line of a file?
    a) read()
    b) readLine()
    c) nextLine()
    d) getLine()
  3. Which of the following is a key benefit of using BufferedReader in Kotlin?
    a) It allows direct access to file metadata
    b) It speeds up file reading operations by buffering input
    c) It automatically writes to files
    d) It automatically encodes file content
  4. How do you close a BufferedReader in Kotlin?
    a) bufferedReader.close()
    b) reader.close()
    c) closeReader()
    d) reader.closeFile()
  5. What function do you use to read the content of a file as a single string in Kotlin?
    a) readText()
    b) readString()
    c) readAll()
    d) getText()
  6. Which function is used to create a BufferedReader from a file in Kotlin?
    a) file.bufferedReader()
    b) FileReader()
    c) BufferedReader.open()
    d) file.newReader()
  7. Which Kotlin function is used to read all lines of a file into a list?
    a) readAllLines()
    b) readLines()
    c) lines()
    d) getFileLines()
  8. How can you handle resources like a BufferedReader automatically closing after use?
    a) Using try-catch block
    b) Using use block
    c) Manually calling close()
    d) Using finally block
  9. What is the default character encoding used by BufferedReader in Kotlin?
    a) UTF-8
    b) ASCII
    c) ISO-8859-1
    d) It depends on the system settings
  10. How can you handle reading from a file that may not exist in Kotlin?
    a) By using a try-catch block
    b) By checking file length before reading
    c) By setting a default value
    d) By using safeRead()

Serialization Basics

  1. Which Kotlin library is used for serialization?
    a) kotlinx.serialization
    b) java.io
    c) Gson
    d) Jackson
  2. How do you serialize a Kotlin object into JSON format?
    a) Json.encodeToString()
    b) toJson()
    c) serializeObject()
    d) convertToJson()
  3. What annotation is used to mark a class for serialization in Kotlin?
    a) @Serializable
    b) @Serialized
    c) @SerializableObject
    d) @JsonSerializable
  4. How do you deserialize a JSON string back into a Kotlin object?
    a) Json.decodeFromString()
    b) toObject()
    c) deserializeJson()
    d) fromJson()
  5. Which function allows you to serialize an object to a file in Kotlin?
    a) writeObject()
    b) Json.encodeToStream()
    c) serializeToFile()
    d) toJsonFile()
  6. Which of the following is required to perform serialization in Kotlin?
    a) The class must implement the Serializable interface
    b) The class must use the @Serializable annotation
    c) The class must extend a specific base class
    d) Serialization is not possible in Kotlin
  7. What is the purpose of @Transient annotation in Kotlin serialization?
    a) To mark a property that should not be serialized
    b) To make a property transient
    c) To mark a property as static
    d) To mark a property for deserialization
  8. What does the Json class in kotlinx.serialization library provide?
    a) Encoding and decoding methods for JSON format
    b) File handling methods for JSON data
    c) HTTP request functions
    d) Data processing utilities
  9. In Kotlin, how can you handle deserialization errors?
    a) By using a try-catch block
    b) By using Json.decodeFromStringOrNull()
    c) By manually checking the data
    d) By using safeDeserialize()
  10. Which of the following is a feature of Kotlin serialization?
    a) Automatic object graph serialization
    b) Supports multiple formats including JSON, CBOR, and ProtoBuf
    c) Requires manual memory management
    d) No support for custom serialization formats

Answer Key

QnoAnswer
1c) readText()
2a) file.writeText("text")
3c) readLines()
4c) Appends text at the end of the file
5a) file.exists()
6a) BufferedReader
7b) createNewFile()
8b) It creates a new file and writes to it
9a) readLine()
10a) By using try-catch blocks
11c) file.forEachLine()
12b) readLine()
13b) It speeds up file reading operations by buffering input
14b) reader.close()
15a) readText()
16a) file.bufferedReader()
17b) readLines()
18b) Using use block
19a) UTF-8
20a) By using a try-catch block
21a) kotlinx.serialization
22a) Json.encodeToString()
23a) @Serializable
24a) Json.decodeFromString()
25b) Json.encodeToStream()
26b) The class must use the @Serializable annotation
27a) To mark a property that should not be serialized
28a) Encoding and decoding methods for JSON format
29b) By using Json.decodeFromStringOrNull()
30b) Supports multiple formats including JSON, CBOR, and ProtoBuf

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top