Home »
MCQs
F# MCQs (Multiple-Choice Questions)
Test your knowledge of F# programming with these multiple-choice questions (MCQs) and answers. These F# MCQs cover functional programming concepts, functions, variables, pattern matching, lists, arrays, sequences, records, discriminated unions, options, modules, classes, interfaces, computation expressions, and other important F# language features.
F# MCQs
F# is a functional-first programming language on the .NET platform. It supports functional, object-oriented, and imperative programming styles, with features such as type inference, pattern matching, immutable data, discriminated unions, computation expressions, and concise function definitions.
List of F# MCQs
Practice the following F# multiple-choice questions to improve your understanding of the language and prepare for technical interviews, examinations, and programming assessments.
1. Which keyword is commonly used to define a value or function in F#?
- var
- let
- define
- val
Answer: B) let
Explanation:
The let keyword is used to bind names to values and functions in F#. Recursive functions can be defined using let rec.
2. Which keyword is required when defining a recursive F# function?
- recursive
- loop
- rec
- self
Answer: C) rec
Explanation:
The rec keyword allows a function to refer to itself. For example, a recursive function is defined using let rec factorial n = ....
3. What is the result of the following F# expression?
let square x = x * x
square 5
- 10
- 15
- 20
- 25
Answer: D) 25
Explanation:
The function multiplies its argument by itself. Therefore, square 5 evaluates to 25.
4. Which statement about F# functions is correct?
- Functions cannot be stored in variables
- Functions can be treated as values
- Functions must always belong to classes
- Functions cannot be passed as arguments
Answer: B) Functions can be treated as values
Explanation:
F# treats functions as first-class values. They can be assigned to names, passed as arguments, returned from other functions, and stored in data structures.
5. Which syntax represents an anonymous function in F#?
lambda x => x + 1
fun x -> x + 1
function x = x + 1
anonymous(x) = x + 1
Answer: B) fun x -> x + 1
Explanation:
The fun keyword introduces a lambda expression in F#. The expression fun x -> x + 1 represents a function that adds one to its argument.
6. Which operator is commonly used to pipe a value into a function in F#?
->
|>
=>
<-
Answer: B) |>
Explanation:
The forward pipe operator |> passes the value on its left as an argument to the function on its right.
7. What does the following expression evaluate to?
5 |> (fun x -> x * 2)
- 5
- 7
- 10
- 25
Answer: C) 10
Explanation:
The value 5 is passed to the anonymous function, which multiplies it by 2. The result is 10.
8. What is a key characteristic of type inference in F#?
- Every variable must always have an explicit type
- The compiler can infer types from expressions
- Types are determined only at runtime
- F# does not use static typing
Answer: B) The compiler can infer types from expressions
Explanation:
F# uses static type inference. The compiler analyzes expressions and function usage to determine types without requiring explicit type annotations in many cases.
9. Which of the following creates an F# list containing three integers?
(1, 2, 3)
[1; 2; 3]
[|1; 2; 3|]
{1; 2; 3}
Answer: B) [1; 2; 3]
Explanation:
F# lists are written using square brackets with elements separated by semicolons. Arrays use [| ... |].
10. Which statement correctly describes an F# list?
- It is mutable by default
- It is an ordered immutable collection
- It can contain values of unrelated types without a common type
- It always provides O(1) indexed access
Answer: B) It is an ordered immutable collection
Explanation:
An F# list is an ordered, immutable series of elements of the same type. F# lists are implemented as singly linked lists, so indexed access requires traversal.
11. Which operator adds an element to the front of an F# list?
::
@
+
++
Answer: A) ::
Explanation:
The cons operator :: adds an element to the beginning of a list. For example, 1 :: [2; 3] produces [1; 2; 3].
12. Which operator concatenates two F# lists?
::
@
||
&&
Answer: B) @
Explanation:
The @ operator concatenates two compatible F# lists into a new list.
13. Which F# function applies a transformation to every element of a list?
List.map
List.find
List.head
List.empty
Answer: A) List.map
Explanation:
List.map applies a specified function to each element and returns a new list containing the transformed values.
14. What does List.filter do?
- Sorts a list
- Removes all elements from a list
- Returns elements satisfying a predicate
- Converts a list into an array
Answer: C) Returns elements satisfying a predicate
Explanation:
List.filter takes a predicate function and returns a new list containing only the elements for which the predicate returns true.
15. Which syntax creates an F# array?
[1; 2; 3]
(1; 2; 3)
[|1; 2; 3|]
{|1; 2; 3|}
Answer: C) [|1; 2; 3|]
Explanation:
F# arrays use [| and |]. Unlike lists, arrays are mutable and provide constant-time indexed access.
16. Which F# collection is represented by the type seq<'T>?
- List
- Array
- Sequence
- Tuple
Answer: C) Sequence
Explanation:
seq<'T> represents an F# sequence. It is an alias for System.Collections.Generic.IEnumerable<'T> and supports lazy evaluation of elements.
17. Which expression creates a sequence containing the integers from 1 through 5?
[1..5]
seq { 1 .. 5 }
array { 1 .. 5 }
sequence(1,5)
Answer: B) seq { 1 .. 5 }
Explanation:
A sequence expression uses seq { ... }. The range 1 .. 5 generates the values from 1 through 5.
18. Which statement about F# sequences is correct?
- All sequence elements must be computed immediately
- Sequences are always mutable
- Sequence elements can be computed on demand
- Sequences can contain only integers
Answer: C) Sequence elements can be computed on demand
Explanation:
F# sequences are commonly used for lazy or on-demand computation. Individual elements are generated as required during enumeration.
19. What is the purpose of the F# option type?
- To represent a value that may or may not exist
- To represent only numeric values
- To create mutable variables
- To define classes
Answer: A) To represent a value that may or may not exist
Explanation:
The option type represents either a value using Some or the absence of a value using None.
20. Which two cases are associated with the F# option type?
- Yes and No
- Some and None
- True and False
- Value and Empty
Answer: B) Some and None
Explanation:
An F# option has two cases: Some(value) when a value exists and None when it does not.
21. Which F# construct is primarily used for branching based on patterns?
switch
match
select
choose
Answer: B) match
Explanation:
The match expression compares a value against a series of patterns and evaluates the expression associated with the first matching pattern.
22. What does the underscore _ represent in an F# pattern?
- A numeric zero
- A wildcard pattern
- A null reference
- An empty list
Answer: B) A wildcard pattern
Explanation:
The underscore is the wildcard pattern. It matches any value without binding that value to a name.
23. What is the purpose of a pattern guard in F#?
- To define a class
- To add an additional Boolean condition to a pattern
- To create a loop
- To convert a value to a string
Answer: B) To add an additional Boolean condition to a pattern
Explanation:
A pattern can include a when clause. The guard must evaluate to true for that pattern branch to be selected.
24. What does the following pattern match?
match value with
| x :: xs -> ...
| [] -> ...
- Only arrays
- A non-empty list and an empty list
- Only tuples
- Only strings
Answer: B) A non-empty list and an empty list
Explanation:
The x :: xs cons pattern decomposes a non-empty list into its head and tail, while [] matches an empty list.
25. Which syntax defines a tuple containing two values in F#?
[1; 2]
(1, 2)
[|1; 2|]
{1, 2}
Answer: B) (1, 2)
Explanation:
Tuples are written using parentheses with comma-separated values. Unlike lists, tuple elements can have different types.
26. Which F# type is designed to represent an aggregate of named values?
- Record
- Sequence
- Option
- Delegate
Answer: A) Record
Explanation:
F# records represent simple aggregates of named values. Record fields are accessed using their field names.
27. Which syntax correctly defines an F# record?
type Person = { Name: string; Age: int }
record Person(Name, Age)
type Person(Name: string, Age: int)
struct Person = (Name, Age)
Answer: A) type Person = { Name: string; Age: int }
Explanation:
F# records are declared with the type keyword followed by a record definition containing named fields and their types.
28. What is a discriminated union in F#?
- A type whose value can be one of several named cases
- A collection that stores only integers
- A mutable array
- A special type of loop
Answer: A) A type whose value can be one of several named cases
Explanation:
Discriminated unions allow a value to represent one of several named cases, with each case optionally carrying associated data.
29. Which syntax correctly declares a simple F# discriminated union?
type Result = Success | Failure
union Result(Success, Failure)
enum Result { Success, Failure }
type Result(Success, Failure)
Answer: A) type Result = Success | Failure
Explanation:
F# discriminated unions are declared with type followed by named cases separated by the pipe symbol.
30. Which discriminated union case can carry an integer value?
Success
Success of int
Success: int
Success(int)
Answer: B) Success of int
Explanation:
A discriminated union case can carry associated data using the of keyword, such as Success of int.
31. Which expression correctly creates an option containing the integer 10?
Option(10)
Some 10
Value 10
Option.SomeValue 10
Answer: B) Some 10
Explanation:
Some is the option case that wraps an existing value. Therefore, Some 10 has type int option.
32. Which keyword is used to define an F# module?
namespace
module
package
group
Answer: B) module
Explanation:
The module keyword groups related F# values, functions, types, and other declarations.
33. Which statement about F# modules is correct?
- They can contain only functions
- They can group values, types, and functions
- They can contain only classes
- They cannot be nested
Answer: B) They can group values, types, and functions
Explanation:
An F# module is a grouping of related code constructs such as values, types, functions, and do bindings. Modules can also be nested.
34. Which keyword is used to define a class type in F#?
class
type
object
define
Answer: B) type
Explanation:
F# uses the type keyword for class definitions. For example, type Person(name: string) = ... defines a class.
35. Which keyword is used to declare an interface in F#?
interface
contract
protocol
abstract-interface
Answer: A) interface
Explanation:
F# interfaces define sets of related members that implementing types must provide. Interface members are generally declared using abstract member.
36. Which keyword is used to declare an abstract interface member in F#?
virtual
abstract
interface
override
Answer: B) abstract
Explanation:
F# interface declarations use abstract member to specify members that implementing types must provide.
37. Which keyword is used to implement an inherited or virtual member in an F# type?
replace
override
implement
virtualize
Answer: B) override
Explanation:
The override keyword is used when a derived F# type provides an implementation for an inherited abstract or virtual member.
38. What is the purpose of the mutable keyword in F#?
- It makes a value recursive
- It allows a binding to be changed after initialization
- It converts a value to an option
- It creates a constant
Answer: B) It allows a binding to be changed after initialization
Explanation:
F# values are immutable by default. A binding declared with mutable can be reassigned using the <- operator.
39. What is the purpose of the F# <- operator when used with a mutable value?
- Function composition
- Assignment
- Pattern matching
- List concatenation
Answer: B) Assignment
Explanation:
The <- operator assigns a new value to a mutable binding or mutable property.
40. Which keyword is commonly used for resource management when the resource should be disposed automatically?
using
dispose
use
release
Answer: C) use
Explanation:
The F# use binding is designed for values that implement System.IDisposable. The resource is disposed when the binding goes out of scope.
41. What does partial application mean in F#?
- Calling a function without compiling it
- Supplying fewer arguments to create a new function
- Removing a function parameter
- Calling a function recursively
Answer: B) Supplying fewer arguments to create a new function
Explanation:
F# supports partial application. Providing some arguments to a function can produce another function that accepts the remaining arguments.
42. What does function composition using the >> operator do?
- Runs two functions in parallel
- Composes functions from left to right
- Compares two functions
- Converts a function to a value
Answer: B) Composes functions from left to right
Explanation:
The >> operator composes two functions so that the output of the first function becomes the input to the second function.
43. Which keyword is used to introduce an asynchronous computation expression in F#?
async
await
future
parallel
Answer: A) async
Explanation:
The async { ... } computation expression is used to construct asynchronous computations in F#. Operations such as let! can bind the result of another asynchronous computation.
44. What does let! commonly do inside an F# computation expression?
- Defines a class
- Binds the result of a computation
- Declares a mutable variable
- Creates a record
Answer: B) Binds the result of a computation
Explanation:
Inside computation expressions, let! binds the result produced by another computation according to the rules defined by the computation-expression builder.
45. Which keyword returns a value from an F# computation expression?
yield
return
result
output
Answer: B) return
Explanation:
The return keyword produces the final value of a computation expression according to the builder's Return implementation.
46. What is the purpose of the yield keyword in an F# sequence expression?
- To define a class
- To produce an element of the sequence
- To terminate the program
- To create an exception
Answer: B) To produce an element of the sequence
Explanation:
Within a sequence expression, yield produces an individual element. The resulting sequence can generate those elements during enumeration.
47. What does the following F# code return?
let result =
match Some 10 with
| Some x -> x * 2
| None -> 0
- 0
- 10
- 20
- None
Answer: C) 20
Explanation:
The value is Some 10, so the first pattern matches and binds x to 10. The expression x * 2 therefore produces 20.
48. What is the output of the following F# code?
let numbers = [1; 2; 3; 4]
let result = numbers |> List.filter (fun x -> x % 2 = 0)
printfn "%A" result
[1; 3]
[2; 4]
[1; 2]
[3; 4]
Answer: B) [2; 4]
Explanation:
The predicate keeps only numbers whose remainder after division by 2 is zero. Therefore, the resulting list contains 2 and 4.
49. What does the following F# code evaluate to?
let classify n =
match n with
| x when x > 0 -> "Positive"
| x when x < 0 -> "Negative"
| _ -> "Zero"
classify -7
- Positive
- Negative
- Zero
- Error
Answer: B) Negative
Explanation:
The first guard fails because -7 is not greater than zero. The second guard succeeds because -7 is less than zero, so the function returns "Negative".
50. What is the result of the following F# code?
type Shape =
| Circle of float
| Rectangle of float * float
let area shape =
match shape with
| Circle r -> System.Math.PI * r * r
| Rectangle (w, h) -> w * h
let result = area (Rectangle (5.0, 4.0))
9.0
20.0
25.0
62.83
Answer: B) 20.0
Explanation:
The value passed to area is the Rectangle case with width 5.0 and height 4.0. Pattern matching selects the rectangle branch, which calculates 5.0 * 4.0, producing 20.0.
Advertisement
Advertisement