Sample questions
Question 1Easy
Which directive adds or removes CSS styles from an object of values?
- ngFor
- ngIf
- ngModel
- ngStyle
Answer: D. ngStyle
Explanation
[ngStyle]="{color: c}".
Step-by-step solution
- Style.
Why the other options are wrong
- A. ngFor repeats elements; it does not change CSS styles.
- B. ngIf adds or removes elements; it does not set styles.
- C. ngModel binds form values; it does not set CSS styles.
Question 2Easy
- Repeats an element for each item in a collection
- Handles form submission events on the host
- Imports a standalone component into a template
- Creates a pipe
Answer: A. Repeats an element for each item in a collection
Explanation
It renders the host element once per item.
Step-by-step solution
- Loop over data.
Why the other options are wrong
- B. ngFor repeats elements; it does not handle submit events.
- C. Standalone imports go in an imports array, not through ngFor.
- D. Pipes are declared with @Pipe; ngFor only repeats elements.
Question 3Easy
Which of these is a built-in structural directive?
- [ngClass]
- (ngSubmit)
- [(ngModel)]
- *ngIf
Answer: D. *ngIf
Explanation
Structural directives change the DOM layout by adding or removing elements, written with an asterisk.
Step-by-step solution
- Star syntax indicates structural.
Why the other options are wrong
- A. ngClass is an attribute directive.
- B. ngSubmit is an event binding, not a structural directive.
- C. ngModel is a form directive.
Question 4Easy
What is <ng-container> mainly used for?
- Defining a lazy-loaded route inside a template block
- Grouping elements without adding an extra DOM element
- Declaring a service provider directly in the template
- Wrapping content in a block-level div so that it can be styled
Answer: B. Grouping elements without adding an extra DOM element
Explanation
ng-container renders no element of its own, so it adds no extra markup.
Step-by-step solution
- Ng-container is a grouping element for template content.
- It renders no element of its own, so it adds no extra markup.
- The correct answer is B (Grouping elements without adding an extra DOM element).
Why the other options are wrong
- A. Lazy routes are defined in the router configuration, not in a template.
- C. Providers go in a providers array, not in a template.
- D. Ng-container adds no div, so it is not used to wrap content for styling.
Tip: Use ng-container when you need a structural directive but no extra wrapper element.
Question 5Medium
How do you write a custom attribute directive that reacts to host element events?
- Use @Output on the host element and bind it in the template
- Attach jQuery handlers in ngOnInit of the parent component
- Use @HostListener inside a class decorated with @Directive
- Extend the Component decorator and override its template
Answer: C. Use @HostListener inside a class decorated with @Directive
Explanation
@HostListener('mouseenter') binds a method to a DOM event on the host element.
Step-by-step solution
- Use @Directive to create a custom directive class.
- Add @HostListener decorator to bind an event to the host element.
- Write a method in the class that reacts to the event.
- The method will execute when the event occurs on the host.
- This allows the directive to respond to DOM events directly.
Why the other options are wrong
- A. Output is for child-to-parent events, not for listening to the host element's own events.
- B. jQuery handlers in a parent's ngOnInit do not make a directive react to its host element.
- D. Extending the Component decorator is not how to build a directive; use @Directive instead.
Tip: @HostListener is used in directives to handle events on the host element.
Question 6Medium
Which attribute-binding form sets the DOM property disabled of a button from a component field busy?
- (disabled)="busy"
- {disabled}="busy"
- [disabled]="busy"
- #disabled="busy"
Answer: C. [disabled]="busy"
Explanation
Square brackets denote property binding from component to element.
Step-by-step solution
- The question asks for a binding that sets the DOM property disabled.
- Angular sets DOM properties with square brackets around the property name.
- The form [disabled]="busy" sets the button's disabled property from the busy field. The correct answer is C ([disabled]="busy").
Why the other options are wrong
- A. Round brackets bind events, not properties.
- B. Curly braces are not Angular binding syntax.
- D. A hash creates a template reference variable, not a property binding.
Tip: [ ] sets a property, ( ) listens for an event, and [( )] binds two ways.
Question 7Medium
What is the purpose of ng-container?
- Declare a service that every component in the template tree can then inject directly
- Group elements for a structural directive without adding an extra DOM element
- Load a lazy module when the route is hit
- Style a list with a theme
Answer: B. Group elements for a structural directive without adding an extra DOM element
Explanation
It renders nothing itself, only its children.
Step-by-step solution
- Avoid extra wrapper elements.
Why the other options are wrong
- A. ng-container groups content; it does not declare a service.
- C. Lazy loading is set up in the routes, not by ng-container.
- D. ng-container adds no styling; it groups content without DOM output.
Question 8Medium
How are [ngClass] and class bindings different?
- [ngClass] only works on components, never on plain DOM elements like div
- [ngClass] removes all static classes from the element
- [ngClass] can toggle several classes from an object, array or string
- There is no difference between them
Answer: C. [ngClass] can toggle several classes from an object, array or string
Explanation
ngClass takes a map of class names to booleans.
Step-by-step solution
- Object form: {active: isOn}.
Why the other options are wrong
- A. ngClass works on plain DOM elements such as div as well.
- B. ngClass adds and removes classes; it keeps static classes intact.
- D. They differ: ngClass takes objects, arrays or strings for many classes.
Question 9Hard
Why can't you place *ngIf and *ngFor on the same element?
- Only one structural directive is allowed per element; nest with ng-container
- Both can sit on one element, but ngFor always runs first and wins
- ngIf overrides ngFor at runtime, so the loop never renders anything
- Structural directives must be declared in a module of their own first
Answer: A. Only one structural directive is allowed per element; nest with ng-container
Explanation
The * syntax wraps the element in an ng-template; two on one element would need two wrappers, so nest with <ng-container>.
Step-by-step solution
- The * syntax wraps an element in an ng-template.
- Two structural directives on one element would need two wrappers.
- Angular allows only one per element, so nest them with ng-container. The correct answer is A.
Why the other options are wrong
- B. NgFor does not run first and win; putting both on one element is not allowed.
- C. NgIf does not override ngFor at runtime; the rule is about the syntax.
- D. Modules are not the reason; the limit applies to the element.
Tip: Put *ngIf and *ngFor on separate elements, grouping with ng-container if needed.
Question 10Hard
In a custom attribute directive, which decorator lets you react to host element events?
- @Input() for values passed from the parent
- @ViewChild() for querying a template element
- @Injectable() for registering a service
- @HostListener
Answer: D. @HostListener
Explanation
@HostListener('click') subscribes to events on the host element.
Step-by-step solution
- Need to listen to host events.
Why the other options are wrong
- A. Input receives values from the parent; it does not listen to host events.
- B. ViewChild queries template elements; it does not handle host events.
- C. Injectable marks services.
Practise all 19 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 →