Master Functions in Dart with these 30 carefully designed MCQs. Explore key topics such as declaring and invoking functions, parameters and return types, optional and named parameters, and arrow functions. Test your knowledge now!
MCQs on Functions in Dart
Declaring and Invoking Functions
How do you declare a function in Dart? a) function myFunction() b) void myFunction() c) func myFunction() d) def myFunction()
Which keyword is used to return a value from a Dart function? a) output b) send c) return d) give
What is the correct syntax to call a function named calculate() in Dart? a) call calculate(); b) calculate[]; c) calculate(); d) function calculate();
What is the return type of a function in Dart that doesn’t return a value? a) null b) void c) dynamic d) none
How do you define a function that returns an integer in Dart? a) int myFunction { return 1; } b) int myFunction() => 1; c) void myFunction() { return 1; } d) function myFunction() { return 1; }
In Dart, what happens if no return statement is specified in a function? a) The program crashes b) The function returns null c) The function returns 0 d) The function doesn’t compile
Which of the following is NOT a valid function name in Dart? a) addNumbers b) _calculate c) my-function d) findSum
In Dart, can a function be nested inside another function? a) Yes, it is allowed b) No, it is not supported
What is the main advantage of using functions in Dart? a) They speed up execution b) They allow code reuse and modularity c) They reduce memory usage d) They handle exceptions automatically
In Dart, functions are: a) Not first-class objects b) First-class objects c) Only callable from the main() function d) Must be declared as public
Parameters and Return Types
Which of the following defines a function with a single integer parameter? a) int add(x) {} b) void add(int x) {} c) add(int x) {} d) var add(x) {}
What does the following function return? int multiply(int a, int b) { return a * b; } a) The sum of a and b b) The product of a and b c) null d) An error
In Dart, can a function have no parameters? a) Yes, all functions must have parameters b) No, functions can exist without parameters
What is the data type of the return value of this function? String greet(String name) { return "Hello, $name"; } a) void b) String c) dynamic d) List
How can you ensure that a Dart function returns a specific data type? a) Declare the return type explicitly before the function name b) Use a specific annotation inside the function body c) Declare the return type explicitly after the function name d) Dart does not support type-specific returns
If a function returns null, what is its likely return type? a) dynamic b) void c) int d) Null
What is the default return type of a Dart function? a) void b) dynamic c) null d) String
In Dart, can a function have multiple return statements? a) Yes, functions can have multiple return statements b) No, only one return statement is allowed
Which operator is used to specify a function’s return type in Dart? a) = b) -> c) : d) None, return types are declared explicitly
What does the following function return? double divide(int a, int b) { return a / b; } a) An integer b) A double c) A string d) None of the above
Optional and Named Parameters
How are optional parameters denoted in Dart? a) By using square brackets [] b) By using curly braces {} c) By using parentheses () d) By using ?
What is the correct way to define a function with a named parameter? a) void myFunction({int x}) b) void myFunction([int x]) c) void myFunction(?int x) d) void myFunction(int x)
In Dart, what happens if a named parameter is not provided a value? a) An error is thrown b) It defaults to null c) It uses the last assigned value d) It defaults to 0
How can you specify a default value for an optional parameter? a) void myFunction([int x = 10]) b) void myFunction({int x}) c) void myFunction([int x == 10]) d) void myFunction({int x = 10})
Named parameters are especially useful when: a) A function takes multiple parameters with clear roles b) A function is recursive c) A function returns a List d) A function uses only primitive data types
Can optional parameters in Dart be both named and positional? a) Yes b) No
How are required named parameters defined in Dart? a) By using the required keyword b) By setting a default value c) By placing them inside square brackets d) By using an exclamation mark !
What is the primary advantage of using named parameters? a) Faster execution b) Clearer and more readable function calls c) Avoids runtime errors d) Automatically checks types
Which of the following is a valid declaration of a function with both optional and named parameters? a) void myFunction(int a, [int b], {int c}) b) void myFunction(int a, int b, int c) c) void myFunction({int a, int b, int c}) d) void myFunction([int a, int b], {int c})
What happens if you call a function with an optional parameter but do not pass a value? a) It throws an error b) The parameter takes its default value or null c) The function does not execute d) The compiler generates a warning
Answers Table
Qno
Answer
1
b) void myFunction()
2
c) return
3
c) calculate();
4
b) void
5
b) int myFunction() => 1;
6
b) The function returns null
7
c) my-function
8
a) Yes, it is allowed
9
b) They allow code reuse and modularity
10
b) First-class objects
11
b) void add(int x) {}
12
b) The product of a and b
13
b) No, functions can exist without parameters
14
b) String
15
a) Declare the return type explicitly before the function name
16
d) Null
17
b) dynamic
18
a) Yes, functions can have multiple return statements
19
d) None, return types are declared explicitly
20
b) A double
21
a) By using square brackets []
22
a) void myFunction({int x})
23
b) It defaults to null
24
d) void myFunction({int x = 10})
25
a) A function takes multiple parameters with clear roles