Haskell Interview Questions Hindi mein

Haskell कोर्स के 25 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Getting Started with Haskell

1. What does purity mean in Haskell?

  1. Functions return the same output for the same input and have no hidden side effects
  2. Functions cannot take arguments
  3. Programs cannot perform I/O
  4. All values are strings
जवाब देखें

जवाब: Functions return the same output for the same input and have no hidden side effects

Effects are tracked in types such as IO; ordinary functions are pure.

पाठ: What Haskell Is and Why Learn It

2. What does the GHCi command :t do?

  1. Runs tests
  2. Terminates GHCi
  3. Toggles laziness
  4. Shows the type of an expression
जवाब देखें

जवाब: Shows the type of an expression

:t (or :type) prints the inferred type of an expression.

पाठ: Installing GHC, Cabal, Stack and GHCi

3. What is the type of `addTax 18` if `addTax :: Int -> Int -> Int`?

  1. Int
  2. Int -> Int -> Int
  3. Int -> Int
  4. It is a type error
जवाब देखें

जवाब: Int -> Int

Curried functions return a new function when partially applied.

पाठ: Expressions, Types and Functions

Lists, Pattern Matching and Recursion

4. What is the cost of prepending an element to a Haskell list with (:)?

  1. O(n)
  2. O(log n)
  3. O(1)
  4. O(n squared)
जवाब देखें

जवाब: O(1)

Cons creates one new cell pointing to the existing list.

पाठ: Lists, Ranges and Comprehensions

5. What does GHC's -Wincomplete-patterns warning (part of -Wall) catch?

  1. Slow code
  2. Functions or case expressions that do not handle every possible input
  3. Unused imports only
  4. Type errors
जवाब देखें

जवाब: Functions or case expressions that do not handle every possible input

It reports missing cases at compile time.

पाठ: Pattern Matching, Guards and case

6. Why is foldl' usually preferred over foldl for summing a large list?

  1. It evaluates the accumulator at each step, avoiding a build-up of unevaluated thunks
  2. It is lazier
  3. It works only on infinite lists
  4. It sorts the list first
जवाब देखें

जवाब: It evaluates the accumulator at each step, avoiding a build-up of unevaluated thunks

Strict accumulation keeps memory use constant.

पाठ: Recursion and Higher-Order Functions

Algebraic Data Types and Type Classes

7. What does newtype Paise = Paise Int give you?

  1. A distinct type wrapping Int with no runtime cost
  2. A type alias with no checking
  3. A mutable integer
  4. A list of integers
जवाब देखें

जवाब: A distinct type wrapping Int with no runtime cost

newtype creates a new type for the compiler that is erased at run time.

पाठ: Algebraic Data Types and Records

8. What does the constraint in `sort :: Ord a => [a] -> [a]` mean?

  1. a must be a list
  2. a must be Int
  3. sort returns an Ord
  4. sort works for any type a that has an Ord instance
जवाब देखें

जवाब: sort works for any type a that has an Ord instance

Constraints restrict type variables to types with the required instances.

पाठ: Type Classes and Instances

9. In `do { a <- step1; b <- step2 a; pure (a, b) }` with Either, what happens if step1 returns Left err?

  1. step2 still runs
  2. It throws an exception
  3. The whole expression is Left err and later steps do not run
  4. It returns Right ()
जवाब देखें

जवाब: The whole expression is Left err and later steps do not run

Either's monad short-circuits on the first Left.

पाठ: Maybe and Either

Functor, Applicative and Monad

10. What is `fmap (+1) Nothing`?

  1. Just 1
  2. An error
  3. Nothing
  4. 1
जवाब देखें

जवाब: Nothing

Mapping over Nothing leaves Nothing: there is no value to transform.

पाठ: Functor: Mapping Inside a Context

11. What does `f <$> Just 1 <*> Nothing` evaluate to (for a two-argument f)?

  1. Just (f 1)
  2. Nothing
  3. An exception
  4. Just 1
जवाब देखें

जवाब: Nothing

If any applicative input is Nothing, the result is Nothing.

पाठ: Applicative: Combining Independent Effects

12. What can Monad express that Applicative alone cannot?

  1. Computations where later steps depend on the results of earlier ones
  2. Pure values
  3. Mapping over lists
  4. Equality checks
जवाब देखें

जवाब: Computations where later steps depend on the results of earlier ones

Bind passes each result to a function that chooses the next computation.

पाठ: Monads and do Notation

IO, Laziness and Structural Classes

13. How does Haskell keep functions pure while still doing I/O?

  1. I/O is described by IO actions in the type, which pure functions cannot run
  2. It forbids I/O
  3. It uses global variables
  4. All functions are impure
जवाब देखें

जवाब: I/O is described by IO actions in the type, which pure functions cannot run

Effects are values of type IO, run only by the runtime from main.

पाठ: IO and Exceptions

14. What is a common cause of space leaks in Haskell?

  1. Using strict folds
  2. Using pattern matching
  3. Writing type signatures
  4. Accumulating unevaluated thunks, for example with lazy foldl or lazy record fields
जवाब देखें

जवाब: Accumulating unevaluated thunks, for example with lazy foldl or lazy record fields

Long chains of thunks consume memory until they are finally forced.

पाठ: Laziness, Thunks and Strictness

15. What does traverse do when the function returns Left for one element in Either?

  1. Skips that element
  2. Throws an exception
  3. Returns that Left for the whole result
  4. Returns Right with an empty list
जवाब देखें

जवाब: Returns that Left for the whole result

traverse sequences the Either effects, short-circuiting on the first Left.

पाठ: Semigroup, Monoid, Foldable and Traversable

Real-World Haskell

16. Why prefer Data.Text over String in real applications?

  1. String does not support Unicode
  2. Text is required by GHC
  3. Text is a compact, efficient representation, while String is a linked list of characters
  4. String cannot be printed
जवाब देखें

जवाब: Text is a compact, efficient representation, while String is a linked list of characters

String is [Char], which is slow and memory-hungry for large text.

पाठ: Modules, Packages, Text and Containers

17. What does ReaderT Config IO add to plain IO?

  1. Mutable state
  2. Access to a shared, read-only Config value via ask
  3. Exceptions
  4. Parallelism
जवाब देखें

जवाब: Access to a shared, read-only Config value via ask

ReaderT threads an environment through computations implicitly.

पाठ: Monad Transformers and Application Structure

18. What does retry do inside an STM transaction?

  1. Aborts the transaction and blocks until one of the TVars it read changes
  2. Retries immediately in a busy loop
  3. Throws an exception
  4. Commits partially
जवाब देखें

जवाब: Aborts the transaction and blocks until one of the TVars it read changes

retry waits for relevant state to change before re-running the transaction.

पाठ: Concurrency: Threads, MVar, STM and async

Testing, Parsing and Web

19. What does QuickCheck do when it finds a failing input?

  1. Shrinks it to a smaller counterexample that still fails
  2. Stops without information
  3. Ignores it
  4. Retries until it passes
जवाब देखें

जवाब: Shrinks it to a smaller counterexample that still fails

Shrinking makes failures much easier to understand.

पाठ: Testing with Hspec and QuickCheck

20. What does the <|> combinator do between two parsers?

  1. Runs both and combines results
  2. Repeats a parser
  3. Skips whitespace
  4. Tries the first parser and, if it fails without consuming input, tries the second
जवाब देखें

जवाब: Tries the first parser and, if it fails without consuming input, tries the second

<|> expresses choice between alternatives.

पाठ: Parser Combinators

21. How are aeson JSON instances most commonly created for record types?

  1. They cannot be created
  2. By writing JSON strings manually
  3. Derived via Generic with deriving (Generic, ToJSON, FromJSON)
  4. From a database schema
जवाब देखें

जवाब: Derived via Generic with deriving (Generic, ToJSON, FromJSON)

Generic deriving creates instances that follow the record's field names.

पाठ: JSON and Web Services

Advanced Types, Performance, Idioms and Revision

22. What does a phantom type parameter such as c in Money c provide?

  1. Runtime currency conversion
  2. A mutable field
  3. A compile-time tag that prevents mixing values with different tags, with no runtime cost
  4. A JSON encoding
जवाब देखें

जवाब: A compile-time tag that prevents mixing values with different tags, with no runtime cost

The parameter exists only in types, so mismatches are caught by the compiler.

पाठ: Type-Level Features and GHC Extensions

23. Which RTS flag prints a garbage collection and memory usage summary?

  1. +RTS -N
  2. +RTS -s
  3. +RTS -p
  4. -O2
जवाब देखें

जवाब: +RTS -s

+RTS -s prints allocation, GC and memory statistics at exit.

पाठ: Performance, Profiling and Tooling

24. In a Haskell do block, what does `return x` do?

  1. Wraps x in the monad; the following lines still run
  2. Exits the function immediately like in C
  3. Throws an exception
  4. Prints x
जवाब देखें

जवाब: Wraps x in the monad; the following lines still run

return (like pure) only injects a value into the monad.

पाठ: Idiomatic Haskell and Common Pitfalls

25. Which class provides >>= for sequencing dependent computations?

  1. Functor
  2. Semigroup
  3. Show
  4. Monad
जवाब देखें

जवाब: Monad

Monad's bind passes each result to the next computation.

पाठ: Revision and Interview Questions

← कोर्स पर वापस जाएँ