MCQs on Modules and Libraries | Haskell

Modules and Libraries in Haskell Programming

Haskell is a powerful functional programming language that emphasizes immutability, purity, and type safety. Understanding how to organize code into modules, import and utilize libraries, and leverage the Haskell standard library is essential for writing effective, reusable code. This guide covers key concepts like organizing code into modules, importing libraries, and testing small libraries in Haskell.


1. Organizing Code with Modules

  1. What is the primary purpose of modules in Haskell?
    • a) To organize code into manageable units
    • b) To provide memory management
    • c) To create standalone executable files
    • d) To handle exceptions globally
  2. How do you define a module in Haskell?
    • a) module ModuleName where
    • b) define ModuleName
    • c) import ModuleName
    • d) module : ModuleName
  3. What is the benefit of using modules in Haskell?
    • a) Modularity and reusability of code
    • b) Simplified debugging process
    • c) Direct access to system hardware
    • d) Enhanced speed of execution
  4. How can you expose functions from a Haskell module?
    • a) By declaring them in the module’s export list
    • b) By using export keyword
    • c) By importing the module into another file
    • d) By writing them in the main program
  5. What does the keyword import do in a Haskell module?
    • a) It allows access to functions from another module
    • b) It defines a new module
    • c) It adds a new function to the module
    • d) It compiles the code
  6. Which of the following is true about modules in Haskell?
    • a) Modules are used to define functions and types in a separate namespace
    • b) Modules cannot have functions with the same name
    • c) Haskell allows only one module per program
    • d) Modules cannot interact with each other
  7. What is the default behavior of a function in a Haskell module if it is not exported?
    • a) The function is private and cannot be accessed outside the module
    • b) The function is global
    • c) The function will cause a compilation error
    • d) The function can only be accessed via imports
  8. How can you hide specific functions when importing a module in Haskell?
    • a) By using hiding clause in the import statement
    • b) By using exclude keyword
    • c) By using noimport keyword
    • d) By commenting out the function
  9. How do you specify a module to import selectively in Haskell?
    • a) import ModuleName (function1, function2)
    • b) import ModuleName only function1
    • c) import ModuleName without function2
    • d) import ModuleName selective
  10. What does the qualified keyword do in Haskell imports?
    • a) It allows you to use functions from a module with a qualified name
    • b) It makes all functions in the module private
    • c) It imports only certain types
    • d) It changes the type of the module

2. Importing and Using Libraries

  1. How do you import a third-party library in Haskell?
    • a) By using the import keyword and specifying the library
    • b) By downloading the library manually
    • c) By using the use keyword
    • d) By linking the library in the .ghci file
  2. Which package manager is commonly used to install libraries in Haskell?
    • a) Stack
    • b) npm
    • c) Maven
    • d) pip
  3. How can you find libraries available for use in Haskell?
    • a) By searching on Hackage
    • b) By querying the compiler
    • c) By checking the system directory
    • d) By using ghc --libraries command
  4. What must you include in your project to use an external library in Haskell?
    • a) The library name in the .cabal file
    • b) The library’s source code in your project
    • c) A direct link to the library’s GitHub page
    • d) The library’s license in your code
  5. How do you install an external library in Haskell using Stack?
    • a) stack install library-name
    • b) cabal install library-name
    • c) stack get library-name
    • d) ghc -install library-name
  6. What is the command to list installed libraries in Haskell using Stack?
    • a) stack list
    • b) ghc -list
    • c) stack show libraries
    • d) stack ghc --list
  7. Which file is used to configure the dependencies of a Haskell project?
    • a) .cabal file
    • b) stack.yaml
    • c) ghci.conf
    • d) libconfig.yaml
  8. What does the cabal tool do in Haskell?
    • a) It manages package dependencies and builds projects
    • b) It compiles code into executable binaries
    • c) It installs third-party libraries manually
    • d) It debugs Haskell programs
  9. How do you import all functions from a Haskell library?
    • a) import LibraryName
    • b) import * from LibraryName
    • c) import LibraryName as *
    • d) import all from LibraryName
  10. What does the import statement in Haskell allow you to do with a library?
    • a) It provides access to the library’s functions and types
    • b) It compiles the library into executable code
    • c) It automatically installs the library
    • d) It imports the library’s source code directly into your program

3. Understanding the Haskell Standard Library

  1. What is the Haskell standard library used for?
    • a) It provides basic functionality for common tasks like data manipulation and I/O
    • b) It only handles mathematical functions
    • c) It defines user interface elements
    • d) It manages network communication
  2. Which module from the Haskell standard library provides functions for manipulating lists?
    • a) Data.List
    • b) System.IO
    • c) Control.Monad
    • d) Data.Map
  3. What function from Data.Maybe is used to extract the value from a Just?
    • a) fromJust
    • b) unwrap
    • c) getValue
    • d) extract
  4. Which module is commonly used to handle I/O operations in Haskell?
    • a) System.IO
    • b) Data.List
    • c) Control.Exception
    • d) Data.Map
  5. What does the Control.Monad module provide in Haskell?
    • a) Functions for working with monads and chaining operations
    • b) Functions for controlling input/output operations
    • c) Data structures for storing large datasets
    • d) Functions for managing concurrency
  6. What is the purpose of the Data.Map module in Haskell?
    • a) It provides an implementation of associative arrays (maps)
    • b) It handles file and directory operations
    • c) It works with mathematical sets
    • d) It defines basic list operations
  7. How do you handle exceptions in Haskell using the standard library?
    • a) Using the Control.Exception module
    • b) Using the Data.Either module
    • c) Using the System.IO module
    • d) Using the Data.Maybe module
  8. Which of the following is a function provided by the Data.List module in Haskell?
    • a) concat
    • b) append
    • c) sum
    • d) findIndex
  9. Which Haskell library provides support for concurrent programming?
    • a) Control.Concurrent
    • b) Data.List
    • c) System.Process
    • d) Data.Map
  10. What type of data does the Data.Either module work with in Haskell?
    • a) It works with values that can either be a success or a failure
    • b) It works with mathematical sets
    • c) It manages input/output data
    • d) It handles event-based data flow

4. Writing and Testing Small Libraries

  1. How do you write a small library in Haskell?
    • a) By defining functions and placing them in a module
    • b) By writing a large executable file
    • c) By adding functions directly into the main program
    • d) By creating a configuration file
  2. What file should you use to configure a small library in Haskell?
    • a) .cabal file
    • b) Makefile
    • c) .haskell file
    • d) config.yaml
  3. What is a good practice for testing small libraries in Haskell?
    • a) Using the HUnit testing framework
    • b) Using manual debugging techniques
    • c) Writing custom logging functions
    • d) Testing without writing specific test cases
  4. Which function from the Test.HUnit module is used to run tests in Haskell?
    • a) runTestTT
    • b) runTests
    • c) testAll
    • d) executeTest
  5. What is the purpose of the hspec library in Haskell?
    • a) It provides a behavior-driven testing framework
    • b) It handles file I/O operations
    • c) It manages concurrency in tests
    • d) It formats test results into reports

Answers

QnoAnswer
1a) To organize code into manageable units
2a) module ModuleName where
3a) Modularity and reusability of code
4a) By declaring them in the module’s export list
5a) It allows access to functions from another module
6a) Modules are used to define functions and types in a separate namespace
7a) The function is private and cannot be accessed outside the module
8a) By using hiding clause in the import statement
9a) import ModuleName (function1, function2)
10a) It allows you to use functions from a module with a qualified name
11a) By using the import keyword and specifying the library
12a) Stack
13a) By searching on Hackage
14a) The library name in the .cabal file
15a) stack install library-name
16a) stack list
17a) .cabal file
18a) It manages package dependencies and builds projects
19a) import LibraryName
20a) It provides access to the library’s functions and types
21a) It provides basic functionality for common tasks like data manipulation and I/O
22a) Data.List
23a) fromJust
24a) System.IO
25a) Functions for working with monads and chaining operations
26a) It provides an implementation of associative arrays (maps)
27a) Using the Control.Exception module
28a) concat
29a) Control.Concurrent
30a) It works with values that can either be a success or a failure
31a) By defining functions and placing them in a module
32a) .cabal file
33a) Using the HUnit testing framework
34a) runTestTT
35a) It provides a behavior-driven testing framework

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