Angular Interview Questions Part 1

What is Angular Framework?

Answer : Angular is a TypeScript-based open-source front-end platform that makes it easy to build applications within web/mobile/desktop. The major features of this framework such as declarative templates, dependency injection, end to end tooling, and many more other features are used to ease the development.


How does an Angular application work?

Answer : The entry point is the main.ts file that is configured in the angular.json. From then on, the AppModule is bootstrapped. The module will then render the AppComponent by using index.html file.


Can you describe some of the advantages of using Angular?

Answer : Angular helps create fast, dynamic, and scalable web applications with ease, using components and modules with a hierarchical dependency injection structure. It facilitates RESTful services and client-server communication with its core modules. Another advantage is that it provides the testing system (with Jasmine and Karma) and its environment already set


What are the key components of Angular?

Answer : Angular has the below key components,

  1. Component: These are the basic building blocks of angular application to control HTML views.
  2. Modules: An angular module is a set of angular basic building blocks like component, directives, services, etc. An application is divided into logical pieces and each piece of code is called “module” which perform a single task.
  3. Templates: This represents the views of an Angular application.
  4. Services: It is used to create components that can be shared across the entire application.
  5. Metadata: This can be used to add more data to an Angular class.


What is the angular CLI?

Answer : Angular CLI(Command Line Interface) is a command-line interface to scaffold and build angular apps using nodejs style (commonJs) modules.


What is Transpiling in Angular?

Answer : Transpiling is the process of converting the typescript into javascript (using Traceur, a JS compiler). Though typescript is used to write code in the Angular applications, the code is internally transpiled into javascript.


What are directives?

Answer : Directives add behavior to an existing DOM element or an existing component instance.

Directives are of three types:

  • Attribute Directives
  • Component Directives
  • Structural Directives

What is a bootstrapping module?

Answer: The root module that you bootstrap to launch the application is called a bootstrapping module. Every Angular application has a bootstrapping module. It is also called the AppModule. The bootstrapping module is mentioned in the AppModule class.


What is the difference between an Annotation and a Decorator in Angular?

Answer: Annotations in angular are “only” metadata set of the class using the reflect Metadata library. They are used to create an “annotation” array. On the other hand, decorators are the design patterns used for separating decoration or modification of a class without actually altering the source code.


What are templates in Angular?

Answer: Templates are the HTML code rendered in the index.html file’s body. The HTML code is a combination of data coming from the controller or the component and the model to provide a dynamic view to the user finally.


What are components?

Answer: Components are the most basic UI building block of an Angular app which formed a tree of Angular components. These components are subset of directives. Unlike directives, components always have a template and only one component can be instantiated per element in a template


Differentiate between Components and Directives in Angular

Answer: Components break up the application into smaller parts; whereas, Directives add behavior to an existing DOM element.


What is a module?

Answer: Modules are logical boundaries in your application and the application is divided into separate modules to separate the functionality of your application.


What are lifecycle hooks available?

Answer: The description of each lifecycle method is as below,

  1. ngOnChanges: When the value of data-bound property changes, then this method is called.
  2. ngOnInit: This is called whenever the initialization of the directive/component after Angular first displays the data-bound properties happens.
  3. ngDoCheck: This is for the detection and to act on changes that Angular can’t or won’t detect on its own.
  4. ngAfterContentInit: This is called in response after Angular projects external content into the component’s view.
  5. ngAfterContentChecked: This is called in response after Angular checks the content projected into the component.
  6. ngAfterViewInit: This is called in response after Angular initializes the component’s views and child views.
  7. ngAfterViewChecked: This is called in response after Angular checks the component’s views and child views.
  8. ngOnDestroy: This is the cleanup phase just before Angular destroys the directive/component.


What is data binding?

Answer: Data binding is a core concept in Angular and allows to define communication between a component and the DOM.


What is the difference between constructor and ngOnInit?

Answer: TypeScript classes have a default method called constructor which is normally used for the initialization purpose. Whereas ngOnInit method is specific to Angular, especially used to define Angular bindings. Even though constructor getting called first, it is preferred to move all of your Angular bindings to ngOnInit method.


What is a service?

Answer: A service is used when a common functionality needs to be provided to various modules. Services allow for greater separation of concerns for your application and better modularity by allowing you to extract common functionality out of components.


What is dependency injection in Angular?

Answer: When a component is dependent on another component the dependency is injected/provided during runtime.


What are pipes?

Answer: A pipe takes in data as input and transforms it to a desired output


What is the use of @Input and @Output?

When it comes to the communication of Angular Components, which are in Parent-Child Relationship; we use @Input in Child Component when we are passing data from Parent to Child Component and @Output is used in Child Component to receive an event from Child to Parent Component.


What is Routing in Angular?

Answer: Routing helps a user in navigating to different pages using links.


What is the purpose of using package.json in the angular project?

Answer: With the existence of package.json, it will be easy to manage the dependencies of the project. If we are using typescript in the angular project then we can mention the typescript package and version of typescript in package.json.


Differentiate between Observables and Promises.

Answer: Observables are lazy, which means nothing happens until a subscription is made. Whereas Promises are eager; which means as soon as a promise is created, the execution takes place. Observable is a stream in which passing of zero or more events is possible and the callback is called for each event. Whereas, promise handles a single event.


 Explain Authentication and Authorization.

Answer: Authentication: The user login credentials are passed to an authenticate API (on the server). On the server side validation of the credentials happens and a JSON Web Token (JWT) is returned. JWT is a JSON object that has some information or attributes about the current user. Once the JWT is given to the client, the client or the user will be identified with that JWT.

Authorization: After logging in successfully, the authenticated or genuine user does not have access to everything. The user is not authorized to access someone else’s data, he/she is authorized to access some data.


What Is Package.json? What’s the difference between package.json vs. package-lock.json?

Answer: With package.json file, it becomes easy to manage the project dependencies. We can mention details like the version, language, etc.… in package.json. For example, if bootstrap is used in our project, we can mention the ng-bootstrap package and its version in package.json.


If you are not familiar with package.json I really recommend this article that will help you build a mental picture of what is package.json file in dept and what’s the difference between package-lock.json and some tips based on my experience.


What is the difference between constructor and ngOnInit?

Answer: TypeScript classes have a default method called a constructor, which is normally used for construction purposes. It has the same purpose as a standard class in Java, such as building an object and initializing it. The constructor is also used to inject the dependencies we need in the component.


On the other hand, the ngOnInit method is specific to Angular, especially used to define Angular bindings. The constructor is always called before ngOninit.


What is zone in Angular?

Answer: A Zone is an execution context that persists across async tasks. Angular relies on zone.js to run Angular’s change detection processes when native JavaScript operations raise events.


What is the purpose of the async pipe?

Answer: Async pipe subscribes to a promise or an observable and returns the latest value. If a new value is emitted, the pipe marks the component that needs to be checked for any changes.


<code>observable|async</code>


What is metadata? How Is Metadata Represented in Angular?

Answer: Metadata is used to decorate a class so that it can configure the expected behavior of the class. The metadata is represented by decorators.


Metadata is represented using decorators like class decorators, property decorators, method decorators.

Examples of decorators in Angular

  • Class decorators: @Component and @NgModule
  • Property decorators: @Input and @Output
  • Method decorators: @HostListener
  • Parameter decorators: @Inject, Optional


What Are Class Decorators in Angular?

Answer: The class decorator contains the metadata of the suitable class type. It appears just before the class definition and declares the class to be of a certain type. Some class decorators are:


  • @Component
  • @NgModule
  • @Pipe
  • @Directive
  • @Injectable.