Home »
MCQs
WPF MCQs (Multiple-Choice Questions)
Practice these WPF MCQs (Multiple-Choice Questions) with answers and explanations to test your knowledge of Windows Presentation Foundation, XAML, controls, layouts, data binding, dependency properties, routed events, commands, styles, templates, resources, and other important WPF concepts.
WPF MCQs
These WPF multiple-choice questions cover fundamental and advanced concepts used when developing Windows desktop applications with WPF and .NET.
List of WPF MCQs
Below is the list of WPF MCQs with answers and explanations.
1. What does WPF stand for?
- Windows Programming Framework
- Windows Presentation Foundation
- Windows Processing Framework
- Web Presentation Framework
Answer: B) Windows Presentation Foundation
Explanation:
WPF stands for Windows Presentation Foundation. It is a .NET UI framework for building Windows desktop applications with features such as XAML, data binding, graphics, animation, styles, and templates.
2. Which markup language is commonly used to define the UI of a WPF application?
- HTML
- XML Schema
- XAML
- JSON
Answer: C) XAML
Explanation:
XAML is a declarative markup language used in WPF to define UI elements, properties, resources, styles, templates, and other interface-related information.
3. Which namespace contains many of the core WPF types?
- System.Web
- System.Windows
- System.Forms
- System.Xaml.Forms
Answer: B) System.Windows
Explanation:
Many fundamental WPF types are located in the System.Windows namespace, including important classes used by windows, controls, and the WPF property and event systems.
4. What is the primary purpose of XAML in a WPF application?
- To store database records
- To define the user interface declaratively
- To compile C# code
- To manage Windows services
Answer: B) To define the user interface declaratively
Explanation:
XAML separates much of the UI definition from application logic. The appearance and structure of controls can be described in XAML while behavior can be implemented in code-behind or other application classes.
5. Which WPF class normally represents the main application object?
- Application
- WindowBase
- WpfApplication
- AppHost
Answer: A) Application
Explanation:
The WPF Application class represents the application itself and provides application-level functionality such as resources and application lifetime management.
6. Which WPF element is commonly used as a top-level desktop window?
- Page
- Window
- Frame
- Panel
Answer: B) Window
Explanation:
The Window class represents a WPF desktop window and can contain controls and layout elements that form the application's user interface.
7. Which WPF panel arranges its children in rows and columns?
- Canvas
- StackPanel
- Grid
- DockPanel
Answer: C) Grid
Explanation:
The Grid panel arranges child elements using rows and columns. It is commonly used for complex and flexible WPF layouts.
8. Which panel arranges its children in a single horizontal or vertical line?
- StackPanel
- Grid
- Canvas
- UniformGrid
Answer: A) StackPanel
Explanation:
StackPanel arranges child elements in a single line. Its Orientation property determines whether the arrangement is horizontal or vertical.
9. Which WPF panel is used for explicit coordinate-based positioning of child elements?
- Grid
- Canvas
- StackPanel
- WrapPanel
Answer: B) Canvas
Explanation:
Canvas allows child elements to be positioned using attached properties such as Canvas.Left and Canvas.Top.
10. Which WPF panel arranges children relative to the edges of the panel?
- DockPanel
- Canvas
- StackPanel
- Grid
Answer: A) DockPanel
Explanation:
DockPanel allows child elements to be docked to the top, bottom, left, or right edge by using the DockPanel.Dock attached property.
11. Which two phases are fundamental to the WPF layout process?
- Load and Save
- Measure and Arrange
- Compile and Execute
- Draw and Print
Answer: B) Measure and Arrange
Explanation:
WPF layout performs a measure pass to determine the desired size of elements and an arrange pass to determine their final position and size.
12. Which property identifies the object used as the source for many WPF bindings?
- SourceContext
- DataContext
- BindingContext
- ObjectContext
Answer: B) DataContext
Explanation:
DataContext provides the default data source for bindings. Its value can be inherited by child elements in the WPF element tree.
13. Which markup extension is commonly used for WPF data binding?
- {Data}
- {BindTo}
- {Binding}
- {Connect}
Answer: C) {Binding}
Explanation:
The {Binding} markup extension is used in XAML to establish a data binding between a target property and a source value.
14. In a typical WPF binding, what is called the binding target?
- The source object property
- The destination dependency property
- The database table
- The XAML file
Answer: B) The destination dependency property
Explanation:
The binding target is normally the dependency property on the UI element that receives the value from the binding source.
15. Which interface is commonly implemented by a binding source to notify WPF that a property value has changed?
- IEnumerable
- IComparable
- INotifyPropertyChanged
- IDisposable
Answer: C) INotifyPropertyChanged
Explanation:
INotifyPropertyChanged allows an object to notify the binding engine when a property changes, which is especially important for one-way and two-way bindings.
16. Which collection is commonly used when WPF needs collection-change notifications?
- List<T>
- ArrayList
- ObservableCollection<T>
- Stack<T>
Answer: C) ObservableCollection<T>
Explanation:
ObservableCollection<T> implements collection-change notification and is commonly used with WPF controls that display dynamic collections.
17. Which property of a WPF TextBox is commonly bound to a view-model string property?
- Caption
- Text
- ValueText
- ContentText
Answer: B) Text
Explanation:
The Text dependency property of TextBox can be connected to a source property using WPF data binding.
18. Which binding mode automatically updates the target when the source changes but does not normally update the source?
- OneWay
- TwoWay
- OneTime
- OneWayToSource
Answer: A) OneWay
Explanation:
OneWay binding transfers changes from the source to the target. Changes made to the target are not automatically written back to the source.
19. Which binding mode allows changes to flow between source and target?
- OneWay
- OneTime
- TwoWay
- OneWayToSource
Answer: C) TwoWay
Explanation:
TwoWay binding allows the source and target to update each other, subject to the property's notification and update behavior.
20. What is a dependency property in WPF?
- A database property
- A property backed by the WPF property system
- A property that can only be used in XAML
- A property defined only in interfaces
Answer: B) A property backed by the WPF property system
Explanation:
A dependency property is registered with the WPF property system and provides features such as data binding, styles, animation, default values, inheritance, and property value precedence.
21. Which base class is associated with WPF dependency-property functionality?
- DependencyObject
- PropertyObject
- WpfPropertyBase
- FrameworkProperty
Answer: A) DependencyObject
Explanation:
DependencyObject provides the infrastructure used by WPF dependency properties and related property-system functionality.
22. Which method is normally used inside a custom dependency property wrapper to retrieve its value?
- GetDependency()
- GetValue()
- ReadProperty()
- GetPropertyValue()
Answer: B) GetValue()
Explanation:
A dependency property wrapper commonly calls GetValue() to retrieve the property's value from the WPF property system.
23. Which method is used to assign a value to a dependency property?
- SetValue()
- AssignValue()
- SetDependency()
- WriteProperty()
Answer: A) SetValue()
Explanation:
SetValue() is used by dependency-property wrappers and WPF code to set values through the dependency property system.
24. Which WPF feature allows a property value to be supplied by a resource reference?
- Resource binding
- Dependency property system
- Event routing only
- Command routing only
Answer: B) Dependency property system
Explanation:
Dependency properties integrate with WPF resources. For example, a control property can reference a resource using {StaticResource} or {DynamicResource}.
25. What is the main difference between StaticResource and DynamicResource in WPF?
- StaticResource is only for colors
- DynamicResource supports runtime resource reevaluation
- DynamicResource can only be used in code
- StaticResource cannot reference ResourceDictionary entries
Answer: B) DynamicResource supports runtime resource reevaluation
Explanation:
A DynamicResource reference allows WPF to reevaluate the resource at runtime when appropriate, while StaticResource is resolved when the XAML is loaded.
26. Which WPF class stores reusable resources such as brushes, styles, and templates?
- ResourceDictionary
- ResourceCollection
- StyleDictionary
- TemplateStore
Answer: A) ResourceDictionary
Explanation:
ResourceDictionary stores reusable objects such as brushes, styles, templates, and other resources that can be referenced by key.
27. Which attribute is commonly used to assign a key to a resource in XAML?
- x:Id
- x:Key
- x:Resource
- KeyName
Answer: B) x:Key
Explanation:
The x:Key directive assigns a key to an object stored in a WPF resource dictionary so that it can be referenced later.
28. What is a WPF Style primarily used for?
- Executing database queries
- Defining reusable property settings for controls
- Creating network sockets
- Compiling XAML into SQL
Answer: B) Defining reusable property settings for controls
Explanation:
A WPF Style can contain setters and triggers that define reusable property values and appearance behavior for controls.
29. Which WPF template changes the visual structure of a control?
- DataTemplate
- ControlTemplate
- ItemTemplate
- ContentDataTemplate
Answer: B) ControlTemplate
Explanation:
A ControlTemplate defines the visual structure of a control. It allows the appearance of a control to be customized without changing its fundamental behavior.
30. What is the primary purpose of a DataTemplate?
- Define how bound data objects are visually represented
- Define database schemas
- Register dependency properties
- Handle keyboard events
Answer: A) Define how bound data objects are visually represented
Explanation:
A DataTemplate defines the visual representation of data objects, commonly when displaying items in controls such as ListBox or ListView.
31. Which WPF feature allows an event to travel through an element tree?
- Dependency property
- Routed event
- Resource dictionary
- Data template
Answer: B) Routed event
Explanation:
Routed events can travel through the WPF element tree and allow handlers on different elements along the event route to respond to the event.
32. Which routing strategy travels from the source element toward its ancestors?
- Tunneling
- Bubbling
- Direct
- Static
Answer: B) Bubbling
Explanation:
In a bubbling routed event, the event starts at the element that raised it and travels upward through its parent elements.
33. Which routing strategy travels from an ancestor toward the event source?
- Bubbling
- Tunneling
- Direct
- Deferred
Answer: B) Tunneling
Explanation:
Tunneling events travel down the element tree from an ancestor toward the element that raised the event. WPF commonly identifies preview events with a Preview prefix.
34. Which property can be set to true to mark a routed event as handled?
- Handled
- IsHandled
- EventHandled
- RouteHandled
Answer: A) Handled
Explanation:
The Handled property of routed event data can be set to true to indicate that the event has been handled.
35. Which interface defines the basic contract for WPF commands?
- ICommand
- ICommandSourceOnly
- IAction
- IEventCommand
Answer: A) ICommand
Explanation:
The ICommand interface defines the basic command contract through members such as Execute, CanExecute, and CanExecuteChanged.
36. Which method of ICommand performs the command operation?
- Run()
- Execute()
- Perform()
- InvokeCommand()
Answer: B) Execute()
Explanation:
The Execute() method performs the action associated with an ICommand.
37. Which ICommand method determines whether a command can currently execute?
- CanRun()
- CanExecute()
- IsEnabled()
- CheckCommand()
Answer: B) CanExecute()
Explanation:
CanExecute() determines whether the command is currently able to execute for the specified parameter and command context.
38. Which class represents a routed command in WPF?
- RoutedCommand
- CommandRouter
- WpfCommand
- CommandEvent
Answer: A) RoutedCommand
Explanation:
RoutedCommand is the WPF implementation used for routed commands. Its execution and query events can travel through the element tree until an appropriate command binding is found.
39. Which class is commonly used to connect a command with its execution logic?
- CommandBinding
- CommandConnector
- CommandHandlerCollection
- CommandRoute
Answer: A) CommandBinding
Explanation:
CommandBinding associates a command with handlers such as Executed and CanExecute.
40. Which WPF class contains predefined commands such as Copy, Cut, and Paste?
- ApplicationCommands
- CommonCommands
- EditingActions
- SystemCommandsOnly
Answer: A) ApplicationCommands
Explanation:
ApplicationCommands provides predefined routed commands for common application operations, including commands such as Copy, Cut, and Paste.
41. Which WPF control is commonly used to display a collection of items?
- ListBox
- Border
- Image
- Label
Answer: A) ListBox
Explanation:
ListBox displays a collection of selectable items and can use data binding and data templates to customize how those items appear.
42. Which property is commonly used to provide a collection of items to an ItemsControl?
- ItemsSource
- DataCollection
- SourceItems
- ItemsData
Answer: A) ItemsSource
Explanation:
The ItemsSource property allows an items control to obtain its items from an external collection or data source.
43. Which WPF control is designed primarily for displaying text that users do not directly edit?
- TextBox
- TextBlock
- RichTextBox
- PasswordBox
Answer: B) TextBlock
Explanation:
TextBlock is commonly used to display text. Unlike TextBox, it is not primarily intended for user text editing.
44. Which WPF property can be used to control how content is aligned horizontally?
- HorizontalAlignment
- HorizontalPosition
- ContentDirection
- AlignHorizontal
Answer: A) HorizontalAlignment
Explanation:
HorizontalAlignment controls how an element is positioned horizontally within the available layout space.
45. Which WPF feature can change a control's property when a specified condition becomes true?
- Style Trigger
- DataContext
- CommandSource
- ResourceKey
Answer: A) Style Trigger
Explanation:
Triggers in WPF styles can respond to property values or other conditions and apply setters or other behavior when the trigger condition is satisfied.
46. Which WPF class is commonly used as the base for elements that participate in layout and have properties such as Width and Height?
- FrameworkElement
- ApplicationElement
- LayoutObject
- ControlElement
Answer: A) FrameworkElement
Explanation:
FrameworkElement provides important WPF framework-level functionality, including layout-related properties, data context, styles, resources, and other features used by UI elements.
47. Which method can be overridden in a custom WPF panel to implement its measurement behavior?
- MeasureOverride()
- MeasureChildren()
- CalculateLayout()
- OnMeasureChildren()
Answer: A) MeasureOverride()
Explanation:
Custom WPF panels can override MeasureOverride() to implement their measurement logic. The corresponding arrangement logic is implemented through ArrangeOverride().
48. Which markup extension references a resource by its key when defining a WPF UI?
- {Resource}
- {StaticResource}
- {ResourceKey}
- {FindResource}
Answer: B) {StaticResource}
Explanation:
{StaticResource} is a XAML markup extension used to reference an object stored in a resource dictionary by its key.
49. A WPF Button has Command="{Binding SaveCommand}". What must the binding source normally expose for this to work as an ICommand-based command?
- A property named SaveCommand that implements ICommand
- A method named SaveCommand with no return value only
- A ResourceDictionary named SaveCommand
- A routed event named SaveCommand
Answer: A) A property named SaveCommand that implements ICommand
Explanation:
The Button's Command property expects an ICommand. Therefore, the binding source should expose a property such as SaveCommand whose value implements ICommand.
50. Consider the following WPF XAML. What value will the TextBlock display when the Window's DataContext refers to an object whose Name property contains Sudhir?
<Window DataContext="{Binding CurrentUser}">
<TextBlock Text="{Binding Name}" />
</Window>
- CurrentUser
- Name
- Sudhir
- Nothing because TextBlock cannot use DataContext
Answer: C) Sudhir
Explanation:
The Window's DataContext is set through the CurrentUser binding. The TextBlock inherits that DataContext and its Text property binds to the object's Name property. If that property contains Sudhir, the TextBlock displays Sudhir.
Advertisement
Advertisement