MCQs on File and I/O Operations | Groovy

Groovy, a powerful language built on the Java Virtual Machine (JVM), makes file handling and input/output (I/O) operations seamless. This quiz will test your knowledge of Groovy’s file handling features, including reading and writing files, traversing directories, working with streams, and processing data in JSON and XML formats.

MCQs: File and I/O Operations in Groovy

Reading and Writing Files

  1. How do you read the contents of a file in Groovy?
    • a) new File("file.txt").read()
    • b) new File("file.txt").text
    • c) File.read("file.txt")
    • d) readFile("file.txt")
  2. Which method is used to write text to a file in Groovy?
    • a) new File("file.txt").write("Hello World")
    • b) File.write("file.txt", "Hello World")
    • c) writeToFile("file.txt", "Hello World")
    • d) new File("file.txt").append("Hello World")
  3. What does the getText() method do in Groovy?
    • a) Retrieves a file as a binary stream
    • b) Reads the contents of a file as text
    • c) Converts text to uppercase
    • d) Returns the metadata of a file
  4. Which Groovy method would you use to create a new file if it doesn’t exist?
    • a) new File("file.txt").createNewFile()
    • b) File.create("file.txt")
    • c) createFile("file.txt")
    • d) new File("file.txt").create()
  5. How can you append data to a file in Groovy?
    • a) new File("file.txt").append("Hello World")
    • b) appendToFile("file.txt", "Hello World")
    • c) File.append("file.txt", "Hello World")
    • d) new File("file.txt").write("Hello World", append=true)

File Traversals

  1. Which method is used in Groovy to list all files in a directory?
    • a) new File("dir").listFiles()
    • b) listDirectory("dir")
    • c) new File("dir").files()
    • d) getFiles("dir")
  2. How can you recursively traverse a directory and its subdirectories in Groovy?
    • a) new File("dir").traverse()
    • b) File.traverse("dir")
    • c) new File("dir").recursiveFiles()
    • d) traverseFiles("dir")
  3. What does the eachFile method do in Groovy?
    • a) Loops through the lines of a file
    • b) Loops through files in a directory
    • c) Reads the contents of a file
    • d) Lists the files in the current directory
  4. Which Groovy method allows you to filter files based on a condition while traversing a directory?
    • a) eachFileRecurse
    • b) findFiles
    • c) eachFileMatch
    • d) eachFileCondition
  5. How do you delete a file using Groovy?
    • a) new File("file.txt").delete()
    • b) File.remove("file.txt")
    • c) deleteFile("file.txt")
    • d) new File("file.txt").remove()

Working with Streams

  1. How do you read a file as an InputStream in Groovy?
    • a) new File("file.txt").inputStream()
    • b) new File("file.txt").getInputStream()
    • c) File.readStream("file.txt")
    • d) new File("file.txt").stream()
  2. What method is used to write data to a file as an OutputStream in Groovy?
    • a) new File("file.txt").outputStream()
    • b) File.writeStream("file.txt")
    • c) new File("file.txt").getOutputStream()
    • d) File.writeToStream("file.txt")
  3. How can you wrap an InputStream in Groovy to read lines one by one?
    • a) new File("file.txt").newReader()
    • b) new File("file.txt").inputStreamReader()
    • c) new File("file.txt").newBufferedReader()
    • d) new File("file.txt").streamReader()
  4. Which method allows you to copy a file using a stream in Groovy?
    • a) new File("source.txt").copyTo("destination.txt")
    • b) File.copy("source.txt", "destination.txt")
    • c) new File("source.txt").copyWithStream("destination.txt")
    • d) File.copyStream("source.txt", "destination.txt")
  5. What method is used to close an OutputStream after writing in Groovy?
    • a) stream.close()
    • b) new File("file.txt").closeStream()
    • c) new File("file.txt").flush()
    • d) outputStream.close()

Working with JSON and XML

  1. How do you parse a JSON string into a Groovy object?
    • a) new JsonSlurper().parseText(jsonString)
    • b) JsonParser.parse(jsonString)
    • c) parseJson(jsonString)
    • d) new JsonParser().fromJson(jsonString)
  2. Which class in Groovy is used to convert an object to a JSON string?
    • a) JsonGenerator
    • b) JsonSlurper
    • c) JsonOutput
    • d) JsonParser
  3. How can you write a JSON object to a file in Groovy?
    • a) JsonOutput.toJson(jsonObject, new File("file.json"))
    • b) new File("file.json").write(JsonOutput.toJson(jsonObject))
    • c) JsonOutput.writeToFile(jsonObject, "file.json")
    • d) File.writeJson(jsonObject, "file.json")
  4. How do you read an XML file in Groovy?
    • a) new XmlParser().parse(new File("file.xml"))
    • b) XmlParser.parse("file.xml")
    • c) new File("file.xml").xml()
    • d) new XmlReader("file.xml").parse()
  5. Which Groovy class is used to create an XML document from an object?
    • a) XmlOutput
    • b) XmlBuilder
    • c) XmlGenerator
    • d) XmlWriter
  6. How can you transform an XML object into a string in Groovy?
    • a) XmlParser.toString()
    • b) new XmlNodePrinter().print(xmlObject)
    • c) XmlOutput.toXmlString(xmlObject)
    • d) xmlObject.toString()
  7. How do you convert a Groovy object to XML?
    • a) XmlOutput.toXml(object)
    • b) new XmlBuilder().build(object)
    • c) new XmlParser().toXml(object)
    • d) object.toXml()
  8. Which of the following will parse a JSON file into a Groovy object?
    • a) new JsonSlurper().parse(new File("file.json"))
    • b) JsonSlurper.parseJson(new File("file.json"))
    • c) JsonParser.parseFile("file.json")
    • d) parseJsonFile("file.json")
  9. How can you add an XML node to an existing XML document in Groovy?
    • a) xml.appendNode(newNode)
    • b) xml.addChild(newNode)
    • c) xml.addNode(newNode)
    • d) xml.createNode(newNode)
  10. How can you handle an exception if JSON parsing fails in Groovy?
    • a) catch(JsonParseException e)
    • b) try { ... } catch (IOException e)
    • c) catch (JsonParseException | IOException e)
    • d) try { ... } catch (Exception e)
  11. What is the Groovy method to convert an XML node to a Groovy map?
    • a) xmlNode.toMap()
    • b) xmlNode.toMapRepresentation()
    • c) xmlNode.convertToMap()
    • d) xmlNode.toMapOfNodes()
  12. Which Groovy class can be used to format a JSON string with pretty printing?
    • a) JsonOutput.prettyPrint()
    • b) JsonFormatter.pretty()
    • c) JsonPrinter.print()
    • d) JsonFormatter.prettyPrint()
  13. What does the new XmlParser() do in Groovy?
    • a) It parses the XML string into an XML document object.
    • b) It writes an XML string to a file.
    • c) It validates an XML document.
    • d) It converts XML to JSON.
  14. How do you get a node’s value from an XML document in Groovy?
    • a) xmlNode.text()
    • b) xmlNode.value()
    • c) xmlNode.getText()
    • d) xmlNode.getValue()
  15. Which Groovy class is used to generate JSON from a Groovy object?
    • a) JsonBuilder
    • b) JsonParser
    • c) JsonOutput
    • d) JsonGenerator

Answer Key

QnoAnswer
1b) new File("file.txt").text
2a) new File("file.txt").write("Hello World")
3b) Reads the contents of a file as text
4a) new File("file.txt").createNewFile()
5a) new File("file.txt").append("Hello World")
6a) new File("dir").listFiles()
7a) new File("dir").traverse()
8b) Loops through files in a directory
9c) eachFileMatch
10a) new File("file.txt").delete()
11a) new File("file.txt").inputStream()
12a) new File("file.txt").outputStream()
13c) new File("file.txt").newBufferedReader()
14a) new File("source.txt").copyTo("destination.txt")
15a) stream.close()
16a) new JsonSlurper().parseText(jsonString)
17c) JsonOutput
18b) new File("file.json").write(JsonOutput.toJson(jsonObject))
19a) new XmlParser().parse(new File("file.xml"))
20b) XmlBuilder
21b) new XmlNodePrinter().print(xmlObject)
22a) XmlOutput.toXml(object)
23a) new JsonSlurper().parse(new File("file.json"))
24a) xml.appendNode(newNode)
25c) `catch (JsonParseException
26a) xmlNode.toMap()
27a) JsonOutput.prettyPrint()
28a) It parses the XML string into an XML document object.
29a) xmlNode.text()
30a) JsonBuilder

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