Angular MCQs – Forms in Angular

21.) What is the role of asyncValidators in Angular forms?

A) They perform synchronous validation
B) They perform asynchronous validation, like checking unique usernames
C) They disable validation temporarily
D) They reset the form

Answer: Option B

Explanation: asyncValidators are used for asynchronous validation, like checking a value with a backend API.

22.) Which lifecycle hook is useful for initializing form data in a component?

A) ngOnChanges
B) ngOnInit
C) ngAfterViewInit
D) ngDoCheck

Answer: Option B

Explanation: ngOnInit() is commonly used for initializing form values when a component loads.

23.) How do you disable a form control in Reactive forms?

A) control.disable()
B) control.setDisabled(true)
C) control.readOnly(true)
D) control.deactivate()

Answer: Option A

Explanation: .disable() is used to disable a form control dynamically in Reactive forms.

24.) Which Angular directive is used for two-way data binding in forms?

A) ngFor
B) ngIf
C) formControl
D) ngModel

Answer: Option D

Explanation: ngModel is used for two-way data binding in Template-driven forms.

25.) How do you check if a form is completely valid before submission?

A) form.isComplete()
B) form.isValid()
C) form.valid
D) form.validateAll()

Answer: Option C

Explanation: .valid returns true only if all form controls meet their validation rules.

26.) What is the purpose of valueChanges in Angular forms?

A) It listens for changes in the form values
B) It resets the form automatically
C) It validates the form
D) It submits the form

Answer: Option A

Explanation: The valueChanges observable allows us to track real-time changes in form fields.

Leave a Reply

Your email address will not be published. Required fields are marked *