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
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
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
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
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);
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
Can closures exist inside functions in Dart? a) Yes, closures can be created inside functions b) No, closures cannot exist inside functions
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
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'); }
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
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
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
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
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
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; }
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
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
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'); }
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
Which Dart collection method is an example of a higher-order function? a) map() b) forEach() c) where() d) All of the above
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
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
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
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
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
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
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
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
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
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
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
Qno
Answer (Option with Text)
1
b) A function that captures variables from its surrounding scope
2
c) It captures variables from its surrounding scope
3
a) It allows access to private variables outside their scope
4
b) var counter = 0; var increment = () { counter++; };
5
c) By capturing and modifying variables in their surrounding scope
6
a) Yes, closures can be created inside functions
7
b) They allow functions to modify variables from their surrounding scope
8
b) var counter = 0; var increaseCounter = () { counter++; };
9
c) To capture variables in callbacks
10
c) Capturing variables from global scope
11
b) A function declared using the => syntax
12
b) It uses the => operator for single-expression functions