0
0
Fluttermobile~20 mins

Control flow (if, for, while, switch) in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Control Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What will this Flutter widget display?
Consider this Flutter code snippet. What text will appear on the screen?
Flutter
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  final int count = 3;

  @override
  Widget build(BuildContext context) {
    String message = '';
    for (int i = 0; i < count; i++) {
      if (i == 2) {
        message += 'End';
      } else {
        message += 'Step ' + i.toString() + ', ';
      }
    }
    return Text(message);
  }
}
AStep 0, Step 1, Step 2, End
BStep 0, Step 1, End
CStep 0, Step 1,
DEnd
Attempts:
2 left
💡 Hint
Look at the loop and what happens when i equals 2.
lifecycle
intermediate
2:00remaining
What happens when this Flutter StatefulWidget runs?
Given this Flutter StatefulWidget code, what will be the value of counter after pressing the button twice?
Flutter
import 'package:flutter/material.dart';

class CounterWidget extends StatefulWidget {
  @override
  State<CounterWidget> createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  int counter = 0;

  void increment() {
    setState(() {
      counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: increment,
      child: Text('Count: $counter'),
    );
  }
}
ACount: 0
BCount: 1
CCount: 2
DCount: null
Attempts:
2 left
💡 Hint
Each button press calls increment which increases counter by 1.
📝 Syntax
advanced
2:00remaining
What error does this Flutter code produce?
Look at this Flutter code snippet. What error will it cause when compiled?
Flutter
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    int x = 5;
    if (x > 3) {
      return Text('Greater');
    } else {
      return Text('Smaller');
    }
  }
}
ASyntaxError: Missing parentheses in if statement
BRuntimeError: Variable x not defined
CNo error, outputs 'Greater'
DTypeError: Cannot compare int with int
Attempts:
2 left
💡 Hint
Check the syntax of the if statement in Dart.
navigation
advanced
2:00remaining
What screen will show after this navigation code runs?
In this Flutter code, what screen will appear after pressing the button?
Flutter
import 'package:flutter/material.dart';

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => SecondScreen()),
        );
      },
      child: Text('Go to Second'),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Second Screen');
  }
}
ASecond Screen appears and FirstScreen is removed from stack
BSecond Screen appears but FirstScreen remains in stack
CFirstScreen remains visible, no navigation happens
DApp crashes due to navigation error
Attempts:
2 left
💡 Hint
pushReplacement replaces the current screen with the new one.
🧠 Conceptual
expert
2:00remaining
What is the output of this Flutter switch statement?
Given this Dart switch statement inside a Flutter widget, what text will be displayed if value is 2?
Flutter
import 'package:flutter/material.dart';

class SwitchWidget extends StatelessWidget {
  final int value = 2;

  @override
  Widget build(BuildContext context) {
    String result;
    switch (value) {
      case 1:
        result = 'One';
        break;
      case 2:
      case 3:
        result = 'Two or Three';
        break;
      default:
        result = 'Other';
    }
    return Text(result);
  }
}
AOne
BOther
CError: Missing break statement
DTwo or Three
Attempts:
2 left
💡 Hint
Cases 2 and 3 share the same output.