MCQs on Functions and Methods | Scala

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

  1. 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 }
  2. What keyword is used to define a function in Scala?
    • A) function
    • B) def
    • C) func
    • D) method
  3. 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) }
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. 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
  2. Which symbol is used to separate parameters and function body in a lambda?
    • A) =>
    • B) ->
    • C) ::
    • D) =
  3. 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)
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. Which method converts a lambda to a function value?
    • A) apply
    • B) toFunction
    • C) asFunction
    • D) lift
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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)
  5. 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
  6. 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
  7. 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
  8. 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
  9. Can functions with default arguments have overloaded versions?
    • A) Yes
    • B) No
    • C) Only with different types
    • D) Only with positional arguments
  10. 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

QnoAnswer
1A) def functionName(parameters): returnType = { body }
2B) def
3B) By simply writing functionName(arguments)
4A) Yes, using tuples
5A) The last evaluated expression
6C) By simply calling the function within itself
7A) def add(a: Int, b: Int): Int = a + b
8B) It denotes the function does not return a meaningful value
9D) Yes, using * after the argument name
10A) After the parameter list, separated by a :
11B) A concise way to define anonymous functions
12A) =>
13B) By passing arguments to the lambda like a regular function
14B) It depends on the number of arguments
15A) Yes, enclosed in parentheses
16B) x => x * 2
17A) A function literal adding two integers
18A) Yes
19D) lift
20B) Int => String
21A) An argument passed with its name explicitly
22B) By using name = value syntax
23B) Arguments with a pre-defined value in the function definition
24A) def func(x: Int = 5): Int = x + 10
25A) Yes, named arguments must follow positional ones
26B) The default value is used
27A) Yes, by specifying a new value
28A) Hello, World!
29A) Yes
30D) All of the above

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