Home »
MCQs
MVVM (Model-View-ViewModel) MCQs (Multiple-Choice Questions)
MVVM (Model-View-ViewModel) is a software architectural pattern that separates the user interface, application data, and presentation logic. It is widely used with technologies such as WPF, .NET MAUI, and other data-binding-based UI frameworks. These MVVM MCQs cover Model, View, ViewModel, data binding, commands, INotifyPropertyChanged, ICommand, ObservableCollection<T>, DataContext, BindingContext, and testability.
MVVM MCQs
Practice these MVVM multiple-choice questions to test your understanding of the Model-View-ViewModel pattern and its implementation in modern application development.
List of MVVM MCQs
The following MVVM MCQs include answers and explanations covering fundamental concepts as well as practical data-binding and command scenarios.
1. What does MVVM stand for?
- Model-View-ViewModel
- Model-Visual-Variable-Model
- Module-View-Value-Manager
- Model-Version-View-Manager
Answer: A) Model-View-ViewModel
Explanation:
MVVM stands for Model-View-ViewModel. It separates an application's data and business concerns from its user interface and presentation logic.
2. What is the primary purpose of the MVVM pattern?
- To tightly couple UI controls with database tables
- To separate UI, presentation logic, and application data
- To eliminate all application models
- To replace object-oriented programming
Answer: B) To separate UI, presentation logic, and application data
Explanation:
MVVM separates the View, ViewModel, and Model responsibilities, making the application easier to maintain, test, and evolve.
3. Which MVVM component is primarily responsible for the user interface?
- Model
- View
- ViewModel
- Repository
Answer: B) View
Explanation:
The View represents the visual interface and its presentation-related behavior. In frameworks such as WPF, the View commonly consists of XAML and associated UI code.
4. What is the primary responsibility of the Model in MVVM?
- Defining button colors
- Representing application data and domain or business logic
- Handling every UI event directly
- Rendering XAML controls
Answer: B) Representing application data and domain or business logic
Explanation:
The Model represents application data and domain concepts and can contain business and validation logic. It is not responsible for rendering the user interface.
5. What is the primary responsibility of the ViewModel?
- Rendering pixels on the screen
- Providing presentation data, state, and commands for the View
- Replacing the database server
- Defining the application's operating system
Answer: B) Providing presentation data, state, and commands for the View
Explanation:
The ViewModel exposes properties and commands that the View can bind to. It also coordinates interactions with Model classes and services.
6. Which relationship is generally intended between a View and its ViewModel?
- The ViewModel directly manipulates every visual control
- The View binds to properties and commands exposed by the ViewModel
- The ViewModel inherits from every View
- The View and ViewModel must always be the same class
Answer: B) The View binds to properties and commands exposed by the ViewModel
Explanation:
MVVM uses data binding and commands to allow the View to consume functionality exposed by the ViewModel without requiring the ViewModel to directly manipulate individual UI controls.
7. Which object commonly connects a WPF View to its ViewModel for data binding?
- DataContext
- ConnectionString
- ApplicationDomain
- ResourceManager
Answer: A) DataContext
Explanation:
In WPF, data binding expressions commonly resolve their source through the DataContext of the element or its binding context inheritance chain.
8. In .NET MAUI, which property normally provides the binding source for a View?
- BindingContext
- DataSource
- ViewModelContext
- ModelContext
Answer: A) BindingContext
Explanation:
In .NET MAUI, the BindingContext is commonly set to a ViewModel instance so that XAML bindings can resolve its properties and commands.
9. What is data binding in MVVM?
- A mechanism for connecting UI properties with data or properties exposed by another object
- A database backup mechanism
- A method for compiling XAML into SQL
- A technique for encrypting ViewModels
Answer: A) A mechanism for connecting UI properties with data or properties exposed by another object
Explanation:
Data binding connects properties of UI elements with properties exposed by a binding source such as a ViewModel.
10. Which binding mode allows changes in the UI to update the bound source as well as changes in the source to update the UI?
- OneWay
- OneTime
- TwoWay
- OneWayToSource
Answer: C) TwoWay
Explanation:
TwoWay binding allows data to flow in both directions: changes in the source can update the target, and changes in the target can update the source.
11. Which interface is commonly implemented by a ViewModel to notify the View when a property value changes?
- IEnumerable
- INotifyPropertyChanged
- IDisposable
- IComparable
Answer: B) INotifyPropertyChanged
Explanation:
INotifyPropertyChanged provides the PropertyChanged event, allowing data-bound controls to be notified when a property changes.
12. Which event is defined by INotifyPropertyChanged?
- ValueChanged
- PropertyChanged
- DataUpdated
- BindingChanged
Answer: B) PropertyChanged
Explanation:
The PropertyChanged event is raised when the value of a property changes so that data-binding clients can refresh the corresponding UI.
13. What information does PropertyChangedEventArgs provide?
- The name of the property that changed
- The database connection string
- The View's screen resolution
- The application's assembly version only
Answer: A) The name of the property that changed
Explanation:
PropertyChangedEventArgs contains the PropertyName property, which identifies the property whose value changed.
14. Which statement best describes the ViewModel's relationship with a specific View implementation?
- The ViewModel should normally directly manipulate controls in the View
- The ViewModel should ideally avoid direct dependency on a specific View implementation
- The ViewModel must inherit from the View
- The ViewModel must contain the View's XAML
Answer: B) The ViewModel should ideally avoid direct dependency on a specific View implementation
Explanation:
A ViewModel is intended to encapsulate presentation logic without directly depending on a particular View implementation. This helps maintain loose coupling and testability.
15. Which interface is commonly used to represent commands in .NET MVVM applications?
- ICommand
- IAction
- IEventCommand
- IUICommandOnly
Answer: A) ICommand
Explanation:
ICommand represents an operation that can be invoked by the View and commonly exposes Execute and CanExecute functionality.
16. Which ICommand member determines whether a command can currently execute?
- CanExecute
- IsEnabled
- AllowExecute
- ExecuteState
Answer: A) CanExecute
Explanation:
CanExecute determines whether the command can execute for a specified parameter. UI controls can use this information to enable or disable command-bound controls.
17. Which ICommand member performs the command operation?
- Run
- Execute
- Perform
- InvokeCommand
Answer: B) Execute
Explanation:
The Execute method contains or invokes the operation represented by the command.
18. What is the purpose of CanExecuteChanged in ICommand implementations?
- To notify command consumers that the result of CanExecute may have changed
- To notify the database that a row changed
- To reload the entire View
- To destroy the command object
Answer: A) To notify command consumers that the result of CanExecute may have changed
Explanation:
CanExecuteChanged allows command consumers to reevaluate whether the command can currently execute.
19. Which ViewModel member is commonly bound to a Button's Command property?
- An ICommand implementation
- A database table
- A XAML document
- A CSS selector
Answer: A) An ICommand implementation
Explanation:
A ViewModel can expose an ICommand property that a Button or another command-capable control binds to, allowing the View to invoke the ViewModel's action.
20. What is the main advantage of using commands instead of placing button logic directly in code-behind?
- Commands help separate user actions from their visual representation
- Commands eliminate the need for data
- Commands automatically create database tables
- Commands make all UI elements static
Answer: A) Commands help separate user actions from their visual representation
Explanation:
Commands allow actions to be exposed by the ViewModel and invoked through data binding, reducing direct coupling between UI controls and application logic.
21. Which collection is particularly useful for notifying a bound UI when items are added or removed?
- List<T>
- ObservableCollection<T>
- Stack<T>
- HashSet<T>
Answer: B) ObservableCollection<T>
Explanation:
ObservableCollection<T> implements collection-change notification, allowing data-bound controls to respond when items are added, removed, or otherwise changed in the collection structure.
22. Which interface provides collection-change notifications?
- INotifyCollectionChanged
- INotifyListChanged
- ICollectionNotification
-
Answer: A) INotifyCollectionChanged
Explanation:
INotifyCollectionChanged defines the CollectionChanged event used to notify consumers about changes to a collection.
23. Which of the following is a common benefit of MVVM for unit testing?
- ViewModel logic can often be tested without creating the actual UI
- Every test must render the complete application window
- Models cannot be tested independently
- Unit tests must execute XAML animations
Answer: A) ViewModel logic can often be tested without creating the actual UI
Explanation:
Because ViewModels can encapsulate presentation logic independently of visual controls, much of their behavior can be tested without requiring the actual UI.
24. Which characteristic helps MVVM reduce coupling between the View and ViewModel?
- Direct control manipulation from the ViewModel
- Data binding and commands
- Global variables for every UI element
- Static references to all Views
Answer: B) Data binding and commands
Explanation:
Data binding allows Views to consume ViewModel properties, while commands expose user actions without requiring the ViewModel to directly manipulate visual controls.
25. In a well-separated MVVM design, which component should normally know about the View's visual implementation?
- The View
- The Model
- The ViewModel
- The data repository
Answer: A) The View
Explanation:
The View is responsible for the visual presentation. The Model should not know about the UI, and the ViewModel should ideally avoid direct dependency on a specific View implementation.
26. Which layer typically coordinates interaction between the View and Model?
- ViewModel
- View only
- Database engine
- CSS layer
Answer: A) ViewModel
Explanation:
The ViewModel commonly coordinates the View's interactions with Model classes and services while exposing data and actions in a form suitable for the View.
27. What is the purpose of a value converter in a data-binding system?
- To transform a value between the form required by the source and target
- To create a database connection
- To compile the ViewModel
- To replace every command
Answer: A) To transform a value between the form required by the source and target
Explanation:
A value converter can transform bound values between representations required by the source and the target, such as converting a Boolean value to a UI visibility representation.
28. In WPF, what does a binding Path generally specify?
- The property or property path to obtain the bound value from the source
- The location of the executable file
- The database server name
- The CSS file used by the View
Answer: A) The property or property path to obtain the bound value from the source
Explanation:
The Path property identifies the source property or nested property path used by a binding.
29. Which binding mode displays source changes in the target but does not normally propagate target changes back to the source?
- TwoWay
- OneWay
- OneWayToSource
- ReverseTwoWay
Answer: B) OneWay
Explanation:
OneWay binding normally propagates changes from the source to the target, while changes made to the target do not update the source through that binding.
30. Which binding mode is intended primarily for sending target changes back to the source?
- OneWayToSource
- OneWay
- OneTime
- SourceOnly
Answer: A) OneWayToSource
Explanation:
OneWayToSource transfers changes from the target back to the source rather than providing normal source-to-target updates.
31. Why should a ViewModel raise PropertyChanged when a calculated property changes?
- So controls bound to the calculated property can refresh
- So the database automatically creates an index
- So the ViewModel becomes a View
- So XAML can be deleted
Answer: A) So controls bound to the calculated property can refresh
Explanation:
If a calculated property's result depends on another property, a change notification for the calculated property may be required so that bound controls obtain its new value.
32. Which approach is generally discouraged in MVVM when the same behavior can be expressed through binding or commands?
- Putting application logic directly into UI event handlers in code-behind
- Exposing ViewModel properties
- Using ICommand
- Using data binding
Answer: A) Putting application logic directly into UI event handlers in code-behind
Explanation:
MVVM generally places presentation logic and user actions in the ViewModel where appropriate, using binding and commands instead of coupling application logic to UI event handlers.
33. Which statement about the Model and View is correct in MVVM?
- The Model should normally be responsible for rendering UI controls
- The Model should normally remain unaware of the View
- The Model must inherit from the View
- The View must directly execute every database operation
Answer: B) The Model should normally remain unaware of the View
Explanation:
The Model represents application or domain data and logic and should not depend on the visual representation of that data.
34. What is the purpose of a DataTemplate in an MVVM-based UI framework such as WPF?
- To define how an object is visually represented
- To create a SQL database
- To implement an HTTP server
- To replace the ViewModel
Answer: A) To define how an object is visually represented
Explanation:
A DataTemplate defines the visual structure used to display an object. In MVVM applications, it can provide the View representation for a ViewModel.
35. What does loose coupling mean in an MVVM context?
- Components have fewer direct dependencies on specific implementations
- All classes must be in one file
- The ViewModel must directly reference every UI control
- The Model must inherit from the View
Answer: A) Components have fewer direct dependencies on specific implementations
Explanation:
Loose coupling means components communicate through abstractions, binding, commands, or services rather than relying heavily on concrete implementations of other components.
36. Which design characteristic makes a ViewModel particularly suitable for unit testing?
- It contains all visual controls
- It can encapsulate presentation logic independently of the UI
- It must inherit from Window
- It must directly manipulate XAML elements
Answer: B) It can encapsulate presentation logic independently of the UI
Explanation:
A ViewModel can expose state and behavior without requiring direct access to visual controls, making its logic easier to test independently.
37. A ViewModel contains an IsBusy property that changes while an asynchronous operation is running. What should typically happen when its value changes?
- The ViewModel should notify the View through property change notification
- The ViewModel should recreate the entire application window
- The Model should modify the XAML directly
- The database should raise a UI event
Answer: A) The ViewModel should notify the View through property change notification
Explanation:
If a View is bound to IsBusy, the ViewModel should raise the appropriate property change notification when its value changes so that the UI can update.
38. A Button should only be enabled when a ViewModel's CanSubmit condition is true. Which MVVM mechanism is appropriate?
- ICommand.CanExecute
- Direct manipulation of Button.IsEnabled from the Model
- Changing the database schema
- Replacing the ViewModel with a View
Answer: A) ICommand.CanExecute
Explanation:
A command can expose whether an action is currently executable through CanExecute. A command-aware control can use this information to determine whether the UI action should be enabled.
39. What is CommandParameter used for?
- Passing a value or object to a command when it executes
- Defining a database connection
- Changing the View's XAML namespace
- Creating a new ViewModel class automatically
Answer: A) Passing a value or object to a command when it executes
Explanation:
CommandParameter allows a View to provide an argument to the command when the command is invoked.
40. Which approach can be used to provide dependencies to a ViewModel while maintaining loose coupling?
- Dependency injection
- Direct access to every UI control
- Global UI variables
- Hard-coded database credentials
Answer: A) Dependency injection
Explanation:
Dependency injection can provide services and other dependencies to a ViewModel without requiring the ViewModel to construct concrete dependencies itself.
41. Which type of operation should generally be handled asynchronously in a ViewModel when it involves potentially long-running I/O?
- Network or file I/O
- Reading a local constant
- Returning a Boolean literal
- Assigning a string literal
Answer: A) Network or file I/O
Explanation:
Asynchronous operations can prevent long-running I/O from blocking the UI thread and help keep the application responsive.
42. Which MVVM concept allows a UI element to invoke ViewModel functionality without knowing how the functionality is implemented?
- Command binding
- Direct control inheritance
- UI field injection
- Visual tree replacement
Answer: A) Command binding
Explanation:
Command binding connects a UI action to a command exposed by the ViewModel, keeping the UI interaction separate from the command's implementation.
43. Which of the following is a common MVVM framework or toolkit for .NET applications?
- .NET Community MVVM Toolkit
- SQL Server Reporting Engine
- ASP.NET Razor Compiler only
- Windows Registry API
Answer: A) .NET Community MVVM Toolkit
Explanation:
The .NET Community MVVM Toolkit provides helpers and source-generator-based features for implementing MVVM patterns in .NET applications.
44. Which statement best describes the ViewModel's properties?
- They provide data and state in a form that the View can consume
- They must always be references to visual controls
- They can only contain database connection strings
- They must always be static
Answer: A) They provide data and state in a form that the View can consume
Explanation:
A ViewModel exposes properties that represent presentation state and data needed by the View. It may also transform or combine model data into a form suitable for display.
45. Which statement about a ViewModel is correct?
- A ViewModel must inherit from a WPF Window
- A ViewModel is normally a non-visual class
- A ViewModel must contain XAML markup
- A ViewModel must directly manipulate the visual tree
Answer: B) A ViewModel is normally a non-visual class
Explanation:
A ViewModel normally does not derive from a visual UI class. It encapsulates presentation logic, state, properties, and commands that can be consumed by the View.
46. Consider the following ViewModel property:
private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
What is the primary purpose of OnPropertyChanged(nameof(Name))?
- To notify data-bound UI elements that Name has changed
- To create a new database table named Name
- To execute a command named Name
- To instantiate the ViewModel
Answer: A) To notify data-bound UI elements that Name has changed
Explanation:
The property change notification tells the binding system that the Name property's value has changed, allowing controls bound to Name to refresh their displayed value.
47. Consider the following command binding:
<Button
Content="Save"
Command="{Binding SaveCommand}" />
Where would SaveCommand normally be defined in an MVVM application?
- In the ViewModel
- In the database schema
- In the operating system
- In the CSS file
Answer: A) In the ViewModel
Explanation:
The Button binds its Command property to SaveCommand. In MVVM, the command is normally exposed by the ViewModel so that the View can invoke the corresponding operation.
48. Consider an ObservableCollection<Product> bound to a ListView. A product is added using the collection's Add method. What allows the bound control to detect the collection change?
- ObservableCollection's collection-change notification
- INotifyPropertyChanged on the View class only
- The Model's database connection
- The XAML compiler restarting the application
Answer: A) ObservableCollection's collection-change notification
Explanation:
ObservableCollection<T> implements INotifyCollectionChanged and raises collection-change notifications when items are added or removed, allowing bound controls to update.
49. A ViewModel exposes a SubmitCommand and an IsFormValid property. The command should execute only when the form is valid. Which design is most appropriate?
- Use the command's CanExecute logic based on IsFormValid
- Disable the database server when the form is invalid
- Put all validation logic inside the Button's visual template
- Make SubmitCommand a static database connection
Answer: A) Use the command's CanExecute logic based on IsFormValid
Explanation:
The command can use CanExecute to determine whether submission is currently allowed. When IsFormValid changes, the command can notify consumers so the UI can reevaluate its enabled state.
50. Consider this MVVM flow: a user changes a value in the View, the bound ViewModel property is updated, the ViewModel recalculates a dependent property, and the View should display the new dependent value. What is required for the final UI update?
- The ViewModel should raise property-change notification for the dependent property
- The Model must recreate the View
- The View must be manually rebuilt after every keystroke
- The application must restart its binding engine
Answer: A) The ViewModel should raise property-change notification for the dependent property
Explanation:
If the View is bound to a calculated property, changing one of its inputs does not by itself guarantee that the binding system knows the calculated property changed. The ViewModel should raise PropertyChanged for the dependent property so the View can retrieve and display its new value.
Advertisement
Advertisement