MCQs on Advanced Functions | Dart

Explore Advanced Functions in Dart with these 30 MCQs. Learn about closures, lambdas, higher-order functions, lexical scope, and anonymous functions. Test your knowledge on Dart’s function features now!


MCQs on Advanced Functions in Dart

Closures

  1. What is a closure in Dart?
    a) A function that doesn’t reference any external variables
    b) A function that captures variables from its surrounding scope
    c) A function without any parameters
    d) A function that only returns values
  2. Which of the following best describes the behavior of a closure?
    a) It only captures variables declared within it
    b) It cannot access variables outside of its scope
    c) It captures variables from its surrounding scope
    d) It only captures global variables
  3. In Dart, closures can:
    a) Modify the values of variables in the surrounding scope
    b) Only read values from the surrounding scope
    c) Only execute independently of the surrounding scope
    d) Be used only for recursion
  4. Which of the following code creates a closure in Dart?
    a) int add(int a, int b) { return a + b; }
    b) var counter = 0; var increment = () { counter++; };
    c) void subtract(int a, int b) { return a - b; }
    d) var message = 'Hello'; print(message);
  5. How do closures help with state management in Dart?
    a) By allowing access to private variables outside their scope
    b) By creating independent execution environments
    c) By capturing and modifying variables in their surrounding scope
    d) By avoiding memory allocation
  6. Can closures exist inside functions in Dart?
    a) Yes, closures can be created inside functions
    b) No, closures cannot exist inside functions
  7. What is the primary advantage of using closures in Dart?
    a) They prevent memory leaks
    b) They allow functions to modify variables from their surrounding scope
    c) They increase execution speed
    d) They enforce security restrictions
  8. Which of the following is an example of a closure in Dart?
    a) int multiply(int a, int b) { return a * b; }
    b) var counter = 0; var increaseCounter = () { counter++; };
    c) var subtract = (int a, int b) => a - b;
    d) void main() { print('Hello World'); }
  9. How are closures used in event-driven programming in Dart?
    a) To store event listeners
    b) To update the UI based on external variables
    c) To capture variables in callbacks
    d) To manage function parameters
  10. Which of the following would not be a typical use of closures in Dart?
    a) Storing functions that manipulate external state
    b) Creating private variables within a function
    c) Capturing variables from global scope
    d) Modifying the behavior of a function over time

Lambdas and Higher-Order Functions

  1. What is a lambda function in Dart?
    a) A function that doesn’t return a value
    b) A function declared using the => syntax
    c) A function that returns an anonymous function
    d) A function without parameters
  2. How does a lambda function differ from a regular function in Dart?
    a) It has a name
    b) It uses the => operator for single-expression functions
    c) It can only have one parameter
    d) It must return void
  3. What does this lambda function return in Dart?dartCopy codevar square = (int x) => x * x; a) The square of x
    b) A string containing the value of x
    c) x multiplied by 2
    d) null
  4. Which of the following is a valid higher-order function in Dart?
    a) List<int> filter(List<int> list, bool Function(int) test) { return list.where(test).toList(); }
    b) int multiply(int a, int b) { return a * b; }
    c) void printMessage() { print("Hello"); }
    d) String concatenate(String a, String b) { return a + b; }
  5. Which of the following best describes a higher-order function in Dart?
    a) A function that is invoked inside another function
    b) A function that only returns integers
    c) A function that takes or returns another function
    d) A function that executes in the background
  6. How do you pass a function as an argument to another function in Dart?
    a) By using the Function type
    b) By passing the function name
    c) By declaring a variable for the function
    d) By using the => operator
  7. Which of the following is an example of using a higher-order function in Dart?
    a) var add = (a, b) => a + b;
    b)List<int> addFive(List<int> numbers) { return numbers.map((x) => x + 5).toList(); }
    c) var multiply = (a, b) => a * b;
    d) void printHello() { print('Hello'); }
  8. How can a higher-order function be used for callback functions in Dart?
    a) By storing the function result
    b) By passing the function to be executed later
    c) By directly invoking the function
    d) By referencing the function name
  9. Which Dart collection method is an example of a higher-order function?
    a) map()
    b) forEach()
    c) where()
    d) All of the above
  10. What is the main advantage of using lambda functions in Dart?
    a) They improve memory usage
    b) They allow compact, one-line function declarations
    c) They improve function readability
    d) They make functions slower

Lexical Scope

  1. What does lexical scope refer to in Dart?
    a) The scope determined by where variables are declared
    b) The scope within a function
    c) The scope outside of all functions
    d) The scope assigned by the Dart compiler
  2. In Dart, if a variable is declared in a function, it is:
    a) Accessible throughout the entire program
    b) Accessible only within that function
    c) Accessible to all functions within the file
    d) Accessible to all global variables
  3. What happens if a variable in Dart is shadowed by a local variable with the same name?
    a) The global variable is accessible
    b) The local variable takes precedence within that scope
    c) Both variables are combined
    d) It causes an error
  4. How does Dart handle lexical scoping in nested functions?
    a) Inner functions can access both local and outer variables
    b) Inner functions can only access their local variables
    c) Inner functions cannot access any variables
    d) Outer functions can access only their local variables
  5. In which scope does the variable x reside in the following code? var x = 5; void test() { var x = 10; print(x); }
    a) Global scope
    b) Inside test() function
    c) Inside the main function
    d) Local scope
  6. Which of the following is an example of lexical scoping?
    a) A function accessing variables from an enclosing function
    b) A variable declared outside all functions being accessed inside a function
    c) A function invoking global variables
    d) None of the above
  7. Can Dart functions access variables declared in their outer scope after returning?
    a) Yes, because Dart supports lexical scoping
    b) No, once a function returns, outer variables are not accessible
  8. In lexical scoping, what happens if a function tries to access a variable that doesn’t exist in its scope?
    a) It throws an error
    b) It returns null
    c) It looks for the variable in the outer scope
    d) It creates a new variable
  9. What is the main benefit of lexical scoping in Dart?
    a) It reduces memory usage
    b) It allows functions to access all global variables
    c) It makes code easier to maintain and debug
    d) It ensures that variables are always constant
  10. Which of the following best illustrates Dart’s handling of lexical scope?
    a) A function can only access variables declared in the main function
    b) A function can access both local and outer variables
    c) A function can only access global variables
    d) Dart functions do not have lexical scoping

Answer Key

QnoAnswer (Option with Text)
1b) A function that captures variables from its surrounding scope
2c) It captures variables from its surrounding scope
3a) It allows access to private variables outside their scope
4b) var counter = 0; var increment = () { counter++; };
5c) By capturing and modifying variables in their surrounding scope
6a) Yes, closures can be created inside functions
7b) They allow functions to modify variables from their surrounding scope
8b) var counter = 0; var increaseCounter = () { counter++; };
9c) To capture variables in callbacks
10c) Capturing variables from global scope
11b) A function declared using the => syntax
12b) It uses the => operator for single-expression functions
13a) The square of x
14a) List<int> filter(List<int> list, bool Function(int) test) { return list.where(test).toList(); }
15c) A function that takes or returns another function
16a) By using the Function type
17b) var addFive = (int x) => x + 5;
18b) By passing the function to be executed later
19d) All of the above
20b) They allow compact, one-line function declarations
21a) The scope determined by where variables are declared
22b) Accessible only within that function
23b) The local variable takes precedence within that scope
24a) Inner functions can access both local and outer variables
25b) Inside test() function
26a) A function accessing variables from an enclosing function
27a) Yes, because Dart supports lexical scoping
28a) It throws an error
29c) It makes code easier to maintain and debug
30b) A function can access both local and outer variables

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