In Elixir, handling files and performing input/output (I/O) operations efficiently is crucial for building real-world applications. This guide covers essential file operations using the File module, explores the power of streams and lazy evaluation, and demonstrates working with JSON, APIs, and CSV files. Mastering these techniques enhances your Elixir programming skills.
1. File Reading and Writing with File Module
Which Elixir module provides functions for file reading and writing?
a) IO
b) File
c) Stream
d) Path
What function is used to read the entire content of a file in Elixir?
a) File.read/1
b) File.read_all/1
c) File.open/1
d) File.read_file/1
Which of the following is used to write data to a file in Elixir?
a) File.write/2
b) File.create/2
c) File.open/2
d) File.append/2
What does the File.read/1 function return in case of an error?
a) :error
b) {:error, reason}
c) nil
d) {:ok, reason}
To open a file in Elixir for reading and writing, which mode should you use?
a) :write
b) :read
c) :read_write
d) :append
Which function would you use to append content to an existing file in Elixir?
a) File.append/2
b) File.write/2
c) File.open/2
d) File.create/2
What does File.read/1 return when the file is successfully read?
a) :ok
b) {:ok, content}
c) {:ok, :empty}
d) content
Which of the following is true about writing files in Elixir?
a) Writing to a file always overwrites the content.
b) You need to use File.write/2 in :append mode to add content.
c) File operations are synchronous by default.
d) All of the above.
How would you read a file line by line in Elixir?
a) Using File.read/1
b) Using File.stream!/1
c) Using IO.read/2
d) Using Stream.read/1
Which Elixir function is used to delete a file?
a) File.delete/1
b) File.remove/1
c) File.destroy/1
d) File.erase/1
2. Stream and Lazy Evaluation
What is the primary benefit of using streams in Elixir?
a) Streams use more memory.
b) Streams process data lazily and avoid loading everything into memory at once.
c) Streams automatically process all data immediately.
d) Streams are slower than lists.
Which Elixir module is commonly used for streaming large files or data sources?
a) IO
b) Stream
c) File
d) Enum
What is the key difference between lists and streams in Elixir?
a) Streams are immutable, while lists are mutable.
b) Streams are evaluated lazily, while lists are evaluated eagerly.
c) Lists use more memory than streams.
d) Streams cannot be enumerated like lists.
Which of the following is an example of lazy evaluation in Elixir streams?
a) Stream.map([1, 2, 3], fn x -> x * 2 end)
b) Enum.map([1, 2, 3], fn x -> x * 2 end)
c) Stream.concat([1, 2, 3], [4, 5, 6])
d) Stream.filter([1, 2, 3], fn x -> x > 1 end)
How would you transform a list into a stream in Elixir?
a) Stream.to_list/1
b) Stream.from_list/1
c) Enum.into/2
d) Stream.list/1
Which function converts a stream into a list in Elixir?
a) Stream.to_list/1
b) Enum.to_list/1
c) List.stream/1
d) List.from_stream/1
What happens if you use Stream in Elixir and do not consume the values?
a) The stream is immediately evaluated.
b) It will not execute any operation until consumption is triggered.
c) The system will throw an error.
d) The stream is cached.
What is the result of using Enum.to_list/1 on a lazy stream in Elixir?
a) It will not process the stream until a transformation is needed.
b) It will immediately evaluate and process the entire stream.
c) It converts the stream into a map.
d) It will result in an error.
How would you filter values from a stream in Elixir?
a) Stream.map/2
b) Stream.filter/2
c) Stream.reduce/2
d) Stream.each/2
What is the most significant advantage of using lazy evaluation in Elixir streams?
a) It reduces memory usage by processing data incrementally.
b) It speeds up the execution by evaluating all data upfront.
c) It allows you to store large datasets in memory.
d) It increases the CPU usage due to immediate processing.
3. Working with JSON and External APIs
Which Elixir library is commonly used to encode and decode JSON?
a) JSON
b) Jason
c) Poison
d) Elixir.JSON
How do you parse JSON in Elixir using Jason?
a) Jason.parse/1
b) Jason.decode/1
c) Jason.load/1
d) Jason.read/1
What does Jason.decode/1 return on successful parsing of JSON data?
a) A map containing the decoded JSON
b) A list of strings
c) A tuple { :ok, data }
d) A tuple { :error, reason }
How can you encode Elixir data into JSON format?
a) Jason.encode/1
b) Jason.parse/1
c) Jason.encode_to_string/1
d) Jason.to_json/1
When interacting with an external API in Elixir, which HTTP client is commonly used?
a) HTTPoison
b) ExHTTP
c) Elixir.HTTP
d) RestClient
Which of the following is used to make a GET request to an API in Elixir?
a) HTTPoison.get/1
b) HTTPoison.post/1
c) HTTPoison.get/2
d) HTTPoison.put/2
Which function from HTTPoison is used to make a POST request in Elixir?
a) HTTPoison.post/1
b) HTTPoison.get/2
c) HTTPoison.put/2
d) HTTPoison.delete/1
What does HTTPoison.get/2 return when making an API request?
a) A tuple {:ok, response} containing the response
b) A map of the response
c) A raw JSON string
d) An error message
Which of the following is necessary when sending data in a POST request with HTTPoison?
a) A URL
b) Headers
c) A payload body
d) All of the above
How would you handle a failed API request in Elixir using HTTPoison?
a) Using {:ok, response} for success, and {:error, reason} for failure
b) Ignoring the error silently
c) Raising an exception for failure
d) Using {:error, exception} for both success and failure
Answer Table
Qno
Answer (Option with Text)
1
b) File
2
a) File.read/1
3
a) File.write/2
4
b) {:error, reason}
5
c) :read_write
6
a) File.append/2
7
b) {:ok, content}
8
d) All of the above
9
b) File.stream!/1
10
a) File.delete/1
11
b) Streams process data lazily and avoid loading everything into memory at once.
12
b) Stream
13
b) Streams are evaluated lazily, while lists are evaluated eagerly.
14
a) Stream.map([1, 2, 3], fn x -> x * 2 end)
15
b) Stream.from_list/1
16
a) Stream.to_list/1
17
b) It will not execute any operation until consumption is triggered.
18
b) It will immediately evaluate and process the entire stream.
19
b) Stream.filter/2
20
a) It reduces memory usage by processing data incrementally.
21
b) Jason
22
b) Jason.decode/1
23
c) A tuple { :ok, data }
24
a) Jason.encode/1
25
a) HTTPoison
26
c) HTTPoison.get/2
27
a) HTTPoison.post/1
28
a) A tuple {:ok, response} containing the response
29
d) All of the above
30
a) Using {:ok, response} for success, and {:error, reason} for failure