0
0
Angularframework~20 mins

FormGroup for grouping controls in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FormGroup Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FormGroup value?
Given this Angular FormGroup setup, what will be the value of form.value after initialization?
Angular
import { FormGroup, FormControl } from '@angular/forms';

const form = new FormGroup({
  firstName: new FormControl('Alice'),
  lastName: new FormControl('Smith')
});

console.log(form.value);
A{"firstName": "Alice", "lastName": "Smith"}
B{"firstName": "", "lastName": ""}
C{"firstName": null, "lastName": null}
D{}
Attempts:
2 left
💡 Hint
Think about what values you set when creating each FormControl.
state_output
intermediate
2:00remaining
What happens when a nested FormGroup is updated?
Consider this Angular FormGroup with a nested FormGroup. What will be the output of form.value after updating the nested control?
Angular
import { FormGroup, FormControl } from '@angular/forms';

const form = new FormGroup({
  user: new FormGroup({
    email: new FormControl('user@example.com'),
    password: new FormControl('1234')
  })
});

form.get('user.email')?.setValue('new@example.com');
console.log(form.value);
A{"user": {"email": "new@example.com", "password": "1234"}}
B{"email": "new@example.com", "password": "1234"}
C{"user": {"email": "user@example.com", "password": "1234"}}
D{"user.email": "new@example.com", "user.password": "1234"}
Attempts:
2 left
💡 Hint
Remember that nested FormGroups keep their structure in the value object.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a FormGroup with controls initialized to empty strings?
Select the code snippet that correctly creates a FormGroup with two controls, both starting with empty strings.
Anew FormGroup([ 'firstName', 'lastName' ])
Bnew FormGroup({ firstName: '', lastName: '' })
Cnew FormGroup({ firstName: new FormControl(''), lastName: new FormControl('') })
Dnew FormGroup(new FormControl(''), new FormControl(''))
Attempts:
2 left
💡 Hint
FormGroup expects an object with FormControl instances as values.
🔧 Debug
advanced
2:00remaining
Why does this FormGroup code cause a runtime error?
Identify the cause of the runtime error in this Angular FormGroup code snippet.
Angular
import { FormGroup, FormControl } from '@angular/forms';

const form = new FormGroup({
  email: new FormControl('test@example.com')
});

form.get('password').setValue('12345');
AsetValue requires an object, not a string
BFormControl must be initialized with null, not a string
CFormGroup keys must be strings, 'password' is invalid
Dform.get('password') returns null, so calling setValue causes an error
Attempts:
2 left
💡 Hint
Check if the control you are trying to access exists in the FormGroup.
🧠 Conceptual
expert
3:00remaining
How does FormGroup track the validity of nested controls?
In Angular, how does a FormGroup determine its overall validity when it contains nested FormGroups and FormControls?
AFormGroup validity is determined randomly at runtime
BFormGroup is valid only if all nested controls and nested FormGroups are valid
CFormGroup validity is always true unless explicitly set to false
DFormGroup validity depends only on its own controls, ignoring nested FormGroups
Attempts:
2 left
💡 Hint
Think about how validation cascades in nested forms.