MCQs on Functions in Dart | Dart

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

  1. How do you declare a function in Dart?
    a) function myFunction()
    b) void myFunction()
    c) func myFunction()
    d) def myFunction()
  2. Which keyword is used to return a value from a Dart function?
    a) output
    b) send
    c) return
    d) give
  3. What is the correct syntax to call a function named calculate() in Dart?
    a) call calculate();
    b) calculate[];
    c) calculate();
    d) function calculate();
  4. What is the return type of a function in Dart that doesn’t return a value?
    a) null
    b) void
    c) dynamic
    d) none
  5. 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; }
  6. 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
  7. Which of the following is NOT a valid function name in Dart?
    a) addNumbers
    b) _calculate
    c) my-function
    d) findSum
  8. In Dart, can a function be nested inside another function?
    a) Yes, it is allowed
    b) No, it is not supported
  9. 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
  10. 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

  1. 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) {}
  2. 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
  3. In Dart, can a function have no parameters?
    a) Yes, all functions must have parameters
    b) No, functions can exist without parameters
  4. 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
  5. 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
  6. If a function returns null, what is its likely return type?
    a) dynamic
    b) void
    c) int
    d) Null
  7. What is the default return type of a Dart function?
    a) void
    b) dynamic
    c) null
    d) String
  8. 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
  9. Which operator is used to specify a function’s return type in Dart?
    a) =
    b) ->
    c) :
    d) None, return types are declared explicitly
  10. 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

  1. How are optional parameters denoted in Dart?
    a) By using square brackets []
    b) By using curly braces {}
    c) By using parentheses ()
    d) By using ?
  2. 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)
  3. 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
  4. 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})
  5. 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
  6. Can optional parameters in Dart be both named and positional?
    a) Yes
    b) No
  7. 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 !
  8. 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
  9. 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})
  10. 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

QnoAnswer
1b) void myFunction()
2c) return
3c) calculate();
4b) void
5b) int myFunction() => 1;
6b) The function returns null
7c) my-function
8a) Yes, it is allowed
9b) They allow code reuse and modularity
10b) First-class objects
11b) void add(int x) {}
12b) The product of a and b
13b) No, functions can exist without parameters
14b) String
15a) Declare the return type explicitly before the function name
16d) Null
17b) dynamic
18a) Yes, functions can have multiple return statements
19d) None, return types are declared explicitly
20b) A double
21a) By using square brackets []
22a) void myFunction({int x})
23b) It defaults to null
24d) void myFunction({int x = 10})
25a) A function takes multiple parameters with clear roles
26a) Yes
27a) By using the required keyword
28b) Clearer and more readable function calls
29a) void myFunction(int a, [int b], {int c})
30b) The parameter takes its default value or null

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