MCQs on Dart Basics | Dart

Mastering Dart basics is essential for building robust Flutter applications. This chapter delves into variables, data types, constants, type inference, comments, and code structure for clean, maintainable code.


Chapter: Dart Basics – MCQs

1. Variables and Data Types

  1. Which keyword is used to declare a variable in Dart?
    • a) var
    • b) val
    • c) let
    • d) int
  2. What is the data type of 3.14 in Dart?
    • a) int
    • b) double
    • c) num
    • d) float
  3. What is the default value of an uninitialized variable in Dart?
    • a) null
    • b) 0
    • c) undefined
    • d) ""
  4. Which of the following is a valid way to declare a list in Dart?
    • a) var list = [1, 2, 3];
    • b) let list = {1, 2, 3};
    • c) val list = [1, 2, 3];
    • d) array list = [1, 2, 3];
  5. What is the supertype of all numeric types in Dart?
    • a) int
    • b) double
    • c) num
    • d) Object
  6. How do you declare a map in Dart?
    • a) var map = {'key': 'value'};
    • b) let map = {'key': 'value'};
    • c) map = ('key', 'value');
    • d) dict map = ['key': 'value'];
  7. What is the output of var x; print(x); in Dart?
    • a) null
    • b) undefined
    • c) 0
    • d) An error
  8. What is the data type of a variable declared with dynamic in Dart?
    • a) It has no specific type
    • b) It is always a string
    • c) It is fixed at runtime
    • d) It can change type during runtime
  9. Which function is used to parse a string into an integer in Dart?
    • a) int.parse()
    • b) string.toInt()
    • c) toInt()
    • d) parseInt()
  10. What does is keyword check in Dart?
    • a) If two variables are equal
    • b) If a variable is null
    • c) If a variable matches a specific type
    • d) If a variable is greater than zero

2. Constants and Final

  1. What is the difference between const and final in Dart?
    • a) const is evaluated at runtime, final at compile-time
    • b) const is evaluated at compile-time, final at runtime
    • c) Both are the same
    • d) Neither can be used in Dart
  2. How do you declare a constant in Dart?
    • a) const pi = 3.14;
    • b) let pi = 3.14;
    • c) val pi = 3.14;
    • d) var pi = 3.14;
  3. Which of the following is true about final variables in Dart?
    • a) They must be initialized at compile-time
    • b) They can only be initialized once
    • c) Their values can be changed later
    • d) They cannot be used with classes
  4. Can a const variable in Dart reference an object?
    • a) Yes, but the object must be immutable
    • b) No, objects cannot be const
    • c) Yes, and the object can change
    • d) Only primitive types can be const
  5. What is the output of the following code?
    const a = 10; final b = 20; a = 30; print(b);
    • a) 30
    • b) An error
    • c) 20
    • d) 10

3. Type Inference

  1. What is type inference in Dart?
    • a) Manually specifying the type of a variable
    • b) Automatically determining the type of a variable
    • c) Declaring variables without initialization
    • d) Assigning the wrong type to a variable
  2. Which of the following demonstrates type inference in Dart?
    • a) var x = 5;
    • b) int x = 5;
    • c) dynamic x = 5;
    • d) x = 5;
  3. Can type inference be used with final variables in Dart?
    • a) Yes, always
    • b) No, never
    • c) Only if the variable is explicitly typed
    • d) Only with const
  4. What happens if you declare a variable with var and assign it a value of type String?
    • a) The type of the variable becomes String
    • b) The type remains dynamic
    • c) The type cannot be determined
    • d) An error occurs
  5. What is the inferred type of the following variable?dartCopy codevar score = 85;
    • a) dynamic
    • b) int
    • c) double
    • d) num

4. Comments and Code Structure

  1. What symbol is used for 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 code?
    • a) To execute special commands
    • b) To make code more readable and maintainable
    • c) To declare constants
    • d) To optimize runtime performance
  4. Which of the following is NOT a valid comment style in Dart?
    • a) ///
    • b) //
    • c) <!-- -->
    • d) /* */
  5. What is the purpose of /// in Dart?
    • a) To write inline comments
    • b) To document code for tools like DartDoc
    • c) To indicate deprecated code
    • d) To mark constants
  6. How does Dart organize code into blocks?
    • a) Using braces {}
    • b) Using parentheses ()
    • c) Using brackets []
    • d) Using indentation
  7. What is the main file that Dart starts executing from in a project?
    • a) main.dart
    • b) start.dart
    • c) index.dart
    • d) app.dart
  8. How do you structure a Dart function?
    • a) return_type function_name(parameters) { body }
    • b) function_name(parameters) { body }
    • c) def function_name { body }
    • d) function function_name { body }
  9. How do you return a value from a Dart function?
    • a) return value;
    • b) yield value;
    • c) return(value);
    • d) value return;
  10. What is the standard entry point of a Dart application?
    • a) void main() { }
    • b) start() { }
    • c) index() { }
    • d) application() { }

Answers Table

QnoAnswer
1a) var
2b) double
3a) null
4a) var list = [1, 2, 3];
5c) num
6a) var map = {'key': 'value'};
7a) null
8d) It can change type during runtime
9a) int.parse()
10c) If a variable matches a specific type
11b) const is evaluated at compile-time, final at runtime
12a) const pi = 3.14;
13b) They can only be initialized once
14a) Yes, but the object must be immutable
15b) An error
16b) Automatically determining the type of a variable
17a) var x = 5;
18a) Yes, always
19a) The type of the variable becomes String
20b) int
21a) //
22a) /* */
23b) To make code more readable and maintainable
24c) <!-- -->
25b) To document code for tools like DartDoc
26a) Using braces {}
27a) main.dart
28a) return_type function_name(parameters) { body }
29a) return value;
30a) void main() { }

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