Start of Scala Functional Programming Concepts Quiz
1. What is functional programming?
- Functional programming is a technique that emphasizes the importance of mutable data within a program.
- Functional programming is a paradigm that uses functions as the central building block of programs, striving to use pure functions and immutable values.
- Functional programming is a method that primarily focuses on using classes and objects in software design.
- Functional programming is a style where procedures are the main components of program logic.
2. What are some key concepts in functional programming?
- Key concepts include encapsulation, polymorphism, inheritance, and abstraction.
- Key concepts include immutability, referential transparency, function composition, and lazy evaluation.
- Key concepts include recursion, iteration, state mutation, and side effects.
- Key concepts include variables, constants, loops, and conditionals.
3. What is immutability in functional programming?
- Immutability means using constants that cannot change, ensuring thread safety.
- Immutability involves using mutable states to enhance performance in coding.
- Immutability is the process of deleting variables in a program, freeing memory.
- Immutability refers to making all variables dynamic, allowing frequent changes.
4. What is referential transparency in functional programming?
- Referential transparency means that the output of a function depends only on its inputs and not on any side effects or external state.
- Referential transparency means that function results are always predictable regardless of input.
- Referential transparency means that outputs can vary with random state fluctuations.
- Referential transparency means that a function can modify its input parameters directly.
5. What is pattern matching in functional programming?
- Pattern matching is a technique used to handle different cases in a program, allowing for more expressive and concise code.
- Pattern matching means assigning values to multiple variables at once.
- Pattern matching is a process of merging multiple functions into one.
- Pattern matching refers to the use of loops to iterate over data.
6. What is function composition in functional programming?
- Function composition refers to the transformation of function parameters into arrays for efficiency.
- Function composition is the method by which variables are created and used in functional programming.
- Function composition is the process of combining two or more functions to create a new function, allowing for more complex operations to be performed.
- Function composition is strictly alternative programming methodologies that simplify code organization.
7. What is lazy evaluation in functional programming?
- Lazy evaluation allows for the simultaneous execution of multiple functions in functional programming.
- Lazy evaluation is a mechanism where expressions are only evaluated when their values are actually needed, which can improve performance by delaying unnecessary computations.
- Lazy evaluation is when functions are defined but never executed, leading to static performance.
- Lazy evaluation means executing all expressions immediately to ensure all values are computed at once.
8. What are higher-order functions in Scala?
- Higher-order functions in Scala are functions that cannot accept parameters.
- Higher-order functions in Scala are functions that only return boolean values.
- Higher-order functions in Scala take other functions as parameters or return functions as output.
- Higher-order functions in Scala are functions that use only primitive data types.
9. What is the map function in Scala?
- The map function combines elements of two collections into a single collection.
- The map function generates a fixed-size array based on input parameters.
- The map function sorts a collection and removes duplicates from it.
- The map function transforms one collection into another by applying a function to each element, changing the element type but keeping the collection type.
10. What is the flatMap function in Scala?
- The flatMap function transforms data types without changing the structure of the collection.
- The flatMap function is similar to map but removes the inner grouping of an item and generates a sequence.
- The flatMap function sums all elements in a collection and returns a single value.
- The flatMap function searches for a specific element in a collection and returns its index.
11. What is the filter function in Scala?
- The filter function removes duplicates from a collection entirely.
- The filter function adds new elements into a collection.
- The filter function sorts a collection in ascending order.
- The filter function creates a new collection containing only elements that satisfy a given predicate.
12. What is the reduce function in Scala?
- The reduce function performs a sort operation on the elements of a collection.
- The reduce function maps each element of a collection to a new collection type.
- The reduce function filters elements from a collection based on a condition.
- The reduce function applies a binary operator to all elements in a collection, reducing them to a single value.
13. How does Scala enforce immutability?
- Scala enforces immutability by allowing only primitive data types to be mutable, keeping complex types constant.
- Scala enforces immutability by making all collections mutable, which prevents unintended data changes.
- Scala enforces immutability by making variables immutable by default, requiring the use of mutable collections like ArrayBuffer for mutable state.
- Scala enforces immutability by using mutable variables for all data types, which allows easy state changes.
14. What is the main entry point of a Scala program?
- run() procedure
- main() method
- execute() command
- start() function
15. How do you create a single-line comment in Scala?
- #
- /* */
- //
- —
16. What is the default value of a string variable in Scala?
- Null
- “
- Undefined
- None
17. What is the subtype of all types in Scala?
- Nothing
- Something
- Everything
- Anything
18. What is returned by a function with no return value in Scala?
- Unit
- None
- Null
- Nothing
19. Can you return two values from a function in Scala?
- Yes, you can return two values from a function in Scala using Options.
- No, you can only return one value from a function.
- No, Scala does not support multiple return values.
- Yes, but only with mutable variables.
20. How do you define a higher-order function in Scala?
- You define a higher-order function in Scala by implementing a class that inherits from a base function type.
- You define a higher-order function in Scala by creating a variable that stores the function`s return value.
- You define a higher-order function in Scala by using recursion to call the same function repeatedly.
- You define a higher-order function in Scala by taking another function as a parameter or returning a function as output, as seen in the example with map.
21. What is the range defined by the statement range(13, 17, 2)?
- 14, 16
- 12, 18
- 15, 17
- 13, 15
22. Which method is not used for printing text on the screen in Scala?
- print()
- printf()
- println()
- printl()
23. What is the output of the code val i = 43; i = 231; printf(i)?
- 231
- Undefined
- Error, as the val keyword defines immutable values in Scala.
- 43
24. What are literals in Scala?
- Literals are methods that modify variables
- Literals are classes that store data types
- Literals are functions that calculate values
- Literals are values that can be assigned to a variable
25. Which conversion is lossy or invalid?
26. What type of typecasting is done by the compiler?
- Manual Conversion
- Static Casting
- Implicit Conversion
- Direct Casting
27. What is explicit conversion?
- Silent Conversion happens in the background.
- Implicit Conversion is decided by the compiler.
- Explicit Conversion is defined by the user.
- Automatic Conversion occurs without user intervention.
28. What is the purpose of the getClass() method?
- The getClass() method returns the runtime class of an object.
- The getClass() method sets the class name of an object.
- The getClass() method creates a new class instance.
- The getClass() method deletes the class of an object.
29. What executes a block of code based on some conditional value in Scala?
- Conditional statements
- Loop expressions
- Function literals
- Class definitions
30. Which is not a conditional statement in Scala?
- This statement is not a conditional statement in Scala.
- for (i <- 1 to 5) { println(i) }
- while (true) { println(`Loop`) }
- if (x > 10) println(`High`)