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
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
How do you declare a nullable variable in Dart? a) int num = null; b) int? num; c) nullable int num; d) int num = 0;
What operator is used to check for null and provide a fallback value in Dart? a) ?? b) && c) != d) ==
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
Which statement is valid when working with null safety? a) String name; b) String? name; c) nullable String name; d) String? name = '';
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
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
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
How do you declare a non-nullable list in Dart? a) List? myList = []; b) List myList; c) List<String?> myList = []; d) List<String> myList = [];
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
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
How is the main() function defined in Dart? a) void Main() b) main() {} c) void main() {} d) public static void main()
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
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) {}
What is the default return type of the main() function? a) int b) void c) null d) String
Which of the following is valid for asynchronous main() in Dart? a) void main() b) Future<void> main() c) async main() d) Future main()
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
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
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
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
How do you write a single-line comment in Dart? a) # b) // c) <!-- --> d) /* */
How do you write a multi-line comment in Dart? a) ## b) <!-- --> c) /* */ d) """ """
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
Which tool helps enforce consistent formatting in Dart code? a) Dart Formatter b) Flutter Format c) dartfmt d) IntelliJ Formatter
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
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
What is the correct syntax for documentation comments in Dart? a) /// b) /** */ c) <!-- --> d) /*** */
Which Dart tool ensures code adheres to best practices and style guidelines? a) Dart Analyzer b) Code Formatter c) Dart Compiler d) Dart Debugger
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
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
Qno
Answer
1
a) To avoid null pointer exceptions
2
b) int? num;
3
a) ??
4
a) Makes a nullable variable non-nullable
5
b) String? name;
6
a) Compilation error
7
a) Declare it with late and assign a value before usage
8
c) Dart 2.12+
9
d) List<String> myList = [];
10
b) Allows safe access to methods or properties on nullable objects
11
b) It defines the entry point of a Dart program
12
c) void main() {}
13
c) Yes, it can return Future<void>
14
c) void main(List<String> args) {}
15
b) void
16
b) Future<void> main()
17
d) dart filename.dart
18
a) Yes, by defining them in the function signature
19
a) Compilation error
20
b) No, it is the entry point and must remain unique
21
b) //
22
c) /* */
23
b) To document the code and improve readability
24
c) dartfmt
25
b) Comment it out with //
26
d) 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