Monty  Boehm

Monty Boehm

1669631700

Best 50 ServiceNow Interview Questions and Answers

In this ServiceNow interview questions blog, I have collected the most frequently asked questions by interviewers. If you wish to brush up your ServiceNow basics, then I would recommend you take a look at this video first. This  video will introduce you to ServiceNow basics and hold you in good state to get started with this ‘ServiceNow Interview Questions’ blog

In case you have attended a ServiceNow interview in the recent past, do paste those ServiceNow interview questions in the comments section and we’ll answer them ASAP. So let us not waste any time and quickly start with this compilation of ServiceNow Interview Questions.

I have divided these questions in two sections:

  1. Basic ServiceNow Interview Questions
  2. Intermediate ServiceNow Interview Questions
  3. Advanced ServiceNow Interview Questions

 So let us start then,

Basic ServiceNow Interview Questions

1) What is ServiceNow?

ServiceNow is a cloud based IT Service Management (ITSM) tool. It provides a single system of record for:

  • IT services
  • Operations                                                                                                                                  
  • Business management

All aspects of IT Services live in the ServiceNow ecosystem. It gives us a complete view of services and resources. This allows for broad control of how to best allocate resources and design the process flow of those services.Refer this link to know more What Is ServiceNow?

2) What is an ‘Application’ in ServiceNow?

Applications in ServiceNow represent packaged solutions for delivering services and managing business processes. In simple words it is a group of modules which provides information related to those modules. For example Incident application will provide information related to Incident Management process.

3) What is full form of CMDB and what is it?

CMDB stands for Configuration Management Database. CMDB is a repository. It acts as a data warehouse for information technology installations. It holds data related to a collection of IT assets, and descriptive relationships between such assets.

4) What is LDAP Integration and its use?

LDAP is Light weight Directory Access Protocol. You can use it for user data population and user authentication. ServiceNow integrates with LDAP directory to streamline the user log in process and to automate the creation of user and assigning them roles.

5) What do you mean by data lookup and record matching?

Data lookup and record matching feature helps to set a field value based on some condition instead of writing scripts.

For example:

On Incident forms, the priority lookup rules sample data automatically. Then, set the incident Priority based on the incident Impact and Urgency values. Data lookup rules allow to specify the conditions and fields where they want data lookup to occur.

6) What is CMDB Baseline?

CMDB Baselines will help you, understand and control the changes made to a configuration Item(CI). These Baselines act as a snapshot of a CI.

7) How to enable or disable an application in ServiceNow?

Following steps will help you do the same:

  • Navigate to “Application Menus” module
  • Open the respective application.
  • Set value for active as ‘true’ to enable it or set it to ‘false’ to disable it.

8) What is a view?

View defines the arrangement of fields on a form or a list. For one single form we can define multiple views according to the user preferences or requirement.

9) What is ACL?

An ACL is access control list that defines what data users can access and how they can access it in ServiceNow.

10) What do you mean by impersonating a user? How it is useful?

Impersonating a user means giving the administrator  access to what the user would have access to. This includes the same menus and modules. ServiceNow records the administrator activities when the user impersonates another user. This feature helps in testing. You can impersonate that user and can test instead of logging out from your session and logging again with the user credentials.

Intermediate ServiceNow Interview Questions

11) What are dictionary overrides?

Dictionary overrides provide the ability to define a field on an extended table differently from the field on the parent table. For example, for a field on the Task [task] table, a dictionary override can change the default value on the Incident [incident] table without affecting the default value on Task [task] or Change [change].

12) What do you mean by coalesce?

Coalesce is a property of a field that we use in transform map field mapping. Coalescing on a field (or set of fields) lets you use the field as a unique key. If a match is found using the coalesce field, the existing record will be updated with the information being imported. If a match is not found, then a new record will be inserted into the database.

13) What are UI policies?

UI policies dynamically change information on a form and control custom process flows for tasks. UI policies are alternative to client scripts. You can use UI policies to set mandatory fields,which are read only and visible on a form. You can also use UI policy for dynamically changing a field on a form.

14) What is a data policy?

With data policies, you can enforce data consistency by setting mandatory and read-only states for fields. Data policies are similar to UI policies, but UI policies only apply to data entered on a form through the standard browser. Data policies can apply rules to all data entered into the system, including data brought in through email, import sets or web services and data entered through the mobile UI.

15) What is a client script?

Client script sits on the client side(the browser) and runs on client side only.Following are the types of client script:

  • OnLoad()
  • OnSubmit()
  • OnChange()
  • OncellEdit)

16) How can you cancel a form submission through client script?

In order to cancel a form submission the onSubmit function should return false. Refer the below mentioned syntax:

function onSubmit() { return false; }

17) What is a business rule?

Business rule is a server side script. It executes each time a record is inserted, updated, deleted, displayed or queried. The key thing to note while creating a business rule is, when and on what action it has to be executed. The business can be run or executed for following states

  • Display
  • Before
  • After

18) Can you call a business rule through a client script?

Yes, it is possible to call a business rule through a client script. You can use glide ajax for the same.

19) What is the Parent table for incident, change and problem? What does it do?

The Task table is the parent table of Incident, Problem & Change. It makes sure any fields, or configurations defined on the parent table automatically apply to the child tables.

20) What is a record producer?

A catalog item that allows users to create task-based records from the Service Catalog is called as a record producer. For example, creating a change record or a problem record using record producer. Record producers provide an alternative way to create records through Service Catalog

21) What is a glide record?

Glide record is a java class. It is used for performing database operations instead of writing SQL queries.

22) What is import set?

An import set is a tool that imports data from various data sources and, then maps that data into ServiceNow tables using transform map. It acts as a staging table for records imported.

23) What is transform Map?

A transform map transforms the record imported into ServiceNow import set table to the target table. It also determines the relationships between fields displaying in an Import Set table and fields in a target table.

24) What do you mean by foreign record insert?

When an import makes a change to a table that is not the target table for that import, this is when we say foreign record insert occurs. This happens when updating a reference field on a table.

25) Which searching technique is used to search a text or record in ServiceNow?

Zing is the text indexing and search engine that performs all text searches in ServiceNow.

b) Advanced ServiceNow Interview Questions:

26) What does the Client Transaction Timings plugin do?

It is used to enhance the system logs. It provides more information on the duration of transactions between the client and the server.

27) What is inactivity monitor?

It triggers an event for a task record if the task is inactive for a certain period of time. If the task remains inactive, the monitor repeats at regular intervals.

28) What is domain separation?

Domain separation is a way to separate data into logically-defined domains. For example a client ABC has two businesses and they are using ServiceNow single instance. They do not want users from one business to see data of other business. Here we can configure domain separation to isolate the records from both business.

29) How can you remove ‘Remember me’ check box from login page?

You can set the property – “glide.ui.forgetme” to true to remove the ‘Remember me’ check box from login page.

30) What is HTML Sanitizer?

The HTML Sanitizer is used to automatically clean up HTML markup in HTML fields and removes unwanted code and protect against security concerns such as cross-site scripting attacks. The HTML sanitizer is active for all instances starting with the Eureka release.

31) What is the significance of cascade variable checkbox in order guide?

Check box is used to select whether the variables used should cascade, which passes their values to the ordered items. If this check box is cleared, variable information entered in the order guide is not passed on to ordered items.

32) What are Gauges?

A gauge is visible on a ServiceNow homepage and can contain up-to-the-minute information about current status of records that exists on ServiceNow tables. A gauge can be based on a report. It can be put on a homepage or a content page.

33) What do you mean by Metrics in ServiceNow?

Metrics, record and measure the workflow of individual records. With metrics, customers can arm their process by providing tangible figures to measure. For example, how long it takes before a ticket is reassigned.

34) What types of searches are available in ServiceNow?

Following searches will help you find information in ServiceNow:

Lists: Find records in a list;

Global text search: Finds records in multiple tables from a single search field.

Knowledge base: Finds knowledge articles.

Navigation filter: Filters the items in the application navigator.

Search screens: Use a form ­like interface to search for records in a table. Administrators can create these custom modules.

35) What is a BSM Map?

BSM Map is a Business Service Management map. It graphically displays the Configuration Items (CI). These items support a business service and indicates the status of those Configuration Items.

36) Which table stores update sets and customization?

Each update set is stored in the Update Set [sys_update_set] table. The customizations that are associated with the update set, are stored in [sys_update_xml] table.

37) What happens when you mark a default update set as complete?

If the Default update set is marked Complete, the system creates another update set named Default1 and uses it as the default update set.

38) Can you add Homepages and Content pages to ‘update sets’ in ServiceNow?

Homepages and content pages don’t get added to ‘update sets’ by default. You need to manually add pages to the current ‘update sets’ by unloading them.

39) What is Reference qualifier?

Reference qualifiers restricts the data, that can be selected for a reference field.

40) What is Performance Analytics in ServiceNow?

Performance Analytics is an additional application in ServiceNow that allows customers to take a snapshot of data at regular intervals and create time series for any Key Performance Indicator (KPI) in the organization.

41) What is the latest servicenow user interface and when was it released?

The latest user interface is UI16 interface. It came in Helsinki release.

42) What is a sys_id?

It is a unique 32-character GUID that identifies each record created in each table in ServiceNow.

43) What is scorecard?

A scorecard measures the performance of an employee or a business process. It is a graphical representation of progress over time. A scorecard belongs to an indicator. The first step is to define the indicators that you want to measure. You can enhance scorecards by adding targets, breakdowns (scores per group), aggregates, and time series.

44) Can you update a record without updating its system fields(like sys_updated_by, sys_updated_on)?

Yes, you can do it by using a function autoSysFields() in your server side scripting. Whenever you are updating a record set the autoSysFields() to false.

Consider following Example:

var gr = new GlideRecord(‘incident’);
gr.query();
if(gr.next()){
gr.autoSysFields(false);
short_description = “Test from Examsmyntra” ;
gr.update();
}

45) What is Reference qualifier?

Reference qualifier is used to restrict the data that is select able for a reference field.

46) What is Performance Analytics in ServiceNow?

Performance Analytics is an additional application in ServiceNow that allows customers to take a snapshot of data at regular intervals and create time series for any key performance indicator (KPI) in the organisation.

47) How to create a new role?

Navigate to User Administration > Role and click New.

48) Can I have more than one function listening to the same thing?
 

You can, but there is no guarantee of sequencing. You cannot predict what order your event handlers will run.

49) Which method do you use to get all the active/inactive records from a table?
 

You can use addActiveQuery() method to get all the active records and addInactiveQuery() to get the all inactive records.

50) What is the difference between next() and _next() method?
 

next() method is responsible to move to the next record in GlideRecord. _next() provides the same functionality as next(), intended to be used in cases when we query the table having a column name as next.

So this brings us to the end of the blog. I hope you enjoyed these ServiceNow Interview Questions. The topics that you learnt in this ServiceNow Interview questions blog are the most sought-after skill sets that recruiters look for in a ServiceNow Professional.

You can also check out our ServiceNow YouTube playlist: 

www.youtube.com/playlist?list=PL9ooVrP1hQOGOrWF7soRFiTVepwQI6Dfw

In case  if you wish to build a career in ServiceNow then check out our ServiceNow Certification Training.

Got a question for us? Please mention it in the comments section of this ServiceNow Interview Questions and we will get back to you.

Original article source at: https://www.edureka.co/

#service #interviewquestion 

Best 50 ServiceNow Interview Questions and Answers
Oral  Brekke

Oral Brekke

1669623000

Best 50 Docker Interview Questions and Answers

Top 50 Docker Interview Questions You Must Prepare

Introduced in 2013, Docker hit the IT industry. It turned out to be a big hit with 13 billion + container image downloads per month in 2022. Increasing demand for docker showed an exponential increase in job openings. Go ahead and take advantage of all the new job openings with this article which lists down 50 most important Docker Interview Questions.

I have categorized these 50 questions into:

  • Docker Basic Questions
  • Docker Basic Commands
  • Docker Advanced Questions

Docker Basic Questions

This category of Docker Interview Questions consists of questions that you’re expected to know. These are the most basic questions. An interviewer will start with these and eventually increase the difficulty level. Let’s have a look at them.

1. What is Hypervisor?

A hypervisor is a software that makes virtualization possible. It is also called Virtual Machine Monitor. It divides the host system and allocates the resources to each divided virtual environment. You can basically have multiple OS on a single host system. There are two types of Hypervisors:

  • Type 1: It’s also called Native Hypervisor or Bare metal Hypervisor. It runs directly on the underlying host system. It has direct access to your host’s system hardware and hence does not require a base server operating system.
  • Type 2: This kind of hypervisor makes use of the underlying host operating system. It’s also called Hosted Hypervisor.

2. What is virtualization?

Virtualization is the process of creating a software-based, virtual version of something(compute storage, servers, application, etc.). These virtual versions or environments are created from a single physical hardware system. Virtualization lets you split one system into many different sections which act like separate, distinct individual systems. A software called Hypervisor makes this kind of splitting possible. The virtual environment created by the hypervisor is called Virtual Machine.

3. What is containerization?

Let me explain this is with an example. Usually, in the software development process, code developed on one machine might not work perfectly fine on any other machine because of the dependencies. This problem was solved by the containerization concept. So basically, an application that is being developed and deployed is bundled and wrapped together with all its configuration files and dependencies. This bundle is called a container. Now when you wish to run the application on another system, the container is deployed which will give a bug-free environment as all the dependencies and libraries are wrapped together. Most famous containerization environments are Docker and Kubernetes.

4. Difference between virtualization and containerization

Once you’ve explained containerization and virtualization, the next expected question would be differences. The question could either be differences between virtualization and containerization or differences between virtual machines and containers. Either way, this is how you respond.

Containers provide an isolated environment for running the application. The entire user space is explicitly dedicated to the application. Any changes made inside the container is never reflected on the host or even other containers running on the same host. Containers are an abstraction of the application layer. Each container is a different application.

Whereas in Virtualization, hypervisors provide an entire virtual machine to the guest(including Kernal). Virtual machines are an abstraction of the hardware layer. Each VM is a physical machine.

5. What is Docker?

Since its a Docker interview, there will be an obvious question about what is Docker. Start with a small definition.

Docker is a containerization platform which packages your application and all its dependencies together in the form of containers so as to ensure that your application works seamlessly in any environment, be it development, test or production. Docker containers, wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries, etc. It wraps basically anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.

6. What is a Docker Container?

Docker containers include the application and all of its dependencies. It shares the kernel with other containers, running as isolated processes in user space on the host operating system. Docker containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud. Docker containers are basically runtime instances of Docker images.

7. What are Docker Images?

When you mention Docker images, your very next question will be “what are Docker images”.

Docker image is the source of Docker container. In other words, Docker images are used to create containers. When a user runs a Docker image, an instance of a container is created. These docker images can be deployed to any Docker environment.

8. What is Docker Hub?

Docker images create docker containers. There has to be a registry where these docker images live. This registry is Docker Hub. Users can pick up images from Docker Hub and use them to create customized images and containers. Currently, the Docker Hub is the world’s largest public repository of image containers.

9. Explain Docker Architecture?

Docker Architecture consists of a Docker Engine which is a client-server application with three major components:

  1. A server which is a type of long-running program called a daemon process (the docker command).
  2. A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  3. A command line interface (CLI) client (the docker command).
  4. The CLI uses the Docker REST API to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI.

Refer to this blog, to read more about Docker Architecture.

10. What is a Dockerfile?

Let’s start by giving a small explanation of Dockerfile and proceed by giving examples and commands to support your arguments.

Docker can build images automatically by reading the instructions from a file called Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

The interviewer does not just expect definitions, hence explain how to use a Dockerfile which comes with experience. Have a look at this tutorial to understand how Dockerfile works.

11. Tell us something about Docker Compose.

Docker Compose is a YAML file which contains details about the services, networks, and volumes for setting up the Docker application. So, you can use Docker Compose to create separate containers, host them and get them to communicate with each other. Each container will expose a port for communicating with other containers.

12. What is Docker Swarm?

You are expected to have worked with Docker Swarm as it’s an important concept of Docker.

Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual Docker host. Docker Swarm serves the standard Docker API, any tool that already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts.

13. What is a Docker Namespace?

A namespace is one of the Linux features and an important concept of containers. Namespace adds a layer of isolation in containers. Docker provides various namespaces in order to stay portable and not affect the underlying host system. Few namespace types supported by Docker – PID, Mount, IPC, User, Network

14. What is the lifecycle of a Docker Container?

This is one of the most popular questions asked in Docker interviews. Docker containers have the following lifecycle:

  • Create a container
  • Run the container
  • Pause the container(optional)
  • Un-pause the container(optional)
  • Start the container
  • Stop the container
  • Restart the container
  • Kill the container
  • Destroy the container

15. What is Docker Machine?

Docker machine is a tool that lets you install Docker Engine on virtual hosts. These hosts can now be managed using the docker-machine commands. Docker machine also lets you provision Docker Swarm Clusters.

Docker Basic Commands

Once you’ve aced the basic conceptual questions, the interviewer will increase the difficulty level. So let’s move on to the next section of this Docker Interview Questions article. This section talks about the commands that are very common amongst docker users.

16. How to check for Docker Client and Docker Server version?

The following command gives you information about Docker Client and Server versions:

$ docker version

17. How do you get the number of containers running, paused and stopped?

You can use the following command to get detailed information about the docker installed on your system.

$ docker info

You can get the number of containers running, paused, stopped, the number of images and a lot more.

18. If you vaguely remember the command and you’d like to confirm it, how will you get help on that particular command?

The following command is very useful as it gives you help on how to use a command, the syntax, etc.

$ docker --help

The above command lists all Docker commands. If you need help with one specific command, you can use the following syntax:

$ docker <command> --help

19. How to login into docker repository?

You can use the following command to login into hub.docker.com:

$ docker login

You’ll be prompted for your username and password, insert those and congratulations, you’re logged in.

20. If you wish to use a base image and make modifications or personalize it, how do you do that?

You pull an image from docker hub onto your local system

It’s one simple command to pull an image from docker hub:

$ docker pull <image_name>

21. How do you create a docker container from an image?

Pull an image from docker repository with the above command and run it to create a container. Use the following command:

$ docker run -it -d <image_name>

Most probably the next question would be, what does the ‘-d’ flag mean in the command?

-d means the container needs to start in the detached mode. Explain a little about the detach mode. Have a look at this blog to get a better understanding of different docker commands.

22. How do you list all the running containers?

The following command lists down all the running containers:

$ docker ps

23. Suppose you have 3 containers running and out of these, you wish to access one of them. How do you access a running container?

The following command lets us access a running container:

$ docker exec -it <container id> bash

The exec command lets you get inside a container and work with it.

24. How to start, stop and kill a container?

The following command is used to start a docker container:

$ docker start <container_id>

and the following for stopping a running container:

$ docker stop <container_id>

kill a container with the following command:

$ docker kill <container_id>

25. Can you use a container, edit it, and update it? Also, how do you make it a new and store it on the local system?

Of course, you can use a container, edit it and update it. This sounds complicated but its actually just one command.

$ docker commit <conatainer id> <username/imagename>

26. Once you’ve worked with an image, how do you push it to docker hub?

$ docker push <username/image name>

27. How to delete a stopped container?

Use the following command to delete a stopped container:

$ docker rm <container id>

28. How to delete an image from the local storage system?

The following command lets you delete an image from the local system:

$ docker rmi <image-id>

29. How to build a Dockerfile? 

Once you’ve written a Dockerfile, you need to build it to create an image with those specifications. Use the following command to build a Dockerfile:

$ docker build <path to docker file>

The next question would be when do you use “.dockerfile_name” and when to use the entire path?

Use “.dockerfile_name” when the dockerfile exits in the same file directory and you use the entire path if it lives somewhere else.

30. Do you know why docker system prune is used? What does it do?

$ docker system prune

The above command is used to remove all the stopped containers, all the networks that are not used, all dangling images and all build caches. It’s one of the most useful docker commands.

Docker Advanced Questions

Once the interviewer knows that you’re familiar with the Docker commands, he/she will start asking about practical applications This section of Docker Interview Questions consists of questions that you’ll only be able to answer when you’ve gained some experience working with Docker.

31. Will you lose your data, when a docker container exists?

No, you won’t lose any data when Docker container exits. Any data that your application writes to the container gets preserved on the disk until you explicitly delete the container. The file system for the container persists even after the container halts.

32. Where all do you think Docker is being used?

When asked such a question, respond by talking about applications of Docker. Docker is being used in the following areas:

  • Simplifying configuration: Docker lets you put your environment and configuration into code and deploy it.
  • Code Pipeline Management: There are different systems used for development and production. As the code travels from development to testing to production, it goes through a difference in the environment. Docker helps in maintaining the code pipeline consistency.
  • Developer Productivity: Using Docker for development gives us two things – We’re closer to production and development environment is built faster.
  • Application Isolation: As containers are applications wrapped together with all dependencies, your apps are isolated. They can work by themselves on any hardware that supports Docker.
  • Debugging Capabilities: Docker supports various debugging tools that are not specific to containers but work well with containers.
  • Multi-tenancy: Docker lets you have multi-tenant applications avoiding redundancy in your codes and deployments.
  • Rapid Deployment: Docker eliminates the need to boost an entire OS from scratch, reducing the deployment time.

33. How is Docker different from other containerization methods?

Docker containers are very easy to deploy in any cloud platform. It can get more applications running on the same hardware when compared to other technologies, it makes it easy for developers to quickly create, ready-to-run containerized applications and it makes managing and deploying applications much easier. You can even share containers with your applications.

If you have some more points to add you can do that but make sure the above explanation is there in your answer.

34. Can I use JSON instead of YAML for my compose file in Docker?

You can use JSON instead of YAML for your compose file, to use JSON file with compose, specify the JSON filename to use, for eg:

$ docker-compose -f docker-compose.json up

35. How have you used Docker in your previous position?

Explain how you have used Docker to help rapid deployment. Explain how you have scripted Docker and used it with other tools like Puppet, Chef or Jenkins. If you have no past practical experience in Docker and instead have experience with other tools in a similar space, be honest and explain the same. In this case, it makes sense if you can compare other tools to Docker in terms of functionality.

36. How far do Docker containers scale? Are there any requirements for the same?

Large web deployments like Google and Twitter and platform providers such as Heroku and dotCloud, all run on container technology. Containers can be scaled to hundreds of thousands or even millions of them running in parallel. Talking about requirements, containers require the memory and the OS at all the times and a way to use this memory efficiently when scaled.

37. What platforms does docker run on?

This is a very straightforward question but can get tricky. Do some company research before going for the interview and find out how the company is using Docker. Make sure you mention the platform company is using in this answer.

Docker runs on various Linux administration:

  • Ubuntu 12.04, 13.04 et al
  • Fedora 19/20+
  • RHEL 6.5+
  • CentOS 6+
  • Gentoo
  • ArchLinux
  • openSUSE 12.3+
  • CRUX 3.0+

It can also be used in production with Cloud platforms with the following services:

  • Amazon EC2
  • Amazon ECS
  • Google Compute Engine
  • Microsoft Azure
  • Rackspace

38. Is there a way to identify the status of a Docker container?

There are six possible states a container can be at any given point – Created, Running, Paused, Restarting, Exited, Dead.

Use the following command to check for docker state at any given point:

$ docker ps

The above command lists down only running containers by default. To look for all containers, use the following command:

$ docker ps -a

39. Can you remove a paused container from Docker?

The answer is no. You cannot remove a paused container. The container has to be in the stopped state before it can be removed.

40. Can a container restart by itself?

No, it’s not possible for a container to restart by itself. By default the flag -restart is set to false.

41. Is it better to directly remove the container using the rm command or stop the container followed by remove container?

Its always better to stop the container and then remove it using the remove command.

$ docker stop <coontainer_id>
$ docker rm -f <container_id>

Stopping the container and then removing it will allow sending SIG_HUP signal to recipients. This will ensure that all the containers have enough time to clean up their tasks. This method is considered a good practice, avoiding unwanted errors.

42. Will cloud overtake the use of Containerization?

Docker containers are gaining popularity but at the same time, Cloud services are giving a good fight. In my personal opinion, Docker will never be replaced by Cloud. Using cloud services with containerization will definitely hype the game. Organizations need to take their requirements and dependencies into consideration into the picture and decide what’s best for them. Most of the companies have integrated Docker with the cloud. This way they can make the best out of both the technologies.

43. How many containers can run per host?

There can be as many containers as you wish per host. Docker does not put any restrictions on it. But you need to consider every container needs storage space, CPU and memory which the hardware needs to support. You also need to consider the application size. Containers are considered to be lightweight but very dependant on the host OS.

44. Is it a good practice to run stateful applications on Docker?

The concept behind stateful applications is that they store their data onto the local file system. You need to decide to move the application to another machine, retrieving data becomes painful. I honestly would not prefer running stateful applications on Docker.

45. Suppose you have an application that has many dependant services. Will docker compose wait for the current container to be ready to move to the running of the next service?

The answer is yes. Docker compose always runs in the dependency order. These dependencies are specifications like depends_on, links, volumes_from, etc.

46. How will you monitor Docker in production?

Docker provides functionalities like docker stats and docker events to monitor docker in production. Docker stats provides CPU and memory usage of the container. Docker events provide information about the activities taking place in the docker daemon.

47. Is it a good practice to run Docker compose in production?

Yes, using docker compose in production is the best practical application of docker compose. When you define applications with compose, you can use this compose definition in various production stages like CI, staging, testing, etc.

48. What changes are expected in your docker compose file while moving it to production?

These are the following changes you need make to your compose file before migrating your application to the production environment:

  • Remove volume bindings, so the code stays inside the container and cannot be changed from outside the container.
  • Binding to different ports on the host.
  • Specify a restart policy
  • Add extra services like log aggregator

49. Have you used Kubernetes? If you have, which one would you prefer amongst Docker and Kubernetes?

Be very honest in such questions. If you have used Kubernetes, talk about your experience with Kubernetes and Docker Swarm. Point out the key areas where you thought docker swarm was more efficient and vice versa. Have a look at this blog for understanding differences between Docker and Kubernetes.

You Docker interview questions are not just limited to the workarounds of docker but also other similar tools. Hence be prepared with tools/technologies that give Docker competition. One such example is Kubernetes.

50. Are you aware of load balancing across containers and hosts? How does it work?

While using docker service with multiple containers across different hosts, you come across the need to load balance the incoming traffic. Load balancing and HAProxy is basically used to balance the incoming traffic across different available(healthy) containers. If one container crashes, another container should automatically start running and the traffic should be re-routed to this new running container. Load balancing and HAProxy works around this concept.

This brings us to the end of the Docker Interview Questions article. With increasing business competition, companies have realized the importance of adapting and taking advantage of the changing market. Few things that kept them in the game were faster scaling of systems, better software delivery, adapting to new technologies, etc. That’s when docker swung into the picture and gave these companies boosting support to continue the race.

If you want to learn more about DevOps, check out the DevOps training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The Edureka DevOps Certification Training course helps learners gain expertise in various DevOps processes and tools such as Puppet, Jenkins, Nagios and GIT for automating multiple steps in SDLC.

Original article source at: https://www.edureka.co/

#docker #interviewquestion 

 Best 50 Docker Interview Questions and Answers
Noah Saunders

Noah Saunders

1660788420

Top 20+ MOST ASKED Linux Interview Questions & Answers

In this tutorial, we show you the most asked Linux Interview Questions and Answers. The most common and important Linux Interview Questions and their Answers. If you are looking to become a Linux Administrator, then this is the right place for you to prepare for the interview.

Did you know that more than 90% of the World’s Fastest Computers use Linux? No doubt why! Linux is fast, powerful, and a techies’ favorite. If you are looking to become a Linux Administrator, then this is the right place for you to prepare for the interview. In this article, I will be discussing some of the most common and important Linux Interview Questions and their Answers. 

Preparing for your interview with this list of questions and answers will astonish your interviewer and help you get your Linux job.  

The following are the questions that are answered in the video:

⏰Timestamps⏰  

00:00 Intro   
00:28 #1 What do you mean by Linux?  
00:58 #2 What are the basic components of Linux? 
02:36 #3 What do you mean by LILO? 
03:24 #4 What do you mean by Linux shell, and what types of shells are there? 
04:22 #5 What do you know and mean by Daemons? 
05:05 #6 What are some of the differences between Cron and Anacron? 
06:23 #7 What do you mean by Load Average in Linux? 
06:53 #8 What are CLI and GUI? 
07:51 #9 What do you mean by SSH? How can we use it? 
08:29 #10 What do you know about file permissions in Linux? 
09:09 #11 Name some of the Linux directory commands. 
09:56 #12 What is the difference between rmdir and rm -r? 
10:38 #13 What do you mean by Desktop Environment? 
11:16 #14 How can you copy a file in Linux? 
12:16 #15 What is the difference between the cp command and the mv command? 
12:58 #16 What should you do when a program fails to execute or how do you kill a process or program in Linux? 
14:08 #17 How can you create a file without opening it, or how can you create a file from the terminal? 
15:00 #18 Why do we use the Export command? 
15:26 #19 What is the Top command, and why do we use it? 
16:17 #20 What do you mean by Shell Script? 
16:45 Outro


This Linux Interview Questions blog is divided into two parts: Part A-Theoretical Questions and Part B-Scenario Based Questions. Let’s get started!

Part A-Theoretical Questions

In this part of Linux Interview Questions, we will discuss the most common theoretical and concept based questions.

1. What is Linux?

Linux is an Open-Source Operating System based on Unix. Linux was first introduced by Linus Torvalds. The main purpose of Linux was to provide free and low-cost Operating System for users who could not afford Operating Systems like Windows or iOS or Unix.

2. What is the difference between Linux and Unix?

The main differences between Linux and UNIX are as follows:

ParameterLinuxUnix
PriceBoth free distributions and paid distributions are available.Different levels of UNIX have a different cost structure
Target UserEveryone (Home user, Developer, etc.)Mainly Internet Server, Workstations, Mainframes.
File System SupportExt2, Ext3, Ext4, Jfs, ReiserFS, Xfs, Btrfs, FAT, FAT32, NTFS.jfs, gpfs, hfs, hfs+, ufs, xfs, zfs,vxfs.
GUIKDE and GnomeCommon Desktop Environment
Viruses listed60-10080-120
Bug Fix SpeedFaster because Linux is Community drivenSlow
PortabilityYesNo
ExamplesUbuntu, Fedora, Red Hat, Kali Linux, Debian, Archlinux, Android, etc. OS X, Solaris, All Linux

Linux vs. Unix – Linux Interview Questions

3. What is Linux Kernel? Is it legal to edit Linux Kernel?

Linux kernel refers to the low-level system software. It is used to manage resources and provide an interface for user interaction.

Yes, it is legal to edit Linux Kernel. Linux is released under the General Public License (General Public License). Any project released under GPL can be modified and edited by the end users. 

4. What is LILO?

LILO stands for LInux LOader. LILO is a Linux Boot Loader that loads Linux Operating System into the main memory to begin execution. Most of the computers come with boot loaders for certain versions of Windows or Mac OS. So, when you want to use Linux OS, you need to install a special boot loader for it. LILO is one such boot loader. 

When the computer is started, BIOS conducts some initial tests and transfers control to the Master Boot Record. From here, LILO loads the Linux OS and starts it.

The advantage of using LILO is that it allows fast boot of Linux OS. 

5. What are the basic components of Linux?

The basic components of Linux are:

  • Kernel: It is the core component of the Operating System that manages operations and hardware.
  • Shell: Shell is a Linux interpreter which is used to execute commands.
  • GUIGUI stands for Graphical User Interface which is another way for a user to interact with the system. But unlike CLI, GUI consists of Images, Buttons, TextBoxes for interaction.
  • System Utilities: These are the software functions that allows the user to manage the computer.
  • Application Programs: Software programs or set of functions designed to accomplish a specific task.

6. Which are the Shells used in Linux?

The most common Shells used in Linux are

  • bash: Bourne Again Shell is the default for most of the Linux distributions
  • ksh: Korn Shell is a high-level programming language shell
  • csh: C Shell follows C like syntax and provides spelling correction and Job Control
  • zsh: Z Shell provides some unique features such as filename generation, startup files, login/logout watching, closing comments etc.
  • fish: Friendly Interactive Shell provides some special features like web-based configuration, auto-suggestions, fully scriptable with clean scripts

7. What is Swap Space?

Swap Space is the additional spaced used by Linux that temporarily holds concurrently running programs when the RAM does not have enough space to hold the programs. When you run a program, it resides on the RAM so that the processor can fetch data quickly. Suppose you are running more programs than the RAM can hold, then these running programs are stored in the Swap Space. The processor will now look for data in the RAM and the Swap Space. 

Swap Space is used as an extension of RAM by Linux.

8. What is the difference between BASH and DOS?

There are 3 main differences between BASH and DOS:

Sl. no.BASHDOS
1.Commands are case-sensitive.Commands are not case-sensitive.
2.

‘/’ (forward slash) is used as a directory separator.

 

(backslash) is used as an escape character.

‘/’ (forward slash) is used as command argument delimiter.

 

(backslash) is used as a directory separator.

3.Follows naming convention: 8 characters for file name postfixed with 3 characters for the extension.No naming convention.

                                                                          Bash vs Dos – Linux Interview Questions

9. What command would you use to check how much memory is being used by Linux?

You can use any of the following commands:

  • free -m
  • vmstat
  • top
  • htop

10. Explain file permission in Linux.

There are 3 kinds of permission in Linux:

  1. Read: Allows a user to open and read the file
  2. Write: Allows a user to open and modify the file
  3. Execute: Allows a user to run the file.

You can change the permission of a file or a directory using the chmodcommand. There are two modes of using the chmod command:

  1. Symbolic mode
  2. Absolute mode

Symbolic mode

The general syntax to change permission using Symbolic mode is as follows:

$ chmod <target>(+/-/=)<permission> <filename>

where <permissions> can be r: read; w: write; x: execute.

<target> can be u : user; g: group; o: other; a: all

'+' is used for adding permission

'-' is used for removing permission

'=' is used for setting the permission

For example, if you want to set the permission such that the user can read, write, and execute it and members of your group can read and execute it, and others may only read it.

Then the command for this will be:

$ chmod u=rwx,g=rx,o=r filename

Absolute mode

The general syntax to change permission using Absolute mode is as follows:

$ chmod <permission> filename

The Absolute mode follows octal representation. The leftmost digit is for the user, the middle digit is for the user group and the rightmost digit is for all.

Below is the table that explains the meaning of the digits that can be used and their effect.

0No permission– – –
1Execute permission– – x
2Write permission– w –
3Execute and write permission: 1 (execute) + 2 (write) = 3– wx
4Read permissionr – –
5Read and execute permission: 4 (read) + 1 (execute) = 5r – x
6Read and write permission: 4 (read) + 2 (write) = 6rw –
7All permissions: 4 (read) + 2 (write) + 1 (execute) = 7rwx

For example, if you want to set the permission such that the user can read, write, and execute it and members of your group can read and execute it, and others may only read it.

Then the command for this will be:

$ chmod 754 filename

11. What are inode and process id?

inode is the unique name given by the operating system to each file. Similarly, process id is the unique id given to each process.

12. Which are the Linux Directory Commands?

There are 5 main Directory Commands in Linux:

pwd: Displays the path of the present working directory.

Syntax: $ pwd

ls: Lists all the files and directories in the present working directory.

Syntax: $ ls

cd: Used to change the present working directory.

Syntax: $ cd <path to new directory>

mkdir: Creates a new directory

Syntax: $ mkdir <name (and path if required) of new directory>

rmdir: Deletes a directory

Syntax: $ rmdir <name (and path if required) of directory>

13. What is Virtual Desktop?

Virtual Desktop is a feature that allows users to use the desktop beyond the physical limits of the screen. Basically, Virtual Desktop creates a virtual screen to expand the limitation of the normal screen. 

There are two ways Virtual Desktop can be implemented:

  1. Switching Desktops
  2. Oversized Desktops

Switching Desktops

In the case of Switching Desktops, you can create discrete virtual desktops to run programs. Here, each virtual desktop will behave as an individual desktop and the programs running on each of these desktops is accessible only to the users who are using that particular desktop. 

Oversized Desktops

Oversized Desktops do not offer a discrete virtual desktop but it allows the user to pan and scroll around the desktop that is larger in size than the physical screen. 

14. Which are the different modes of vi editor?

There are 3 modes of vi editor:

  1. Regular/Command mode: Lets you view the content
  2. Insertion/edit mode: Lets you delete or insert content
  3. Replacement mode: Lets you overwrite content

15. What are daemons?

A daemon is a computer program that runs as a background process to provide functions that might not be available in the base Operating System. Daemons are usually used to run services in the background without directly being in control of interactive users. The purpose of Daemons are to handle periodic requests and then forward the requests to appropriate programs for execution. 

16. What are the process states in Linux?

The process states are as follows:

  • Ready: The process is created and is ready to run
  • Running: The process is being executed
  • Blocked or wait: Process is waiting for input from the user
  • Terminated or Completed: Process completed execution, or was terminated by the Operating System
  • Zombie: Process terminated, but the information still exists in the process table.

17. Explain grep command.

Grep stands for Global Regular Expression Print. The grep command is used to search for a text in a file by pattern matching based on regular expression.

Syntaxgrep [options] pattern [files]

Example:

$ grep -c "linux" interview.txt

This command will print the count of the word “linux” in the “interview.txt” file.

18. Explain Process Management System Calls in Linux

The System Calls to manage the process are:

  • fork () : Used to create a new process
  • exec() : Execute a new program
  • wait() : Wait until the process finishes execution
  • exit() : Exit from the process

And the System Calls used to get Process ID are:

  • getpid():- get the unique process id of the process
  • getppid():- get the parent process unique id

19. Explain the ‘ls’ command in Linux

The ls command is used to list the files in a specified directory. The general syntax is:

$ ls <options> <directory>

For example, if you want to list all the files in the Example directory, then the command will be as follows:

$ ls Example/

There are different options that can be used with the ls command. These options give additional information about the file/ folder. For example:

-l lists long format (shows the permissions of the file)
-a lists all files including hidden files
-i lists files with their inode number
-s lists files with their size
-S lists files with their size and sorts the list by file size
-t sorts the listed files by time and date

20. Explain the redirection operator.

The redirection operator is used to redirect the output of a particular command as an input to another command or file.

There are two ways of using this:

‘>’ overwrites the existing content of the file or creates a new file.

‘>>’ appends the new content to the end of the file or creates a new file. 

Suppose the content of the file is as follows:

cat file - linux interview questions - edureka

Now when you use the ‘>’ redirection operator, the contents of the file are overwritten.

redirection operator overwritten - linux interview questions - edureka

and when you use ‘>>’, the contents are appended:

redirection operator append - linux interview questions - edureka

21. Why is the tar command used?

The tar command is used to extract or create an archived file.

Suppose you want to extract all the files from the archive named sample.tar.gz, then the command will be:

$ tar -xvzf sample.tar.gz

Suppose you want to create an archive of all the files stored in the path /home/linux/, then the command will be:

$ tar -cvzf filename.tar.gz

where c: create archive, x: extract, v: verbose, f: file

22. What is a Latch?

A Latch is a temporary storage device controlled by timing signal which can either store 0 or 1. A Latch has two stable states (high-output or 1, and low-output or 0) and is mainly used to store state information. A Latch can store one bit of data as long as it is powered on. 

23. What is a Microprocessor?

A Microprocessor is a device that executes instructions. It is a single-chip device that fetches the instruction from the memory, decodes it and executes it. A Microprocessor can carry out 3 basic functions:

  1. Mathematical operations like addition, subtraction, multiplication, and division
  2. Move data from one memory location to another
  3. Make decisions based on conditions and jump to new different instructions based on the decision.

 

24. Explain Regular Expressions and Grep

Regular Expressions are used to search for data having a particular pattern. Some of the commands used with Regular Patterns are: tr, sed, vi and grep.

Some of the common symbols used in Regular Expressions are:

.  Match any character
^  Match the beginning of the String
$  Match the end of the String
*  Match zero or more characters
   Represents special characters
?  Match exactly one character

Suppose the content of a file is as follows:

list contect - linux interview questions - edureka

If you want to list the entries that start with the character ‘a’, then the command would be:

$ cat linux.txt | grep ^a

grep a - - linux interview questions - edureka

If you want to list the entries that start has the character ‘n’, then the command would be:

$ cat linux.txt | grep n

grep n - linux interview questions - edureka

25. What is the minimum number of disk partitions required to install Linux?

The minimum number of partitions required is 2.

One partition is used as the local file system where all the files are stored. This includes files of the OS, files of applications and services, and files of the user. And the other partition is used as Swap Space which acts as an extended memory for RAM. 

Part B – Scenario Based Questions

Interviewers will ask scenario based questions along with theoretical questions to check how much hands-on knowledge you have. In this part of Linux Interview Questions, we will discuss such questions. 

1. How to copy a file in Linux?

You can use the cp command to copy a file in Linux. The general syntax is:

$ cp <source> <destination>

Suppose you want to copy a file named questions.txt from the directory /new/linux to /linux/interview, then the command will be:

$ cp questions.txt /new/linux /linux/interview

2. How to terminate a running process in Linux?

Every process has a unique process id. To terminate the process, we first need to find the process id. The ps command will list all the running processes along with the process id. And then we use the kill command to terminate the process.

The command for listing down all the processes:

$ ps

Suppose the process id of the process you want to terminate is 3849, then you will have to terminate it like this:

$ kill 3849

3. How to rename a file in Linux?

There is no specific command to rename a file in Linux. But you use the copy or move command to rename the file.

Using the Move command

$ mv <oldname> <newname>

Using the Copy command

$ cp <oldname> <newname>

And then delete the old file.

$ rm <oldname>

4. How to write the output of a command to a file?

You can use the redirection operator (>) to do this.

Syntax: $ (command) > (filename)

5. How to see the list of mounted devices on Linux?

By running the following command:

$ mount -l

6. How to find where a file is stored in Linux?

You can use the locate command to find the path to the file.

Suppose you want to find the locations of a file name sample.txt, then your command would be:

$ locate sample.txt

7. How to find the difference in two configuration files?

You can use the diff command for this: 

$ diff abc.conf xyz.conf
 

diff - linux interview questions - edureka

8. Write a bash script to delete all the files in the current directory that contains the word “linux”.

for i in *linux*; do rm $i; done

9. How would you create a text file without opening it?

The touch command can be used to create a text file without opening it. The touch command will create an empty file. The syntax is as follows:

$ touch <filename>

Suppose you want to create a file named sample.txt, then the command would be:

$ touch sample.txt

10. How would you delete a directory in Linux?

There are two commands that can be used to delete a directory in Linux.

  • rmdir

$ rmdir <directory name>

  • rm -rf 

$ rm -rf <directory name>

Note: The command rm -rf should be used carefully because it will delete all the data without any warnings. 

11. How would you schedule a task in Linux?

There are two commands to schedule tasks in Linux: cron and at.

The cron command is used to repeatedly schedule a task at a specific time. The tasks are stored in a cron file and then executed using the cron command. The cron command reads the string from this file and schedules the task. The syntax for the string to enter in the cron file is as follows:

<minute> <hour> <day> <month> <weekday> <command>

Suppose you want to run a command at 4 pm every Sunday, then the string would be:

0 16 * * 0 <command>

The at command is used to schedule a task only once at the specified time.

Suppose you want to shut down the system at 6 pm today, then the command for this would be:

$ echo "shutdown now" | at -m 18:00

12. Suppose you try to delete a file using the rm command and the deletion fails. What could be the possible reason?

  • The path specified to the file or the file name mentioned might be wrong
  • The user trying to delete the file might not have permissions to delete the file.

13. How do you look at the contents of a file named sample.z?

The .z extension means that the file has been compressed. To look at the contents of the compressed file, you can use the zcat command. Example:

$ zcat sample.z

14. How to copy files to a Floppy Disk safely?

Follow these steps to copy files to a Floppy Disk safely:

  1. Mount the floppy disk
  2. Copy the files
  3. Unmount the floppy disk

If you don’t unmount the floppy disk, then the data might become corrupted. 

15. How to identify which shell you are using?

Open the terminal and run:

$ echo $SHELL

This will print the name of the Shell being used. 

16. How can you login to another system in your network from your system?

SSH can be used for this. The Syntax is as follows:

ssh <username>@<ip address>

Suppose you want to login into a system with IP address 192.168.5.5 as a user “mike”, then the command would be:

$ ssh mike@192.168.5.5

17. How would you open a file in read-only mode using the vim editor?

$ vim -R <filename>

18. How would you search for a specific Employee ID in a file using the vim editor?

$ vim +/<employee id to be searched> <filename>

19. How to jump to a particular line in a file using vim editor?

$ vim +<line number> <filename>

20. How do you sort the entries in a text file in ascending order?

This can be done using the sort command.

$ sort sample.txt

21. What is the export command used for?

The export command is used to set and reload the environment variables. For example, if you want to set the Java path, then the command would be:

$ export JAVA_HOME = /home/user/Java/bin

22. How do you check if a particular service in running?

$ service <servicename> status

23. How do you check the status of all the services?

$ service --status-all

24. How do you start and stop a service?

To start:

$ service <servicename> start

To stop:

$ service <servicename> start

25. Explain the free command.

This command is used to display the free, used, swap memory available in the system. 

Typical free command output. The output is displayed in bytes.

$ free

free - linux interview questions - edureka

I hope these Linux Interview Questions will help you perform well in your interview. And I wish you all the best!

Original article source at https://www.edureka.co

#linux #interviewquestion 

Top 20+ MOST ASKED Linux Interview Questions & Answers
Zachary Palmer

Zachary Palmer

1654485990

Java Interview Question | HashMap in Java with Internal Working

Java Interview Question | HashMap in Java with Internal Working

In this article, we will see how hashmap’s get and put method works internally. What operations are performed. How the hashing is done. How the value is fetched by key. How the key-value pair is stored.

  1. int hash
  2. K key
  3. V value
  4. Node next

Now we will see how this works. First we will see the hashing process. 
 

Hashing

Hashing is a process of converting an object into integer form by using the method hashCode(). Its necessary to write hashCode() method properly for better performance of HashMap. Here I am taking key of my own class so that I can override hashCode() method to show different scenarios. My Key class is 

//custom Key class to override hashCode()
// and equals() method
class Key
{
  String key;
  Key(String key)
  {
    this.key = key;
  }
  
  @Override
  public int hashCode() 
  {
     return (int)key.charAt(0);
  }

  @Override
  public boolean equals(Object obj)
  {
    return key.equals((String)obj);
  }
}

Here overrided hashCode() method returns the first character’s ASCII value as hash code. So whenever the first character of key is same, the hash code will be same. You should not approach this criteria in your program. It is just for demo purpose. As HashMap also allows null key, so hash code of null will always be 0.
 

hashCode() method

hashCode() method is used to get the hash Code of an object. hashCode() method of object class returns the memory reference of object in integer form. Definition of hashCode() method is public native hashCode(). It indicates the implementation of hashCode() is native because there is not any direct method in java to fetch the reference of object. It is possible to provide your own implementation of hashCode(). 
In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index. 
 

equals() method

equals method is used to check that 2 objects are equal or not. This method is provided by Object class. You can override this in your class to provide your own implementation. 
HashMap uses equals() to compare the key whether they are equal or not. If equals() method return true, they are equal otherwise not equal. 
 

Buckets

A bucket is one element of HashMap array. It is used to store nodes. Two or more nodes can have the same bucket. In that case link list structure is used to connect the nodes. Buckets are different in capacity. A relation between bucket and capacity is as follows: 

capacity = number of buckets * load factor

A single bucket can have more than one nodes, it depends on hashCode() method. The better your hashCode() method is, the better your buckets will be utilized. 
 

Index Calculation in Hashmap

Hash code of key may be large enough to create an array. hash code generated may be in the range of integer and if we create arrays for such a range, then it will easily cause outOfMemoryException. So we generate index to minimize the size of array. Basically following operation is performed to calculate index. 

index = hashCode(key) & (n-1).

where n is number of buckets or the size of array. In our example, I will consider n as default size that is 16. 

Why the above method is used to calculate the index

Using a bitwise AND operator is similar to doing bit masking wherein only the lower bits of the hash integer are considered which in turn provides a very efficient method of calculating the modulus based on the length of the hashmap.
 

  • Initially Empty hashMap: Here, the hashmap is size is taken as 16.
HashMap map = new HashMap();
  • HashMap : 
empty_hasharray
  • Inserting Key-Value Pair: Putting one key-value pair in above HashMap 
map.put(new Key("vishal"), 20);
  • Steps: 
  1. Calculate hash code of Key {“vishal”}. It will be generated as 118.
  2. Calculate index by using index method it will be 6.
  3. Create a node object as :
{
  int hash = 118

  // {"vishal"} is not a string but 
  // an object of class Key
  Key key = {"vishal"}

  Integer value = 20
  Node next = null
}

Place this object at index 6, if no other object is presented there.

  • Inserting another Key-Value Pair: Now, putting other pair that is, 
map.put(new Key("sachin"), 30);
  • Steps:
  1. Calculate hashCode of Key {“sachin”}. It will be generated as 115.
  2. Calculate index by using index method it will be 3.
  3. Create a node object as :
{
  int hash = 115
  Key key = {"sachin"}
  Integer value = 30
  Node next = null
}
  • In Case of collision: Now, putting another pair that is, 
map.put(new Key("vaibhav"), 40);
  • Steps:
    1. Calculate hash code of Key {“vaibhav”}. It will be generated as 118.
    2. Calculate index by using index method it will be 6.
    3. Create a node object as :
 {
  int hash = 118
  Key key = {"vaibhav"}
  Integer value = 40
  Node next = null
}
  1. Place this object at index 6 if no other object is presented there.
  2. In this case a node object is found at the index 6 – this is a case of collision.
  3. In that case, check via hashCode() and equals() method that if both the keys are same.
  4. If keys are same, replace the value with current value.
  5. Otherwise connect this node object to the previous node object via linked list and both are stored at index 6. 
    Now HashMap becomes :

3_hasharray

 

Using get method()

Now lets try some get method to get a value. get(K key) method is used to get a value by its key. If you don’t know the key then it is not possible to fetch a value. 

  • Fetch the data for key sachin:
map.get(new Key("sachin"));
  • Steps:
    1. Calculate hash code of Key {“sachin”}. It will be generated as 115.
    2. Calculate index by using index method it will be 3.
    3. Go to index 3 of array and compare first element’s key with given key. If both are equals then return the value, otherwise check for next element if it exists.
    4. In our case it is found as first element and returned value is 30.
  • Fetch the data for key vaibahv:
map.get(new Key("vaibhav"));
  • Steps:
    1. Calculate hash code of Key {“vaibhav”}. It will be generated as 118.
    2. Calculate index by using index method it will be 6.
    3. Go to index 6 of array and compare first element’s key with given key. If both are equals then return the value, otherwise check for next element if it exists.
    4. In our case it is not found as first element and next of node object is not null.
    5. If next of node is null then return null.
    6. If next of node is not null traverse to the second element and repeat the process 3 until key is not found or next is not null.
    7. Time complexity is almost constant for put and get method until rehashing is not done.
    8. In case of collision, i.e. index of two or more nodes are same, nodes are joined by link list i.e. second node is referenced by first node and third by second and so on.
    9. If key given already exist in HashMap, the value is replaced with new value.
    10. hash code of null key is 0.
    11. When getting an object with its key, the linked list is traversed until the key matches or null is found on next field.

Working of HashMap in Java

What is Hashing

It is the process of converting an object into an integer value. The integer value helps in indexing and faster searches.

What is HashMap

HashMap is a part of the Java collection framework. It uses a technique called Hashing. It implements the map interface. It stores the data in the pair of Key and Value. HashMap contains an array of the nodes, and the node is represented as a class. It uses an array and LinkedList data structure internally for storing Key and Value. There are four fields in HashMap.

Working of HashMap in Java

Before understanding the internal working of HashMap, you must be aware of hashCode() and equals() method.

  • equals(): It checks the equality of two objects. It compares the Key, whether they are equal or not. It is a method of the Object class. It can be overridden. If you override the equals() method, then it is mandatory to override the hashCode() method.
  • hashCode(): This is the method of the object class. It returns the memory reference of the object in integer form. The value received from the method is used as the bucket number. The bucket number is the address of the element inside the map. Hash code of null Key is 0.
  • Buckets: Array of the node is called buckets. Each node has a data structure like a LinkedList. More than one node can share the same bucket. It may be different in capacity.

Working of HashMap in Java

Insert Key, Value pair in HashMap

We use put() method to insert the Key and Value pair in the HashMap. The default size of HashMap is 16 (0 to 15).

Example

In the following example, we want to insert three (Key, Value) pair in the HashMap.

HashMap<String, Integer> map = new HashMap<>();  
map.put("Aman", 19);  
map.put("Sunny", 29);  
map.put("Ritesh", 39);  

Let's see at which index the Key, value pair will be saved into HashMap. When we call the put() method, then it calculates the hash code of the Key "Aman." Suppose the hash code of "Aman" is 2657860. To store the Key in memory, we have to calculate the index.

Calculating Index

Index minimizes the size of the array. The Formula for calculating the index is:

Index = hashcode(Key) & (n-1)  

Where n is the size of the array. Hence the index value for "Aman" is:

Index = 2657860 & (16-1) = 4  

The value 4 is the computed index value where the Key and value will store in HashMap.

Working of HashMap in Java

Hash Collision

This is the case when the calculated index value is the same for two or more Keys. Let's calculate the hash code for another Key "Sunny." Suppose the hash code for "Sunny" is 63281940. To store the Key in the memory, we have to calculate index by using the index formula.

Index=63281940 & (16-1) = 4  

The value 4 is the computed index value where the Key will be stored in HashMap. In this case, equals() method check that both Keys are equal or not. If Keys are same, replace the value with the current value. Otherwise, connect this node object to the existing node object through the LinkedList. Hence both Keys will be stored at index 4.

Working of HashMap in Java

Similarly, we will store the Key "Ritesh." Suppose hash code for the Key is 2349873. The index value will be 1. Hence this Key will be stored at index 1.

Working of HashMap in Java

get() method in HashMap

get() method is used to get the value by its Key. It will not fetch the value if you don't know the Key. When get(K Key) method is called, it calculates the hash code of the Key.

Suppose we have to fetch the Key "Aman." The following method will be called.

map.get(new Key("Aman"));  

It generates the hash code as 2657860. Now calculate the index value of 2657860 by using index formula. The index value will be 4, as we have calculated above. get() method search for the index value 4. It compares the first element Key with the given Key. If both keys are equal, then it returns the value else check for the next element in the node if it exists. In our scenario, it is found as the first element of the node and return the value 19.

Let's fetch another Key "Sunny."

The hash code of the Key "Sunny" is 63281940. The calculated index value of 63281940 is 4, as we have calculated for put() method. Go to index 4 of the array and compare the first element's Key with the given Key. It also compares Keys. In our scenario, the given Key is the second element, and the next of the node is null. It compares the second element Key with the specified Key and returns the value 29. It returns null if the next of the node is null.

#java #interviewquestion #programming 

 

Java Interview Question | HashMap in Java with Internal Working

123 Essential JavaScript Interview Question

123-JavaScript-Interview-Questions

This book's goal is to help javascript frontend developers prepare for technical job interviews through a collection of carefully compiled questions.

Want to buy a book in paper form? Want some badass flashcards?

  • This Book will be soon completed and then it will be available to buy in paper form. If you want me to send you an early copy of this book, please add your name and email address in this Google Form.
  • If you don't want to wait, you can buy Yuri's JavaScript Flashcards, a set of frontend interview questions sorted by popularity among interviewers printed on beautiful poker-size flashcards.

Question 1. What's the difference between undefined and not defined in JavaScript

Answer

In JavaScript if you try to use a variable that doesn't exist and has not been declared, then JavaScript will throw an error var name is not defined and the script will stop executing thereafter. But If you use typeof undeclared_variable then it will return undefined.

Before starting further discussion let's understand the difference between declaration and definition.

var x is a declaration because we are not defining what value it holds yet, but we are declaring its existence and the need for memory allocation.

var x; // declaring x
console.log(x); // output: undefined

var x = 1 is both declaration and definition, here declaration and assignment of value happen inline for variable x—what we are doing is called "initialisation". In JavaScript both variable declarations and function declarations go to the top of the scope in which they are declared, then assignment happens—this series of events is called "hoisting".

A variable can be declared but not defined. When we try to access it, It will result undefined.

var x; // Declaration
typeof x === 'undefined'; // Will return true

A variable can be neither declared nor defined. When we try to reference such variable then the result will be not defined.

console.log(y);  // Output: ReferenceError: y is not defined

Ref Link:

http://stackoverflow.com/questions/20822022/javascript-variable-definition-declaration

Question 2. For which value of x the results of the following statements are not the same?

if( x <= 100 ) {...}
if( !(x > 100) ) {...}

Answer

NaN <= 100 is false and NaN > 100 is also false, so if the value of x is NaN, the statements are not the same.

The same holds true for any value of x that being converted to type Number, returns NaN, e.g.: undefined, [1,2,5], {a:22} , etc.

This is why you need to pay attention when you deal with numeric variables. NaN can’t be equal, less than or more than any other numeric value, so the only reliable way to check if the value is NaN, is to use the isNaN() function.

Question 3. What is the drawback of declaring methods directly in JavaScript objects?

Answer

One of the drawbacks of declaring methods directly in JavaScript objects is that they are very memory inefficient. When you do that, a new copy of the method is created for each instance of an object. Here's an example:

var Employee = function (name, company, salary) {
  this.name = name || "";       
  this.company = company || "";
  this.salary = salary || 5000;

  // We can create a method like this:
  this.formatSalary = function () {
      return "$ " + this.salary;
  };
};

// Alternatively we can add the method to Employee's prototype:
Employee.prototype.formatSalary2 = function() {
    return "$ " + this.salary;
}

//creating objects
var emp1 = new Employee('Yuri Garagin', 'Company 1', 1000000);
var emp2 = new Employee('Dinesh Gupta', 'Company 2', 1039999);
var emp3 = new Employee('Erich Fromm', 'Company 3', 1299483);

In this case each instance variable emp1, emp2, emp3 has its own copy of theformatSalary method. However the formatSalary2 will only be added once to Employee.prototype.

Question 4. What is “closure” in javascript? Can you provide an example?

Answer

A closure is a function defined inside another function (called parent function) and as such it has access to the variables declared and defined within its parent function's scope.

The closure has access to the variables in three scopes:

  • Variable declared in its own scope
  • Variable declared in its parent function's scope
  • Variable declared in the global namespace
var globalVar = "abc"; //Global variable

// Parent self-invoking function
(function outerFunction (outerArg) { // start of outerFunction's scope

  var outerFuncVar = 'x'; // Variable declared in outerFunction's function scope   
  
  // Closure self-invoking function
  (function innerFunction (innerArg) { // start of innerFunction's scope

    var innerFuncVar = "y"; // variable declared in innerFunction's function scope
    console.log(         
      "outerArg = " + outerArg + "\n" +
      "outerFuncVar = " + outerFuncVar + "\n" +
      "innerArg = " + innerArg + "\n" +
      "innerFuncVar = " + innerFuncVar + "\n" +
      "globalVar = " + globalVar);
      
  // end of innerFunction's scope
  
  })(5); // Pass 5 as parameter to our Closure

// end of outerFunction's scope

})(7); // Pass 7 as parameter to the Parent function

innerFunction is a closure which is defined inside outerFunction and consequently has access to all the variables which have been declared and defined within outerFunction's scope as well as any variables residing in the program's global scope.

The output of the code above would be:

outerArg = 7
outerFuncVar = x
innerArg = 5
innerFuncVar = y
globalVar = abc

Question 5. Write a mul function which will work properly when invoked with following syntax.

console.log(mul(2)(3)(4)); // output : 24
console.log(mul(4)(3)(4)); // output : 48

Answer

function mul (x) {
  return function (y) { // anonymous function
    return function (z) { // anonymous function
      return x * y * z;
    };
  };
}

Here the mul function accepts the first argument and returns an anonymous function which then takes the second parameter and returns one last anonymous function which finally takes the third and final parameter; the last function then multiplies x, y and z, and returns the result of the operation.

In Javascript, a function defined inside another function has access to the outer function's scope and can consequently return, interact with or pass on to other functions, the variables belonging to the scopes that incapsulate it.

  • A function is an instance of the Object type
  • A function can have properties and has a link to its constructor method
  • A function can be stored as a variable
  • A function can be passed as a parameter to another function
  • A function can be returned by another function

Question 6. How to empty an array in JavaScript?

For instance:

var arrayList =  ['a', 'b', 'c', 'd', 'e', 'f'];

How can we empty the array above?

Answer

There are a couple of ways by which we can empty an array, So let's discuss all the possible way by which we can empty an array.

Method 1

arrayList = [];

The code above will set the variable arrayList to a new empty array. This is recommended if you don't have references to the original array arrayList anywhere else because It will actually create a new empty array. You should be careful with this way of empty the array, because if you have referenced this array from another variable, then the original reference array will remain unchanged, Only use this way if you have only referenced the array by its original variable arrayList.

For instance:

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList;  // Referenced arrayList by another variable
arrayList = []; // Empty the array
console.log(anotherArrayList); // Output ['a', 'b', 'c', 'd', 'e', 'f']

Method 2

arrayList.length = 0;

The code above will clear the existing array by setting its length to 0. This way of emptying an array will also update all the reference variables that point to the original array.

For instance:

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList;  // Referenced arrayList by another variable
arrayList.length = 0; // Empty the array by setting length to 0
console.log(anotherArrayList); // Output []

Method 3

arrayList.splice(0, arrayList.length);

Above implementation will also work perfectly. This way of empty the array will also update all the references of the original array.

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created array
var anotherArrayList = arrayList;  // Referenced arrayList by another variable
arrayList.splice(0, arrayList.length); // Empty the array by setting length to 0
console.log(anotherArrayList); // Output []

Method 4

while(arrayList.length) {
  arrayList.pop();
}

Above implementation can also empty the array. But not recommended to use often.

Question 7. How to check if an object is an array or not?

Answer

The best way to find whether an object is instance of a particular class or not using toString method from Object.prototype

var arrayList = [1 , 2, 3];

One of the best use cases of type checking of an object is when we do method overloading in JavaScript. To understand this, let's say we have a method called greet which can take a single string and also a list of strings. To make our greet method workable in both situation we need to know what kind of parameter is being passed: is it single value or list of values?

function greet(param) {
  if() {
    // here have to check whether param is array or not
  }
  else {
  }
}

However, in the above implementation it might not necessary to check the type of the array, we can check for single value string and put array logic code in else block, let see below code for the same.

 function greet(param) {
   if(typeof param === 'string') {
   }
   else {
     // If param is of type array then this block of code would execute
   }
 }

Now it's fine we can go with the previous two implementations, but when we have a situation like a parameter can be single value, array, and object type then we will be in trouble.

Coming back to checking the type of an object, As we mentioned that we can use Object.prototype.toString

if(Object.prototype.toString.call(arrayList) === '[object Array]') {
  console.log('Array!');
}

If you are using jQuery then you can also used jQuery isArray method:

if($.isArray(arrayList)) {
  console.log('Array');
} else {
  console.log('Not an array');
}

FYI jQuery uses Object.prototype.toString.call internally to check whether an object is an array or not.

In modern browser, you can also use:

Array.isArray(arrayList);

Array.isArray is supported by Chrome 5, Firefox 4.0, IE 9, Opera 10.5 and Safari 5

Question 8. What will be the output of the following code?

var output = (function(x) {
  delete x;
  return x;
})(0);

console.log(output);

Answer

The code above will output 0 as output. delete operator is used to delete a property from an object. Here x is not an object, it's a local variable. delete operator doesn't affect local variables.

Question 9. What will be the output of the following code?

var x = 1;
var output = (function() {
  delete x;
  return x;
})();

console.log(output);

Answer

The code above will output 1 as output. delete operator is used to delete a property from an object. Here x is not an object it's global variable of type number.

Question 10. What will be the output of the following code?

var x = { foo : 1};
var output = (function() {
  delete x.foo;
  return x.foo;
})();

console.log(output);

Answer

The code above will output undefined as output. delete operator is used to delete a property from an object. Here x is an object which has foo as a property and from a self-invoking function, we are deleting the foo property of object x and after deletion, we are trying to reference deleted property foo which result undefined.

Question 11. What will be the output of the following code?

var Employee = {
  company: 'xyz'
}
var emp1 = Object.create(Employee);
delete emp1.company
console.log(emp1.company);

AnswerThe code above will output `xyz` as output. Here `emp1` object got company as **prototype** property. delete operator doesn't delete prototype property.

emp1 object doesn't have company as its own property. you can test it console.log(emp1.hasOwnProperty('company')); //output : false However, we can delete company property directly from Employee object using delete Employee.company or we can also delete from emp1 object using __proto__ property delete emp1.__proto__.company.

Question 12. What is undefined x 1 in JavaScript

var trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];

Answer- When you run the code above and do `console.log(trees);` in chrome developer console then you will get `["redwood", "bay", "cedar", undefined × 1, "maple"]`. - In the recent versions of Chrome you will see the word `empty` of `undefined x 1`. - When you run the same code in Firefox browser console then you will get `["redwood", "bay", "cedar", undefined, "maple"]`

Clearly we can see that Chrome has its own way of displaying uninitialized index in arrays. However when you check trees[3] === undefined in any browser you will get similar output as true.

Note: Please remember that you need not check for the uninitialized index of the array in trees[3] === 'undefined × 1' it will give an error because 'undefined × 1' this is just way of displaying an uninitialized index of an array in chrome.

Question 13. What will be the output of the following code?

var trees = ["xyz", "xxxx", "test", "ryan", "apple"];
delete trees[3];
console.log(trees.length);

AnswerThe code above will output `5` as output. When we used `delete` operator for deleting an array element then, the array length is not affected by this. This holds even if you deleted all elements of an array using `delete` operator.

So when delete operator removes an array element that deleted element is no longer present in the array. In place of value at deleted index undefined x 1 in chrome and undefined is placed at the index. If you do console.log(trees) output ["xyz", "xxxx", "test", undefined × 1, "apple"] in Chrome and in Firefox ["xyz", "xxxx", "test", undefined, "apple"].

Question 14. What will be the output of the following code?

var bar = true;
console.log(bar + 0);   
console.log(bar + "xyz");  
console.log(bar + true);  
console.log(bar + false);

Answer

The code above will output 1, "truexyz", 2, 1 as output. Here's a general guideline for the plus operator:

  • Number + Number -> Addition
  • Boolean + Number -> Addition
  • Boolean + Boolean -> Addition
  • Number + String -> Concatenation
  • String + Boolean -> Concatenation
  • String + String -> Concatenation

Question 15. What will be the output of the following code?

var z = 1, y = z = typeof y;
console.log(y);

Answer

The code above will print string "undefined" as output. According to associativity rule operator with the same precedence are processed based on their associativity property of operator. Here associativity of the assignment operator is Right to Left so first typeof y will evaluate first which is string "undefined" and assigned to z and then y would be assigned the value of z. The overall sequence will look like that:

var z;
z = 1;
var y;
z = typeof y;
y = z;

Question 16. What will be the output of the following code?

// NFE (Named Function Expression)
var foo = function bar() { return 12; };
typeof bar();

Answer

The output will be Reference Error. To fix the bug we can try to rewrite the code a little bit:

Sample 1

var bar = function() { return 12; };
typeof bar();

or

Sample 2

function bar() { return 12; };
typeof bar();

The function definition can have only one reference variable as a function name, In sample 1 bar is reference variable which is pointing to anonymous function and in sample 2 we have function statement and bar is the function name.

var foo = function bar() {
  // foo is visible here
  // bar is visible here
  console.log(typeof bar()); // Works here :)
};
// foo is visible here
// bar is undefined here

Question 17a. What is the difference between declaring a function in the formats listed below?

var foo = function() {
  // Some code
}
function bar () {
  // Some code
}

Answer

The main difference is that function foo is defined at run-time and is called a function expression, whereas function bar is defined at parse time and is called a function statement. To understand it better, let's take a look at the code below :

// Run-Time function declaration
  foo(); // Call foo function here, It will give an error
  var foo = function() {
    console.log("Hi I am inside Foo");
  };
// Parse-Time function declaration
bar(); // Call bar function here, It will not give an Error
function bar() {
  console.log("Hi I am inside Foo");
}

Question 17b. What is the output of the following?

bar();
(function abc(){console.log('something')})();
function bar(){console.log('bar got called')};

Answer

The output will be :

bar got called
something

Since the function is called first and defined during parse time the JS engine will try to find any possible parse time definitions and start the execution loop which will mean function is called first even if the definition is post another function.

Question 18. In which case the function definition is not hoisted in JavaScript?

Answer

Let's take the following function expression

 var foo = function foo() {
     return 12;
 }

In JavaScript var-declared variables and functions are hoisted. Let's take function hoisting first. Basically, the JavaScript interpreter looks ahead to find all the variable declaration and hoists them to the top of the function where it's declared. For example:

foo(); // Here foo is still undefined
var foo = function foo() {
  return 12;
};

The code above behind the scene look something like this:

var foo = undefined;
foo(); // Here foo is undefined
foo = function foo() {
  // Some code stuff
}
var foo = undefined;
foo = function foo() {
  // Some code stuff
}
foo(); // Now foo is defined here

Question 19. What will be the output of the following code?

var salary = "1000$";

(function () {
  console.log("Original salary was " + salary);

  var salary = "5000$";

  console.log("My New Salary " + salary);
})();

Answer

The code above will output: undefined, 5000$ because of hoisting. In the code presented above, you might be expecting salary to retain it values from outer scope until the point that salary was re-declared in the inner scope. But due to hoisting salary value was undefined instead. To understand it better have a look of the following code, here salary variable is hoisted and declared at the top in function scope. When we print its value using console.log the result is undefined. Afterwards the variable is redeclared and the new value "5000$" is assigned to it.

var salary = "1000$";

(function () {
  var salary = undefined;
  console.log("Original salary was " + salary);

  salary = "5000$";

  console.log("My New Salary " + salary);
})();

Question 20. What’s the difference between typeof and instanceof?

Answer

typeof is an operator that returns a string with the type of whatever you pass.

The typeof operator checks if a value belongs to one of the seven basic types: number, string, boolean, object, function, undefined or Symbol.

typeof(null) will return object.

instanceof is much more intelligent: it works on the level of prototypes. In particular, it tests to see if the right operand appears anywhere in the prototype chain of the left. instanceof doesn’t work with primitive types. The instanceof operator checks the current object and returns true if the object is of the specified type, for example:

var dog = new Animal();
dog instanceof Animal; // Output : true

Here dog instanceof Animal is true since dog inherits from Animal.prototype

var name = new String("xyz");
name instanceof String; // Output : true

Ref Link: http://stackoverflow.com/questions/2449254/what-is-the-instanceof-operator-in-javascript

Question 21. Calculate the length of the associative array

var counterArray = {
  A : 3,
  B : 4
};
counterArray["C"] = 1;

Answer

First of all, in the case of JavaScript an associative array is the same as an object. Secondly, even though there is no built-in function or property available to calculate the length/size an object, we can write such function ourselves.

Method 1

Object has keys method which can be used to calculate the length of object.

Object.keys(counterArray).length; // Output 3

Method 2

We can also calculate the length of object by iterating through the object and by doing a count of own property of object. This way we will ignoge the properties that came from the object's prototype chain:

function getLength(object) {
  var count = 0;
  for(key in object) {
    // hasOwnProperty method check own property of object
    if(object.hasOwnProperty(key)) count++;
  }
  return count;
}

Method 3

All modern browsers (including IE9+) support the getOwnPropertyNames method, so we can calculate the length using the following code:

Object.getOwnPropertyNames(counterArray).length; // Output 3

Method 4

Underscore and lodash libraries have the method size dedicated to calculate the object length. We don't recommend to include one of these libraries just to use the size method, but if it's already used in your project - why not?

_.size({one: 1, two: 2, three: 3});
=> 3

Question 22. Difference between Function, Method and Constructor calls in JavaScript.

Answer

If your are familiar with Object-oriented programming, More likely familiar to thinking of functions, methods, and class constructors as three separate things. But In JavaScript, these are just three different usage patterns of one single construct.

functions : The simplest usages of function call:

function helloWorld(name) {
  return "hello world, " + name;
}

helloWorld("JS Geeks"); // "hello world JS Geeks"

Methods in JavaScript are nothing more than object properties that are functions.

var obj = {
  helloWorld : function() {
    return "hello world, " + this.name;
  },
  name: 'John Carter'
}
obj.helloWorld(); // // "hello world John Carter"

Notice how helloWorld refer to this properties of obj. Here it's clear or you might have already understood that this gets bound to obj. But the interesting point that we can copy a reference to the same function helloWorld in another object and get a difference answer. Let see:

var obj2 = {
  helloWorld : obj.helloWorld,
  name: 'John Doe'
}
obj2.helloWorld(); // "hello world John Doe"

You might be wonder what exactly happens in a method call here. Here we call the expression itself determine the binding of this this, The expression obj2.helloWorld() looks up the helloWorld property of obj and calls it with receiver object obj2.

The third use of functions is as constructors. Like function and method, constructors are defined with function.

function Employee(name, age) {
  this.name = name;
  this.age = age;
}

var emp1 = new Employee('John Doe', 28);
emp1.name; // "John Doe"
emp1.age; // 28

Unlike function calls and method calls, a constructor call new Employee('John Doe', 28) creates a brand new object and passes it as the value of this, and implicitly returns the new object as its result.

The primary role of the constructor function is to initialize the object.

Question 23. What would be the output of the following code?

function User(name) {
  this.name = name || "JsGeeks";
}

var person = new User("xyz")["location"] = "USA";
console.log(person);

Answer

The output of above code would be "USA". Here new User("xyz") creates a brand new object and created property location on that and USA has been assigned to object property location and that has been referenced by the person.

Let say new User("xyz") created a object called foo. The value "USA" will be assigned to foo["location"], but according to ECMAScript Specification , pt 12.14.4 the assignment will itself return the rightmost value: in our case it's "USA". Then it will be assigned to person.

To better understand what's going on here, try to execute this code in console, line by line:

function User(name) {
 this.name = name || "JsGeeks";
}

var person;
var foo = new User("xyz");
foo["location"] = "USA";
// the console will show you that the result of this is "USA"

Question 24. What are Service Workers and when can you use them?

Answer

It’s a technology that allows your web application to use cached resources first, and provide default experience offline, before getting more data from the network later. This principle is commonly known as Offline First.

Service Workers actively use promises. A Service Worker has to be installed,activated and then it can react on fetch, push and sync events.

As of 2017, Service Workers are not supported in IE and Safari.

Question 25. What is the difference between a method and a function in javascript?

Answer

In JS, that difference is quite subtle. A function is a piece of code that is called by name and function itself not associated with any object and not defined inside any object. It can be passed data to operate on (i.e. parameter) and can optionally return data (the return value).

// Function statement
function myFunc() {
  // Do some stuff;
}

// Calling the function
myFunc();

Here myFunc() function call is not associated with object hence not invoked through any object.

A function can take a form of immediately invoked function expression (IIFE):


// Anonymous Self-invoking Function
(function() {
  // Do some stuff;
})();

Finally there are also arrow functions:

const myFunc = arg => {
    console.log("hello", arg)
} 

A method is a piece of code that is called by its name and that is associated with the object. Methods are functions. When you call a method like this obj1.myMethod(), the reference to obj1 gets assigned (bound) to this variable. In other words, the value of this will be obj1 inside myMethod.

Here are some examples of methods:

Example 1

var obj1 = {
  attribute: "xyz",
  myMethod: function () {  // Method
    console.log(this.attribute);
  }
};

// Call the method
obj1.myMethod();

Here obj1 is an object and myMethod is a method which is associated with obj1.

Example 2

In ES6 we have classes. There the methods will look like this:

class MyAwesomeClass {
  myMethod() {
    console.log("hi there");
  }
}

const obj1 = new MyAwesomeClass();
obj1.myMethod();

Understand: the method is not some kind of special type of a function, and it's not about how you declare a function. It's the way we call a function. Look at that:

var obj1 = {
  prop1: "buddy"
}; 
var myFunc = function () {
  console.log("Hi there", this);
};
// let's call myFunc as a function: 
myFunc(); // will output "Hi there undefined" or "Hi there Window"
 
obj1.myMethod = myFunc;
//now we're calling myFunc as a method of obj1, so this will point to obj1
obj1.myMethod(); // will print "Hi there" following with obj1. 

Question 26. What is IIFE (Immediately Invoked Function Expression) and how it can be useful?

Answer

Definition

IIFE a function that runs as soon as it's defined. Usually it's anonymous (doesn't have a function name), but it also can be named. Here's an example of IIFE:

(function() {
  console.log("Hi, I'm IIFE!");
})();
// outputs "Hi, I'm IIFE!"

Explanation

So, here's how it works. Remember the difference between function statements (function a () {}) and function expressions (var a = function() {})? So, IIFE is a function expression. To make it an expression we surround our function declaration into the parens. We do it to explicitly tell the parser that it's an expression, not a statement (JS doesn't allow statements in parens).

After the function you can see the two () braces, this is how we run the function we just declared.

That's it. The rest is details.

The function inside IIFE doesn't have to be anonymous. This one will work perfectly fine and will help to detect your function in a stacktrace during debugging:

(function myIIFEFunc() {
  console.log("Hi, I'm IIFE!");
})();
// outputs "Hi, I'm IIFE!"

It can take some parameters:

Here there value "Yuri" is passed to the param1 of the function.

(function myIIFEFunc(param1) {
  console.log("Hi, I'm IIFE, " + param1);
})("Yuri");
// outputs "Hi, I'm IIFE, Yuri!"

It can return a value:

var result = (function myIIFEFunc(param1) {
  console.log("Hi, I'm IIFE, " + param1);
  return 1;
})("Yuri");
// outputs "Hi, I'm IIFE, Yuri!"
// result variable will contain 1

You don't have to surround the function declaration into parens, although it's the most common way to define IIFE. Instead you can use any of the following forms:

  • ~function(){console.log("hi I'm IIFE")}()
  • !function(){console.log("hi I'm IIFE")}()
  • +function(){console.log("hi I'm IIFE")}()
  • -function(){console.log("hi I'm IIFE")}()
  • (function(){console.log("hi I'm IIFE")}());
  • var i = function(){console.log("hi I'm IIFE")}();
  • true && function(){ console.log("hi I'm IIFE") }();
  • 0, function(){ console.log("hi I'm IIFE") }();
  • new function(){ console.log("hi I'm IIFE") }
  • new function(){ console.log("hi I'm IIFE") }()

Applications and usefulness

Variables and functions that you declare inside an IIFE are not visible to the outside world, so you can:

  • Use the IIFE for isolating parts of the code to hide details of implementation.
  • Specify the input interface of your code by passing commonly used global objects (window, document, jQuery, etc.) IIFE’s parameters, and then reference these global objects within the IIFE via a local scope.
  • Use it in closures, when you use closures in loops.
  • IIFE is the basis of in the module pattern in ES5 code, it helps to prevent polluting the global scope and provide the module interface to the outside.

Question 27. Describe Singleton Pattern In JavaScript

Answer

The singleton pattern is an often used JavaScript design pattern. It provides a way to wrap the code into a logical unit that can be accessed through a single variable. The Singleton design pattern is used when only one instance of an object is needed throughout the lifetime of an application. In JavaScript, Singleton pattern have many uses, they can be used for NameSpacing, which reduce the number of global variables in your page (prevent from polluting global space), organizing the code in a consistent manner, which increase the readability and maintainability of your pages.

There are two important points in the traditional definition of Singleton pattern:

  • There should be only one instance allowed for a class and
  • We should allow global point of access to that single instance

Let me define singleton pattern in JavaScript context:

It is an object that is used to create namespace and group together a related set of methods and attributes (encapsulation) and if we allow to initiate then it can be initiated only once.

In JavaScript, we can create singleton though object literal. However, there is some another way but that I will cover in next post.

A singleton object consists of two parts: The object itself, containing the members (Both methods and attributes) within it, and global variable used to access it. The variable is global so that object can be accessed anywhere in the page, this is a key feature of the singleton pattern.

JavaScript: A Singleton as a Namespace

As I have already stated above that singleton can be used to declare Namespace in JavaScript. NameSpacing is a large part of responsible programming in JavaScript. Because everything can be overwritten, and it is very easy to wipe out variable by mistake or a function, or even a class without even knowing it. A common example which happens frequently when you are working with another team member parallel,

function findUserName(id) {

}

/* Later in the page another programmer
added code */
var findUserName = $('#user_list');

/* You are trying to call :( */
console.log(findUserName())

One of the best ways to prevent accidentally overwriting variable is to namespace your code within a singleton object.

/*  Using Namespace */

var MyNameSpace = {
  findUserName : function(id) {},
  // Other methods and attribute go here as well
}

/* Later in the page another programmer
added code */
var findUserName = $('#user_list');

/* You are trying to call and you make this time workable */
console.log(MyNameSpace.findUserName());

Singleton Design Pattern Implementation

/* Lazy Instantiation skeleton for a singleton pattern */

var MyNameSpace = {};
MyNameSpace.Singleton = (function() {

  // Private attribute that holds the single instance
  var singletonInstance;  

  // All of the normal code goes here
  function constructor() {
    // Private members
    var privateVar1 = "Nishant";
    var privateVar2 = [1,2,3,4,5];

    function privateMethod1() {
      // code stuff
    }

    function privateMethod1() {
      // code stuff
    }

    return {
      attribute1 : "Nishant",
      publicMethod: function() {
        alert("Nishant");// some code logic
      }
    }
  }

  return {
    // public method (Global access point to Singleton object)
    getInstance: function() {
      //instance already exist then return  
      if(!singletonInstance) {
        singletonInstance = constructor();
      }
      return singletonInstance;           
    }           
  }

})();   

// getting access of publicMethod
console.log(MyNamespace.Singleton.getInstance().publicMethod());

The singleton implemented above is easy to understand. The singleton class maintains a static reference to the lone singleton instance and return that reference from the static getInstance() method.

Question 28. What are the ways of creating objects in JavaScript ?

Answer

Method 1: Function based

This method is useful if we want to create several similar objects. In the code sample below, we wrote the function Employee and used it as a constructor by calling it with the new operator.


  function Employee(fName, lName, age, salary){
      this.firstName = fName;
      this.lastName = lName;
      this.age = age;
      this.salary = salary;
  }

  // Creating multiple object which have similar property but diff value assigned to object property.
  var employee1 = new Employee('John', 'Moto', 24, '5000$');
  var employee2 = new Employee('Ryan', 'Jor', 26, '3000$');
  var employee3 = new Employee('Andre', 'Salt', 26, '4000$');

Method 2: Object Literal

Object Literal is best way to create an object and this is used frequently. Below is code sample for create employee object which contains property as well as method.

var employee = {
    name : 'Nishant',
    salary : 245678,
    getName : function(){
        return this.name;
    }
}

The code sample below is Nested Object Literal, Here address is an object inside employee object.

var employee = {
    name : 'Nishant',
    salary : 245678,
    address : {
        addressLine1 : 'BITS Pilani',
        addressLine2 : 'Vidya Vihar'.
        phoneNumber: {
          workPhone: 7098889765,
          homePhone: 1234567898
        }
    }
}

Method 3: From Object using new keyword

In the code below, a sample object has been created using Object's constructor function.

var employee = new Object(); // Created employee object using new keywords and Object()
employee.name = 'Nishant';
employee.getName = function(){
    return this.name;
}

Method 4:** Using Object.create

Object.create(obj) will create a new object and set the obj as its prototype. It’s a modern way to create objects that inherit properties from other objects. Object.create function doesn’t run the constructor. You can use Object.create(null) when you don’t want your object to inherit the properties of Object.

Question 29. Write a function called deepClone which takes an object and creates a object copy of it.

var newObject = deepClone(obj);

Answer

function deepClone(object){
    var newObject = {};
    for(var key in object){
        if(typeof object[key] === 'object'  && object[key] !== null ){
         newObject[key] = deepClone(object[key]);
        }else{
         newObject[key] = object[key];
        }
    }
    return newObject;
}

Explanation: We have been asked to do deep copy of object so What's basically it's mean ??. Let's understand in this way you have been given an object personalDetail this object contains some property which again a type of object here as you can see address is an object and phoneNumber in side an address is also an object. In simple term personalDetail is nested object(object inside object). So Here deep copy means we have to copy all the property of personalDetail object including nested object.

var personalDetail = {
    name : 'Nishant',
    address : {
      location: 'xyz',
      zip : '123456',
      phoneNumber : {
        homePhone: 8797912345,
        workPhone : 1234509876
      }
    }
}

So when we do deep clone then we should copy every property (including the nested object).

Question 30. Best way to detect undefined object property in JavaScript.

Answer

Suppose we have given an object person

var person = {
    name: 'Nishant',
    age : 24
}

Here the person object has a name and age property. Now we are trying to access the salary property which we haven't declared on the person object so while accessing it will return undefined. So how we will ensure whether property is undefined or not before performing some operation over it?

Explanation:

We can use typeof operator to check undefined

if(typeof someProperty === 'undefined'){
    console.log('something is undefined here');
}

Now we are trying to access salary property of person object.

if(typeof person.salary === 'undefined'){
    console.log("salary is undefined here because we haven't declared");
}

Question 31. Write a function called Clone which takes an object and creates a object copy of it but not copy deep property of object.

   var objectLit = {foo : 'Bar'}; 
    var cloneObj = Clone(obj); // Clone is the function which you have to write 
    console.log(cloneObj === Clone(objectLit)); // this should return false
    console.log(cloneObj == Clone(objectLit)); // this should return true

Answer

function Clone(object){
  var newObject = {};
  for(var key in object){
      newObject[key] = object[key];
  }
  return newObject;
}

Question 32. What are promises and how they are useful?

Answer

We use promises for handling asynchronous interactions in a sequential manner. They are especially useful when we need to do an async operation and THEN do another async operation based on the results of the first one. For example, if you want to request the list of all flights and then for each flight you want to request some details about it. The promise represents the future value. It has an internal state (pending, fulfilled and rejected) and works like a state machine.

A promise object has then method, where you can specify what to do when the promise is fulfilled or rejected.

You can chain then() blocks, thus avoiding the callback hell. You can handle errors in the catch() block. After a promise is set to fulfilled or rejected state, it becomes immutable.

Also mention that you know about more sophisticated concepts:

  • async/await which makes the code appear even more linear
  • RxJS observables can be viewed as the recyclable promises

Be sure that you can implement the promise, read one of the articles on a topic, and learn the source code of the simplest promise implementation.

Question 33. How to check whether a key exist in a JavaScript object or not.

Answer

Let say we have person object with property name and age

var person = {
    name: 'Nishant',
    age: 24
}

Now we want to check whether name property exist in person object or not ?

In JavaScript object can have own property, in above example name and age is own property of person object. Object also have some of inherited property of base object like toString is inherited property of person object.

So how we will check whether property is own property or inherited property.

Method 1: We can use in operator on objet to check own property or inherited property.

console.log('name' in person); // checking own property print true 
console.log('salary' in person); // checking undefined property print false

in operator also look into inherited property if it doesn't find property defined as own property. For instance If I check existence of toString property as we know that we haven't declared this property on person object so in operator look into there base property.

Here

console.log('toString' in person); // Will print true

If we want to test property of object instance not inherited properties then we will use hasOwnProperty method of object instance.

console.log(person.hasOwnProperty('toString')); // print false
console.log(person.hasOwnProperty('name')); // print true
console.log(person.hasOwnProperty('salary')); // print false

Question 34. What is NaN, why do we need it, and when can it break the page?

Answer

NaN stands for “not a number.” and it can break your table of numbers when it has an arithmetic operation that is not allowed. Here are some examples of how you can get NaN:

Math.sqrt(-5);
Math.log(-1);
parseFloat("foo"); /* this is common: you get JSON from the server, convert some strings from JSON to a number and end up with NaN in your UI. */

NaN is not equal to any number, it’s not less or more than any number, also it's not equal to itself:

NaN !== NaN
NaN < 2 // false
NaN > 2 // false
NaN === 2 // false

To check if the current value of the variable is NaN, you have to use the isNaN function. This is why we can often see NaN in the webpages: it requires special check which a lot of developers forget to do.

Further reading: great blogpost on ariya.io

Question 35. Fix the bug using ES5 only

var arr = [10, 32, 65, 2];
for (var i = 0; i < arr.length; i++) {
  setTimeout(function() {
    console.log('The index of this number is: ' + i);
  }, 3000);
}

Answer

For ES6, you can just replace var i with let i.

For ES5, you need to create a function scope like here:

var arr = [10, 32, 65, 2];
for (var i = 0; i < arr.length; i++) {
  setTimeout(function(j) {
    return function () {
      console.log('The index of this number is: ' + j)
    };
  }(i), 3000);
}

This can also achieve by forEach (allows you to keep that variable within the forEach’s scope)

var arr = [10, 32, 65, 2];
arr.forEach(function(ele, i) {
  setTimeout(function() {
    console.log('The index of this number is: ' + i);
  }, 3000);
})

Question 36. How to check if the value of a variable in an array?

Answer

We always encounter in such situation where we need to know whether value is type of array or not.

For instance : the code below perform some operation based value type

function(value){
    if("value is an array"){
        // Then perform some operation
    }else{
        // otherwise
    }
}

Let's discuss some way to detect an array in JavaScript.

Method 1:

Juriy Zaytsev (Also known as kangax) proposed an elegant solution to this.

    function isArray(value){
        return Object.prototype.toString.call(value) === '[object Array]';
    }

This approach is most popular way to detecting a value of type array in JavaScript and recommended to use. This approach relies on the fact that, native toString() method on a given value produce a standard string in all browser.

Method 2:

Duck typing test for array type detection

 // Duck typing arrays
 function isArray(value){
     return typeof value.sort === 'function';
 }

As we can see above isArray method will return true if value object have sort method of type function. Now assume you have created a object with sort method

    var bar = {
        sort: function(){
            // Some code 
        }
    }

Now when you check isArray(bar) then it will return true because bar object has sort method, But the fact is bar is not an array.

So this method is not a best way to detect an array as you can see it's not handle the case when some object has sort method.

Method 3:

ECMAScript 5 has introduced Array.isArray() method to detect an array type value. The sole purpose of this method is accurately detecting whether a value is an array or not.

In many JavaScript libraries you may see the code below for detecting an value of type array.

function(value){
   // ECMAScript 5 feature
    if(typeof Array.isArray === 'function'){
        return Array.isArray(value);
    }else{
       return Object.prototype.toString.call(value) === '[object Array]';
    }
}

Method 4:

You can query the constructor name:

function isArray(value) {
    return value.constructor.name === "Array";
}

Method 5:

You check if a given value is an instanceof Array:

function isArray(value) {
    return value instanceof Array;
}

Question 37. Best way to detect reference values of any type in JavaScript ?

Answer

In Javascript Object are called as reference type, Any value other then primitive is definitely a reference type. There are several built-in reference type such as Object, Array, Function, Date, null and Error.

Detecting object using typeof operator

console.log(typeof {});           // object
console.log(typeof []);           // object
console.log(typeof new Array());  // object
console.log(typeof null);         // object 
console.log(typeof new RegExp()); // object
console.log(typeof new Date());   // object

But the downside of using typeof operator to detect an object is that typeof returns object for null (However this is fact that null is an object in JavaScript).

The best way to detect an object of specific reference type using instanceof operator.

Syntax : value instanceof constructor

//Detecting an array
if(value instanceof Array){
    console.log("value is type of array");
}
// Employee constructor function
function Employee(name){
    this.name = name; // Public property
}

var emp1 = new Employee('John');

console.log(emp1 instanceof Employee); // true

instanceof not only check the constructor which is used to create an object but also check it's prototype chain see below example.

console.log(emp1 instanceof Object); // true

Question 38. How does Object.create method works JavaScript?

Answer

The ECMAScript 5 Object.create() method is the easiest way for one object to inherit from another, without invoking a constructor function.

For instance:

var employee = {
  name: 'Nishant',
  displayName: function () {
    console.log(this.name);
  }
};

var emp1 = Object.create(employee);
console.log(emp1.displayName());  // output "Nishant"

In the example above, we create a new object emp1 that inherits from employee. In other words emp1's prototype is set to employee. After this emp1 is able to access the same properties and method on employee until new properties or method with the same name are defined.

For instance: Defining displayName() method on emp1 will not automatically override the employee displayName.

emp1.displayName = function() {
    console.log('xyz-Anonymous');
};

employee.displayName(); //Nishant
emp1.displayName();//xyz-Anonymous

In addition to this Object.create() method also allows to specify a second argument which is an object containing additional properties and methods to add to the new object.

For example

var emp1 = Object.create(employee, {
    name: {
        value: "John"
    }
});

emp1.displayName(); // "John"
employee.displayName(); // "Nishant"

In the example above, emp1 is created with it's own value for name, so calling displayName() method will display "John" instead of "Nishant".

Object created in this manner give you full control over newly created object. You are free to add, remove any properties and method you want.

Question 39. How to use constructor functions for inheritance in JavaScript?

Answer

Let say we have Person class which has name, age, salary properties and incrementSalary() method.

function Person(name, age, salary) {
  this.name = name;
  this.age = age;
  this.salary = salary;
  this.incrementSalary = function (byValue) {
    this.salary = this.salary + byValue;
  };
}

Now we wish to create Employee class which contains all the properties of Person class and wanted to add some additional properties into Employee class.

function Employee(company){
    this.company = company;
}

//Prototypal Inheritance 
Employee.prototype = new Person("Nishant", 24,5000);

In the example above, Employee type inherits from Person. It does so by assigning a new instance of Person to Employee prototype. After that, every instance of Employee inherits its properties and methods from Person.

//Prototypal Inheritance 
Employee.prototype = new Person("Nishant", 24,5000);

var emp1 = new Employee("Google");

console.log(emp1 instanceof Person); // true
console.log(emp1 instanceof Employee); // true

Let's understand Constructor inheritance

//Defined Person class
function Person(name){
    this.name = name || "Nishant";
}

var obj = {};

// obj inherit Person class properties and method 
Person.call(obj); // constructor inheritance

console.log(obj); // Object {name: "Nishant"}

Here we saw calling Person.call(obj) define the name properties from Person to obj.

console.log(name in obj); // true

Type-based inheritance is best used with developer defined constructor function rather than natively in JavaScript. In addition to this also allows flexibility in how we create similar type of object.

Question 40. How we can prevent modification of object in JavaScript ?.

Answer

ECMAScript 5 introduce several methods to prevent modification of object which lock down object to ensure that no one, accidentally or otherwise, change functionality of Object.

There are three levels of preventing modification:

1: Prevent extensions :

No new properties or methods can be added to the object, but one can change the existing properties and method.

For example:

var employee = {
    name: "Nishant"
};

// lock the object 
Object.preventExtensions(employee);

// Now try to change the employee object property name
employee.name = "John"; // work fine 

//Now try to add some new property to the object
employee.age = 24; // fails silently unless it's inside the strict mode

2: Seal :

It is same as prevent extension, in addition to this also prevent existing properties and methods from being deleted.

To seal an object, we use Object.seal() method. you can check whether an object is sealed or not using Object.isSealed();

var employee = {
    name: "Nishant"
};

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee)); // true

delete employee.name // fails silently unless it's in strict mode

// Trying to add new property will give an error
employee.age = 30; // fails silently unless in strict mode

when an object is sealed, its existing properties and methods can't be removed. Sealed object are also non-extensible.

3: Freeze :

Same as seal, In addition to this prevent existing properties methods from being modified (All properties and methods are read only).

To freeze an object, use Object.freeze() method. We can also determine whether an object is frozen using Object.isFrozen();

var employee = {
    name: "Nishant"
};

//Freeze the object
Object.freeze(employee); 

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee));     // true
console.log(Object.isFrozen(employee));     // true


employee.name = "xyz"; // fails silently unless in strict mode
employee.age = 30;     // fails silently unless in strict mode
delete employee.name   // fails silently unless it's in strict mode

Frozen objects are considered both non-extensible and sealed.

Recommended:

If you are decided to prevent modification, sealed, freeze the object then use in strict mode so that you can catch the error.

For example:

"use strict";

var employee = {
    name: "Nishant"
};

//Freeze the object
Object.freeze(employee); 

// Seal the object 
Object.seal(employee);

console.log(Object.isExtensible(employee)); // false
console.log(Object.isSealed(employee));     // true
console.log(Object.isFrozen(employee));     // true


employee.name = "xyz"; // fails silently unless in strict mode
employee.age = 30;     // fails silently unless in strict mode
delete employee.name;  // fails silently unless it's in strict mode

Question 41. Write a log function which will add prefix (your message) to every message you log using console.log ?

For example, If you log console.log("Some message") then output should be (your message) Some message

Answer

Logging error message or some informative message is always required when you dealing with client side JavaScript using console.log method. Some time you want to add some prefix to identify message generated log from your application hence you would like to prefix your app name in every console.log.

A general way to do this keep adding your app name in every console.log message like

console.log('your app name' + 'some error message');

But doing in this way you have to write your app name everytime when you log message using console.

There are some best way we can achieve this

function appLog() {
  var args = Array.prototype.slice.call(arguments);
  args.unshift('your app name');
  console.log.apply(console, args);
}

appLog("Some error message"); 
//output of above console: 'your app name Some error message'

Question 42 . Write a function which will test string as a literal and as an object ?

For example: We can create string using string literal and using String constructor function.

 // using string literal
 var ltrlStr = "Hi I am string literal";
 // using String constructor function 
 var objStr = new String("Hi I am string object");

Answer

We can use typeof operator to test string literal and instanceof operator to test String object.

 function isString(str) {
     return typeof(str) == 'string' || str instanceof String;
 }
 
 var ltrlStr = "Hi I am string literal";
 var objStr = new String("Hi I am string object");
 console.log(isString(ltrlStr)); // true
 console.log(isString(objStr)); // true

Question 43 . What is typical use case for anonymous function in JavaScript ?

Answer

Anonymous functions basically used in following scenario.

No name is needed if function is only used in one place, then there is no need to add a name to function.

Let's take the example of setTimeout function

Here there is no need of using named function when we are sure that function which will alert hello would use only once in application.

setTimeout(function(){
    alert("Hello");
},1000);

Anonymous functions are declared inline and inline functions have advantages in the case that they can access variable in the parent scopes.

Let's take a example of event handler. Notify event of particular type (such as click) for a given object.

Let say we have HTML element (button) on which we want to add click event and when user do click on button we would like to execute some logic.

Add Event Listener

Above example shows used of anonymous function as a callback function in event handler.

var btn = document.getElementById('myBtn');
btn.addEventListener('click', function () {
  alert('button clicked');
});
<button id="myBtn"></button>

Passing anonymous function as a parameter to calling function.

Example:

// Function which will execute callback function
function processCallback(callback){
    if(typeof callback === 'function'){
        callback();
    }
}

// Call function and pass anonymous function as callback 
processCallback(function(){
    alert("Hi I am anonymous callback function");
});

The best way to make a decision for using anonymous function is to ask the following question:

Will the function which I am going to define, be used anywhere else?

If your answer is yes then go and create named function rather anonymous function.

Advantage of using anonymous function:

  1. It can reduce a bit of code, particularly in recursive function and in callback function.
  2. Avoid needless global namespace pollutions.

Question 44 . How to set a default parameter value ?

Answer

If you are coming from python/c# you might be using default value for function parameter incase value(formal parameter) has not been passed. For instance :

// Define sentEmail function 
// configuration : Configuration object
// provider : Email Service provider, Default would be gmail
def sentEmail(configuration, provider = 'Gmail'):
    # Your code logic

In Pre ES6/ES2015

There are a lot of ways by which you can achieve this in pre ES2015.

Let's understand the code below by which we achieved setting default parameter value.

Method 1: Setting default parameter value

function sentEmail(configuration, provider) {
  // Set default value if user has not passed value for provider
  provider = typeof provider !== 'undefined' ? provider : 'Gmail'  
  // Your code logic
;
}
// In this call we are not passing provider parameter value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
});
// Here we are passing Yahoo Mail as a provider value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
}, 'Yahoo Mail');

Method 2: Setting default parameter value

function sentEmail(configuration, provider) {
  // Set default value if user has not passed value for provider
  provider = provider || 'Gmail'  
  // Your code logic
;
}
// In this call we are not passing provider parameter value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
});
// Here we are passing Yahoo Mail as a provider value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
}, 'Yahoo Mail');

Method 3: Setting default parameter value in ES6

function sendEmail(configuration, provider = "Gmail") {
  // Set default value if user has not passed value for provider
  
  // Value of provider can be accessed directly
  console.log(`Provider: ${provider}`);
}

// In this call we are not passing provider parameter value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
});
// Here we are passing Yahoo Mail as a provider value
sentEmail({
  from: 'xyz@gmail.com',
  subject: 'Test Email'
}, 'Yahoo Mail');

Question 45. Write code for merge two JavaScript Object dynamically.

Let say you have two objects

var person = {
    name : 'John',
    age  : 24
}

var address = {
    addressLine1 : 'Some Location x',
    addressLine2 : 'Some Location y',
    city : 'NewYork'
} 

Write merge function which will take two object and add all the own property of second object into first object.

Answer

merge(person , address); 
 
/* Now person should have 5 properties 
name , age , addressLine1 , addressLine2 , city */

Method 1: Using ES6, Object.assign method

const merge = (toObj, fromObj) => Object.assign(toObj, fromObj);

Method 2: Without using built-in function

function merge(toObj, fromObj) {
  // Make sure both of the parameter is an object
  if (typeof toObj === 'object' && typeof fromObj === 'object') {
    for (var pro in fromObj) {
      // Assign only own properties not inherited properties
      if (fromObj.hasOwnProperty(pro)) {
        // Assign property and value
        toObj[pro] = fromObj[pro];
      }
    }
  }else{
      throw "Merge function can apply only on object";
  }
}

Question 46. What is non-enumerable property in JavaScript and how you can create one?

Answer

Object can have properties that don't show up when you iterate through object using for...in loop or using Object.keys() to get an array of property names. This properties is know as non-enumerable properties.

Let say we have following object

var person = {
    name: 'John'
};
person.salary = '10000$';
person['country'] = 'USA';

console.log(Object.keys(person)); // ['name', 'salary', 'country']

As we know that person object properties name, salary ,country are enumerable hence it's shown up when we called Object.keys(person).

To create a non-enumerable property we have to use Object.defineProperty(). This is a special method for creating non-enumerable property in JavaScript.

var person = {
    name: 'John'
};
person.salary = '10000$';
person['country'] = 'USA';

// Create non-enumerable property
Object.defineProperty(person, 'phoneNo',{
    value : '8888888888',
    enumerable: false
})

Object.keys(person); // ['name', 'salary', 'country']

In the example above phoneNo property didn't show up because we made it non-enumerable by setting enumerable:false

Bonus

Now let's try to change value of phoneNo

person.phoneNo = '7777777777'; 

Object.defineProperty() also lets you create read-only properties as we saw above, we are not able to modify phoneNo value of a person object. This is because descriptor has writable property, which is false by default. Changing non-writable property value will return error in strict mode. In non-strict mode it won't through any error but it won't change the value of phoneNo.

Question 47. What is Function binding ?

Answer

Function binding falls in advance JavaScript category and this is very popular technique to use in conjunction with event handler and callback function to preserve code execution context while passing function as a parameter.

Let's consider the following example:

var clickHandler = {
    message: 'click event handler',
    handleClick: function(event) {
        console.log(this.message);
    }
};

var btn = document.getElementById('myBtn');
// Add click event to btn
btn.addEventListener('click', clickHandler.handleClick);

Here in this example clickHandler object is created which contain message properties and handleClick method.

We have assigned handleClick method to a DOM button, which will be executed in response of click. When the button is clicked, then handleClick method is being called and console message. Here console.log should log the click event handler message but it actually log undefined.

The problem of displaying undefined is because of the execution context of clickHandler.handleClick method is not being saved hence this pointing to button btn object. We can fix this issue using bind method.

var clickHandler = {
    message: 'click event handler',
    handleClick: function(event) {
        console.log(this.message);
    }
};

var btn = document.getElementById('myBtn');
// Add click event to btn and bind the clickHandler object
btn.addEventListener('click', clickHandler.handleClick.bind(clickHandler));

bind method is available to all the function similar to call and apply method which take argument value of this.

Coding Questions

Passing values by reference vs by value

For a JS developer, it's crucially important to understand which values are passed by reference, and which ones are passed by value. Remember that objects, including arrays are passed by reference while strings, booleans and numbers are passed by value.

1. What would be the output of following code?

var strA = "hi there";
var strB = strA;
strB="bye there!";
console.log (strA)

Answer

The output will be 'hi there' because we're dealing with strings here. Strings are passed by value, that is, copied.

2. What would be the output of following code?

var objA = {prop1: 42};
var objB = objA; 
objB.prop1 = 90;
console.log(objA) 

Answer

The output will be {prop1: 90} because we're dealing with objects here. Objects are passed by reference, that is, objA and objB point to the same object in memory.

3. What would be the output of following code?

var objA = {prop1: 42};
var objB = objA;
objB = {};
console.log(objA)

Answer

The output will be {prop1: 42}.

When we assign objA to objB, the objB variable will point to the same object as the objB variable.

However, when we reassign objB to an empty object, we simply change where objB variable references to. This doesn't affect where objA variable references to.

4. What would be the output of following code?

var arrA = [0,1,2,3,4,5];
var arrB = arrA;
arrB[0]=42;
console.log(arrA)

Answer

The output will be [42,1,2,3,4,5].

Arrays are object in JavaScript and they are passed and assigned by reference. This is why both arrA and arrB point to the same array [0,1,2,3,4,5]. That's why changing the first element of the arrB will also modify arrA: it's the same array in the memory.

5. What would be the output of following code?

var arrA = [0,1,2,3,4,5];
var arrB = arrA.slice();
arrB[0]=42;
console.log(arrA)

Answer

The output will be [0,1,2,3,4,5].

The slice function copies all the elements of the array returning the new array. That's why arrA and arrB reference two completely different arrays.

6. What would be the output of following code?

var arrA = [{prop1: "value of array A!!"},  {someProp: "also value of array A!"}, 3,4,5];
var arrB = arrA;
arrB[0].prop1=42;
console.log(arrA);

Answer

The output will be [{prop1: 42}, {someProp: "also value of array A!"}, 3,4,5].

Arrays are object in JS, so both varaibles arrA and arrB point to the same array. Changing arrB[0] is the same as changing arrA[0]

7. What would be the output of following code?

var arrA = [{prop1: "value of array A!!"}, {someProp: "also value of array A!"},3,4,5];
var arrB = arrA.slice();
arrB[0].prop1=42;
arrB[3] = 20;
console.log(arrA);

Answer

The output will be [{prop1: 42}, {someProp: "also value of array A!"}, 3,4,5].

The slice function copies all the elements of the array returning the new array. However, it doesn't do deep copying. Instead it does shallow copying. You can imagine slice implemented like this:

function slice(arr) {
   var result = [];
   for (i = 0; i< arr.length; i++) {
       result.push(arr[i]);
   }
   return result; 
}

Look at the line with result.push(arr[i]). If arr[i] happens to be a number or string, it will be passed by value, in other words, copied. If arr[i] is an object, it will be passed by reference.

In case of our array arr[0] is an object {prop1: "value of array A!!"}. Only the reference to this object will be copied. This effectively means that arrays arrA and arrB share first two elements.

This is why changing the property of arrB[0] in arrB will also change the arrA[0].

Hoisting

1. console.log(employeeId);

  1. Some Value
  2. Undefined
  3. Type Error
  4. ReferenceError: employeeId is not defined

Answer

  1. ReferenceError: employeeId is not defined

2. What would be the output of following code?

console.log(employeeId);
var employeeId = '19000';
  1. Some Value
  2. undefined
  3. Type Error
  4. ReferenceError: employeeId is not defined

Answer

  1. undefined

3. What would be the output of following code?

var employeeId = '1234abe';
(function(){
    console.log(employeeId);
    var employeeId = '122345';
})();
  1. '122345'
  2. undefined
  3. Type Error
  4. ReferenceError: employeeId is not defined

Answer

  1. undefined

4. What would be the output of following code?

var employeeId = '1234abe';
(function() {
    console.log(employeeId);
    var employeeId = '122345';
    (function() {
        var employeeId = 'abc1234';
    }());
}());
  1. '122345'
  2. undefined
  3. '1234abe'
  4. ReferenceError: employeeId is not defined

Answer

  1. undefined

5. What would be the output of following code?

(function() {
    console.log(typeof displayFunc);
    var displayFunc = function(){
        console.log("Hi I am inside displayFunc");
    }
}());
  1. undefined
  2. function
  3. 'Hi I am inside displayFunc'
  4. ReferenceError: displayFunc is not defined

Answer

  1. undefined

6. What would be the output of following code?

var employeeId = 'abc123';
function foo(){
    employeeId = '123bcd';
    return;
}
foo();
console.log(employeeId);
  1. undefined
  2. '123bcd'
  3. 'abc123'
  4. ReferenceError: employeeId is not defined

Answer

  1. '123bcd'

7. What would be the output of following code?

var employeeId = 'abc123';

function foo() {
    employeeId = '123bcd';
    return;

    function employeeId() {}
}
foo();
console.log(employeeId);
  1. undefined
  2. '123bcd'
  3. 'abc123'
  4. ReferenceError: employeeId is not defined

Answer

  1. 'abc123'

8. What would be the output of following code?

var employeeId = 'abc123';

function foo() {
    employeeId();
    return;

    function employeeId() {
        console.log(typeof employeeId);
    }
}
foo();
  1. undefined
  2. function
  3. string
  4. ReferenceError: employeeId is not defined

Answer

  1. 'function'

9. What would be the output of following code?

function foo() {
    employeeId();
    var product = 'Car'; 
    return;

    function employeeId() {
        console.log(product);
    }
}
foo();
  1. undefined
  2. Type Error
  3. 'Car'
  4. ReferenceError: product is not defined

Answer

  1. undefined

10. What would be the output of following code?

(function foo() {
    bar();

    function bar() {
        abc();
        console.log(typeof abc);
    }

    function abc() {
        console.log(typeof bar);
    }
}());
  1. undefined undefined
  2. Type Error
  3. function function
  4. ReferenceError: bar is not defined

Answer

  1. function function

Objects

1. What would be the output of following code ?

(function() {
    'use strict';

    var person = {
        name: 'John'
    };
    person.salary = '10000$';
    person['country'] = 'USA';

    Object.defineProperty(person, 'phoneNo', {
        value: '8888888888',
        enumerable: true
    })

    console.log(Object.keys(person)); 
})();
  1. Type Error
  2. undefined
  3. ["name", "salary", "country", "phoneNo"]
  4. ["name", "salary", "country"]

Answer

  1. ["name", "salary", "country", "phoneNo"]

2. What would be the output of following code ?

(function() {
    'use strict';

    var person = {
        name: 'John'
    };
    person.salary = '10000$';
    person['country'] = 'USA';

    Object.defineProperty(person, 'phoneNo', {
        value: '8888888888',
        enumerable: false
    })

    console.log(Object.keys(person)); 
})();
  1. Type Error
  2. undefined
  3. ["name", "salary", "country", "phoneNo"]
  4. ["name", "salary", "country"]

Answer

  1. ["name", "salary", "country"]

3. What would be the output of following code ?

(function() {
    var objA = {
        foo: 'foo',
        bar: 'bar'
    };
    var objB = {
        foo: 'foo',
        bar: 'bar'
    };
    console.log(objA == objB);
    console.log(objA === objB);
}());
  1. false true
  2. false false
  3. true false
  4. true true

Answer

  1. false false

4. What would be the output of following code ?

(function() {
    var objA = new Object({foo: "foo"});
    var objB = new Object({foo: "foo"});
    console.log(objA == objB);
    console.log(objA === objB);
}());
  1. false true
  2. false false
  3. true false
  4. true true

Answer

  1. false false

5. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = Object.create({
        foo: 'foo'
    });
    console.log(objA == objB);
    console.log(objA === objB);
}());
  1. false true
  2. false false
  3. true false
  4. true true

Answer

  1. false false

6. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = Object.create(objA);
    console.log(objA == objB);
    console.log(objA === objB);
}());
  1. false true
  2. false false
  3. true false
  4. true true

Answer

  1. false false

7. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = Object.create(objA);
    console.log(objA.toString() == objB.toString());
    console.log(objA.toString() === objB.toString());
}());
  1. false true
  2. false false
  3. true false
  4. true true

Answer

  1. true true

8. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = objA;
    console.log(objA == objB);
    console.log(objA === objB);
    console.log(objA.toString() == objB.toString());
    console.log(objA.toString() === objB.toString());
}());
  1. true true true false
  2. true false true true
  3. true true true true
  4. true true false false

Answer

  1. true true true true

9. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = objA;
    objB.foo = 'bar';
    console.log(objA.foo);
    console.log(objB.foo);
}());
  1. foo bar
  2. bar bar
  3. foo foo
  4. bar foo

Answer

  1. bar bar

10. What would be the output of following code ?

(function() {
    var objA = Object.create({
        foo: 'foo'
    });
    var objB = objA;
    objB.foo = 'bar';

    delete objA.foo;
    console.log(objA.foo);
    console.log(objB.foo);
}());
  1. foo bar
  2. bar bar
  3. foo foo
  4. bar foo

Answer

  1. foo foo

11. What would be the output of following code ?

(function() {
    var objA = {
        foo: 'foo'
    };
    var objB = objA;
    objB.foo = 'bar';

    delete objA.foo;
    console.log(objA.foo);
    console.log(objB.foo);
}());
  1. foo bar
  2. undefined undefined
  3. foo foo
  4. undefined bar

Answer

  1. undefined undefined

Arrays

1. What would be the output of following code?

(function() {
    var array = new Array('100');
    console.log(array);
    console.log(array.length);
}());
  1. undefined undefined
  2. [undefined × 100] 100
  3. ["100"] 1
  4. ReferenceError: array is not defined

Answer

  1. ["100"] 1

2. What would be the output of following code?

(function() {
    var array1 = [];
    var array2 = new Array(100);
    var array3 = new Array(['1',2,'3',4,5.6]);
    console.log(array1);
    console.log(array2);
    console.log(array3);
    console.log(array3.length);
}());
  1. [] [] [Array[5]] 1
  2. [] [undefined × 100] Array[5] 1
  3. [] [] ['1',2,'3',4,5.6] 5
  4. [] [] [Array[5]] 5

Answer

  1. [] [] [Array[5]] 1

3. What would be the output of following code?

(function () {
  var array = new Array('a', 'b', 'c', 'd', 'e');
  array[10] = 'f';
  delete array[10];
  console.log(array.length);
}());
  1. 11
  2. 5
  3. 6
  4. undefined

Answer

  1. 11

4. What would be the output of following code?

(function(){
    var animal = ['cow','horse'];
        animal.push('cat');
        animal.push('dog','rat','goat');
        console.log(animal.length);
})();
  1. 4
  2. 5
  3. 6
  4. undefined

Answer

  1. 6

5. What would be the output of following code?

(function(){
    var animal = ['cow','horse'];
        animal.push('cat');
        animal.unshift('dog','rat','goat');
        console.log(animal);
})();
  1. [ 'dog', 'rat', 'goat', 'cow', 'horse', 'cat' ]
  2. [ 'cow', 'horse', 'cat', 'dog', 'rat', 'goat' ]
  3. Type Error
  4. undefined

Answer

  1. [ 'dog', 'rat', 'goat', 'cow', 'horse', 'cat' ]

6. What would be the output of following code?

(function(){
    var array = [1,2,3,4,5];
    console.log(array.indexOf(2));
    console.log([{name: 'John'},{name : 'John'}].indexOf({name:'John'}));
    console.log([[1],[2],[3],[4]].indexOf([3]));
    console.log("abcdefgh".indexOf('e'));
})();
  1. 1 -1 -1 4
  2. 1 0 -1 4
  3. 1 -1 -1 -1
  4. 1 undefined -1 4

Answer

  1. 1 -1 -1 4

7. What would be the output of following code?

(function(){
    var array = [1,2,3,4,5,1,2,3,4,5,6];
    console.log(array.indexOf(2));
    console.log(array.indexOf(2,3));
    console.log(array.indexOf(2,10));
})();
  1. 1 -1 -1
  2. 1 6 -1
  3. 1 1 -1
  4. 1 undefined undefined

Answer

  1. 1 6 -1

8. What would be the output of following code?

(function(){
    var numbers = [2,3,4,8,9,11,13,12,16];
    var even = numbers.filter(function(element, index){
        return element % 2 === 0; 
    });
    console.log(even);

    var containsDivisibleby3 = numbers.some(function(element, index){
        return element % 3 === 0;
    });

    console.log(containsDivisibleby3);    
})();
  1. [ 2, 4, 8, 12, 16 ] [ 0, 3, 0, 0, 9, 0, 12]
  2. [ 2, 4, 8, 12, 16 ] [ 3, 9, 12]
  3. [ 2, 4, 8, 12, 16 ] true
  4. [ 2, 4, 8, 12, 16 ] false

Answer

  1. [ 2, 4, 8, 12, 16 ] true

9. What would be the output of following code?

(function(){
    var containers = [2,0,false,"", '12', true];
    var containers = containers.filter(Boolean);
    console.log(containers);
    var containers = containers.filter(Number);
    console.log(containers);
    var containers = containers.filter(String);
    console.log(containers);
    var containers = containers.filter(Object);
    console.log(containers);        
})();
  1. [ 2, '12', true ] [ 2, '12', true ] [ 2, '12', true ] [ 2, '12', true ]
  2. [false, true] [ 2 ] ['12'] [ ]
  3. [2,0,false,"", '12', true] [2,0,false,"", '12', true] [2,0,false,"", '12', true] [2,0,false,"", '12', true]
  4. [ 2, '12', true ] [ 2, '12', true, false ] [ 2, '12', true,false ] [ 2, '12', true,false]

Answer

  1. [ 2, '12', true ] [ 2, '12', true ] [ 2, '12', true ] [ 2, '12', true ]

10. What would be the output of following code?

(function(){
    var list = ['foo','bar','john','ritz'];
        console.log(list.slice(1));    
        console.log(list.slice(1,3));
        console.log(list.slice());
        console.log(list.slice(2,2));
        console.log(list);                
})();
  1. [ 'bar', 'john', 'ritz' ] [ 'bar', 'john' ] [ 'foo', 'bar', 'john', 'ritz' ] [] [ 'foo', 'bar', 'john', 'ritz' ]
  2. [ 'bar', 'john', 'ritz' ] [ 'bar', 'john','ritz ] [ 'foo', 'bar', 'john', 'ritz' ] [] [ 'foo', 'bar', 'john', 'ritz' ]
  3. [ 'john', 'ritz' ] [ 'bar', 'john' ] [ 'foo', 'bar', 'john', 'ritz' ] [] [ 'foo', 'bar', 'john', 'ritz' ]
  4. [ 'foo' ] [ 'bar', 'john' ] [ 'foo', 'bar', 'john', 'ritz' ] [] [ 'foo', 'bar', 'john', 'ritz' ]

Answer

  1. [ 'bar', 'john', 'ritz' ] [ 'bar', 'john' ] [ 'foo', 'bar', 'john', 'ritz' ] [] [ 'foo', 'bar', 'john', 'ritz' ]

11. What would be the output of following code?

(function(){
    var list = ['foo','bar','john'];
        console.log(list.splice(1));        
        console.log(list.splice(1,2));
        console.log(list);            
})();
  1. [ 'bar', 'john' ] [] [ 'foo' ]
  2. [ 'bar', 'john' ] [] [ 'bar', 'john' ]
  3. [ 'bar', 'john' ] [ 'bar', 'john' ] [ 'bar', 'john' ]
  4. [ 'bar', 'john' ] [] []

Answer

  1. [ 'bar', 'john' ] [] [ 'foo' ]

12. What would be the output of following code?

(function(){
    var arrayNumb = [2, 8, 15, 16, 23, 42];
    arrayNumb.sort();
    console.log(arrayNumb);
})();
  1. [2, 8, 15, 16, 23, 42]
  2. [42, 23, 26, 15, 8, 2]
  3. [ 15, 16, 2, 23, 42, 8 ]
  4. [ 2, 8, 15, 16, 23, 42 ]

Answer

  1. [ 15, 16, 2, 23, 42, 8 ]

Functions

1. What would be the output of following code ?

function funcA(){
    console.log("funcA ", this);
    (function innerFuncA1(){
        console.log("innerFunc1", this);
        (function innerFunA11(){
            console.log("innerFunA11", this);
        })();
    })();
}
    
console.log(funcA());
  1. funcA Window {...} innerFunc1 Window {...} innerFunA11 Window {...}
  2. undefined
  3. Type Error
  4. ReferenceError: this is not defined

Answer

1)

2. What would be the output of following code ?

var obj = {
    message: "Hello",
    innerMessage: !(function() {
        console.log(this.message);
    })()
};
    
console.log(obj.innerMessage);
  1. ReferenceError: this.message is not defined
  2. undefined
  3. Type Error
  4. undefined true

Answer

  1. undefined true

3. What would be the output of following code ?

var obj = {
    message: "Hello",
    innerMessage: function() {
        return this.message;
    }
};
    
console.log(obj.innerMessage());
  1. Hello
  2. undefined
  3. Type Error
  4. ReferenceError: this.message is not defined

Answer

  1. Hello

4. What would be the output of following code ?

var obj = {
  message: 'Hello',
  innerMessage: function () {
    (function () {
      console.log(this.message);
    }());
  }
};
console.log(obj.innerMessage());
  1. Type Error
  2. Hello
  3. undefined
  4. ReferenceError: this.message is not defined

Answer

  1. undefined

5. What would be the output of following code ?

var obj = {
  message: 'Hello',
  innerMessage: function () {
      var self = this;
    (function () {
      console.log(self.message);
    }());
  }
};
console.log(obj.innerMessage());
  1. Type Error
  2. 'Hello'
  3. undefined
  4. ReferenceError: self.message is not defined

Answer

  1. 'Hello'

6. What would be the output of following code ?

function myFunc(){
    console.log(this.message);
}
myFunc.message = "Hi John";
    
console.log(myFunc());
  1. Type Error
  2. 'Hi John'
  3. undefined
  4. ReferenceError: this.message is not defined

Answer

  1. undefined

7. What would be the output of following code ?

function myFunc(){
    console.log(myFunc.message);
}
myFunc.message = "Hi John";
    
console.log(myFunc());
  1. Type Error
  2. 'Hi John'
  3. undefined
  4. ReferenceError: this.message is not defined

Answer

  1. 'Hi John'

8. What would be the output of following code ?

function myFunc() {
  myFunc.message = 'Hi John';
  console.log(myFunc.message);
}
console.log(myFunc());
  1. Type Error
  2. 'Hi John'
  3. undefined
  4. ReferenceError: this.message is not defined

Answer

  1. 'Hi John'

9. What would be the output of following code ?

function myFunc(param1,param2) {
  console.log(myFunc.length);
}
console.log(myFunc());
console.log(myFunc("a","b"));
console.log(myFunc("a","b","c","d"));
  1. 2 2 2
  2. 0 2 4
  3. undefined
  4. ReferenceError

Answer

a) 2 2 2

10. What would be the output of following code ?

function myFunc() {
  console.log(arguments.length);
}
console.log(myFunc());
console.log(myFunc("a","b"));
console.log(myFunc("a","b","c","d"));
  1. 2 2 2
  2. 0 2 4
  3. undefined
  4. ReferenceError

Answer

  1. 0 2 4

Object Oriented

1. What would be the output of following code ?

function Person(name, age){
    this.name = name || "John";
    this.age = age || 24;
    this.displayName = function(){
        console.log(this.name);
    }
}

Person.name = "John";
Person.displayName = function(){
    console.log(this.name);
}

var person1 = new Person('John');
    person1.displayName();
    Person.displayName();
  1. John Person
  2. John John
  3. John undefined
  4. John John

Answer

  1. John Person

Scopes

1. What would be the output of following code ?

function passWordMngr() {
    var password = '12345678';
    this.userName = 'John';
    return {
        pwd: password
    };
}
// Block End
var userInfo = passWordMngr();
console.log(userInfo.pwd);
console.log(userInfo.userName);
  1. 12345678 Window
  2. 12345678 John
  3. 12345678 undefined
  4. undefined undefined

Answer

  1. 12345678 undefined

2. What would be the output of following code ?

var employeeId = 'aq123';
function Employee() {
  this.employeeId = 'bq1uy';
}
console.log(Employee.employeeId);
  1. Reference Error
  2. aq123
  3. bq1uy
  4. undefined

Answer

  1. undefined

3. What would be the output of following code ?

var employeeId = 'aq123';

function Employee() {
    this.employeeId = 'bq1uy';
}
console.log(new Employee().employeeId);
Employee.prototype.employeeId = 'kj182';
Employee.prototype.JobId = '1BJKSJ';
console.log(new Employee().JobId);
console.log(new Employee().employeeId);
  1. bq1uy 1BJKSJ bq1uy undefined
  2. bq1uy 1BJKSJ bq1uy
  3. bq1uy 1BJKSJ kj182
  4. undefined 1BJKSJ kj182

Answer

  1. bq1uy 1BJKSJ bq1uy

4. What would be the output of following code ?

var employeeId = 'aq123';
(function Employee() {
    try {
        throw 'foo123';
    } catch (employeeId) {
        console.log(employeeId);
    }
    console.log(employeeId);
}());
  1. foo123 aq123
  2. foo123 foo123
  3. aq123 aq123
  4. foo123 undefined

Answer

  1. foo123 aq123

Call, Apply, Bind

1. What would be the output of following code ?

(function() {
    var greet = 'Hello World';
    var toGreet = [].filter.call(greet, function(element, index) {
        return index > 5;
    });
    console.log(toGreet);
}());
  1. Hello World
  2. undefined
  3. World
  4. [ 'W', 'o', 'r', 'l', 'd' ]

Answer

  1. [ 'W', 'o', 'r', 'l', 'd' ]

2. What would be the output of following code ?

(function() {
    var fooAccount = {
        name: 'John',
        amount: 4000,
        deductAmount: function(amount) {
            this.amount -= amount;
            return 'Total amount left in account: ' + this.amount;
        }
    };
    var barAccount = {
        name: 'John',
        amount: 6000
    };
    var withdrawAmountBy = function(totalAmount) {
        return fooAccount.deductAmount.bind(barAccount, totalAmount);
    };
    console.log(withdrawAmountBy(400)());
    console.log(withdrawAmountBy(300)());
}());
  1. Total amount left in account: 5600 Total amount left in account: 5300
  2. undefined undefined
  3. Total amount left in account: 3600 Total amount left in account: 3300
  4. Total amount left in account: 5600 Total amount left in account: 5600

Answer

  1. Total amount left in account: 5600 Total amount left in account: 5300

3. What would be the output of following code ?

(function() {
    var fooAccount = {
        name: 'John',
        amount: 4000,
        deductAmount: function(amount) {
            this.amount -= amount;
            return this.amount;
        }
    };
    var barAccount = {
        name: 'John',
        amount: 6000
    };
    var withdrawAmountBy = function(totalAmount) {
        return fooAccount.deductAmount.apply(barAccount, [totalAmount]);
    };
    console.log(withdrawAmountBy(400));
    console.log(withdrawAmountBy(300));
    console.log(withdrawAmountBy(200));
}());
  1. 5600 5300 5100
  2. 3600 3300 3100
  3. 5600 3300 5100
  4. undefined undefined undefined

Answer

  1. 5600 5300 5100

4. What would be the output of following code ?

(function() {
    var fooAccount = {
        name: 'John',
        amount: 6000,
        deductAmount: function(amount) {
            this.amount -= amount;
            return this.amount;
        }
    };
    var barAccount = {
        name: 'John',
        amount: 4000
    };
    var withdrawAmountBy = function(totalAmount) {
        return fooAccount.deductAmount.call(barAccount, totalAmount);
    };
    console.log(withdrawAmountBy(400));
    console.log(withdrawAmountBy(300));
    console.log(withdrawAmountBy(200));
}());
  1. 5600 5300 5100
  2. 3600 3300 3100
  3. 5600 3300 5100
  4. undefined undefined undefined

Answer

  1. 3600 3300 3100

5. What would be the output of following code ?

(function greetNewCustomer() {
    console.log('Hello ' + this.name);
}.bind({
    name: 'John'
})());
  1. Hello John
  2. Reference Error
  3. Window
  4. undefined

Answer

  1. Hello John

6. Suggest your question!

Callback Functions

1. What would be the output of following code ?

function getDataFromServer(apiUrl){
    var name = "John";
    return {
        then : function(fn){
            fn(name);
        }
    }
}

getDataFromServer('www.google.com').then(function(name){
    console.log(name);
});
  1. John
  2. undefined
  3. Reference Error
  4. fn is not defined

Answer

  1. John

2. What would be the output of following code ?

(function(){
    var arrayNumb = [2, 8, 15, 16, 23, 42];
    Array.prototype.sort = function(a,b){
        return a - b;
    };
    arrayNumb.sort();
    console.log(arrayNumb);
})();

(function(){
    var numberArray = [2, 8, 15, 16, 23, 42];
    numberArray.sort(function(a,b){
        if(a == b){
            return 0;
        }else{
            return a < b ? -1 : 1;
        }
    });
    console.log(numberArray);
})();

(function(){
    var numberArray = [2, 8, 15, 16, 23, 42];
    numberArray.sort(function(a,b){
        return a-b;
    });
    console.log(numberArray);
})();
  1. [ 2, 8, 15, 16, 23, 42 ] [ 2, 8, 15, 16, 23, 42 ] [ 2, 8, 15, 16, 23, 42 ]
  2. undefined undefined undefined
  3. [42, 23, 16, 15, 8, 2] [42, 23, 16, 15, 8, 2] [42, 23, 16, 15, 8, 2]
  4. Reference Error

Answer

  1. [ 2, 8, 15, 16, 23, 42 ] [ 2, 8, 15, 16, 23, 42 ] [ 2, 8, 15, 16, 23, 42 ]

Return Statement

1. What would be the output of following code ?

(function(){
    function sayHello(){
        var name = "Hi John";
        return 
        {
            fullName: name
        }
    }
    console.log(sayHello().fullName);
})();
  1. Hi John
  2. undefined
  3. Reference Error
  4. Uncaught TypeError: Cannot read property 'fullName' of undefined

Answer

  1. Uncaught TypeError: Cannot read property 'fullName' of undefined

2. What would be the output of following code ?

function getNumber(){
    return (2,4,5);
}

var numb = getNumber();
console.log(numb);
  1. 5
  2. undefined
  3. 2
  4. (2,4,5)

Answer

  1. 5

3. What would be the output of following code ?

function getNumber(){
    return;
}

var numb = getNumber();
console.log(numb);
  1. null
  2. undefined
  3. ""
  4. 0

Answer

  1. undefined

4. What would be the output of following code ?

function mul(x){
    return function(y){
        return [x*y, function(z){
            return x*y + z;
        }];
    }
}

console.log(mul(2)(3)[0]);
console.log(mul(2)(3)[1](4));
  1. 6, 10
  2. undefined undefined
  3. Reference Error
  4. 10, 6

Answer

  1. 6, 10

5. What would be the output of following code ?

function mul(x) {
    return function(y) {
        return {
            result: x * y,
            sum: function(z) {
                return x * y + z;
            }
        };
    };
}
console.log(mul(2)(3).result);
console.log(mul(2)(3).sum(4));
  1. 6, 10
  2. undefined undefined
  3. Reference Error
  4. 10, 6

Answer

  1. 6, 10

6. What would be the output of following code ?

function mul(x) {
    return function(y) {
        return function(z) {
            return function(w) {
                return function(p) {
                    return x * y * z * w * p;
                };
            };
        };
    };
}
console.log(mul(2)(3)(4)(5)(6));
  1. 720
  2. undefined
  3. Reference Error
  4. Type Error

Answer

  1. 720

7. What would be the output of following code ?

function getName1(){
    console.log(this.name);
}

Object.prototype.getName2 = () =>{
    console.log(this.name)
}

let personObj = {
    name:"Tony",
    print:getName1
}

personObj.print();
personObj.getName2();
  1. undefined undefined
  2. Tony undefined
  3. undefined Tony
  4. Tony Tony

Answer

  1. Tony undefined

Explaination: getName1() function works fine because it's being called from personObj, so it has access to this.name property. But when while calling getnName2 which is defined under Object.prototype doesn't have any proprty named this.name. There should be name property under prototype. Following is the code:

function getName1(){
    console.log(this.name);
}

Object.prototype.getName2 = () =>{
  console.log(Object.getPrototypeOf(this).name);
}

let personObj = {
    name:"Tony",
    print:getName1
}

personObj.print();
Object.prototype.name="Steve";
personObj.getName2();

Contributing

We always appreciate your feedback on how the book can be improved, and more questions can be added. If you think you have some question then please add that and open a pull request.

Author: Ganqqwerty
Source Code: https://github.com/ganqqwerty/123-Essential-JavaScript-Interview-Questions 
License: BSD-3-Clause license

#javascript #interviewquestion 

123 Essential JavaScript Interview Question
Jade Bird

Jade Bird

1652927735

Top 100+ DevOps Interview Questions and Answers You Should Know

This blog features the most frequently asked DevOps interview questions and answers that you must prepare to ace your interview

Are you a DevOps engineer or thinking of getting into DevOps? Well then, you have landed on the right article. In DevOps Interview Questions article, I have listed out dozens of possible questions that interviewers ask potential DevOps hires. 

The crucial thing to understand is that DevOps is not merely a collection of technologies but rather a way of thinking, a culture. DevOps requires a cultural shift that merges operations with development and demands a linked toolchain of technologies to facilitate collaborative change. Since the DevOps philosophy is still at a very nascent stage, application of DevOps as well as the bandwidth required to adapt and collaborate, varies from organization to organization. However, you can develop a portfolio of DevOps skills that can present you as a perfect candidate for any type of organization.

If you want to develop your DevOps skills in a thoughtful, structured manner and get certified as a DevOps Engineer, we would be glad to help. Once you finish the DevOps certification training, we promise that you will be able to handle a variety of DevOps roles in the industry.

What are the requirements to become a DevOps Engineer?

When looking to fill out DevOps roles, organizations look for a clear set of skills. The most important of these are:

  • Experience with infrastructure automation tools like Chef, Puppet, Ansible, SaltStack or Windows PowerShell DSC.
  • Fluency in web languages like Ruby, Python, PHP or Java.
  • Interpersonal skills that help you communicate and collaborate across teams and roles.

If you have the above skills, then you are ready to start preparing for your DevOps interview! If not, don’t worry – our DevOps certification training will help you master DevOps.

In order to structure the questions below, I put myself in your shoes. Most of the answers in this blog are written from your perspective, i.e. someone who is a potential DevOps expert. I have also segregated the questions in the following manner:

  • General DevOps Interview Questions
  • Version Control System Interview Questions
  • Continuous Integration Interview Questions
  • Continuous Testing Interview Questions
  • Configuration Management Interview Questions
  • Continuous Monitoring Interview Questions
  • Containerization and Virtualization Interview Questions

These are the top questions you might face in a DevOps job interview:

General DevOps Interview Questions

This category will include questions that are not related to any particular DevOps stage. Questions here are meant to test your understanding about DevOps rather than focusing on a particular tool or a stage.

Q1. What are the fundamental differences between DevOps & Agile?

The differences between the two are listed down in the table below.

FeaturesDevOpsAgile
AgilityAgility in both Development & OperationsAgility in only Development
Processes/ PracticesInvolves processes such as CI, CD, CT, etc.Involves practices such as Agile Scrum, Agile Kanban, etc.
Key Focus AreaTimeliness & quality have equal priorityTimeliness is the main priority
Release Cycles/ Development SprintsSmaller release cycles with immediate feedbackSmaller release cycles
Source of FeedbackFeedback is from self (Monitoring tools)Feedback is from customers
Scope of WorkAgility & need for AutomationAgility only

DevOps vs Agile

Q2. What is the need for DevOps?

According to me, this answer should start by explaining the general market trend. Instead of releasing big sets of features, companies are trying to see if small features can be transported to their customers through a series of release trains. This has many advantages like quick feedback from customers, better quality of software etc. which in turn leads to high customer satisfaction. To achieve this, companies are required to:

  1. Increase deployment frequency
  2. Lower failure rate of new releases
  3. Shortened lead time between fixes
  4. Faster mean time to recovery in the event of new release crashing

DevOps fulfills all these requirements and helps in achieving seamless software delivery. You can give examples of companies like Etsy, Google and Amazon which have adopted DevOps to achieve levels of performance that were unthinkable even five years ago. They are doing tens, hundreds or even thousands of code deployments per day while delivering world class stability, reliability and security.

If I have to test your knowledge on DevOps, you should know the difference between Agile and DevOps. The next question is directed towards that.

Q3. How is DevOps different from Agile / SDLC?

I would advise you to go with the below explanation:

Agile is a set of values and principles about how to produce i.e. develop software. Example: if you have some ideas and you want to turn those ideas into working software, you can use the Agile values and principles as a way to do that. But, that software might only be working on a developer’s laptop or in a test environment. You want a way to quickly, easily and repeatably move that software into production infrastructure, in a safe and simple way. To do that you need DevOps tools and techniques.

You can summarize by saying Agile software development methodology focuses on the development of software but DevOps on the other hand is responsible for development as well as deployment of the software in the safest and most reliable way possible. 

Now remember, you have included DevOps tools in your previous answer so be prepared to answer some questions related to that.

Q4. Which are the top DevOps tools? Which tools have you worked on?

The most popular DevOps tools are mentioned below:

  • Git : Version Control System tool
  • Jenkins : Continuous Integration tool
  • Selenium : Continuous Testing tool
  • Puppet, Chef, Ansible : Configuration Management and Deployment tools
  • Nagios : Continuous Monitoring tool
  • Docker : Containerization tool

You can also mention any other tool if you want, but make sure you include the above tools in your answer.
The second part of the answer has two possibilities:

  1. If you have experience with all the above tools then you can say that I have worked on all these tools for developing good quality software and deploying those softwares easily, frequently, and reliably.
  2. If you have experience only with some of the above tools then mention those tools and say that I have specialization in these tools and have an overview about the rest of the tools.

Q5. How do all these tools work together?

Given below is a generic logical flow where everything gets automated for seamless delivery. However, this flow may vary from organization to organization as per the requirement.

  1. Developers develop the code and this source code is managed by Version Control System tools like Git etc.
  2. Developers send this code to the Git repository and any changes made in the code is committed to this Repository.
  3. Jenkins pulls this code from the repository using the Git plugin and build it using tools like Ant or Maven.
  4. Configuration management tools like puppet deploys & provisions testing environment and then Jenkins releases this code on the test environment on which testing is done using tools like selenium.
  5. Once the code is tested, Jenkins send it for deployment on the production server (even production server is provisioned & maintained by tools like puppet).
  6. After deployment It is continuously monitored by tools like Nagios.
  7. Docker containers provides testing environment to test the build features.

devops tools - devops interview questions

Q6. What are the advantages of DevOps?

For this answer, you can use your past experience and explain how DevOps helped you in your previous job. If you don’t have any such experience, then you can mention the below advantages.

Technical benefits:

  • Continuous software delivery
  • Less complex problems to fix
  • Faster resolution of problems

Business benefits:

  • Faster delivery of features
  • More stable operating environments
  • More time available to add value (rather than fix/maintain)

Q7. Mention some of the core benefits of DevOps?

  • Faster development of software and quick deliveries.
  • DevOps methodology is flexible and adaptable to changes easily.
  • Compared to the previous software development models confusion about the project is decreased due to which the product quality is increased.
  • The gap between the development team and operation team is bridged. i.e, the communication between the teams has been increased.
  • Efficiency is increased by the addition of automation of continuous integration and continuous deployment.
  • Customer satisfaction is enhanced.

Q8. What is the most important thing DevOps helps us achieve?

According to me, the most important thing that DevOps helps us achieve is to get the changes into production as quickly as possible while minimizing risks in software quality assurance and compliance. This is the primary objective of DevOps. 
However, you can add many other positive effects of DevOps. For example, clearer communication and better working relationships between teams i.e. both the Ops team and Dev team collaborate together to deliver good quality software which in turn leads to higher customer satisfaction.

Q9. Explain with a use case where DevOps can be used in industry/ real-life.

There are many industries that are using DevOps so you can mention any of those use cases, you can also refer the below example:
Etsy is a peer-to-peer e-commerce website focused on handmade or vintage items and supplies, as well as unique factory-manufactured items. Etsy struggled with slow, painful site updates that frequently caused the site to go down. It affected sales for millions of Etsy’s users who sold goods through online market place and risked driving them to the competitor.
With the help of a new technical management team, Etsy transitioned from its waterfall model, which produced four-hour full-site deployments twice weekly, to a more agile approach. Today, it has a fully automated deployment pipeline, and its continuous delivery practices have reportedly resulted in more than 50 deployments a day with fewer disruptions.

Q10. Explain your understanding and expertise on both the software development side and the technical operations side of an organization you have worked with in the past.

For this answer, share your past experience and try to explain how flexible you were in your previous job. You can refer the below example:
DevOps engineers almost always work in a 24/7 business-critical online environment. I was adaptable to on-call duties and was available to take up real-time, live-system responsibility. I successfully automated processes to support continuous software deployments. I have experience with public/private clouds, tools like Chef or Puppet, scripting and automation with tools like Python and PHP, and a background in Agile.

Q11. What are the anti-patterns of DevOps?

A pattern is common usage usually followed. If a pattern commonly adopted by others does not work for your organization and you continue to blindly follow it, you are essentially adopting an anti-pattern. There are myths about DevOps. Some of them include:

  • DevOps is a process
  • Agile equals DevOps?
  • We need a separate DevOps group
  • Devops will solve all our problems
  • DevOps means Developers Managing Production
  • DevOps is Development-driven release management
    1. DevOps is not development driven.
    2. DevOps is not IT Operations driven.
  • We can’t do DevOps – We’re Unique
  • We can’t do DevOps – We’ve got the wrong people

Q12. Explain the different phases in DevOps methodology?

The various phases of the DevOps lifecycle are as follows:

  • Plan – In this stage, all the requirements of the project and everything regarding the project like time for each stage, cost, etc are discussed. This will help everyone in the team to get a brief idea about the project.
  • Code – The code is written over here according to the client’s requirements. Here codes are written in the form of small codes called units.
  • Build – Building of the units is done in this step.
  • Test – Testing is done in this stage and if there are mistakes found it is returned for re-build.
  • Integrate – All the units of the codes are integrated into this step.
  • Deploy – codeDevOpsNow is deployed in this step on the client’s environment.
  • Operate – Operations are performed on the code if required.
  • Monitor – Monitoring of the application is done over here in the client’s environment.

Q13. Explain your understanding and expertise on both the software development side and the technical operations side of an organization you have worked with in the past?

  • Deployment frequency: This measures how frequently a new feature is deployed.
  • Change failure rate: This is used to measure the number of failures in deployment.
  • Mean Time to Recovery (MTTR): The time is taken to recover from a failed deployment.

Q14. What are the KPIs that are used for gauging the success of a DevOps team?

KPI Means Key Performance Indicators are used to measure the performance of a DevOps team, identify mistakes and rectify them. This helps the DevOps team to increase productivity and which directly impacts revenue.

There are many KPIs which one can track in a DevOps team. Following are some of them:

  • Change Failure rates: This is used to measure the number of failures in deployments.
  • Meantime to recovery (MTTR): The time is taken to recover from a failed deployment.
  • Lead time: This helps to measure the time taken to deploy on the production environment.
  • Deployment frequency: This measures how frequently a new feature is deployed.
  • Change volume: This is used to measure how much code is changed from the existing code.
  • Cycle time: This is used to measure total application development time.
  • Customer Ticket: This helps us to measure the number of errors detected by the end-user.
  • Availability: This is used to determine the downtime of the application.
  • Defect escape rate: This helps us to measure the number of issues that are needed to be detected as early as possible.
  • Time of detection: This helps you understand whether your response time and application monitoring processes are functioning correctly.

Q15. Why has DevOps become famous?

As we know before DevOps there are two other software development models:

  • Waterfall model
  • Agile model

In the waterfall model, we have limitations of one-way working and lack of communication with customers. This was overcome in Agile by including the communication between the customer and the company by taking feedback. But in this model, another issue is faced regarding communication between the Development team and operations team due to which there is a delay in the speed of production. This is where DevOps is introduced. It bridges the gap between the development team and the operation team by including the automation feature. Due to this, the speed of production is increased. By including automation, testing is integrated into the development stage. Which resulted in finding the bugs at the very initial stage which increased the speed and efficiency.

Q16. How does AWS contribute to DevOps?

AWS [Amazon Web Services ] is one of the famous cloud providers. In AWS DevOps is provided with some benefits:

  • Flexible Resources: AWS provides all the DevOps resources which are flexible to use.
  • Scaling: we can create several instances on AWS with a lot of storage and computation power.
  • Automation: Automation is provided by AWS like CI/CD
  • Security: AWS provides security when we create an instance like IAM

Version Control System (VCS) Interview Questions – DevOps Interview Questions

Now let’s look at some interview questions on VCS. 

Q1. What is Version control?

This is probably the easiest question you will face in the interview. My suggestion is to first give a definition of Version control. It is a system that records changes to a file or set of files over time so that you can recall specific versions later. Version control systems consist of a central shared repository where teammates can commit changes to a file or set of file. Then you can mention the uses of version control.

Version control allows you to:

  • Revert files back to a previous state.
  • Revert the entire project back to a previous state.
  • Compare changes over time.
  • See who last modified something that might be causing a problem.
  • Who introduced an issue and when.

Q2. What are the benefits of using version control?

I will suggest you to include the following advantages of version control:

  1. With Version Control System (VCS), all the team members are allowed to work freely on any file at any time. VCS will later allow you to merge all the changes into a common version.
  2. All the past versions and variants are neatly packed up inside the VCS. When you need it, you can request any version at any time and you’ll have a snapshot of the complete project right at hand.
  3. Every time you save a new version of your project, your VCS requires you to provide a short description of what was changed. Additionally, you can see what exactly was changed in the file’s content. This allows you to know who has made what change in the project.
  4. A distributed VCS like Git allows all the team members to have complete history of the project so if there is a breakdown in the central server you can use any of your teammate’s local Git repository.

Q3. Describe branching strategies you have used.

This question is asked to test your branching experience so tell them about how you have used branching in your previous job and what purpose does it serves, you can refer the below points:

  • Feature branching
    A feature branch model keeps all of the changes for a particular feature inside of a branch. When the feature is fully tested and validated by automated tests, the branch is then merged into master.
  • Task branching
    In this model each task is implemented on its own branch with the task key included in the branch name. It is easy to see which code implements which task, just look for the task key in the branch name.
  • Release branching
    Once the develop branch has acquired enough features for a release, you can clone that branch to form a Release branch. Creating this branch starts the next release cycle, so no new features can be added after this point, only bug fixes, documentation generation, and other release-oriented tasks should go in this branch. Once it is ready to ship, the release gets merged into master and tagged with a version number. In addition, it should be merged back into develop branch, which may have progressed since the release was initiated.

In the end tell them that branching strategies varies from one organization to another, so I know basic branching operations like delete, merge, checking out a branch etc.

Q4. Which VCS tool you are comfortable with?

You can just mention the VCS tool that you have worked on like this: “I have worked on Git and one major advantage it has over other VCS tools like SVN is that it is a distributed version control system.”
Distributed VCS tools do not necessarily rely on a central server to store all the versions of a project’s files. Instead, every developer “clones” a copy of a repository and has the full history of the project on their own hard drive.

Q5. What is Git?

I will suggest that you attempt this question by first explaining about the architecture of git as shown in the below diagram. You can refer to the explanation given below:

  • Git is a Distributed Version Control system (DVCS). It can track changes to a file and allows you to revert back to any particular change.
  • Its distributed architecture provides many advantages over other Version Control Systems (VCS) like SVN one major advantage is that it does not rely on a central server to store all the versions of a project’s files. Instead, every developer “clones” a copy of a repository I have shown in the diagram below with “Local repository” and has the full history of the project on his hard drive so that when there is a server outage, all you need for recovery is one of your teammate’s local Git repository.
  • There is a central cloud repository as well where developers can commit changes and share it with other teammates as you can see in the diagram where all collaborators are commiting changes “Remote repository”.

git architecture - devops interview questions

Q6. Explain some basic Git commands?

Below are some basic Git commands:

git commands - devops interview questions

Q7. In Git how do you revert a commit that has already been pushed and made public?

There can be two answers to this question so make sure that you include both because any of the below options can be used depending on the situation:

  • Remove or fix the bad file in a new commit and push it to the remote repository. This is the most natural way to fix an error. Once you have made necessary changes to the file, commit it to the remote repository for that I will use
    git commit -m “commit message”
  • Create a new commit that undoes all changes that were made in the bad commit.to do this I will use a command
    git revert <name of bad commit>

Q8. How do you squash last N commits into a single commit?

There are two options to squash last N commits into a single commit. Include both of the below mentioned options in your answer:

  • If you want to write the new commit message from scratch use the following command
    git reset –soft HEAD~N &&
    git commit
  • If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use
    git reset –soft HEAD~N &&
    git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”

Q9. What is Git bisect? How can you use it to determine the source of a (regression) bug?

I will suggest you to first give a small definition of Git bisect, Git bisect is used to find the commit that introduced a bug by using binary search. Command for Git bisect is
git bisect <subcommand> <options>
Now since you have mentioned the command above, explain what this command will do, This command uses a binary search algorithm to find which commit in your project’s history introduced a bug. You use it by first telling it a “bad” commit that is known to contain the bug, and a “good” commit that is known to be before the bug was introduced. Then Git bisect picks a commit between those two endpoints and asks you whether the selected commit is “good” or “bad”. It continues narrowing down the range until it finds the exact commit that introduced the change.

Q10. What is Git rebase and how can it be used to resolve conflicts in a feature branch before merge?

According to me, you should start by saying git rebase is a command which will merge another branch into the branch where you are currently working, and move all of the local commits that are ahead of the rebased branch to the top of the history on that branch.
Now once you have defined Git rebase time for an example to show how it can be used to resolve conflicts in a feature branch before merge, if a feature branch was created from master, and since then the master branch has received new commits, Git rebase can be used to move the feature branch to the tip of master.
The command effectively will replay the changes made in the feature branch at the tip of master, allowing conflicts to be resolved in the process. When done with care, this will allow the feature branch to be merged into master with relative ease and sometimes as a simple fast-forward operation.

Q11. How do you configure a Git repository to run code sanity checking tools right before making commits, and preventing them if the test fails?

I will suggest you to first give a small introduction to sanity checking, A sanity or smoke test determines whether it is possible and reasonable to continue testing.
Now explain how to achieve this, this can be done with a simple script related to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script one can run other tools, such as linters and perform sanity checks on the changes being committed into the repository.
Finally give an example, you can refer the below script:

#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$')
if [ -z files ]; then
exit 0
fi
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
fi
echo “Some .go files are not fmt’d”
exit 1</p>
<p style="text-align: justify;"><span>

This script checks to see if any .go file that is about to be committed needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.

Q12. How do you find a list of files that has changed in a particular commit?

For this answer instead of just telling the command, explain what exactly this command will do so you can say that, To get a list files that has changed in a particular commit use command
git diff-tree -r {hash}
Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.
You can also include the below mention point although it is totally optional but will help in impressing the interviewer.
The output will also include some extra information, which can be easily suppressed by including two flags:
git diff-tree –no-commit-id –name-only -r {hash}
Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the file names, instead of their paths.

Q13. How do you setup a script to run every time a repository receives new commits through push?

There are three ways to configure a script to run every time a repository receives new commits through push, one needs to define either a pre-receive, update, or a post-receive hook depending on when exactly the script needs to be triggered.

  • Pre-receive hook in the destination repository is invoked when commits are pushed to it. Any script bound to this hook will be executed before any references are updated. This is a useful hook to run scripts that help enforce development policies.
  • Update hook works in a similar manner to pre-receive hook, and is also triggered before any updates are actually made. However, the update hook is called once for every commit that has been pushed to the destination repository.
  • Finally, post-receive hook in the repository is invoked after the updates have been accepted into the destination repository. This is an ideal place to configure simple deployment scripts, invoke some continuous integration systems, dispatch notification emails to repository maintainers, etc.

Hooks are local to every Git repository and are not versioned. Scripts can either be created within the hooks directory inside the “.git” directory, or they can be created elsewhere and links to those scripts can be placed within the directory.

Q14. How will you know in Git if a branch has already been merged into master?

I will suggest you to include both the below mentioned commands:
git branch –merged lists the branches that have been merged into the current branch.
git branch –no-merged lists the branches that have not been merged.

Q15. Explain the difference between a centralized and distributed version control system (VCS).?

Centralized Version ControlDistributed Version control
In this, we will not be having a copy of the main repository on the local repository.In this, all the developers will be having a copy of the main repository on their local repository
There is a need for the internet for accessing the main repository data because we will not be having another copy of the server on the local repositoryThere is no need for the internet for accessing the main repository data because we will be having another copy of the server on the local repository.
If the main server crashes then there will be a problem in accessing the server for the developers.If there is a crash on the main server there will be no problem faced regarding the availability of the server.

Q16. What is the difference between Git Merge and Git Rebase?

Here, both are merging mechanisms but the difference between the Git Merge and Git Rebase is, in Git Merge logs will be showing the complete history of commits.

However, when one does Git Rebase, the logs are rearranged. The rearrangement is done to make the logs look linear and simple to understand. This is also a drawback since other team members will not understand how the different commits were merged into one another.

Q17. Explain the difference between git fetch and git pull?

Git PullGit Fetch
DevOps is used to update the working directory with the latest changes from the remote server.Git fetch gets new data from a remote repository to the local repository
Git pull is used to get the data to the local repository and the data is merged in the working repositoryGit fetch is only used to get the data to the local repository but the data is not merged in the working repository
Command – git pull originCommand – git fetch origin

Q18. Can you explain the “Shift left to reduce failure” concept in DevOps?

Shift left is a concept used in DevOps for a better level of security, performance, etc. Let us get in detail with an example, if we see all the phases in DevOps we can say that security is tested before the step of deployment. By using the left shift method we can include the security in the development phase which is on the left.[will be shown in the diagram] not only in development we can integrate with all phases like before development and in the testing phase too. This probably increases the level of security by finding the errors in the very initial stages.

Continuous Integration Interview Questions – DevOps Interview Questions

Now, let’s look at Continuous Integration interview questions:

Q1. What is meant by Continuous Integration?

I will advise you to begin this answer by giving a small definition of Continuous Integration (CI). It is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
I suggest that you explain how you have implemented it in your previous job. You can refer the below given example:

Jenkins standalone architecture - devops interview questions

In the diagram shown above:

  1. Developers check out code into their private workspaces.
  2. When they are done with it they commit the changes to the shared repository (Version Control Repository).
  3. The CI server monitors the repository and checks out changes when they occur.
  4. The CI server then pulls these changes and builds the system and also runs unit and integration tests.
  5. The CI server will now inform the team of the successful build.
  6. If the build or tests fails, the CI server will alert the team.
  7. The team will try to fix the issue at the earliest opportunity.
  8. This process keeps on repeating.

Q2. Why do you need a Continuous Integration of Dev & Testing?

For this answer, you should focus on the need of Continuous Integration. My suggestion would be to mention the below explanation in your answer:
Continuous Integration of Dev and Testing improves the quality of software, and reduces the time taken to deliver it, by replacing the traditional practice of testing after completing all development. It allows Dev team to easily detect and locate problems early because developers need to integrate code into a shared repository several times a day (more frequently). Each check-in is then automatically tested.

Q3. What are the success factors for Continuous Integration?

Here you have to mention the requirements for Continuous Integration. You could include the following points in your answer:

  • Maintain a code repository
  • Automate the build
  • Make the build self-testing
  • Everyone commits to the baseline every day
  • Every commit (to baseline) should be built
  • Keep the build fast
  • Test in a clone of the production environment
  • Make it easy to get the latest deliverables
  • Everyone can see the results of the latest build
  • Automate deployment

Q4. Explain how you can move or copy Jenkins from one server to another?

I will approach this task by copying the jobs directory from the old server to the new one. There are multiple ways to do that; I have mentioned them below:
You can:

  • Move a job from one installation of Jenkins to another by simply copying the corresponding job directory.
  • Make a copy of an existing job by making a clone of a job directory by a different name.
  • Rename an existing job by renaming a directory. Note that if you change a job name you will need to change any other job that tries to call the renamed job.

Q5. Explain how can create a backup and copy files in Jenkins?

Answer to this question is really direct. To create a backup, all you need to do is to periodically back up your JENKINS_HOME directory. This contains all of your build jobs configurations, your slave node configurations, and your build history. To create a back-up of your Jenkins setup, just copy this directory. You can also copy a job directory to clone or replicate a job or rename the directory.

Q6. Explain how you can setup Jenkins job?

My approach to this answer will be to first mention how to create Jenkins job. Go to Jenkins top page, select “New Job”, then choose “Build a free-style software project”.
Then you can tell the elements of this freestyle job:

  • Optional SCM, such as CVS or Subversion where your source code resides.
  • Optional triggers to control when Jenkins will perform builds.
  • Some sort of build script that performs the build (ant, maven, shell script, batch file, etc.) where the real work happens.
  • Optional steps to collect information out of the build, such as archiving the artifacts and/or recording javadoc and test results.
  • Optional steps to notify other people/systems with the build result, such as sending e-mails, IMs, updating issue tracker, etc..

Q7. Mention some of the useful plugins in Jenkins.

Below, I have mentioned some important Plugins:

  • Maven 2 project
  • Amazon EC2
  • HTML publisher
  • Copy artifact
  • Join
  • Green Balls

These Plugins, I feel are the most useful plugins. If you want to include any other Plugin that is not mentioned above, you can add them as well. But, make sure you first mention the above stated plugins and then add your own.

Q8. How will you secure Jenkins?

The way I secure Jenkins is mentioned below. If you have any other way of doing it, please mention it in the comments section below:

  • Ensure global security is on.
  • Ensure that Jenkins is integrated with my company’s user directory with appropriate plugin.
  • Ensure that matrix/Project matrix is enabled to fine tune access.
  • Automate the process of setting rights/privileges in Jenkins with custom version controlled script.
  • Limit physical access to Jenkins data/folders.
  • Periodically run security audits on same.

Jenkins is one of the many popular tools that are used extensively in DevOps. Edureka’s DevOps Certification course will provide you hands-on training with Jenkins and high quality guidance from industry experts. Give it a look:

Q9. What is the Blue/Green Deployment Pattern?

This is a continuous deployment strategy that is generally used to decrease downtime. This is used for transferring the traffic from one instance to another.

For Example, let us take a situation where we want to include a new version of code. Now we have to replace the old version with a new version of code. The old version is considered to be in a blue environment and the new version is considered as a green environment. we had made some changes to the existing old version which transformed into a new version with minimum changes.
Now to run the new version of the instance we need to transfer the traffic from the old instance to the new instance. That means we need to transfer the traffic from the blue environment to the green environment. The new version will be running on the green instance. Gradually the traffic is transferred to the green instance. The blue instance will be kept on idle and used for the rollback.

In Blue-Green deployment, the application is not deployed in the same environment. Instead, a new server or environment is created where the new version of the application is deployed.

Once the new version of the application is deployed in a separate environment, the traffic to the old version of the application is redirected to the new version of the application.

We follow the Blue-Green Deployment model, so that any problem which is encountered in the production environment for the new application if detected. The traffic can be immediately redirected to the previous Blue environment, with minimum or no impact on the business. Following diagram shows, Blue-Green Deployment.

Q10. Explain how you can setup Jenkins job?

A pattern can be defined as an ideology on how to solve a problem. Now anti-pattern can be defined as a method that would help us to solve the problem now but it may result in damaging our system [i.e, it shows how not to approach a problem ].

Some of the anti-patterns we see in DevOps are:

  • DevOps is a process and not a culture.
  • DevOps is nothing but Agile.
  • There should be a separate DevOps group.
  • DevOps solves every problem.
  • DevOps equates to developers running a production environment.
  • DevOps follows Development-driven management
  • DevOps does not focus much on development.
  • As we are a unique organization, we don’t follow the masses and hence we won’t implement DevOps.
  • We don’t have the right set of people, hence we cant implement DevOps culture.

Q11. How will you approach a project that needs to implement DevOps?

First, if we want to approach a project that needs DevOps, we need to know a few concepts like :

  • Any programming language [C, C++, JAVA, Python..] concerning the project.
  • Get an idea of operating systems for management purposes [like memory management, disk management..etc].
  • Get an idea about networking and security concepts.
  • Get the idea about what DevOps is, what is continuous integration, continuous development, continuous delivery, continuous deployment, monitoring, and its tools used in various phases.[like GIT, Docker, Jenkins,…etc]
  • Now after this interact with other teams and design a roadmap for the process.
  • Once all teams get cleared then create a proof of concept and start according to the project plan.
  • Now the project is ready to go through the phases of DevOps.Version control, integration, testing, deployment, delivery, and monitoring.

Continuous Testing Interview Questions – DevOps Interview Questions

Now let’s move on to the Continuous Testing questions.

Q1. What is Continuous Testing?

I will advise you to follow the below mentioned explanation:
Continuous Testing is the process of executing automated tests as part of the software delivery pipeline to obtain immediate feedback on the business risks associated with in the latest build. In this way, each build is tested continuously, allowing Development teams to get fast feedback so that they can prevent those problems from progressing to the next stage of Software delivery life-cycle. This dramatically speeds up a developer’s workflow as there’s no need to manually rebuild the project and re-run all tests after making changes.

Q2. What is Automation Testing?

Automation testing or Test Automation is a process of automating the manual process to test the application/system under test. Automation testing involves use of separate testing tools which lets you create test scripts which can be executed repeatedly and doesn’t require any manual intervention.

Q3. What are the benefits of Automation Testing?

I have listed down some advantages of automation testing. Include these in your answer and you can add your own experience of how Continuous Testing helped your previous company:

  • Supports execution of repeated test cases
  • Aids in testing a large test matrix
  • Enables parallel execution
  • Encourages unattended execution
  • Improves accuracy thereby reducing human generated errors
  • Saves time and money

Q4. How to automate Testing in DevOps lifecycle?

I have mentioned a generic flow below which you can refer to:
In DevOps, developers are required to commit all the changes made in the source code to a shared repository. Continuous Integration tools like Jenkins will pull the code from this shared repository every time a change is made in the code and deploy it for Continuous Testing that is done by tools like Selenium as shown in the below diagram.
In this way, any change in the code is continuously tested unlike the traditional approach.

automate testing - devops interview questions

Q5. Why is Continuous Testing important for DevOps?

You can answer this question by saying, “Continuous Testing allows any change made in the code to be tested immediately. This avoids the problems created by having “big-bang” testing left to the end of the cycle such as release delays and quality issues. In this way, Continuous Testing facilitates more frequent and good quality releases.”

Q6. What are the key elements of Continuous Testing tools?

Key elements of Continuous Testing are:

  • Risk Assessment: It Covers risk mitigation tasks, technical debt, quality assessment and test coverage optimization to ensure the build is ready to progress toward next stage.
  • Policy Analysis: It ensures all processes align with the organization’s evolving business and compliance demands are met.
  • Requirements Traceability: It ensures true requirements are met and rework is not required. An object assessment is used to identify which requirements are at risk, working as expected or require further validation.
  • Advanced Analysis: It uses automation in areas such as static code analysis, change impact analysis and scope assessment/prioritization to prevent defects in the first place and accomplishing more within each iteration.
  • Test Optimization: It ensures tests yield accurate outcomes and provide actionable findings. Aspects include Test Data Management, Test Optimization Management and Test Maintenance
  • Service Virtualization: It ensures access to real-world testing environments. Service visualization enables access to the virtual form of the required testing stages, cutting the waste time to test environment setup and availability.

Q7. Which Testing tool are you comfortable with and what are the benefits of that tool?

Here mention the testing tool that you have worked with and accordingly frame your answer. I have mentioned an example below:
I have worked on Selenium to ensure high quality and more frequent releases.

Some advantages of Selenium are:

  • It is free and open source
  • It has a large user base and helping communities
  • It has cross Browser compatibility (Firefox, chrome, Internet Explorer, Safari etc.)
  • It has great platform compatibility (Windows, Mac OS, Linux etc.)
  • It supports multiple programming languages (Java, C#, Ruby, Python, Pearl etc.)
  • It has fresh and regular repository developments
  • It supports distributed testing

Q8. What are the Testing types supported by Selenium?

Selenium supports two types of testing:
Regression Testing: It is the act of retesting a product around an area where a bug was fixed.
Functional Testing: It refers to the testing of software features (functional points) individually.

Q9. What is Selenium IDE?

My suggestion is to start this answer by defining Selenium IDE. It is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests. Selenium IDE includes the entire Selenium Core, allowing you to easily and quickly record and play back tests in the actual environment that they will run in.
Now include some advantages in your answer. With autocomplete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer.

Q10. What is the difference between Assert and Verify commands in Selenium?

I have mentioned differences between Assert and Verify commands below:

  • Assert command checks whether the given condition is true or false. Let’s say we assert whether the given element is present on the web page or not. If the condition is true, then the program control will execute the next test step. But, if the condition is false, the execution would stop and no further test would be executed.
  • Verify command also checks whether the given condition is true or false. Irrespective of the condition being true or false, the program execution doesn’t halts i.e. any failure during verification would not stop the execution and all the test steps would be executed.

Q11. How to launch Browser using WebDriver?

The following syntax can be used to launch Browser:
WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();

Q12. When should I use Selenium Grid?

For this answer, my suggestion would be to give a small definition of Selenium Grid. It can be used to execute same or different test scripts on multiple platforms and browsers concurrently to achieve distributed test execution. This allows testing under different environments and saving execution time remarkably.

Learn Automation testing and other DevOps concepts in live instructor-led online classes in our DevOps Certification course.

Q13. Can you differentiate between continuous testing and automation testing?

Continuous TestingAutomation Testing
Continuous Testing is a process that involves executing all the automated test cases as a part of the software delivery pipelineAutomation testing is a process tool that involves testing the code repetitively without manual intervention.
This process mainly focuses on business risks.This process mainly focuses on a bug-free environment.
It’s comparatively slow than automation testingIt’s comparatively faster than continuous testing.

 

Configuration Management Interview Questions – DevOps Interview Questions

Now let’s check how much you know about Configuration Management.

Q1. What are the goals of Configuration management processes?

The purpose of Configuration Management (CM) is to ensure the integrity of a product or system throughout its life-cycle by making the development or deployment process controllable and repeatable, therefore creating a higher quality product or system. The CM process allows orderly management of system information and system changes for purposes such as to:

  • Revise capability,
  • Improve performance,
  • Reliability or maintainability,
  • Extend life,
  • Reduce cost,
  • Reduce risk and
  • Liability, or correct defects.

Q2. What is the difference between Asset Management and Configuration Management?

Given below are few differences between Asset Management and Configuration Management:

asset management configuration management - devops interview questions

Q3. What is the difference between an Asset and a Configuration Item?

According to me, you should first explain Asset. It has a financial value along with a depreciation rate attached to it. IT assets are just a sub-set of it. Anything and everything that has a cost and the organization uses it for its asset value calculation and related benefits in tax calculation falls under Asset Management, and such item is called an asset.
Configuration Item on the other hand may or may not have financial values assigned to it. It will not have any depreciation linked to it. Thus, its life would not be dependent on its financial value but will depend on the time till that item becomes obsolete for the organization.

Now you can give an example that can showcase the similarity and differences between both:
1) Similarity:
Server – It is both an asset as well as a CI.
2) Difference:
Building – It is an asset but not a CI.
Document – It is a CI but not an asset

Q4. What do you understand by “Infrastructure as code”? How does it fit into the DevOps methodology? What purpose does it achieve?

Infrastructure as Code (IAC) is a type of IT infrastructure that operations teams can use to automatically manage and provision through code, rather than using a manual process.
Companies for faster deployments treat infrastructure like software: as code that can be managed with the DevOps tools and processes. These tools let you make infrastructure changes more easily, rapidly, safely and reliably.

Q5. Which among Puppet, Chef, SaltStack and Ansible is the best Configuration Management (CM) tool? Why?

This depends on the organization’s need so mention few points on all those tools:
Puppet is the oldest and most mature CM tool. Puppet is a Ruby-based Configuration Management tool, but while it has some free features, much of what makes Puppet great is only available in the paid version. Organizations that don’t need a lot of extras will find Puppet useful, but those needing more customization will probably need to upgrade to the paid version.
Chef is written in Ruby, so it can be customized by those who know the language. It also includes free features, plus it can be upgraded from open source to enterprise-level if necessary. On top of that, it’s a very flexible product.
Ansible is a very secure option since it uses Secure Shell. It’s a simple tool to use, but it does offer a number of other services in addition to configuration management. It’s very easy to learn, so it’s perfect for those who don’t have a dedicated IT staff but still need a configuration management tool.
SaltStack is python based open source CM tool made for larger businesses, but its learning curve is fairly low.

Q6. What is Puppet?

I will advise you to first give a small definition of Puppet. It is a Configuration Management tool which is used to automate administration tasks.
Now you should describe its architecture and how Puppet manages its Agents. Puppet has a Master-Slave architecture in which the Slave has to first send a Certificate signing request to Master and Master has to sign that Certificate in order to establish a secure connection between Puppet Master and Puppet Slave as shown on the diagram below. Puppet Slave sends request to Puppet Master and Puppet Master then pushes configuration on Slave.
Refer the diagram below that explains the above description.

what is puppet - devops interview questions

Q7. Before a client can authenticate with the Puppet Master, its certs need to be signed and accepted. How will you automate this task?

The easiest way is to enable auto-signing in puppet.conf.
Do mention that this is a security risk. If you still want to do this:

  • Firewall your puppet master – restrict port tcp/8140 to only networks that you trust.
  • Create puppet masters for each ‘trust zone’, and only include the trusted nodes in that Puppet masters manifest.
  • Never use a full wildcard such as *.

Q8. Describe the most significant gain you made from automating a process through Puppet.

For this answer, I will suggest you to explain you past experience with Puppet. you can refer the below example:
I automated the configuration and deployment of Linux and Windows machines using Puppet. In addition to shortening the processing time from one week to 10 minutes, I used the roles and profiles pattern and documented the purpose of each module in README to ensure that others could update the module using Git. The modules I wrote are still being used, but they’ve been improved by my teammates and members of the community

Q9. Which open source or community tools do you use to make Puppet more powerful?

Over here, you need to mention the tools and how you have used those tools to make Puppet more powerful. Below is one example for your reference:
Changes and requests are ticketed through Jira and we manage requests through an internal process. Then, we use Git and Puppet’s Code Manager app to manage Puppet code in accordance with best practices. Additionally, we run all of our Puppet changes through our continuous integration pipeline in Jenkins using the beaker testing framework.

 

Q10. What are Puppet Manifests?

It is a very important question so make sure you go in a correct flow. According to me, you should first define Manifests. Every node (or Puppet Agent) has got its configuration details in Puppet Master, written in the native Puppet language. These details are written in the language which Puppet can understand and are termed as Manifests. They are composed of Puppet code and their filenames use the .pp extension.
Now give an exampl. You can write a manifest in Puppet Master that creates a file and installs apache on all Puppet Agents (Slaves) connected to the Puppet Master.

Q11. What is Puppet Module and How it is different from Puppet Manifest?

For this answer, you can go with the below mentioned explanation:
A Puppet Module is a collection of Manifests and data (such as facts, files, and templates), and they have a specific directory structure. Modules are useful for organizing your Puppet code, because they allow you to split your code into multiple Manifests. It is considered best practice to use Modules to organize almost all of your Puppet Manifests.
Puppet programs are called Manifests which are composed of Puppet code and their file names use the .pp extension.

Q12. What is Facter in Puppet?

You are expected to answer what exactly Facter does in Puppet so according to me, you should say, “Facter gathers basic information (facts) about Puppet Agent such as hardware details, network settings, OS type and version, IP addresses, MAC addresses, SSH keys, and more. These facts are then made available in Puppet Master’s Manifests as variables.”

Q13. What is Chef?

Begin this answer by defining Chef. It is a powerful automation platform that transforms infrastructure into code. Chef is a tool for which you write scripts that are used to automate processes. What processes? Pretty much anything related to IT.
Now you can explain the architecture of Chef, it consists of:

  • Chef Server: The Chef Server is the central store of your infrastructure’s configuration data. The Chef Server stores the data necessary to configure your nodes and provides search, a powerful tool that allows you to dynamically drive node configuration based on data.
  • Chef Node: A Node is any host that is configured using Chef-client. Chef-client runs on your nodes, contacting the Chef Server for the information necessary to configure the node. Since a Node is a machine that runs the Chef-client software, nodes are sometimes referred to as “clients”.
  • Chef Workstation: A Chef Workstation is the host you use to modify your cookbooks and other configuration data.

chef architecture - devops interview questions

Q14. What is a resource in Chef?

My suggestion is to first define Resource. A Resource represents a piece of infrastructure and its desired state, such as a package that should be installed, a service that should be running, or a file that should be generated.
You should explain about the functions of Resource for that include the following points:

  • Describes the desired state for a configuration item.
  • Declares the steps needed to bring that item to the desired state.
  • Specifies a resource type such as package, template, or service.
  • Lists additional details (also known as resource properties), as necessary.
  • Are grouped into recipes, which describe working configurations.

Q15. What do you mean by recipe in Chef?

For this answer, I will suggest you to use the above mentioned flow: first define Recipe. A Recipe is a collection of Resources that describes a particular configuration or policy. A Recipe describes everything that is required to configure part of a system.
After the definition, explain the functions of Recipes by including the following points:

  • Install and configure software components.
  • Manage files.
  • Deploy applications.
  • Execute other recipes.

Q16. How does a Cookbook differ from a Recipe in Chef?

The answer to this is pretty direct. You can simply say, “a Recipe is a collection of Resources, and primarily configures a software package or some piece of infrastructure. A Cookbook groups together Recipes and other information in a way that is more manageable than having just Recipes alone.”

Q17. What happens when you don’t specify a Resource’s action in Chef?

My suggestion is to first give a direct answer: when you don’t specify a resource’s action, Chef applies the default action.
Now explain this with an example, the below resource:
file ‘C:UsersAdministratorchef-reposettings.ini’ do
content ‘greeting=hello world’
end
is same as the below resource:
file ‘C:UsersAdministratorchef-reposettings.ini’ do
action :create
content ‘greeting=hello world’
end
because: create is the file Resource’s default action.

Q18. What is Ansible module?

Modules are considered to be the units of work in Ansible. Each module is mostly standalone and can be written in a standard scripting language such as Python, Perl, Ruby, bash, etc.. One of the guiding properties of modules is idempotency, which means that even if an operation is repeated multiple times e.g. upon recovery from an outage, it will always place the system into the same state.

Q19. What are playbooks in Ansible?

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process. Playbooks are designed to be human-readable and are developed in a basic text language.
At a basic level, playbooks can be used to manage configurations of and deployments to remote machines.

Q20. How do I see a list of all of the ansible_ variables?

Ansible by default gathers “facts” about the machines under management, and these facts can be accessed in Playbooks and in templates. To see a list of all of the facts that are available about a machine, you can run the “setup” module as an ad-hoc action:
Ansible -m setup hostname
This will print out a dictionary of all of the facts that are available for that particular host.

Q21. How can I set deployment order for applications?

WebLogic Server 8.1 allows you to select the load order for applications. See the Application MBean Load Order attribute in Application. WebLogic Server deploys server-level resources (first JDBC and then JMS) before deploying applications. Applications are deployed in this order: connectors, then EJBs, then Web Applications. If the application is an EAR, the individual components are loaded in the order in which they are declared in the application.xml deployment descriptor.

Q22. Can I refresh static components of a deployed application without having to redeploy the entire application?

Yes, you can use weblogic.Deployer to specify a component and target a server, using the following syntax:
java weblogic.Deployer -adminurl http://admin:7001 -name appname -targets server1,server2 -deploy jsps/*.jsp

Q23. How do I turn the auto-deployment feature off?

The auto-deployment feature checks the applications folder every three seconds to determine whether there are any new applications or any changes to existing applications and then dynamically deploys these changes.

The auto-deployment feature is enabled for servers that run in development mode. To disable auto-deployment feature, use one of the following methods to place servers in production mode:

  • In the Administration Console, click the name of the domain in the left pane, then select the Production Mode checkbox in the right pane.
  • At the command line, include the following argument when starting the domain’s Administration Server:
    -Dweblogic.ProductionModeEnabled=true
  • Production mode is set for all WebLogic Server instances in a given domain.

Q24. When should I use the external_stage option?

Set -external_stage using weblogic.Deployer if you want to stage the application yourself, and prefer to copy it to its target by your own means.

Ansible and Puppet are two of the most popular configuration management tools among DevOps engineers. Learn them and more in our DevOps Masters Program designed by industry experts to certify you as a DevOps Engineer!

Q25. What is the use of SSH?
 

Generally, SSH is used for connecting two computers and helps to work on them remotely. SSH is mostly used by the operations team as the operations team will be dealing with managing tasks with which they will require the admin system remotely. The developers will also be using SSH but comparatively less than the operations team as most of the time they will be working in the local systems. As we know, the DevOps development team and operation team will collaborate and work together.SSH will be used when the operations team faces any problem and needs some assistance from the development team then SSH is used.

Q26. Can you tell me something about Memcached?
 

Memcached is a Free & open-source, high-performance, distributed memory object caching system.

This is generally used in the management of memory in dynamic web applications by caching the data in RAM. This helps to reduce the frequency of fetching from external sources. This also helps in speeding up the dynamic web applications by alleviating database load.

Conclusion: DevOps is a culture of collaboration between the Development team and operation team to work together to bring out an efficient and fast software product. So these are a few top DevOps interview questions that are covered in this blog. This blog will be helpful to prepare for a DevOps interview.

Q27. Do you know about post mortem meetings in DevOps?
 

By the name, we can say it is a type of meeting which is conducted at the end of the project. In this meeting, all the teams come together and discuss the failures in the current project. Finally, they will conclude how to avoid them and what measures need to be taken in the future to avoid these failures.

Q28. What does CAMS stand for in DevOps?
 

In DevOps, CAMS stands for Culture, Automation, Measurement, and Sharing.

  • Culture: Culture is like a base of DevOps. It is implementing all the operations between the operations team and development team in a particular manner to make things comfortable for the completion of the software production.
  • Automation: Automation is one of the key features of DevOps. It is used to reduce the time gap between the processes like testing and deployment. In the normal software development methods, we can see only one team will be working simultaneously. But in DevOps, we can see all the teams working together as there is an implementation of automation. All the changes made were reflected in the other teams to work on.
  • Measurement: Measurement in the CAMS model of DevOps is related to measuring crucial factors in the software development process, which can indicate the overall performance of the team. Measuring factors such as income, costs, revenue, mean time between failures, etc. The most crucial aspect of Measurement is to pick the right metrics to track. At the same time, to push the team for better performance, one also needs to incentivize the correct metrics.
  • Sharing: This Sharing Culture plays a key role in DevOps as this helps in sharing the knowledge with other people of the team. This helps in the increase of people who know DevOps.This culture can be increased by involving Q and A sessions with teams regularly so that all the people will be giving their insight about the problem faced and it can be solved quickly by which we can gain the knowledge.

Continuous Monitoring Interview Questions – DevOps Interview Questions

Let’s test your knowledge on Continuous Monitoring.

Q1. Why is Continuous monitoring necessary?

I will suggest you to go with the below mentioned flow:
Continuous Monitoring allows timely identification of problems or weaknesses and quick corrective action that helps reduce expenses of an organization. Continuous monitoring provides solution that addresses three operational disciplines known as:

  • continuous audit
  • continuous controls monitoring
  • continuous transaction inspection

Q2. What is Nagios?

You can answer this question by first mentioning that Nagios is one of the monitoring tools. It is used for Continuous monitoring of systems, applications, services, and business processes etc in a DevOps culture. In the event of a failure, Nagios can alert technical staff of the problem, allowing them to begin remediation processes before outages affect business processes, end-users, or customers. With Nagios, you don’t have to explain why an unseen infrastructure outage affect your organization’s bottom line.
Now once you have defined what is Nagios, you can mention the various things that you can achieve using Nagios.
By using Nagios you can:

  • Plan for infrastructure upgrades before outdated systems cause failures.
  • Respond to issues at the first sign of a problem.
  • Automatically fix problems when they are detected.
  • Coordinate technical team responses.
  • Ensure your organization’s SLAs are being met.
  • Ensure IT infrastructure outages have a minimal effect on your organization’s bottom line.
  • Monitor your entire infrastructure and business processes.

This completes the answer to this question. Further details like advantages etc. can be added as per the direction where the discussion is headed.

Q3. How does Nagios works?

I will advise you to follow the below explanation for this answer:
Nagios runs on a server, usually as a daemon or service. Nagios periodically runs plugins residing on the same server, they contact hosts or servers on your network or on the internet. One can view the status information using the web interface. You can also receive email or SMS notifications if something happens.
The Nagios daemon behaves like a scheduler that runs certain scripts at certain moments. It stores the results of those scripts and will run other scripts if these results change.

Now expect a few questions on Nagios components like Plugins, NRPE etc..

Q4. What are Plugins in Nagios?

Begin this answer by defining Plugins. They are scripts (Perl scripts, Shell scripts, etc.) that can run from a command line to check the status of a host or service. Nagios uses the results from Plugins to determine the current status of hosts and services on your network.
Once you have defined Plugins, explain why we need Plugins. Nagios will execute a Plugin whenever there is a need to check the status of a host or service. Plugin will perform the check and then simply returns the result to Nagios. Nagios will process the results that it receives from the Plugin and take the necessary actions.

Q5. What is NRPE (Nagios Remote Plugin Executor) in Nagios?

For this answer, give a brief definition of Plugins. The NRPE addon is designed to allow you to execute Nagios plugins on remote Linux/Unix machines. The main reason for doing this is to allow Nagios to monitor “local” resources (like CPU load, memory usage, etc.) on remote machines. Since these public resources are not usually exposed to external machines, an agent like NRPE must be installed on the remote Linux/Unix machines.

I will advise you to explain the NRPE architecture on the basis of diagram shown below. The NRPE addon consists of two pieces:

  • The check_nrpe plugin, which resides on the local monitoring machine.
  • The NRPE daemon, which runs on the remote Linux/Unix machine.

There is a SSL (Secure Socket Layer) connection between monitoring host and remote host as shown in the diagram below.

nrpe architecture - devops interview questions

Q6. What do you mean by passive check in Nagios?

According to me, the answer should start by explaining Passive checks. They are initiated and performed by external applications/processes and the Passive check results are submitted to Nagios for processing.
Then explain the need for passive checks. They are useful for monitoring services that are Asynchronous in nature and cannot be monitored effectively by polling their status on a regularly scheduled basis. They can also be used for monitoring services that are Located behind a firewall and cannot be checked actively from the monitoring host.

Q7. When Does Nagios Check for external commands?

Make sure that you stick to the question during your explanation so I will advise you to follow the below mentioned flow. Nagios check for external commands under the following conditions:

  • At regular intervals specified by the command_check_interval option in the main configuration file or,
  • Immediately after event handlers are executed. This is in addition to the regular cycle of external command checks and is done to provide immediate action if an event handler submits commands to Nagios.

Q8. What is the difference between Active and Passive check in Nagios?

For this answer, first point out the basic difference Active and Passive checks. The major difference between Active and Passive checks is that Active checks are initiated and performed by Nagios, while passive checks are performed by external applications.
If your interviewer is looking unconvinced with the above explanation then you can also mention some key features of both Active and Passive checks:
Passive checks are useful for monitoring services that are:

  • Asynchronous in nature and cannot be monitored effectively by polling their status on a regularly scheduled basis.
  • Located behind a firewall and cannot be checked actively from the monitoring host.

The main features of Actives checks are as follows:

  • Active checks are initiated by the Nagios process.
  • Active checks are run on a regularly scheduled basis.

Q9. How does Nagios help with Distributed Monitoring?

The interviewer will be expecting an answer related to the distributed architecture of Nagios. So, I suggest that you answer it in the below mentioned format:
With Nagios you can monitor your whole enterprise by using a distributed monitoring scheme in which local slave instances of Nagios perform monitoring tasks and report the results back to a single master. You manage all configuration, notification, and reporting from the master, while the slaves do all the work. This design takes advantage of Nagios’s ability to utilize passive checks i.e. external applications or processes that send results back to Nagios. In a distributed configuration, these external applications are other instances of Nagios.

Q10. Explain Main Configuration file of Nagios and its location?

First mention what this main configuration file contains and its function. The main configuration file contains a number of directives that affect how the Nagios daemon operates. This config file is read by both the Nagios daemon and the CGIs (It specifies the location of your main configuration file).
Now you can tell where it is present and how it is created. A sample main configuration file is created in the base directory of the Nagios distribution when you run the configure script. The default name of the main configuration file is nagios.cfg. It is usually placed in the etc/ subdirectory of you Nagios installation (i.e. /usr/local/nagios/etc/).

Q11. Explain how Flap Detection works in Nagios?

I will advise you to first explain Flapping first. Flapping occurs when a service or host changes state too frequently, this causes lot of problem and recovery notifications.
Once you have defined Flapping, explain how Nagios detects Flapping. Whenever Nagios checks the status of a host or service, it will check to see if it has started or stopped flapping. Nagios follows the below given procedure to do that:

  • Storing the results of the last 21 checks of the host or service analyzing the historical check results and determine where state changes/transitions occur
  • Using the state transitions to determine a percent state change value (a measure of change) for the host or service
  • Comparing the percent state change value against low and high flapping thresholds

A host or service is determined to have started flapping when its percent state change first exceeds a high flapping threshold. A host or service is determined to have stopped flapping when its percent state goes below a low flapping threshold.

Q12. What are the three main variables that affect recursion and inheritance in Nagios?

According to me the proper format for this answer should be:
First name the variables and then a small explanation of each of these variables:

  • Name
  • Use
  • Register

Then give a brief explanation for each of these variables. Name is a placeholder that is used by other objects. Use defines the “parent” object whose properties should be used. Register can have a value of 0 (indicating its only a template) and 1 (an actual object). The register value is never inherited.

Q13. What is meant by saying Nagios is Object Oriented?

Answer to this question is pretty direct. I will answer this by saying, “One of the features of Nagios is object configuration format in that you can create object definitions that inherit properties from other object definitions and hence the name. This simplifies and clarifies relationships between various components.”

Q14. What is State Stalking in Nagios?

I will advise you to first give a small introduction on State Stalking. It is used for logging purposes. When Stalking is enabled for a particular host or service, Nagios will watch that host or service very carefully and log any changes it sees in the output of check results.
Depending on the discussion between you and interviewer you can also add, “It can be very helpful in later analysis of the log files. Under normal circumstances, the result of a host or service check is only logged if the host or service has changed state since it was last checked.”

Want to get trained in monitoring tools like Nagios? Want to certified as a DevOps Engineer? Make sure you check out our DevOps Engineer Course Masters Program.

Containerization and Virtualization Interview Questions – DevOps Interview Questions

Let’s see how much you know about containers and VMs.

Q1. What are containers?

My suggestion is to explain the need for containerization first, containers are used to provide consistent computing environment from a developer’s laptop to a test environment, from a staging environment into production.
Now give a definition of containers, a container consists of an entire runtime environment: an application, plus all its dependencies, libraries and other binaries, and configuration files needed to run it, bundled into one package. Containerizing the application platform and its dependencies removes the differences in OS distributions and underlying infrastructure.

containers - devops interview questions

Q2. What are the advantages that Containerization provides over virtualization?

Below are the advantages of containerization over virtualization:

  • Containers provide real-time provisioning and scalability but VMs provide slow provisioning
  • Containers are lightweight when compared to VMs
  • VMs have limited performance when compared to containers
  • Containers have better resource utilization compared to VMs

Q3. How exactly are containers (Docker in our case) different from hypervisor virtualization (vSphere)? What are the benefits?

Given below are some differences. Make sure you include these differences in your answer:

docker vsphere - devops interview questions

Q4. What is Docker image?

I suggest that you go with the below mentioned flow:
Docker image is the source of Docker container. In other words, Docker images are used to create containers. Images are created with the build command, and they’ll produce a container when started with run. Images are stored in a Docker registry such as registry.hub.docker.com because they can become quite large, images are designed to be composed of layers of other images, allowing a minimal amount of data to be sent when transferring images over the network.
Tip: Be aware of Dockerhub in order to answer questions on pre-available images.

Q5. What is Docker container?

This is a very important question so just make sure you don’t deviate from the topic. I advise you to follow the below mentioned format:
Docker containers include the application and all of its dependencies but share the kernel with other containers, running as isolated processes in user space on the host operating system. Docker containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud.
Now explain how to create a Docker container, Docker containers can be created by either creating a Docker image and then running it or you can use Docker images that are present on the Dockerhub.
Docker containers are basically runtime instances of Docker images.

Q6. What is Docker hub?

Answer to this question is pretty direct. Docker hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.

Q7. How is Docker different from other container technologies?

According to me, below points should be there in your answer:
Docker containers are easy to deploy in a cloud. It can get more applications running on the same hardware than other technologies, it makes it easy for developers to quickly create, ready-to-run containerized applications and it makes managing and deploying applications much easier. You can even share containers with your applications.
If you have some more points to add you can do that but make sure the above the above explanation is there in your answer.

Q8. What is Docker Swarm?

You should start this answer by explaining Docker Swarn. It is native clustering for Docker which turns a pool of Docker hosts into a single, virtual Docker host. Docker Swarm serves the standard Docker API, any tool that already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts.
I will also suggest you to include some supported tools:

  • Dokku
  • Docker Compose
  • Docker Machine
  • Jenkins

Q9. What is Dockerfile used for?

This answer according to me should begin by explaining the use of Dockerfile. Docker can build images automatically by reading the instructions from a Dockerfile.
Now I suggest you to give a small definition of Dockerfle. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

Now expect a few questions to test your experience with Docker.

Q10. Can I use json instead of yaml for my compose file in Docker?

You can use json instead of yaml for your compose file, to use json file with compose, specify the filename to use for eg:
docker-compose -f docker-compose.json up

Q11. Tell us how you have used Docker in your past position?

Explain how you have used Docker to help rapid deployment. Explain how you have scripted Docker and used Docker with other tools like Puppet, Chef or Jenkins. If you have no past practical experience in Docker and have past experience with other tools in similar space, be honest and explain the same. In this case, it makes sense if you can compare other tools to Docker in terms of functionality.

Q12. How to create Docker container?

I will suggest you to give a direct answer to this. We can use Docker image to create Docker container by using the below command:
docker run -t -i <image name> <command name>
This command will create and start container.
You should also add, If you want to check the list of all running container with status on a host use the below command:
docker ps -a

Q13. How to stop and restart the Docker container?

In order to stop the Docker container you can use the below command:
docker stop <container ID>
Now to restart the Docker container you can use:
docker restart <container ID>

Q14. How far do Docker containers scale?

Large web deployments like Google and Twitter, and platform providers such as Heroku and dotCloud all run on container technology, at a scale of hundreds of thousands or even millions of containers running in parallel.

Q15. What platforms does Docker run on?

I will start this answer by saying Docker runs on only Linux and Cloud platforms and then I will mention the below vendors of Linux:

  • Ubuntu 12.04, 13.04 et al
  • Fedora 19/20+
  • RHEL 6.5+
  • CentOS 6+
  • Gentoo
  • ArchLinux
  • openSUSE 12.3+
  • CRUX 3.0+

Cloud:

  • Amazon EC2
  • Google Compute Engine
  • Microsoft Azure
  • Rackspace

Note that Docker does not run on Windows or Mac.

Q16. Do I lose my data when the Docker container exits?

You can answer this by saying, no I won’t lose my data when the Docker container exits. Any data that your application writes to disk gets preserved in its container until you explicitly delete the container. The file system for the container persists even after the container halts.

Q17. Do I lose my data when the Docker container exits?

DevOps Pipeline can be defined as a set of tools and processes in which both the development team and operations team work together. In DevOps automation, CI/CD plays an important role. Now if we look into the flow of DevOps, First when we complete continuous integration. Then the next step towards continuous delivery is triggered. After the continuous delivery, the next step of continuous deployment will be triggered. The connection of all these functions can be defined as a pipeline.

Q18. Differentiate between Continuous Deployment and Continuous Delivery?
 

Continuous DeploymentContinuous Delivery
Deployment to production is automatedDeployment to production is manual
App deployment to production can be done, as soon as the code passes all the tests. There is no release day, in continuous DeploymentApp deployment to production is done manually on a certain day, called “release day”

And, that’s it!

I hope these questions help you crack your DevOps interview. If you’re searching for a demanding and rewarding career. Whether you’ve worked in DevOps or are new to the field, the PG in DevOps is what you need to learn how to succeed. From the basic to the most advanced techniques, we cover everything.

All the best for your interview!

Original article source at https://www.edureka.co

#devops #interviewquestion

Top 100+ DevOps Interview Questions and Answers You Should Know
Corey Brooks

Corey Brooks

1649401164

Practice for 3 Types of SQL Interview Questions

Preparing for SQL Interviews? Watch This | SQL Interview | Data Science Interview

SQL is a necessity for data scientist. This video looks at 3 categories of SQL questions. Knowing the categories of questions will ensure that you prepare fully and don’t overlook any skills while practicing.

- Computing Ratios
- Data Categorization
- Cumulative Sums

Timestamp
00:00 SQL Interview
01:02 SQL Fundamentals
02:15 Computing Ratios
06:07 Data Categorization
09:12 Cumulative Sums
11:39 Next Video - Window Functions

#sql #datascience #interviewquestion 

Practice for 3 Types of SQL Interview Questions

Top 20 C# Interview Questions and Answers

C# is a popular Microsoft-developed, general-purpose, OOPs language. In this article learn the top C# interview questions and answers to crack your next interview.

During the development of the .Net Framework, Anders Hejlsberg and his colleagues created C#. C# is intended for the CLI (Common Language Infrastructure), consisting of a runtime environment and an executable code that enables the use of a variety of high-level languages on a variety of computer platforms and architectures.

C# programming features:

  • Windows Integration
  • A library that is standard
  • Automated Garbage Collection
  • Boolean Conditions
  • Lambda Expressions and LINQ
  • Proper Indexers
  • Generics that are easy-to-use
  • Assembly Versioning
  • Multithreading that is simple
  • Event Managers and Delegates
  • Properties and Events
  • Conditional Compilation

Top 20 C# Interview Questions and Answers

Now, let us take a look at the top 20 C# interview questions that you might face!

1. Differentiate between Break and Continue Statement.

Continue statement - Used in jumping over a particular iteration and getting into the next iteration of the loop.

Break statement - Used to skip the next statements of the current iteration and come out of the loop. 

2. List the different types of comments in C#.

 The different types of comments in C# are:

 

  • XML comments

 

Example -

/// example of XML comment

 

  • Single Line comments

 

Example -

// example of single-line comment

 

  • Multi-line comments

 

Example -

/* example of an 
multiline comment */

3. Explain the four steps involved in the C# code compilation.

Four steps of code compilation in C# include -

  • Source code compilation in managed code.
  • Newly created code is clubbed with assembly code.
  • The Common Language Runtime (CLR) is loaded.
  • Assembly execution is done through CLR.

4. Discuss the various methods to pass parameters in a method.

The various methods of passing parameters in a method include -

  • Output parameters: Lets the method return more than one value.
  • Value parameters: The formal value copies and stores the value of the actual argument, which enables the manipulation of the formal parameter without affecting the value of the actual parameter.
  • Reference parameters: The memory address of the actual parameter is stored in the formal argument, which means any change to the formal parameter would reflect on the actual argument too.

5. Name all the C# access modifiers.

The C# access modifiers are -

  • Private Access Modifier - A private attribute or method is one that can only be accessed from within the class.
  • Public Access Modifier - When an attribute or method is declared public, it can be accessed from anywhere in the code.
  • Internal Access Modifier - When a property or method is defined as internal, it can only be accessible from the current assembly point of that class.
  • Protected Access Modifier - When a user declares a method or attribute as protected, it can only be accessed by members of that class and those who inherit it.

6. Mention all the advantages of C#.

The following are the advantages of C# -

  • C# is component-oriented.
  • It is an object-oriented language.
  • The syntax is really easy to grasp.
  • It is easier to learn.
  • C# is part of the framework called .NET

7. Mention the important IDEs for C# development provided by Microsoft.

The following IDEs’ are useful in C# development -

  • MonoDevelop
  • Visual Studio Code (VS Code)
  • Browxy
  • Visual Studio Express (VSE)
  • Visual Web Developer (VWD)

8. Why do we use C# language?

Below are the reasons why we use the C# language -

  • C# is a component-oriented language.
  • It is easy to pass parameters in the C# language.
  • The C# language can be compiled on many platforms.
  • The C# language follows a structured approach.
  • It is easy to learn and pick up.
  • The C# language produces really efficient and readable programmes.

9. Mention the features of C# briefly.

Some of the main features of C# are -

  • C# is a safely typed and managed language.
  • C# is object-oriented in nature.
  • C# is a Cross-platform friendly language.
  • C# is a platform-independent language when it comes to compilation.
  • C# is general purpose in nature.
  • C# is used in implementing Destructors and Constructors.
  • C# is part of the .NET framework.
  • C# is an easy-to-learn and easy-to-grasp language.
  • C# is a structured language.

10. Mention all the differences between void, Public and Static keywords.

void - A method of a void type of value does not return anything.

Static - To declare a member of a specific type, we use the Static keyword.

Public - A member declared with the public keyword states that can be accessed by any other member of any other class publicly.

11. Define Class.

A class is an object's blueprint. It specifies the many types of data and functions that objects will have. A class allows you to combine variables of various events, types, and methods to construct your own custom types. A class in C# is defined via the class keyword.

12. What is meant by Unmanaged or Managed Code?

In simple terms, managed code is code that is executed by the CLR (Common Language Runtime). This means that every application code is totally dependent on the .NET platform and is regarded as overseen in light of it. Code executed by a runtime programme that is not part of the .NET platform is considered unmanaged code. Memory, security, and other activities related to execution will be handled by the application's runtime.

13. What is meant by an Abstract Class?

It's a type of class whose objects can't be instantiated, and it's signified by the term 'abstract'. It consists of a methodology or a single approach.

14. Differentiate between finalize blocks and finalize.

Once the try and catch blocks have been completed, the finalize block is called since it is used for exception handling. No matter if the exception has been captured, this block of code is run. In general, the code in this block is cleaner.

Just before garbage collection, the finalize method is called. The main priorities of the finalize method are to clean up unmanaged code, which is automatically triggered whenever an instance is not re-called.

15. Define an Object.

An object is a representation of a class that is used to access the class's methods. The "New" keyword is used to create an object, and a class that generates an object in memory has information about that class's methods, variables, and behavior.

16. What is a Sealed Class in C#?

To prevent a class from being inherited, we need to construct sealed classes. Sealable modifiers are used to do this. A compilation problem happens if we try to forcefully define a sealed class as a base class.

17. What is meant by method overloading in C#?

Creating numerous named and unique signatures containing methods with the same class is known as method overloading. Overload resolution is used by the compiler to identify which method will be invoked when you compile.

18. What is meant by an Interface?

An interface is a class that does not have any implementation. Only the declarations of events, properties, and attributes are included.

19. What is meant by a Partial Class?

A partial class effectively breaks a class's definition into various classes in the same or other source code files. A class definition can be written in numerous files, but it is compiled as a single class at runtime, and when a class is formed, all methods from all source files can be accessed using the same object. The keyword 'partial' denotes this.

20. Differentiate between method overloading and method overriding?

Method overriding modifies derived class definition, which modifies the method behavior. Method overloading is the process of defining the same name method but with distinct signatures under the same class.

Original article source at https://www.simplilearn.com

#csharp #programming #interviewquestion

Top 20 C# Interview Questions and Answers
Joshua Yates

Joshua Yates

1646298510

5 System Architecture Questions For Web Developers

5 System Architecture Questions For Web Developers [Interview Level]

Tune into this video, to learn about the best scalable architectural practices which you should be following as a full-stack developer when you're building your frontend and backend.

Drop a comment and let us know if you've watched this video till the end!

Timestamps
0:00 frontend and backend - best scalable architecture 
9:06 Why are websockets unscalable (and how to scale them) 
20:05 High scale Video processing pipeline architecture 
36:15 Reverse proxy explained in 10 minutes 
45:43Deploying a MERN Stack App On Production - Best Practices

#programming #fullstack #interviewquestion

5 System Architecture Questions For Web Developers
Code  Camp

Code Camp

1641956201

Improve Your Problem-Solving Skills with 10 Common Coding Problems

10 Common Coding Interview Problems - Solved!

Preparing for coding interviews? Competitive programming? Learn to solve 10 common coding problems and improve your problem-solving skills.

⌨️ (0:00:00) Introduction
⌨️ (0:00:37) Valid anagram
⌨️ (0:05:10) First and last index in sorted array
⌨️ (0:13:44) Kth largest element
⌨️ (0:19:50) Symmetric tree
⌨️ (0:26:42) Generate parentheses
⌨️ (0:37:03) Gas station
⌨️ (0:50:06) Course schedule
⌨️ (1:06:50) Kth permutation
⌨️ (1:20:13) Minimum window substring
⌨️ (1:47:46) Largest rectangle in histogram
⌨️ (2:10:30) Conclusion

💻 Code: https://gist.github.com/syphh/173172ec9a4a1376e5096a187ecbddc9

#programming #developer #interviewquestion

Improve Your Problem-Solving Skills with 10 Common Coding Problems

Uber Data Science Python Interview Question Walkthrough

Uber Data Science Python Interview Question Walkthrough

In this video, we will take a close look at a difficult data science interview question from Uber and walk you through a solution in Python. It turns out that after finding just a few simple, yet clever steps, this question becomes easier to solve.

Timeline:

  • Intro: (0:00​​​)
  • Interview Question: (0:42​​)
  • Framework to solve the problem: (1:32​​​)
  • Understand your data: (1:50​​​)
  • Formulate your approach: (3:07​​​)
  • Code Execution: (6:27​​​)
  • Conclusion: (29:12​​)

#python #interviewquestion 
 

 

Uber Data Science Python Interview Question Walkthrough
Garry Taylor

Garry Taylor

1635742006

Five Programming Interview Red Flags

Here are five red flags you need to look for from your prospective employer when you go to a face to face interview:

1. Disorganization
2. Trash Talking
3. Inappropriate or Offensive Behavior
4. Bait-and-Switch 
5. "Many Hats"

If you see an employer demonstrating one of the above behaviors - run!

#programming #interviewquestion #developer #jobs 

Five Programming Interview Red Flags
Marlee  Carter

Marlee Carter

1634543701

Top 70+ Most Frequently Asked DevOps Interview Questions and Answers

DevOps is the intersection point of software development, operations, and quality assurance (QA). If you are planning to start a career in this field, you must prepare the top DevOps interview questions that you might face in your job interview. In this article, we have listed 70+ most frequently asked DevOps interview questions and answers to boost your interview preparation.

#devops #interviewquestion 

Top 70+ Most Frequently Asked DevOps Interview Questions and Answers
Aron  Lemke

Aron Lemke

1634093580

Top 50 ASP.NET Interview Questions Along with Answers

ASP.NET is an open-source web application framework developed by Microsoft and it is the subset of the .NET framework, the successor of the classic Active Server Pages(ASP). This is used to create web services and applications. Here, we have made a list of the top 50 ASP.NET interview questions along with their answers. The questions are from basic to advanced levels. So these will help you cr@ck the interview.

#aspdotnet #interviewquestion 

Top 50 ASP.NET Interview Questions Along with Answers
Ruthe  Sawayn

Ruthe Sawayn

1628502540

C#: Interview questions: What Is Difference Between string and String ?

In this video I’ll share the difference between string with small ‘s’ and String with ‘S’.

This question I have faced in many companies interview like - Microsoft, Deloitte, Tech M.

#csharp #interviewquestion #microsoftinterviewquestion

C#: Interview questions: What Is Difference Between string and String ?