Explore the core concepts of object-oriented programming (OOP) in Dart, including classes, constructors, getters, and setters. Learn how to create and manage objects with Dart’s powerful features.
Classes and Objects
What is the keyword used to define a class in Dart? a) define b) class c) structure d) object
How do you create an object from a class in Dart? a) Using the new keyword b) Using the object() function c) Direct instantiation with parentheses d) Both a and c
What is a class in Dart used for? a) Storing variables only b) Creating templates for objects c) Managing data flow d) Controlling program execution
What is an instance of a class known as? a) Constructor b) Object c) Module d) Class
How would you create an object of class Person? a) var p = Person(); b) var p = new Person; c) Person obj = new Person(); d) Person.create();
Which of the following is correct for calling a method of an object person of class Person? a) person->method(); b) person.method(); c) person:method(); d) method(person);
Constructors (Default, Named, Factory)
What is the purpose of a constructor in Dart? a) To initialize class variables b) To define class behavior c) To manage object memory d) To perform object destruction
Which constructor type is invoked when an object is created without passing parameters? a) Named constructor b) Default constructor c) Factory constructor d) Parameterized constructor
How do you define a named constructor in Dart? a) ClassName.constructorName() b) ClassName::constructorName() c) ClassName.constructorName; d) ClassName.constructorName()
What is the main advantage of using a factory constructor in Dart? a) It creates only one instance of a class b) It provides initialization logic c) It allows multiple inheritance d) It makes the constructor abstract
How do you create a default constructor in Dart? a) ClassName() {} b) ClassName {} c) ClassName() d) void ClassName() {}
How is a named constructor called in Dart? a) new ClassName() namedConstructor(); b) ClassName.constructorName(); c) new ClassName.namedConstructor(); d) ClassName.namedConstructor();
Can Dart classes have multiple constructors? a) Yes, using named constructors b) No, only one constructor is allowed c) Yes, but only one default constructor d) Yes, but only one factory constructor
What is the keyword used for a factory constructor in Dart? a) create b) factory c) new d) instantiate
Which of the following correctly defines a factory constructor? a) factory Person() { return Person(); } b) factory Person() { this = new Person(); } c) Person factory() {} d) factory Person() { Person obj = new Person(); return obj; }
Getters and Setters
What is the purpose of a getter in Dart? a) To allow direct access to a private field b) To define the behavior of a field c) To fetch and return the value of a field d) To handle exceptions
Which of the following defines a getter for the field age in Dart? a) int get age => _age; b) int set age(_age) => _age; c) age() => _age; d) int _get age;
How is a setter defined in Dart? a) set age(value) => _age = value; b) age set(value) => _age = value; c) void set age(value) { _age = value; } d) set(age) => _age = value;
What is the keyword used to define a setter in Dart? a) get b) set c) modify d) setTo
Which of the following defines a setter for the field age? a) set age(value) => _age = value; b) age = value => _age; c) set age(value) => value = _age; d) set age(value) { _age = value; }
In Dart, what does the getter return when accessing the value of a private field? a) Null b) The private field’s value c) An error d) A default value
How do you call a getter in Dart? a) object.getterName b) object.getter() c) object.getterName(); d) object.getterName
Which of the following describes an example of using a setter? a) object.setter(value) b) object.setter(value); c) object.setter = value; d) object.setter = value()
Can getters and setters be defined in a class without explicit fields in Dart? a) Yes, but only for private fields b) No, they must have a backing field c) Yes, but only for public fields d) Yes, with the use of static fields
How does Dart handle access to private class members using getters and setters? a) They provide direct access to private members b) They provide controlled access to private members c) They prevent access to private members d) They override class-level access control
In which scenario would you prefer to use a setter in Dart? a) When you need to initialize a field b) When you need to calculate a value c) When you need to restrict the modification of fields d) When you need to modify the value of a field
What happens when you assign a value to a setter in Dart? a) It triggers the setter method b) It directly updates the private field c) It updates the public field d) It triggers the getter method
Can getters and setters be used to modify the internal state of an object in Dart? a) No, they are used for read-only access b) Yes, they can read and modify the internal state c) Yes, but only for public fields d) No, setters are used to fetch data
How can a getter be used in Dart? a) To retrieve private data from a class b) To trigger calculations or logic c) To update public fields d) To invoke a method
What is a key difference between a getter and a setter in Dart? a) A getter returns a value, while a setter accepts one b) A setter returns a value, while a getter accepts one c) A getter modifies the value, while a setter returns it d) Both getters and setters are used to modify values
Answer Key
QNo
Answer (Option with text)
1
b) class
2
d) Both a and c
3
b) Creating templates for objects
4
b) Object
5
a) var p = Person();
6
b) person.method();
7
a) To initialize class variables
8
b) Default constructor
9
a) ClassName.constructorName()
10
a) It creates only one instance of a class
11
a) ClassName() {}
12
c) new ClassName.namedConstructor();
13
a) Yes, using named constructors
14
b) factory
15
a) factory Person() { return Person(); }
16
c) To fetch and return the value of a field
17
a) int get age => _age;
18
a) set age(value) => _age = value;
19
b) set
20
d) set age(value) { _age = value; }
21
b) The private field’s value
22
a) object.getterName
23
c) object.setter = value;
24
b) No, they must have a backing field
25
b) They provide controlled access to private members
26
d) When you need to modify the value of a field
27
a) It triggers the setter method
28
b) Yes, they can read and modify the internal state
29
a) To retrieve private data from a class
30
a) A getter returns a value, while a setter accepts one