F# Interview Questions

1) What is F#?
F# is a Computer programming language. It was designed and developed by Microsoft. It supports functional, object-oriented and imperative programming approaches. You can create an application by using this programming language.

2) What are the features of F#?
F# has many features. Following are the main features of F#:

Type inference
Type extension
Less code
Immutable data
Pattern matching
Assertion
Lambda expression
Function composition and pipelining
Object expression
Lazy computation and many more
Pause

Next
Mute
Current Time
0:11
/
Duration
18:10

Fullscreen
3) What are the available data types in F#?
F# provides a rich set of data types. It helps to deal with any data whether it is scientific data, data of business analysis, etc. You can see the table of data types here.

Data Types in F#

Types
Data Types
Primitive data types
char, byte, bool, int, float
Derived data types
class, array, list, records, sequence
Enumeration
enum
Unit type
It is used if other data types are not specified.
4) What is the unit type in F#?
The unit type is a type which indicates the absence of specific value. The unit type has only a single value. This value acts as a placeholder when no other value exist.

Example:

let function1 x y = x + y

function1 10 20 // this line results a compiler warning

// changing the code to the following and eliminates the warning
let result = function1 10 20

// Use this if you are calling the function and don’t want the return value
function1 10 20 |> ignore
5) What is upcasting and downcasting in F#?
Casting is a process of converting one type to another type. F# provides mainly two operators to deal with upcasting and downcasting. The :> operator is used to upcast object and :?> operator is used to downcast object.

Example:

type BaseClass() =
class
member this.ShowClassName()=
printfn “BaseClass”
end

type DerivedClass() =
class
inherit BaseClass()
member this.ShowClassName()=
printfn “DerivedClass”
end

let baseClass = new BaseClass()
let derivedClass : DerivedClass = new DerivedClass()
baseClass.ShowClassName()
derivedClass.ShowClassName()
let castIntoBaseClass = derivedClass :> BaseClass // upcasting
castIntoBaseClass.ShowClassName()
let castIntoDerivedClass = baseClass :?> DerivedClass // downcasting
castIntoDerivedClass.ShowClassName()
6) What are available operators in F#?
An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, bitwise, logical, etc.

There are following types of operators to perform different types of operations in F# language.

Arithmetic operators: The Arithmetic operators take numerical values as their operands and return a single numerical value.
Boolean operators: The Boolean operators are used to check conditional expressions. It returns true if expression satisfies the condition otherwise it returns false.
Bitwise operators: In F#, the bitwise operator works on individual bits and return a result after evaluation.
Nullable operators: The Nullable operators are used to work with database queries. It handles null values which are stored in tables in place of data.
7) Which tokens are available in F#?
Keywords: It contains the links to information about all F# language keywords.
Symbol and operators: It contains a table of symbols and operators that are used in the F# language.
8) What is function composition and pipelining in F#?
In F#, functions can be composed from other functions. It is a process of composing in which a function represents the application of two composed functions. F# function pipelining allows us to call functions in the chain. Pipelining operator takes a function and an argument as operands and returns a value.

F# Function Composition Example
let function1 name=
name + ” FSharp”
let function2 name =
name + ” Programming”

let programmingName = function1 >> function2
let result = programmingName “Hello”
printf “%s” result
F# Function Pipelining Example
let function1 name=
name + ” FSharp”
let function2 name =
name + ” Programming”

let result = “Hello” |> function1 |> function2
printf “%s” result
9) What is lambda expression in F#?
Lambda expression is an unnamed or anonymous function. Sometimes instead of defining a full name function, you may create a lambda expression. It optimizes the code. You must use a fun keyword to define lambda expression.

Example
let result = (fun a -> a+1) 100
printf “%d” result
10) What is an inline function in F#?
The F# inline function is a function that is integrated directly into the calling code. It helps to optimize code and sometimes can improve performance too.

Example
type InlineClass() =
class
member inline this.inlineFunction(a) = a*a
end

let obj = new InlineClass()
let result = obj.inlineFunction 2
printf “%d” result

11) What is let bindings in F#?
Binding is a process of associating of identifier or function to a value. Let keyword is used to bind an identifier to a value. In F#, We use let keyword to declare variable, function and private class members.

F# Let binding in function
We use let keyword to define a function in F#.

let ShowName() =
printf “Hello FSharp”
ShowName()

12) What is type annotation in F#?
F# allows type annotation so that you can explicitly mention the type of identifier or parameter or return type of a function. You must use: (colon) to apply annotation in F#.

Example
let a:int = 10
printf “%d” a

13) What is “do bindings” in F#?
Do binding is used to execute code without defining a function or any type. You can write independent code by using do binding in F#.

Example
type DoBindingClass() =
class
do printf “Hello FSharp”
end
new DoBindingClass()

14) What is Type Inference in F#?
Type inference means when you are writing code then you don’t need to specify the type of values or variables. F# compiler is strong enough to infer the type of value.

Example
let add a b = //Here, all parameters are inferred to int because of passing values during calling
a+b // Return type of this function is int because the type of this expression is int.
printf “%d” (add 10 20)
15) What is Automatic Generalization in F#?
When code does not specify any type explicitly, then the compiler considers generic type. It is called an automatic generalization. It helps to write generic code without increasing complexity.

16) What are the Tuples in F#?
In F#, tuples are a collection of anonymous values. Values may be the same or different types. It allows us to put expression as a value also.

Example
let (id,name) = (1, “Fsharp”)
printfn “%d” id
printfn “%s” name
17) Can a function return multiple values in F#?
Yes, by using a tuple, you can return multiple values in a function.

Example
let TupleExample a b =
let c = a+b
let d = a-b
(c, d)

let tupleValues = TupleExample 50 10
printf “%A” tupleValues
18) What is a list in F#?
It is an immutable collection of same type elements. It maintains the order of elements.

F# List Example
let list = [1;2;3;4;5;6;7;8;9]
for i in list do
printfn “%d” i

19) What is Sequence in F#?
The Sequence is a series of the same type of elements. It provides better performance than list.

Example
You can create sequence expression like following. Here, we have used Seq.iter () function to iterate sequence. We can also use for loop or array format specifier to iterate sequence elements.

let seq1 =
seq { 0 .. 10 }
Seq.iter(fun a->printf ” %d” a)seq1

20) What is Array in F#?
Arrays are mutable collections of data of the same type. It starts from index 0 and goes to n-1 where n is the length of arrays.

Example
let arr = [| 1; 2; 3; 4; 5; |] // Creating and initializing array
for i = 0 to arr.Length-1 do // Traversing of array
printfn “%d” arr.[i]


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *