Master key Scala concepts like defining and invoking functions, using function literals and lambdas, and understanding named and default arguments with these 30 multiple-choice questions designed to sharpen your expertise.
Functions and Methods in Scala
1. Defining and Invoking Functions
How do you define a function in Scala?
A) def functionName(parameters): returnType = { body }
B) function functionName(parameters): returnType { body }
C) def functionName(parameters) -> returnType { body }
D) func functionName: parameters -> returnType { body }
What keyword is used to define a function in Scala?
A) function
B) def
C) func
D) method
How do you invoke a function in Scala?
A) By using the call keyword
B) By simply writing functionName(arguments)
C) By using invoke(functionName, arguments)
D) By using brackets { functionName(arguments) }
Can a function in Scala return multiple values?
A) Yes, using tuples
B) No, only single values are allowed
C) Yes, but only with lists
D) No, Scala does not support it
What is the return type of a function with no explicit return statement?
A) The last evaluated expression
B) Always Unit
C) Always Any
D) None
How do you define a recursive function in Scala?
A) By using the recursive keyword
B) By marking it as @tailrec
C) By simply calling the function within itself
D) By using the self keyword
Which of the following is a valid Scala function?
A) def add(a: Int, b: Int): Int = a + b
B) function add(a: Int, b: Int): Int { return a + b }
C) func add(a, b): Int { a + b }
D) def add(a: Int, b: Int) -> Int: a + b
What is the purpose of the Unit return type in a Scala function?
A) It ensures the function must return a value
B) It denotes the function does not return a meaningful value
C) It specifies the function can return any value
D) It is used for functions that return collections
Can Scala functions have variable-length arguments?
A) Yes, using * before the argument name
B) Yes, using ... after the argument name
C) Yes, using * after the argument type
D) Yes, using * after the argument name
How do you specify the return type of a function in Scala?
A) After the parameter list, separated by a :
B) Before the parameter list, separated by ->
C) After the parameter list, enclosed in {}
D) Before the function name, prefixed with type
2. Function Literals and Lambdas
What is a function literal in Scala?
A) A named function defined in a class
B) A concise way to define anonymous functions
C) A function stored as a string literal
D) A reserved keyword for methods
Which symbol is used to separate parameters and function body in a lambda?
A) =>
B) ->
C) ::
D) =
How do you invoke a lambda function in Scala?
A) Using the call keyword
B) By passing arguments to the lambda like a regular function
C) By assigning it to a variable
D) Using invoke(lambda, arguments)
What is the type of a lambda function in Scala?
A) It is always Function1
B) It depends on the number of arguments
C) It is always inferred as Any
D) It must be explicitly defined
Can lambda functions have multiple parameters in Scala?
A) Yes, enclosed in parentheses
B) No, only one parameter is allowed
C) Yes, separated by commas
D) No, parameters are optional
What is the correct syntax for a lambda function with one parameter in Scala?
A) x -> x * 2
B) x => x * 2
C) x :: x * 2
D) x = x * 2
What does the following represent: (x: Int, y: Int) => x + y?
A) A function literal adding two integers
B) A method definition
C) A variable declaration
D) A tuple of integers
Can lambdas be used as arguments to higher-order functions in Scala?
A) Yes
B) No
C) Only when explicitly cast
D) Only with type inference
Which method converts a lambda to a function value?
A) apply
B) toFunction
C) asFunction
D) lift
What is the return type of this lambda: (x: Int) => x.toString?
A) Function1[Int, String]
B) Int => String
C) String
D) Any
3. Named and Default Arguments
What is a named argument in Scala?
A) An argument passed with its name explicitly
B) A global variable passed to a function
C) A reserved parameter in Scala methods
D) A default parameter value
How are named arguments specified in a function call?
A) By prefixing the argument name with :
B) By using name = value syntax
C) By enclosing the name and value in {}
D) By declaring them at the start of the function
What are default arguments in Scala?
A) Arguments that cannot be changed
B) Arguments with a pre-defined value in the function definition
C) Arguments that must be specified explicitly
D) Arguments declared using def keyword
How do you define a function with a default argument in Scala?
A) def func(x: Int = 5): Int = x + 10
B) def func(x = 5: Int): Int = x + 10
C) func def x: Int = 5 -> Int + 10
D) func def x(Int 5 = value)
Can you mix named and positional arguments in Scala?
A) Yes, named arguments must follow positional ones
B) No, mixing is not allowed
C) Yes, but only with default arguments
D) Only in higher-order functions
What happens if you skip a default argument in a function call?
A) It throws a runtime error
B) The default value is used
C) It assumes the value null
D) The function fails compilation
Can default arguments be overridden in Scala?
A) Yes, by specifying a new value
B) No, they are fixed during function definition
C) Yes, only for the last parameter
D) No, only named arguments can override them
What is the output of def greet(name: String = "World"): String = s"Hello, $name!" when invoked as greet()?
A) Hello, World!
B) Hello, !
C) It throws a runtime error
D) The function fails compilation
Can functions with default arguments have overloaded versions?
A) Yes
B) No
C) Only with different types
D) Only with positional arguments
How do default arguments improve code readability?
A) By reducing the need for method overloading
B) By simplifying function calls
C) By minimizing boilerplate code
D) All of the above
Answers
Qno
Answer
1
A) def functionName(parameters): returnType = { body }
2
B) def
3
B) By simply writing functionName(arguments)
4
A) Yes, using tuples
5
A) The last evaluated expression
6
C) By simply calling the function within itself
7
A) def add(a: Int, b: Int): Int = a + b
8
B) It denotes the function does not return a meaningful value
9
D) Yes, using * after the argument name
10
A) After the parameter list, separated by a :
11
B) A concise way to define anonymous functions
12
A) =>
13
B) By passing arguments to the lambda like a regular function
14
B) It depends on the number of arguments
15
A) Yes, enclosed in parentheses
16
B) x => x * 2
17
A) A function literal adding two integers
18
A) Yes
19
D) lift
20
B) Int => String
21
A) An argument passed with its name explicitly
22
B) By using name = value syntax
23
B) Arguments with a pre-defined value in the function definition
24
A) def func(x: Int = 5): Int = x + 10
25
A) Yes, named arguments must follow positional ones