MCQs on File I/O and Serialization | Scala

Learn Scala’s file I/O operations, handling JSON, XML, and serialization libraries. These 30 MCQs will help you master reading, writing files, and working with popular libraries like Circe and Play JSON.


File I/O and Serialization in Scala

1. Reading and Writing Files

  1. Which method is used to read a file in Scala using Source?
    • A) Source.read()
    • B) Source.fromFile()
    • C) Source.readAll()
    • D) Source.open()
  2. What is the purpose of PrintWriter in Scala?
    • A) To read data from a file
    • B) To write data to a file
    • C) To open a file for reading
    • D) To read the file line by line
  3. How can you write text to a file in Scala?
    • A) By using new FileWriter()
    • B) By using new PrintWriter()
    • C) By using Source.write()
    • D) By using File.create()
  4. Which of the following is true about Source.fromFile() in Scala?
    • A) It reads a file line by line
    • B) It opens the file for writing
    • C) It returns a File object
    • D) It creates a new file if it doesn’t exist
  5. What does BufferedSource in Scala allow?
    • A) Writing text to files
    • B) Fast reading from large files
    • C) Serializing objects
    • D) Reading files as streams
  6. Which method can be used to append to a file in Scala?
    • A) FileWriter.append()
    • B) PrintWriter.append()
    • C) Source.append()
    • D) File.create()
  7. What is the default character encoding used by Source.fromFile() in Scala?
    • A) UTF-16
    • B) UTF-32
    • C) ISO-8859-1
    • D) UTF-8
  8. Which Scala function is used to close a file after reading from it?
    • A) close()
    • B) closeFile()
    • C) Source.close()
    • D) File.close()
  9. Which of the following is a correct way to read a file into a string in Scala?
    • A) Source.fromFile("file.txt").mkString
    • B) File.read("file.txt")
    • C) Source.read("file.txt")
    • D) Source.load("file.txt")
  10. In Scala, how can you ensure that resources are released after reading from a file?
    • A) By using try-with-resources
    • B) By using finally block
    • C) By using using method from scala.util
    • D) By calling close() on the file object

2. Working with JSON, XML, and Serialization Libraries

  1. Which library is commonly used in Scala for handling JSON data?
    • A) Circe
    • B) Play JSON
    • C) Jackson
    • D) All of the above
  2. Which method in Circe is used to encode a Scala object into JSON?
    • A) encode()
    • B) toJson()
    • C) asJson()
    • D) convertToJson()
  3. How do you parse a JSON string into a Scala object using Circe?
    • A) decode()
    • B) parseJson()
    • C) jsonParse()
    • D) fromJson()
  4. In Scala, which XML library is used for working with XML files?
    • A) XML
    • B) scala.xml
    • C) JDOM
    • D) Play XML
  5. Which method is used to create a JsObject from a Scala case class in Play JSON?
    • A) Json.toJson()
    • B) Json.parse()
    • C) toJson()
    • D) Json.write()
  6. How can you deserialize a JSON string into a Scala case class using Circe?
    • A) decode[YourClass](jsonString)
    • B) jsonString.toCaseClass()
    • C) fromJson(jsonString)
    • D) asCaseClass(jsonString)
  7. What type of data structure is used in Play JSON to represent a JSON object?
    • A) JsObject
    • B) JsonObject
    • C) JsonMap
    • D) JSonObject
  8. What does scala.xml.XML.loadFile() do in Scala?
    • A) It parses an XML file into a Scala XML object
    • B) It serializes an XML file
    • C) It loads an XML schema
    • D) It prints an XML file
  9. Which of the following is a correct way to parse an XML string into a Scala XML object?
    • A) XML.loadString()
    • B) XML.parse()
    • C) XML.stringToXML()
    • D) parseXML()
  10. What does the toJson() method do in Play JSON?
    • A) Converts an object into a JSON string
    • B) Converts a JSON string into an object
    • C) Converts an XML object into JSON
    • D) Serializes a class into JSON format

3. Using Libraries like Circe or Play JSON

  1. Which Scala library provides automatic derivation of encoders and decoders for case classes?
    • A) Circe
    • B) Play JSON
    • C) Jackson
    • D) Upickle
  2. Which import is required to use Circe’s JSON support in Scala?
    • A) import io.circe._
    • B) import play.api.libs.json._
    • C) import com.fasterxml.jackson._
    • D) import org.json4s._
  3. Which typeclass does Circe use for serializing and deserializing JSON?
    • A) Encoder and Decoder
    • B) Marshaller and Unmarshaller
    • C) Writer and Reader
    • D) JsonFormat
  4. Which of the following is a correct way to serialize a Scala object using Play JSON?
    • A) Json.stringify(Json.toJson(object))
    • B) object.toJson()
    • C) Json.encode(object)
    • D) object.toJsonString()
  5. How can you perform a safe JSON deserialization in Circe?
    • A) By using decodeEither()
    • B) By using parseJson()
    • C) By using safeDecode()
    • D) By using tryDecode()
  6. What is the purpose of Json.format() in Play JSON?
    • A) It automatically provides reads and writes for case classes
    • B) It parses JSON into case class
    • C) It serializes JSON into strings
    • D) It converts XML into JSON
  7. What feature does Circe provide for better handling of default values?
    • A) JsonDefaults
    • B) deriveEncoder and deriveDecoder
    • C) Json.optional()
    • D) defaultValue()
  8. How can you perform pretty-printing of JSON in Circe?
    • A) Printer.pretty()
    • B) Json.prettyPrint()
    • C) jsonString.prettyPrint()
    • D) json.pretty()
  9. How does Play JSON handle case class fields that are optional?
    • A) By using Option in the case class
    • B) By ignoring them during deserialization
    • C) By using Nullable
    • D) By adding a default value
  10. Which of the following methods can be used to serialize and deserialize a Scala Map using Play JSON?
    • A) Json.formatMap()
    • B) Json.toMap() and Json.fromMap()
    • C) Map.format()
    • D) Map.toJson()

Answer Key

QnoAnswer (Option with the text)
1B) Source.fromFile()
2B) To write data to a file
3B) By using new PrintWriter()
4A) It reads a file line by line
5B) Fast reading from large files
6B) PrintWriter.append()
7D) UTF-8
8A) close()
9A) Source.fromFile("file.txt").mkString
10C) By using using method from scala.util
11D) All of the above
12C) asJson()
13A) decode[YourClass](jsonString)
14B) scala.xml
15A) Json.toJson()
16A) decode[YourClass](jsonString)
17A) JsObject
18A) It parses an XML file into a Scala XML object
19A) XML.loadString()
20A) Converts an object into a JSON string
21A) Circe
22A) import io.circe._
23A) Encoder and Decoder
24A) Json.stringify(Json.toJson(object))
25A) By using decodeEither()
26A) It automatically provides reads and writes for case classes
27B) deriveEncoder and deriveDecoder
28A) Printer.pretty()
29A) By using Option in the case class
30B) Json.toMap() and Json.fromMap()

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