MCQs on Dart Syntax Essentials | Dart

Explore Dart Syntax Essentials with these 30 MCQs, covering null safety introduction, the main() function, comments and formatting, and debugging basics. Master foundational Dart programming concepts effectively through these topic-wise questions.


Null Safety Introduction

  1. What is the primary purpose of null safety in Dart?
    a) To avoid null pointer exceptions
    b) To optimize code formatting
    c) To improve runtime performance
    d) To simplify debugging
  2. How do you declare a nullable variable in Dart?
    a) int num = null;
    b) int? num;
    c) nullable int num;
    d) int num = 0;
  3. What operator is used to check for null and provide a fallback value in Dart?
    a) ??
    b) &&
    c) !=
    d) ==
  4. What does the ! operator do in Dart null safety?
    a) Makes a nullable variable non-nullable
    b) Assigns null to a variable
    c) Throws an error if the value is null
    d) Converts non-nullable to nullable
  5. Which statement is valid when working with null safety?
    a) String name;
    b) String? name;
    c) nullable String name;
    d) String? name = '';
  6. What happens if you try to assign null to a non-nullable variable?
    a) Compilation error
    b) Runtime error
    c) The variable is set to null by default
    d) The program continues without issues
  7. How do you initialize a late variable in Dart?
    a) Declare it with late and assign a value before usage
    b) Assign null to it
    c) Use the ? operator
    d) Declare it as final
  8. Which of the following enables null safety in Dart?
    a) Dart 1.x
    b) Dart 2.x
    c) Dart 2.12+
    d) Dart 3.x
  9. How do you declare a non-nullable list in Dart?
    a) List? myList = [];
    b) List myList;
    c) List<String?> myList = [];
    d) List<String> myList = [];
  10. What does the ?. operator do in Dart?
    a) Throws an exception when the object is null
    b) Allows safe access to methods or properties on nullable objects
    c) Forces non-nullable access
    d) Assigns null to a variable

The main() Function

  1. What is the role of the main() function in Dart?
    a) It initializes variables
    b) It defines the entry point of a Dart program
    c) It handles all asynchronous tasks
    d) It is optional in Dart
  2. How is the main() function defined in Dart?
    a) void Main()
    b) main() {}
    c) void main() {}
    d) public static void main()
  3. Can the main() function return a value in Dart?
    a) Yes, it must return an integer
    b) No, it cannot return any value
    c) Yes, it can return Future<void> for async operations
    d) No, it is predefined to return null
  4. What is the correct way to pass command-line arguments to the main() function?
    a) void main() {}
    b) void main(String args) {}
    c) void main(List<String> args) {}
    d) void main(arguments) {}
  5. What is the default return type of the main() function?
    a) int
    b) void
    c) null
    d) String
  6. Which of the following is valid for asynchronous main() in Dart?
    a) void main()
    b) Future<void> main()
    c) async main()
    d) Future main()
  7. How do you execute a Dart program from the command line?
    a) dart run filename.dart
    b) dart execute filename.dart
    c) dart start filename.dart
    d) dart filename.dart
  8. Can the main() function accept optional arguments?
    a) Yes, by defining them in the function signature
    b) No, only required arguments are allowed
    c) Yes, using a Map data structure
    d) No, it only accepts List arguments
  9. What happens if the main() function is missing in a Dart program?
    a) Compilation error
    b) The program runs with a default main function
    c) Runtime error
    d) The program exits without running
  10. In Dart, can main() be overloaded?
    a) Yes, with multiple parameter lists
    b) No, it is the entry point and must remain unique
    c) Yes, by defining multiple main functions
    d) No, it’s not allowed in any language

Comments and Formatting

  1. How do you write a single-line comment in Dart?
    a) #
    b) //
    c) <!-- -->
    d) /* */
  2. How do you write a multi-line comment in Dart?
    a) ##
    b) <!-- -->
    c) /* */
    d) """ """
  3. What is the purpose of comments in Dart?
    a) To enhance code performance
    b) To document the code and improve readability
    c) To execute code snippets
    d) To debug the program
  4. Which tool helps enforce consistent formatting in Dart code?
    a) Dart Formatter
    b) Flutter Format
    c) dartfmt
    d) IntelliJ Formatter
  5. How can you disable a specific line of code during execution in Dart?
    a) Use #
    b) Comment it out with //
    c) Use the break keyword
    d) It cannot be disabled
  6. Which of the following is not a valid type of comment in Dart?
    a) Single-line comment
    b) Multi-line comment
    c) Documentation comment
    d) Inline execution comment
  7. What is the correct syntax for documentation comments in Dart?
    a) ///
    b) /** */
    c) <!-- -->
    d) /*** */
  8. Which Dart tool ensures code adheres to best practices and style guidelines?
    a) Dart Analyzer
    b) Code Formatter
    c) Dart Compiler
    d) Dart Debugger
  9. What does Dart’s dartfmt tool do?
    a) Runs a Dart script
    b) Formats Dart code for readability
    c) Optimizes Dart code performance
    d) Compiles Dart code
  10. Why is proper code formatting important in Dart?
    a) To improve program speed
    b) To adhere to Dart’s style guide and improve code readability
    c) To make debugging easier
    d) To enable null safety

Answer Key

QnoAnswer
1a) To avoid null pointer exceptions
2b) int? num;
3a) ??
4a) Makes a nullable variable non-nullable
5b) String? name;
6a) Compilation error
7a) Declare it with late and assign a value before usage
8c) Dart 2.12+
9d) List<String> myList = [];
10b) Allows safe access to methods or properties on nullable objects
11b) It defines the entry point of a Dart program
12c) void main() {}
13c) Yes, it can return Future<void>
14c) void main(List<String> args) {}
15b) void
16b) Future<void> main()
17d) dart filename.dart
18a) Yes, by defining them in the function signature
19a) Compilation error
20b) No, it is the entry point and must remain unique
21b) //
22c) /* */
23b) To document the code and improve readability
24c) dartfmt
25b) Comment it out with //
26d) Inline execution comment

27
a) ///
28
a) Dart Analyzer
29
b) Formats Dart code for readability
30
b) To adhere to Dart’s style guide and improve code readability

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