Sample questions
Question 1Easy
When should you use a StatefulWidget instead of a StatelessWidget?
- When the widget holds data that changes during its lifetime
- When the widget needs to receive arguments from its parent
- When the widget is the root widget passed to runApp
- When the widget contains more than one child widget
Answer: A. When the widget holds data that changes during its lifetime
Explanation
A StatelessWidget depends only on its configuration and inherited data. A StatefulWidget has a State object for data that changes, such as a counter or a text field's value.
Step-by-step solution
- StatefulWidget manages changing data over time.
- StatelessWidget is static and doesn't track state.
- Changing data needs StatefulWidget to update UI.
- So use StatefulWidget when data changes during lifetime.
- Final answer: A (When the widget holds data that changes during its lifetime).
Why the other options are wrong
- B. Both widget types take arguments from their parent through constructor fields, so that is not the test.
- C. The root widget passed to runApp can be stateless; it needs state only if its own data changes.
- D. The number of children does not decide it; a StatelessWidget can hold many children without any state.
Tip: Remember: Stateful for change, Stateless for fixed data.
Question 2Easy
Where is the changing data of a StatefulWidget stored?
- In the BuildContext that is passed to the build method
- In its State object, which is created by createState
- In the StatefulWidget's own fields, which are reassigned
- In a global map that Flutter keeps for every widget
Answer: B. In its State object, which is created by createState
Explanation
The widget itself is immutable and may be recreated often. The State object persists across rebuilds and holds mutable data.
Step-by-step solution
- StatefulWidget creates a State object via createState method.
- The State object stores changing data between widget rebuilds.
- BuildContext is used for building the UI, not for storing state.
- State object is linked to the widget and persists across rebuilds.
- Therefore, changing data is stored in the State object.
Why the other options are wrong
- A. BuildContext is a location handle used for lookups; it does not store the widget's changing data.
- C. A StatefulWidget is immutable, so its own fields cannot be reassigned; the data lives in State.
- D. Flutter does not keep a global map of widget data; state lives on the State object.
Tip: Remember: State object holds state, not the widget.
Question 3Easy
Which method of a widget returns the widget tree that describes its UI?
- paint
- createElement
- render
- build
Answer: D. build
Explanation
build(BuildContext) returns the widgets that make up this part of the UI. Flutter calls it whenever the widget needs to be redrawn.
Step-by-step solution
- A widget describes its UI in
build(). build returns the widget tree.
Why the other options are wrong
- A. paint belongs to render objects.
- B. createElement creates an Element.
- C. There is no render method on widgets.
Tip: build builds the UI.
Question 4Easy
In Flutter, what is a widget?
- A native platform view such as an Android View or UIView
- A background thread that runs the layout of the screen
- A mutable object that directly paints pixels on the screen
- An immutable description of part of the user interface
Answer: D. An immutable description of part of the user interface
Explanation
Widgets are lightweight, immutable configurations. The framework builds elements and render objects from them to do layout and painting.
Step-by-step solution
- Widgets are immutable descriptions of UI parts.
- They define how the UI should look and behave.
- Flutter uses widgets to build the entire app interface.
- Widgets cannot be changed after creation.
- They are used to create layouts and render objects.
- Final answer: An immutable description of part of the user interface.
Why the other options are wrong
- A. A widget is a Dart description, not a native Android View or UIView; those are separate.
- B. Widgets are not threads; layout runs in Flutter's rendering pipeline on the UI thread.
- C. Widgets are immutable configurations, not mutable objects that paint pixels; render objects do the painting.
Tip: Remember, widgets are static and used to create dynamic UIs in Flutter.
Question 5Medium
- The global app theme object passed to every widget
- A reference to the parent widget's State object and its fields
- A handle to the widget's location in the element tree
- A copy of the device's screen size and orientation
Answer: C. A handle to the widget's location in the element tree
Explanation
BuildContext is actually the Element for that widget. It lets the widget look up ancestors, such as Theme.of(context) or Navigator.of(context).
Step-by-step solution
- BuildContext is a handle to the widget's location in the element tree.
- It allows widgets to access ancestor data like themes or navigation routes.
- This makes it possible to use functions like Theme.of(context) and Navigator.of(context).
- The context provides access to the widget's position in the tree structure.
- Thus, BuildContext is a handle to the widget's location in the element tree.
Why the other options are wrong
- A. Theme is looked up through BuildContext, but the context itself is not the theme object.
- B. A context is not the State object; it is a handle that can reach ancestors but holds no fields.
- D. Screen size comes from MediaQuery, not from the context itself; the context is a tree location handle.
Question 6Medium
Flutter keeps three trees. Which one is long-lived, holds State objects and links widgets to render objects?
- The layer tree
- The element tree
- The render object tree
- The widget tree
Answer: B. The element tree
Explanation
Widgets are short-lived configurations. Elements persist across rebuilds, own the State of stateful widgets and manage the RenderObjects.
Step-by-step solution
- Flutter maintains three parallel trees: widget, element, and render object trees.
- Widgets are short‑lived configurations rebuilt on each setState.
- Render objects perform layout and painting but do not keep state.
- Elements persist across rebuilds, own State objects and connect widgets to render objects.
- The element tree.
Why the other options are wrong
- A. The layer tree is built for compositing at paint time; it does not hold State objects.
- C. Render objects do layout and painting but keep no State, so they are not the answer.
- D. Widgets are short-lived immutable configurations, rebuilt often, so they do not hold State objects.
Tip: Only the element tree holds State; widgets are temporary.
Question 7Medium
An async callback calls setState after the user has already left the screen. What is the usual fix?
- Check that mounted is true before calling setState
- Make the widget a StatelessWidget instead
- Call setState inside dispose instead of the callback
- Wrap the setState call in a try/finally block
Answer: A. Check that mounted is true before calling setState
Explanation
After dispose, setState throws an error. Checking mounted (or cancelling the work in dispose) avoids updating a State that is no longer in the tree.
Step-by-step solution
- SetState after screen leaves causes errors.
- Check mounted before setState to avoid errors.
- Mounted is true only if widget is still in tree.
- This prevents unnecessary updates and errors.
- Final answer: Check that mounted is true before calling setState.
Why the other options are wrong
- B. Making it stateless removes setState, which is not the fix; the callback still needs a mounted check.
- C. Calling setState inside dispose runs after the State is torn down, so it is not a safe fix.
- D. try/finally does not stop setState from running after unmount; it only runs cleanup code.
Tip: Check mounted before setState to avoid post-dispose errors.
Question 8Medium
Where should you create an AnimationController or subscribe to a stream in a StatefulWidget?
- the widget's constructor, before the State object exists
- build, so the controller is recreated each time the UI changes
- initState, which runs once when the State is created
- dispose, so the controller is ready when the widget appears
Answer: C. initState, which runs once when the State is created
Explanation
initState runs exactly once per State object. Objects created there are released in dispose.
Step-by-step solution
- initState runs once when State is created.
- Controllers and subscriptions go there.
- They are released in dispose.
Why the other options are wrong
- A. State doesn't exist in the constructor.
- B. build runs many times.
- D. dispose is for cleanup.
Tip: init in initState, free in dispose.
Question 9Hard
Inside the build method that creates a Scaffold, calling Scaffold.of(context) with that same build method's context fails. Why?
- Scaffold.of only works in release mode, not in debug builds
- Its context is above the Scaffold, so no Scaffold is found
- The Scaffold is still null until the second frame is drawn
- Scaffold.of can only be called from inside an initState method
Answer: B. Its context is above the Scaffold, so no Scaffold is found
Explanation
of(context) searches ancestors of the given context. The build method's context is the parent of the Scaffold, so wrap the child in a Builder (or use a separate widget) to get a context below it.
Step-by-step solution
- Scaffold.of(context) looks for Scaffold in ancestor widgets.
- Build method's context is parent of Scaffold, not inside it.
- Context must be inside Scaffold to find it using Scaffold.of(context).
- Wrap child widget with Builder to get correct context inside Scaffold.
- This ensures Scaffold.of(context) finds the correct Scaffold widget.
Why the other options are wrong
- A. Scaffold.of has no release-mode rule; it fails in any mode when the context is above the Scaffold.
- C. The Scaffold exists as soon as build returns; the context simply cannot see it from above.
- D. Scaffold.of works from build as well as from initState; the problem is the context's position.
Tip: Use Builder inside Scaffold to get correct context for Scaffold.of(context).
Question 10Hard
A State copies widget.initialValue into a field in initState. Later the parent rebuilds and passes a different initialValue. What does the child show?
- The old value, because initState does not run again for the reused State
- The new value, because initState runs again whenever the widget changes
- A blank widget until the State is disposed and recreated by the parent
- An error, because the parent may not change a StatefulWidget's arguments
Answer: A. The old value, because initState does not run again for the reused State
Explanation
The same State is kept when the widget type and key match. To react to new arguments, compare them in didUpdateWidget, or read widget.initialValue directly.
Step-by-step solution
- State is reused if widget type and key match, so initState does not run again.
- widget.initialValue is copied once in initState, not updated on parent rebuild.
- Child shows the old value from initState until didUpdateWidget is called.
- DidUpdateWidget is not used here, so child retains initial value.
- Therefore, child displays the old value from initState.
Why the other options are wrong
- B. initState does not run again on a rebuild with the same State; it runs only once per State.
- C. The child is not blank; the State keeps running and the parent's new value is simply not copied.
- D. No error occurs; passing new arguments to a StatefulWidget is normal and allowed.
Tip: If State is reused, initState does not run again. Use didUpdateWidget for updates.
Practise all 15 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →