Challenge - 5 Problems
Master of Classes and Objects
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter widget?
Consider this Flutter code defining a class and using it in a widget. What text will appear on the screen?
Flutter
class Person { final String name; Person(this.name); } class MyWidget extends StatelessWidget { final Person person = Person('Alice'); @override Widget build(BuildContext context) { return Text(person.name); } }
Attempts:
2 left
💡 Hint
Look at how the Person class stores the name and how the Text widget uses it.
✗ Incorrect
The Person class stores the name 'Alice'. The Text widget displays person.name, so it shows 'Alice'.
🧠 Conceptual
intermediate2:00remaining
What does this Dart class constructor do?
Look at this Dart class constructor. What is its purpose?
Flutter
class Car { String model; int year; Car(this.model, this.year); }
Attempts:
2 left
💡 Hint
The constructor uses 'this' to assign parameters to fields.
✗ Incorrect
The constructor Car(this.model, this.year) assigns the passed arguments to the object's fields.
❓ lifecycle
advanced2:00remaining
What happens when this Flutter StatefulWidget is created?
Examine this Flutter StatefulWidget code. What is printed when the widget is first inserted into the widget tree?
Flutter
class CounterWidget extends StatefulWidget { @override State<CounterWidget> createState() => _CounterWidgetState(); } class _CounterWidgetState extends State<CounterWidget> { int count = 0; @override void initState() { super.initState(); print('Init count: $count'); } @override Widget build(BuildContext context) { return Text('Count: $count'); } }
Attempts:
2 left
💡 Hint
initState runs once when the widget is created before build.
✗ Incorrect
initState prints 'Init count: 0' because count is initialized to 0 before build runs.
📝 Syntax
advanced2:00remaining
Which option correctly defines a Dart class with a named constructor?
Select the correct Dart code that defines a class with a named constructor called 'fromJson'.
Attempts:
2 left
💡 Hint
Named constructors use the class name dot constructor name syntax with a body.
✗ Incorrect
Option A correctly defines a named constructor with a body that assigns the name field.
🔧 Debug
expert2:00remaining
Why does this Flutter code cause a runtime error?
This Flutter code tries to display a person's age but crashes at runtime. Why?
Flutter
class Person { int? age; Person(this.age); } class AgeWidget extends StatelessWidget { final Person person = Person(null); @override Widget build(BuildContext context) { return Text('Age: ' + person.age!.toString()); } }
Attempts:
2 left
💡 Hint
Consider null safety and the non-null assertion operator when accessing nullable fields.
✗ Incorrect
person.age is null. The expression person.age!.toString() uses the non-null assertion operator '!' on a null value, causing a runtime error: "Null check operator used on a null value".