Bird
Raised Fist0
Angularframework~20 mins

FormControl basics in Angular - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FormControl Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the initial value of this FormControl?
Consider the following Angular code snippet creating a FormControl. What will be the initial value of nameControl.value?
Angular
import { FormControl } from '@angular/forms';

const nameControl = new FormControl('Alice');
A'' (empty string)
B'Alice'
Cundefined
Dnull
Attempts:
2 left
💡 Hint
Check what value is passed when creating the FormControl.
❓ state_output
intermediate
2:00remaining
What is the value of emailControl.value after update?
Given this Angular FormControl, what will be the value of emailControl.value after calling setValue('user@example.com')?
Angular
import { FormControl } from '@angular/forms';

const emailControl = new FormControl('');
emailControl.setValue('user@example.com');
Anull
Bundefined
C'user@example.com'
D'' (empty string)
Attempts:
2 left
💡 Hint
setValue changes the FormControl's value.
❓ lifecycle
advanced
2:00remaining
What happens when markAsTouched() is called on a FormControl?
In Angular, after calling markAsTouched() on a FormControl, which of the following is true?
Angular
import { FormControl } from '@angular/forms';

const control = new FormControl('');
control.markAsTouched();
AThe control's <code>untouched</code> property becomes true
BThe control's <code>dirty</code> property becomes true
CThe control's <code>pristine</code> property becomes true
DThe control's <code>touched</code> property becomes true
Attempts:
2 left
💡 Hint
Touched means the user has focused and left the control.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a FormControl with a required validator?
Select the correct Angular code to create a FormControl with an initial empty string and a required validator.
Angular
import { Validators } from '@angular/forms';
Anew FormControl('', [Validators.required])
Bnew FormControl('', Validators.required)
Cnew FormControl(Validators.required, '')
Dnew FormControl([Validators.required], '')
Attempts:
2 left
💡 Hint
Validators go as the second argument in an array.
🔧 Debug
expert
3:00remaining
Why does this FormControl's value not update on input?
Given this Angular template and component code, why does the FormControl's value not update when typing in the input field? Component: import { Component } from '@angular/core'; import { FormControl } from '@angular/forms'; @Component({ selector: 'app-email', templateUrl: './email.component.html' }) export class EmailComponent { email = new FormControl(''); } Template:
Angular
<input type="email" [formControl]="email" (input)="email.setValue($event.target.value)" />
AThe (input) event handler causes a loop and prevents proper update
BThe FormControl is missing a validator
CThe input type should be text, not email
DThe FormControl must be initialized with null, not empty string
Attempts:
2 left
💡 Hint
Using both [formControl] and (input) with setValue causes conflicts.

Practice

(1/5)
1. What is the primary purpose of FormControl in Angular?
easy
A. To create a complete form with multiple inputs
B. To manage the value and validation state of a single form input
C. To style form elements with CSS
D. To handle HTTP requests in forms

Solution

  1. Step 1: Understand FormControl role

    FormControl is designed to track the value and validation status of one input field.
  2. Step 2: Differentiate from other form classes

    Unlike FormGroup or FormArray, it does not manage multiple inputs or groups.
  3. Final Answer:

    To manage the value and validation state of a single form input -> Option B
  4. Quick Check:

    FormControl = single input manager [OK]
Hint: FormControl handles one input's value and validation [OK]
Common Mistakes:
  • Confusing FormControl with FormGroup
  • Thinking FormControl handles styling
  • Assuming FormControl manages HTTP
2. Which of the following is the correct way to create a FormControl with an initial value of 'hello'?
easy
A. new FormControl('hello')
B. FormControl('hello')
C. new FormControl = 'hello'
D. FormControl.create('hello')

Solution

  1. Step 1: Recall FormControl instantiation syntax

    In Angular, FormControl is created by calling new FormControl(initialValue).
  2. Step 2: Check each option

    new FormControl('hello') uses the correct constructor syntax. Options A, B, and D are invalid or incorrect syntax.
  3. Final Answer:

    new FormControl('hello') -> Option A
  4. Quick Check:

    Use new keyword with FormControl constructor [OK]
Hint: Use new FormControl(value) to create a control [OK]
Common Mistakes:
  • Omitting new keyword
  • Using assignment instead of constructor
  • Calling non-existent create method
3. Given this code snippet:
const nameControl = new FormControl('Alice');
nameControl.setValue('Bob');
console.log(nameControl.value);

What will be logged to the console?
medium
A. undefined
B. 'Alice'
C. 'Bob'
D. Error: setValue is not a function

Solution

  1. Step 1: Understand initial value assignment

    The FormControl is created with initial value 'Alice'.
  2. Step 2: Apply setValue method

    The setValue('Bob') updates the control's value to 'Bob'.
  3. Step 3: Check logged value

    Logging nameControl.value outputs the updated value 'Bob'.
  4. Final Answer:

    'Bob' -> Option C
  5. Quick Check:

    setValue changes value immediately [OK]
Hint: setValue updates FormControl value instantly [OK]
Common Mistakes:
  • Assuming initial value stays after setValue
  • Thinking setValue is asynchronous
  • Confusing value property with method
4. Identify the error in this code:
const ageControl = new FormControl();
ageControl.setValue;
medium
A. setValue is called without parentheses, so no value is set
B. FormControl must have an initial value
C. setValue cannot be used on FormControl
D. ageControl is not declared properly

Solution

  1. Step 1: Check setValue usage

    The code uses ageControl.setValue without parentheses, so the method is referenced but not called.
  2. Step 2: Understand consequences

    Because setValue is not called, no value is set and no error occurs, but the intended update does not happen.
  3. Final Answer:

    setValue is called without parentheses, so no value is set -> Option A
  4. Quick Check:

    Call setValue() with parentheses to update value [OK]
Hint: Always use parentheses to call setValue() [OK]
Common Mistakes:
  • Forgetting parentheses when calling methods
  • Thinking FormControl needs initial value
  • Misunderstanding method vs property
5. You want to create a FormControl that starts empty but requires the user to enter a value. Which code snippet correctly sets this up with validation?
hard
A. new FormControl('', { required: true })
B. new FormControl('', required)
C. new FormControl(Validators.required, '')
D. new FormControl('', [Validators.required])

Solution

  1. Step 1: Recall FormControl constructor signature

    The constructor accepts initial value as first argument, and validators as second argument in an array.
  2. Step 2: Analyze each option

    new FormControl('', { required: true }) uses invalid options object. new FormControl('', required) references undefined required. new FormControl(Validators.required, '') swaps arguments incorrectly. new FormControl('', [Validators.required]) correctly passes an array with Validators.required.
  3. Final Answer:

    new FormControl('', [Validators.required]) -> Option D
  4. Quick Check:

    Validators go in array as second argument [OK]
Hint: Pass validators as array in second argument [OK]
Common Mistakes:
  • Confusing with template-driven {required: true}
  • Forgetting Validators prefix
  • Swapping argument order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard