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
Which keyword is used to declare a variable in Dart?
a) var
b) val
c) let
d) int
What is the data type of 3.14 in Dart?
a) int
b) double
c) num
d) float
What is the default value of an uninitialized variable in Dart?
a) null
b) 0
c) undefined
d) ""
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];
What is the supertype of all numeric types in Dart?
a) int
b) double
c) num
d) Object
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'];
What is the output of var x; print(x); in Dart?
a) null
b) undefined
c) 0
d) An error
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
Which function is used to parse a string into an integer in Dart?
a) int.parse()
b) string.toInt()
c) toInt()
d) parseInt()
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
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
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;
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
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
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
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
Which of the following demonstrates type inference in Dart?
a) var x = 5;
b) int x = 5;
c) dynamic x = 5;
d) x = 5;
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
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
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
What symbol is used for 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 code?
a) To execute special commands
b) To make code more readable and maintainable
c) To declare constants
d) To optimize runtime performance
Which of the following is NOT a valid comment style in Dart?
a) ///
b) //
c) <!-- -->
d) /* */
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
How does Dart organize code into blocks?
a) Using braces {}
b) Using parentheses ()
c) Using brackets []
d) Using indentation
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
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 }
How do you return a value from a Dart function?
a) return value;
b) yield value;
c) return(value);
d) value return;
What is the standard entry point of a Dart application?
a) void main() { }
b) start() { }
c) index() { }
d) application() { }
Answers Table
Qno
Answer
1
a) var
2
b) double
3
a) null
4
a) var list = [1, 2, 3];
5
c) num
6
a) var map = {'key': 'value'};
7
a) null
8
d) It can change type during runtime
9
a) int.parse()
10
c) If a variable matches a specific type
11
b) const is evaluated at compile-time, final at runtime
12
a) const pi = 3.14;
13
b) They can only be initialized once
14
a) Yes, but the object must be immutable
15
b) An error
16
b) Automatically determining the type of a variable