Angular MCQs With Code Snippets
Angular MCQs With Code Snippets
1@Component({
2 selector: 'app-root',
4})
7}
A) Hello, Angular!
B) app-root
C) Undefined
D) Error
““
1@Injectable({
2 providedIn: 'root'
3})
5 getData() {
7 }
8}
““
““
1@Component({
2 selector: 'app-example',
6 isVisible = true;
7}
A) Displays "Visible"
B) Displays nothing
C) Throws an error
D) Displays "isVisible"
““
““
““
1@Component({
2 selector: 'app-child',
4})
7}
A) A service
B) A directive
C) A child component that receives data
D) A parent component
““
““
1@Component({
2 selector: 'app-root',
4})
6 count = 0;
7 increment() {
8 this.count++;
9 }
10}
A) 0
B) 1
C) Increments count on button click
D) Displays an error
““
““
““
1@NgModule({
2 declarations: [AppComponent],
3 imports: [BrowserModule],
4 bootstrap: [AppComponent]
5})
A) Defines a service
B) Configures the root module
C) Creates a component
D) None of the above
““
““
““
1@Component({
2 selector: 'app-root',
4})
6 name = '';
7}
““
““
1@Component({
2 selector: 'app-root',
4})
7}
““
““
1@Component({
2 selector: 'app-root',
4})
6 isActive = true;
7}
““
““
1@Component({
2 selector: 'app-root',
4})
6 isVisible = false;
7 toggle() {
8 this.isVisible = !this.isVisible;
9 }
10}
““
““
1@Component({
2 selector: 'app-root',
4})
““
““
3 }
4 ```
5 A) HELLO ANGULAR
6 B) hello angular
7 C) Error
8 D) Hello Angular
9 **Answer:** A
10
12 ```” “
13 @Component({
14 selector: 'app-root',
16 })
18 age: number;
19 }
20 ```
24 D) Throws an error
25 **Answer:** C
26
28 ```” “
29 @Component({
30 selector: 'app-root',
32 })
34 items = [];
35 addItem() {
36 this.items.push('New Item');
37 }
38 }
39 ```
41 B) Displays nothing
42 C) Throws an error
44 **Answer:** A
45
47 ```” “
48 @Component({
49 selector: 'app-root',
51 })
53 getGreeting() {
55 }
56 }
57 ```
59 B) Displays nothing
60 C) Throws an error
61 D) Displays "getGreeting()"
62 **Answer:** A
63
65 ```” “
66 @Component({
67 selector: 'app-root',
71 color = 'blue';
72 }
73 ```
76 C) Throws an error
77 D) Displays nothing
78 **Answer:** A
79
81 ```” “
82 @Component({
83 selector: 'app-root',
85 })
87 logValue(value: string) {
88 console.log(value);
89 }
90 }
91 ```
93 B) Displays an error
94 C) Does nothing
95 D) Logs "undefined"
96 **Answer:** A
97
100 @Component({
103 })
106 }
107 ```
108 A) Visible
112 **Answer:** B
113
115 ```” “
116 @Component({
119 })
122 reset() {
123 this.count = 0;
124 }
125 }
126 ```
131 **Answer:** A
132
134 ```” “
135 @Component({
138 })
141 }
142 ```
143 A) 3
144 B) 0
145 C) Undefined
146 D) Error
147 **Answer:** A
148
150 ```” “
151 @Component({
154 })
157 }
158 ```
You
21. What will the following code output?
““
““
1@Component({
2 selector: 'app-root',
4})
6 isVisible = false;
7 toggle() {
8 this.isVisible = !this.isVisible;
9 }
10}
A) Displays "Visible"
B) Displays nothing
C) Displays "Toggle"
D) Throws an error
“ “ (Initially displays nothing, but toggles to display "Visible" on button click)
““
““
1@Component({
2 selector: 'app-root',
4})
6 email: string;
7}
““
““
1@Component({
2 selector: 'app-root',
4})
6 firstName = 'John';
7 lastName = 'Doe';
8 getFullName() {
10 }
11}
A) John Doe
B) John
C) Doe
D) Error
““
““
““
1@Component({
2 selector: 'app-root',
4})
7 addItem() {
8 this.items.push('New Item');
9 }
10}
““
““
1@Component({
2 selector: 'app-root',
4})
7}
““
““
1@Component({
2 selector: 'app-root',
4})
6 searchTerm: string;
7}
““
““
1@Component({
2 selector: 'app-root',
4})
6 count = 0;
7 increment() {
8 this.count++;
9 }
10}
A) Displays 0
B) Displays 1 on button click
C) Displays an error
D) Displays nothing
““
““
““
1@Component({
2 selector: 'app-root',
4})
6 isActive = true;
7}
A) Displays "Active"
B) Displays "Inactive"
C) Displays nothing
D) Throws an error
““
““
““
1@Component({
2 selector: 'app-root',
4})
7}
A) 3
B) 0
C) Undefined
D) Error
““