CCNA Scripting, Containers, and Automation Questions

75 of 106 questions · Page 1/2 · Scripting, Containers, and Automation · Answers revealed

1
MCQhard

An administrator is managing a Kubernetes cluster. A pod is running but not responding as expected. The administrator wants to view the standard output logs from the pod's main container. Which kubectl command should be used?

A.kubectl exec <pod-name> -- cat /var/log/app.log
B.kubectl get pod <pod-name> -o yaml
C.kubectl describe pod <pod-name>
D.kubectl logs <pod-name>
AnswerD

This command fetches the logs from the pod.

Why this answer

kubectl logs <pod-name> retrieves the logs from the specified pod.

2
Multi-Selectmedium

A Kubernetes administrator needs to expose a deployment externally using a Service. Which THREE of the following Service types can be used? (Select THREE).

Select 3 answers
A.ClusterIP
B.LoadBalancer
C.ExternalName
D.NodePort
E.Ingress
AnswersB, C, D

Provisions an external load balancer (cloud provider).

Why this answer

A LoadBalancer Service type provisions an external load balancer (e.g., from a cloud provider) that distributes traffic to the pods. This directly exposes the deployment to external clients by assigning a public IP or DNS name, making it a valid choice for external exposure.

Exam trap

The Linux+ exam often tests the distinction between Ingress (a separate API object) and Service types, leading candidates to mistakenly select Ingress as a Service type when it is actually a routing layer that requires a Service underneath.

3
Multi-Selectmedium

A system administrator is writing a Bash script that must check if a file exists and is readable. Which two test expressions can be used to achieve this? (Choose two.)

Select 2 answers
A.-e /path/to/file
B.-s /path/to/file
C.-r /path/to/file
D.-x /path/to/file
E.-f /path/to/file
AnswersA, C

Correct. -e checks if the file exists.

Why this answer

The -e test checks if a file exists, and -r checks if it is readable. The -f test checks if it is a regular file, which also implies existence but not necessarily readable. The question asks for existence and readability, so -e and -r are correct.

4
MCQmedium

An Ansible playbook includes a handler that restarts a service when a configuration file is changed. Which directive in a task triggers the handler?

A.register:
B.when:
C.handlers:
D.notify:
AnswerD

Correct. notify tells a handler to run if the task has changed.

Why this answer

The 'notify' directive is used to call a handler when a task makes a change (i.e., when the task's state is changed).

5
MCQmedium

A container is running a web server on port 8080 internally, and the administrator wants to access it on the host on port 80. Which Docker run option accomplishes this?

A.-p 80:8080
B.-P
C.-p 8080:80
D.--expose 8080
AnswerA

Correct. Maps host port 80 to container port 8080.

Why this answer

Port mapping is done using -p host_port:container_port. So -p 80:8080 maps host port 80 to container port 8080.

6
MCQhard

A system administrator wants to create a bind mount in Docker to share a host directory `/data` with a container at `/mnt/data`. Which of the following docker run options should be used?

A.-v /data:/mnt/data
B.--mount type=volume,source=/data,target=/mnt/data
C.-v /data:ro
D.-v /mnt/data:/data
AnswerA

This creates a bind mount from /data to /mnt/data.

Why this answer

Bind mounts are specified with -v or --mount. The -v syntax is host-path:container-path.

7
MCQeasy

A user wants to run a Docker container in detached mode, remove it automatically after it stops, and map host port 8080 to container port 80. Which command accomplishes this?

A.docker run -d --rm -p 8080:80 image
B.docker run -it --rm -p 80:8080 image
C.docker create --rm -p 8080:80 image
D.docker start -d -p 8080:80 image
AnswerA

Correct flags for detached mode, auto-remove, and port mapping.

Why this answer

docker run --rm -d -p 8080:80 image runs in detached mode (-d), auto-removes (--rm), and maps port 8080 to 80.

8
MCQeasy

A Linux administrator wants to ensure a bash script stops execution immediately if any command fails. Which line should be added to the script?

A.set -x
B.set -u
C.set -e
D.set -o pipefail
AnswerC

Correct. This causes the script to exit on any command failure.

Why this answer

The 'set -e' command causes the script to exit immediately when a command returns a non-zero exit status.

9
MCQeasy

Which of the following is a key difference between Docker and Podman?

A.Docker does not support container images.
B.Podman can run containers without root privileges.
C.Podman requires a daemon to run containers.
D.Docker can only run on Linux.
AnswerB

Podman supports rootless containers natively.

Why this answer

Podman is daemonless and can run containers rootless, while Docker typically runs with a daemon and root privileges.

10
MCQmedium

An administrator wants to use Ansible to ensure a service is running on a remote host. Which Ansible module should be used in a playbook?

A.service
B.command
C.copy
D.shell
AnswerA

Correct. The service module ensures a service is started, stopped, restarted, etc.

Why this answer

The service module manages services on remote hosts. The command and shell modules run arbitrary commands but are not idempotent. The copy module copies files.

11
MCQmedium

An administrator needs to create a Docker image from a Dockerfile located in the current directory and tag it as 'myapp:v1'. Which command should be used?

A.docker create -t myapp:v1 .
B.docker commit myapp:v1 .
C.docker build -t myapp:v1 .
D.docker image create myapp:v1 .
AnswerC

Correct. Builds and tags the image.

Why this answer

docker build with -t tag creates the image. The period indicates the build context.

12
MCQhard

A system administrator is creating an Ansible playbook to deploy a web server on a group of hosts. The playbook needs to install the nginx package, start the service, and ensure it is enabled on boot. Which playbook structure is correct?

A.--- - hosts: webservers tasks: - name: Install nginx apt: name: nginx state: present - name: Start and enable nginx service: name: nginx state: started enabled: yes
B.--- - hosts: webservers tasks: - name: Install nginx apt: name: nginx state: present - name: Start and enable nginx systemd: name: nginx state: started enabled: yes
C.--- - hosts: webservers tasks: - name: Install nginx command: apt install -y nginx - name: Start nginx command: systemctl start nginx
D.--- - hosts: webservers tasks: - name: Install nginx yum: name: nginx state: present - name: Start and enable nginx service: name: nginx state: started enabled: yes
AnswerA

Correct. Uses 'apt' to install nginx (implied Debian/Ubuntu) and 'service' module to start and enable it. The 'service' module works with both SysV and systemd, ensuring portability.

Why this answer

Option A is correct because it uses the 'service' module, which abstracts away the underlying init system and works on both SysV init and systemd systems, making it the recommended cross-platform choice. Option B is incorrect because the 'systemd' module is specific to systemd-managed services; while it works on many modern Linux distributions, it may not be available on older systems, and the 'service' module is preferred for portability. Options C and D are incorrect: C uses raw 'command' and 'command' to run shell commands, bypassing Ansible's idempotency and module benefits; D uses 'yum' instead of 'apt', which is inconsistent with the Debian/Ubuntu context implied by the 'apt' module in the correct option.

Exam trap

Candidates often assume that because the service is managed by systemd, the 'systemd' module must be used. However, Ansible's 'service' module works with both SysV and systemd, and is the recommended module for managing services across all distributions.

13
Multi-Selectmedium

A Linux administrator is writing a Bash script that must accept command-line options for an input file (-i) and an output file (-o). Which of the following are valid methods to parse these options? (Choose TWO.)

Select 2 answers
A.Using positional parameters $1, $2 directly
B.Using a here-document
C.Using the shift command only
D.Using the getopt command
E.Using the getopts builtin command
AnswersD, E

getopt is another tool for option parsing.

Why this answer

getopts is a built-in for parsing options, and getopt is an external command. Both can be used.

14
Multi-Selectmedium

A bash script uses a loop to iterate over files in a directory. Which TWO of the following loop constructs will correctly iterate over each .txt file in the current directory? (Select TWO).

Select 2 answers
A.for file in '*.txt'; do
B.for file in $(ls *.txt); do
C.for file in *.txt; do
D.for ((i=0; i<${#files[@]}; i++)); do
E.while IFS= read -r file; do done < <(find . -maxdepth 1 -name '*.txt')
AnswersC, E

Glob expands to all .txt files.

Why this answer

for file in *.txt expands to all matching filenames; the C-style for loop can also be used with array of filenames, but direct expansion is simpler.

15
MCQmedium

A Bash script contains the following line: set -e. What is the effect of this command?

A.It enables debugging by printing commands and their arguments as they are executed.
B.It prevents the script from overwriting existing files.
C.It causes the script to exit if any command fails.
D.It treats unset variables as an error and exits.
AnswerC

Correct. set -e exits on error.

Why this answer

set -e causes the script to exit immediately if any command exits with a non-zero status. This is useful for catching errors early.

16
MCQeasy

In bash, what is the difference between single quotes and double quotes?

A.Single quotes prevent variable expansion, double quotes allow it.
B.Both allow variable expansion, but double quotes also allow globbing.
C.Single quotes allow variable expansion, double quotes do not.
D.There is no difference; they are interchangeable.
AnswerA

Correct. Single quotes are literal, double quotes interpret variables.

Why this answer

Single quotes preserve the literal value of each character, while double quotes allow variable expansion and command substitution.

17
Multi-Selectmedium

An administrator is troubleshooting a Pod in a Kubernetes cluster that is not starting. Which TWO kubectl commands are most useful for diagnosing the issue? (Choose TWO.)

Select 2 answers
A.kubectl get nodes
B.kubectl apply -f pod.yaml
C.kubectl delete pod <pod-name>
D.kubectl logs <pod-name>
E.kubectl describe pod <pod-name>
AnswersD, E

Retrieves logs from the container, useful for application errors.

Why this answer

kubectl describe pod provides detailed event and status information, and kubectl logs retrieves container logs.

18
MCQeasy

A Linux administrator needs to write a Bash script that runs a series of commands and stops immediately if any command fails. Which directive should be included at the beginning of the script?

A.#!/bin/bash
B.trap ... ERR
C.set -e
D.set -x
AnswerC

Correct. set -e exits on any command failure.

Why this answer

The 'set -e' directive causes the script to exit immediately if any command returns a non-zero exit status, which is useful for error handling.

19
MCQhard

A Kubernetes administrator needs to expose a deployment named 'web-app' running on port 80 internally within the cluster. Which kubectl command creates a service of type ClusterIP that maps port 80 to the target port 8080 on the pods?

A.kubectl create service nodeport web-app --tcp=80:8080
B.kubectl expose deployment web-app --port=80 --target-port=8080
C.kubectl expose pod web-app --port=80 --target-port=8080
D.kubectl create service clusterip web-app --tcp=80:8080
AnswerB

Correct. This creates a Service targeting port 80 and forwarding to container port 8080.

Why this answer

To create a ClusterIP service, use 'kubectl expose deployment web-app --port=80 --target-port=8080'. The --type=ClusterIP is default.

20
Multi-Selectmedium

A systems administrator is writing a bash script that must accept command-line options: -f for a configuration file, -v for verbose mode, and -d for a directory. Which TWO of the following are correct ways to parse these options using getopts? (Select TWO).

Select 2 answers
A.while getopts 'f v d' opt; do
B.while getopts 'f:vd' opt; do
C.while getopts ':f:vd:' opt; do
D.while getopts 'fv:d:' opt; do
E.while getopts 'f:vd:' opt; do
AnswersC, E

Leading colon suppresses error messages; same effect as A for option parsing.

Why this answer

getopts option string 'f:vd:' means -f requires an argument, -v is boolean, -d requires an argument. The colon after f and d indicates argument required.

21
MCQeasy

A Docker container is running in the background. Which command allows the administrator to execute an interactive bash shell inside the running container named 'webapp'?

A.docker start -i webapp
B.docker run -it webapp bash
C.docker exec -it webapp bash
D.docker attach webapp
AnswerC

Correct. Exec runs a command interactively in the running container.

Why this answer

docker exec -it runs an interactive command in a running container. The other commands are for different purposes.

22
Multi-Selectmedium

A Linux administrator is writing a bash script to process a list of usernames stored in an array. The script must iterate over each username and print a welcome message. Which TWO of the following loops will correctly iterate over the array 'users' and print 'Welcome, <username>'? (Choose TWO.)

Select 2 answers
A.for user in $users; do echo "Welcome, $user"; done
B.for user in "${users[*]}"; do echo "Welcome, $user"; done
C.while read user; do echo "Welcome, $user"; done < <(printf '%s\n' "${users[@]}")
D.for ((i=0; i<${#users[@]}; i++)); do echo "Welcome, ${users[$i]}"; done
E.for user in ${users[@]}; do echo "Welcome, $user"; done
AnswersD, E

Correct. Uses C-style for loop with array length and indexing.

Why this answer

Arrays can be iterated using for with ${users[@]} or using indices. The correct methods are direct iteration over elements and using a C-style for loop with indices.

23
MCQhard

An administrator is troubleshooting a Kubernetes deployment that is not receiving traffic. The deployment has one replica and the pod is running. The service is of type ClusterIP. Which command would help verify that the service endpoints are correctly associated with the pod?

A.kubectl describe pod
B.kubectl get pods -o wide
C.kubectl get svc
D.kubectl get endpoints
AnswerD

Correct. This shows the endpoints associated with each service.

Why this answer

kubectl describe service shows the endpoints and selector labels. The other commands show different information.

24
MCQeasy

A Linux administrator writes a bash script that needs to exit immediately if any command fails. Which of the following should be included at the beginning of the script?

A.set -x
B.set -u
C.set -e
D.set -o pipefail
AnswerC

set -e exits the script on any command failure.

Why this answer

The 'set -e' command causes the shell to exit if any command exits with a non-zero status, which is useful for error handling.

25
MCQmedium

A DevOps engineer needs to debug a bash script that unexpectedly fails when processing files. Which of the following should be added to the script to print each command before execution?

A.set -v
B.set -e
C.set -x
D.set -u
AnswerC

set -x prints commands and their arguments as they are executed.

Why this answer

set -x enables a trace of each command, printing them to stderr before execution.

26
Multi-Selecthard

An administrator needs to deploy an application stack that includes a web server and a database. The containers must be able to communicate with each other and be easily managed together. Which TWO tools or methods can accomplish this? (Select TWO.)

Select 2 answers
A.Ansible playbook with docker_container module
B.Docker Compose
C.Podman pods
D.Docker run with --link
E.Kubernetes
AnswersB, E

Docker Compose is designed for multi-container applications.

Why this answer

Docker Compose allows defining multi-container applications in a single file and manages networking and dependencies. Kubernetes orchestrates containers across a cluster. Both can manage a stack of containers with networking.

27
MCQeasy

A Linux administrator wants to ensure a Bash script exits immediately if any command fails. Which directive should be included at the beginning of the script?

A.set -o pipefail
B.set -u
C.set -e
D.set -x
AnswerC

set -e causes the script to exit if any command fails.

Why this answer

The set -e command tells the shell to exit if any command exits with a non-zero status.

28
MCQhard

A DevOps team is using Ansible to manage configuration of web servers. They need to ensure that the Apache service is running and enabled at boot on all servers in the 'webservers' group. Which playbook task accomplishes this?

A.- name: Ensure Apache is running systemd: name: httpd state: started enabled: yes
B.- name: Ensure Apache is running shell: systemctl start httpd && systemctl enable httpd
C.- name: Ensure Apache is running command: systemctl enable --now httpd
D.- name: Ensure Apache is running service: name: httpd state: started enabled: yes
AnswerD

Correct. The service module with state=started and enabled=yes ensures the service is running and enabled.

Why this answer

The Ansible service module can manage services; the 'enabled' and 'state' parameters control boot status and running state.

29
MCQmedium

A DevOps engineer is writing a Bash script that checks if a file exists and is readable. Which test condition should be used inside an if statement?

A.[[ -e file ]]
B.[[ -f file ]]
C.[[ -s file ]]
D.[[ -r file ]]
AnswerD

Correct. -r returns true if the file exists and has read permission.

Why this answer

The -e test checks if a file exists, and -r checks if it is readable. Using -f also checks for a regular file, but the question requires existence and readability, so combining -e and -r is correct. However, the options include -f and -r, but -f alone does not check readability.

The correct combination is -e and -r, but since that is not an option, the best answer is -r because it implies existence? Actually, -r returns true only if the file exists and is readable. So -r alone satisfies both conditions.

30
MCQmedium

A Podman user wants to run a container that automatically removes itself after it stops, runs in detached mode, and maps host port 80 to container port 8080. Which command is correct?

A.podman run -rm -d -p 8080:80 nginx
B.podman run --rm -d -p 80:8080 nginx
C.podman run --remove -d -p 80:8080 nginx
D.podman run --rm -d -p 80:8080 nginx
AnswerB

Correct. All options are correctly specified.

Why this answer

The correct command is `podman run --rm -d -p 80:8080 nginx`. Option A incorrectly maps port 8080 on the host to port 80 in the container (should be host:container). Option C uses an invalid flag `--remove`; the correct flag is `--rm`.

Options B and D are identical commands, both using the correct flags. Therefore, if B is correct, D should also be correct; the discrepancy is likely a typo in the question.

31
Multi-Selectmedium

A Linux administrator is writing a Bash script that uses a function. Which two statements about Bash functions are correct? (Choose TWO.)

Select 2 answers
A.Functions are called by their name without parentheses.
B.Functions can return a value using the return statement.
C.Function definitions must be placed at the beginning of the script.
D.Functions cannot accept arguments.
E.Functions can be called before they are defined.
AnswersA, B

Functions are invoked by name; parentheses are used only in definition.

Why this answer

Bash functions must be defined before use and are called by name without parentheses.

32
MCQmedium

In a Kubernetes cluster, a developer needs to create a Deployment that runs three replicas of a container image 'myapp:1.0' and exposes port 8080. Which YAML snippet correctly defines this Deployment?

A.apiVersion: v1 kind: Pod metadata: name: myapp spec: replicas: 3 containers: - name: myapp image: myapp:1.0 ports: - containerPort: 8080
B.apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 3 selector: app: myapp template: containers: - name: myapp image: myapp:1.0 ports: - containerPort: 8080
C.apiVersion: apps/v1 kind: Deployment metadata: name: myapp spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:1.0 ports: - containerPort: 8080
D.apiVersion: v1 kind: Deployment metadata: name: myapp spec: replicas: 3 template: spec: containers: - name: myapp image: myapp:1.0 ports: - containerPort: 8080
AnswerC

Correct. Includes required apiVersion, selector, and template with labels.

Why this answer

A Deployment YAML must have apiVersion, kind, metadata, spec with replicas and template containing container spec with image and ports.

33
MCQeasy

A Kubernetes YAML manifest defines a Deployment. Which field specifies the number of pod replicas to run?

A.metadata.replicas
B.spec.template.replicas
C.spec.containers.replicas
D.spec.replicas
AnswerD

This is the correct field to set the number of replicas.

Why this answer

In a Deployment spec, the 'replicas' field sets the desired number of pod instances.

34
Multi-Selecthard

A Kubernetes administrator is creating a YAML manifest for a Deployment. Which three fields are required in a valid Deployment specification? (Choose three.)

Select 3 answers
A.apiVersion
B.spec
C.kind
D.status
E.metadata
AnswersA, C, E

Correct. Every Kubernetes object requires apiVersion.

Why this answer

A Deployment manifest requires apiVersion, kind, and metadata (name). The spec is also required but the question asks for fields, and spec is a top-level field; however, metadata is the one that contains name. Typically, apiVersion, kind, and metadata are required.

35
MCQeasy

In a Bash script, what is the purpose of the shebang '#!/bin/bash'?

A.It specifies the path to the Bash executable that should run the script.
B.It defines a variable for the Bash version.
C.It sets the script's permissions to executable.
D.It enables debug mode for the script.
AnswerA

The shebang line indicates the interpreter.

Why this answer

The shebang tells the system which interpreter to use to execute the script.

36
MCQmedium

A Linux administrator needs to run a Docker container in detached mode with port mapping from host port 8080 to container port 80, and mount a host directory /data to /app/data inside the container. Which command achieves this?

A.docker run -d -p 8080:80 -v /data:/app/data --name webapp nginx
B.docker run -it -p 8080:80 -v /data:/app/data --name webapp nginx
C.docker run -d -P -v /data:/app/data --name webapp nginx
D.docker start -d -p 8080:80 -v /data:/app/data webapp
AnswerA

This command correctly uses -d for detached, -p for port mapping, and -v for volume mount.

Why this answer

docker run -d -p 8080:80 -v /data:/app/data --name webapp nginx runs the container detached, maps ports, and mounts a bind volume.

37
MCQeasy

In a Bash script, what is the correct way to check if a file named '/etc/passwd' exists and is a regular file?

A.if [ -r /etc/passwd ]
B.if [ -e /etc/passwd ]
C.if [ -s /etc/passwd ]
D.if [ -f /etc/passwd ]
AnswerD

-f checks if the file exists and is a regular file.

Why this answer

The -f file test operator returns true if the file exists and is a regular file.

38
MCQmedium

A developer wants to run a containerized application using Podman in a rootless environment. Which of the following is a key difference between Podman and Docker that the developer should be aware of?

A.Podman can run containers without a daemon
B.Podman requires a daemon to manage containers
C.Podman only supports rootful containers
D.Podman uses a different CLI syntax than Docker
AnswerA

Correct. Podman is daemonless and supports rootless containers.

Why this answer

Podman supports rootless containers natively without requiring a daemon, unlike Docker which traditionally requires a daemon (dockerd) running as root.

39
MCQhard

A containerized application uses a volume to persist data. The administrator needs to create a new volume named 'data-vol' and mount it to /app/data in the container. Which Docker command accomplishes this?

A.docker run --mount type=volume,source=data-vol,target=/app/data myapp
B.docker run -v /data-vol:/app/data myapp
C.docker run -v data-vol:/app/data myapp
D.docker volume create data-vol && docker run -v data-vol:/app/data myapp
AnswerC

Correct. This creates a volume 'data-vol' if it doesn't exist and mounts it to /app/data.

Why this answer

Docker volumes can be created with 'docker volume create' and then used with the -v flag in 'docker run'.

40
MCQmedium

A DevOps engineer is writing a Dockerfile. The application requires environment variables to be set at runtime from a file. Which Dockerfile instruction should be used to achieve this?

A.CMD export
B.ARG
C.ENV
D.RUN export
AnswerC

ENV sets environment variables in the Dockerfile.

Why this answer

The ENV instruction sets environment variables in the Dockerfile, but for runtime from a file, the --env-file option is used with docker run. However, within the Dockerfile, ENV is the instruction to set environment variables.

41
MCQhard

An administrator is writing a bash script that loops through all .log files in /var/log and prints the file name if the file is larger than 100 kilobytes. Which loop correctly implements this?

A.ls -l /var/log/*.log | awk '$5>100 {print $NF}'
B.for f in /var/log/*.log; do if [ -s $f -a $(stat -c%s $f) -gt 100 ]; then echo $f; fi; done
C.find /var/log -name '*.log' -size +100k -exec echo {} \;
D.for f in /var/log/*.log; do if [ $f -gt 100k ]; then echo $f; fi; done
AnswerC

Correct. find with -size +100k matches files larger than 100 kilobytes.

Why this answer

The find command with -size filters files larger than 100k, and -exec with {} runs the command for each file. The other options have syntax errors or incorrect logic.

42
Multi-Selectmedium

A system administrator is writing a bash script that must check if a file exists and is readable before processing. Which TWO test expressions can be used in an if statement? (Select TWO.)

Select 2 answers
A.[ -e /path/to/file ]
B.[ -f /path/to/file ]
C.[ -x /path/to/file ]
D.[ -s /path/to/file ]
E.[ -r /path/to/file ]
AnswersA, E

Correct. -e checks if file exists.

Why this answer

The -e test checks existence, and -r checks readability. Other options are for different attributes.

43
MCQhard

In a Bash script, the administrator wants to capture the output of a command into a variable. Which syntax should be used?

A.$VAR
B.$((command))
C.`command`
D.$(command)
AnswerD

Correct. $(command) is the modern syntax for command substitution, capturing the command's stdout into a variable.

Why this answer

Command substitution can be done with $(command) or backticks `command`. The latter is deprecated. $(( )) is for arithmetic expansion. $VAR is for variable expansion.

44
MCQeasy

In a Bash script, what is the difference between single quotes and double quotes?

A.Both behave the same.
B.Single quotes allow variable expansion; double quotes do not.
C.Double quotes prevent all substitutions; single quotes allow command substitution.
D.Single quotes prevent variable expansion; double quotes allow it.
AnswerD

Double quotes permit $VAR and $(command) expansions.

Why this answer

Single quotes preserve the literal value of each character, while double quotes allow variable expansion and command substitution.

45
Multi-Selecthard

A DevOps engineer is creating a Dockerfile for a Node.js application. Which THREE of the following instructions are valid and commonly used in a Dockerfile? (Choose THREE.)

Select 3 answers
A.EXECUTE node app.js
B.COPY . /app
C.RUN npm install
D.INSTALL package.json
E.FROM node:14
AnswersB, C, E

COPY copies files from host to container.

Why this answer

Common Dockerfile instructions include FROM, RUN, COPY, EXPOSE, CMD, ENTRYPOINT, etc. The valid ones here are FROM, RUN, and COPY.

46
MCQmedium

A Docker container needs to persistently store data that should survive container removal and be accessible by other containers. Which storage method should be used?

A.Volume
B.Bind mount
C.Container layer
D.tmpfs mount
AnswerA

Correct. Volumes are the preferred mechanism for persistent and sharable data.

Why this answer

Volumes are managed by Docker and persist independently of containers. Bind mounts depend on host filesystem structure. tmpfs is temporary and stored in memory. The question asks for persistent storage that can be shared, so volumes are best.

47
MCQmedium

An administrator writes a bash script that uses a function to check if a file exists and is readable. The function returns 0 if the file meets the conditions, and 1 otherwise. Which of the following correctly implements this function?

A.check_file() { [[ -f "$1" ]] && [[ -r "$1" ]] && return 0; return 1; }
B.check_file() { test -f "$1" && test -r "$1" && return 0 || return 1; }
C.check_file() { if [ -f "$1" -a -r "$1" ]; then return 0; else return 1; fi; }
D.check_file() { if [[ -f "$1" && -r "$1" ]]; then return 0; else return 1; fi; }
AnswerD

Correct. This function uses the modern [[ ]] syntax with a single compound condition (&&) inside the brackets, which is clear, reliable, and portable within bash. The if-else structure explicitly handles both outcomes.

Why this answer

The function uses file test operators -f (regular file) and -r (readable) combined with && inside [[ ]] to check both conditions. The return statement returns 0 for success (true) or 1 for failure.

48
MCQmedium

A team uses Ansible for configuration management. They want to ensure a service is running on all managed nodes. Which Ansible module should be used in the playbook?

A.systemd
B.service
C.command
D.shell
AnswerB

Correct. The service module ensures a service is in the desired state.

Why this answer

The service module manages services (start, stop, restart, etc.). The other modules are for different purposes.

49
MCQmedium

A Linux administrator is writing a bash script that needs to iterate over all files ending with .log in /var/log and output the number of lines in each file. Which loop construct should be used?

A.while read f; do wc -l "$f"; done < /var/log/*.log
B.for f in /var/log/*.log; do wc -l "$f"; done
C.for ((i=0; i<${#files[@]}; i++)); do wc -l "${files[$i]}"; done
D.until [ -z "$f" ]; do wc -l "$f"; shift; done
AnswerB

Correct. The for loop iterates over each file matching the glob.

Why this answer

A for loop with a glob pattern is the simplest way to iterate over files matching a pattern.

50
Multi-Selecthard

A DevOps engineer is building a Docker image for a web application. They want to ensure the image is as small as possible and that sensitive data is not left in layers. Which two practices help achieve these goals? (Choose TWO.)

Select 2 answers
A.Using the ADD instruction instead of COPY.
B.Using multi-stage builds to separate build and runtime environments.
C.Combining multiple RUN commands into one using &&.
D.Using the latest tag for base images.
E.Installing all packages in a single layer.
AnswersB, C

Keeps final image clean.

Why this answer

Combining RUN commands reduces layers, and using multi-stage builds avoids including build tools in the final image.

51
MCQmedium

In a bash script, a developer needs to parse command-line options such as -f filename and -v (verbose). Which built-in command is best suited for this task?

A.getopt
B.getopts
C.case
D.shift
AnswerB

Correct. getopts is the built-in command for parsing options.

Why this answer

getopts is a bash built-in for parsing command-line options. It handles short options and requires option arguments.

52
MCQhard

An administrator is investigating a Bash script that is failing unexpectedly. They want to see each command as it is executed to debug the script. Which command should be added to the script?

A.set -v
B.set -x
C.set -e
D.set -o xtrace
AnswerB

set -x prints commands and their arguments during execution.

Why this answer

set -x enables a trace of commands and their arguments as they are executed.

53
MCQmedium

In a Bash script, the following array is defined: fruits=('apple' 'banana' 'cherry'). Which syntax correctly iterates over all elements of the array?

A.for fruit in "${fruits[*]}"; do echo $fruit; done
B.for fruit in "${fruits[@]}"; do echo $fruit; done
C.for fruit in ${fruits[@]}; do echo $fruit; done
D.for fruit in ${fruits}; do echo $fruit; done
AnswerB

Correct. Iterates over each element as separate items.

Why this answer

To iterate over all elements of an array, use for fruit in "${fruits[@]}"; do ... done. The [@] syntax expands to all elements.

54
MCQeasy

A Linux administrator is writing a bash script that must exit immediately if any command fails. Which of the following should be included at the beginning of the script?

A.set -e
B.set -o pipefail
C.set -u
D.set -x
AnswerA

Correct. set -e exits on any command failure.

Why this answer

The set -e command causes the shell to exit if any command exits with a non-zero status, which is exactly what is needed to ensure the script stops on failure.

55
MCQmedium

A Bash script contains the following function: ```bash myfunc() { local result="$(( $1 + $2 ))" echo "$result" } ``` If the script calls `myfunc 5 10`, what is the output?

A.510
B.5+10
C.the function returns nothing
D.15
AnswerD

The arithmetic expansion $((5+10)) yields 15.

Why this answer

The function adds the two arguments (5+10=15) and echoes the result, which is 15.

56
Multi-Selectmedium

In a bash script, a function is defined to calculate a value. Which TWO of the following are valid ways to return a value from the function to the caller? (Select TWO).

Select 2 answers
A.exit 42
B.echo 42
C.return 42
D.return 'value'
E.print 42
AnswersB, C

Prints 42 to stdout; caller can capture with result=$(function).

Why this answer

The return statement sets the exit status (0-255). echo outputs to stdout which can be captured via command substitution. Functions cannot return arbitrary integers directly via return if >255.

57
Multi-Selectmedium

An administrator is creating an Ansible playbook to configure multiple web servers. Which of the following are valid ways to define variables for different groups of hosts? (Choose TWO.)

Select 2 answers
A.In a group_vars directory
B.In the inventory file using :vars
C.In a host_vars directory
D.In the playbook itself using vars_files
E.In the roles directory
AnswersA, C

Variables defined in group_vars apply to all hosts in the group.

Why this answer

In Ansible, group_vars and host_vars directories are used to define variables per group or per host.

58
Multi-Selectmedium

In an Ansible playbook, which THREE of the following are valid modules for managing files and packages? (Select THREE).

Select 3 answers
A.get_url
B.copy
C.apt
D.file
E.command
AnswersB, C, D

Copies files to remote hosts.

Why this answer

Option B (copy) is correct because the copy module is a dedicated Ansible module for copying files from the local control node to remote hosts, supporting attributes like owner, permissions, and content. It is the standard idempotent way to manage file distribution in playbooks.

Exam trap

CompTIA Linux+ often tests the distinction between modules that manage state (copy, file, apt) versus modules that execute commands (command) or retrieve remote content (get_url), trapping candidates who think any module that touches a file is a 'file management' module.

59
MCQhard

A Bash script uses getopts to parse command-line options. The script is invoked as: ./script -a -b value. Which getopts string should be used to correctly parse -a (no argument) and -b (requires an argument)?

A."ab:"
B.":ab"
C."ab"
D."a:b"
AnswerA

Correct. 'a' takes no argument, 'b:' requires an argument.

Why this answer

In getopts, a colon after an option indicates it requires an argument. So 'ab:' means -a takes no argument, -b takes an argument. The colon at the beginning is for silent error handling.

60
MCQeasy

A Linux administrator writes a Bash script and includes the line `#!/bin/bash` at the top. What is the purpose of this line?

A.It sets the script to run in the background.
B.It enables debugging mode.
C.It specifies the interpreter to execute the script.
D.It defines a variable for the script.
AnswerC

The shebang indicates the path to the interpreter.

Why this answer

The shebang line tells the system which interpreter to use to execute the script. In this case, it specifies the Bash shell.

61
MCQmedium

A system administrator is writing a Bash script that needs to iterate over a list of IP addresses stored in a file, one per line. Which loop construct should be used?

A.for i in $(cat ip_list.txt); do ... done
B.for ((i=0; i<$(wc -l < ip_list.txt); i++)); do ... done
C.while read line; do ... done < ip_list.txt
D.until IFS= read -r line; do ... done < ip_list.txt
AnswerA

Correct. `for i in $(cat ip_list.txt); do ... done` iterates over each line from the output of `cat`, treating it as a list item. This works well when the file contains one IP per line without spaces.

Why this answer

Option A is correct because the `for` loop with command substitution (`$(cat ip_list.txt)`) iterates over each line as an item, which is a common and straightforward approach for reading a list of IP addresses from a file. While `while read` (option C) is a valid alternative for line-by-line processing, it is not the loop construct typically expected for iterating over a list in this context. The question asks for a loop that iterates over a list, and the `for` loop is the most direct choice.

Options B and D are incorrect due to syntax or inappropriate loop types.

62
MCQmedium

A developer is writing a Dockerfile. The application requires a configuration file that should be copied from the build context and the container should expose port 8080. Which combination of Dockerfile instructions is correct?

A.COPY config.txt /app/ and EXPOSE 8080
B.ADD config.txt /app/ and WORKDIR 8080
C.ADD config.txt /app/ and RUN expose 8080
D.COPY config.txt /app/ and CMD 8080
AnswerA

Correct. COPY copies the file, EXPOSE documents the port.

Why this answer

COPY adds files from the build context, and EXPOSE documents the port. Other instructions serve different purposes.

63
MCQhard

A DevOps engineer is writing a Bash script that iterates over all items in an array. The array is declared as 'fruits=("apple" "banana" "cherry")'. Which loop correctly iterates over each element?

A.for fruit in "${fruits[@]}"; do ... done
B.for (i=0; i<${#fruits[@]}; i++); do ... done
C.for fruit in ${fruits[*]}; do ... done
D.for fruit in $fruits; do ... done
AnswerA

Properly iterates over each array element as separate words, even if they contain spaces.

Why this answer

To iterate over array elements, use "${fruits[@]}" with quotes. The @ expands to all elements.

64
Multi-Selectmedium

In a Bash script, which THREE of the following are valid ways to define a function? (Select THREE.)

Select 3 answers
A.function myfunc() { commands; }
B.myfunc = () { commands; }
C.function myfunc { commands; }
D.def myfunc { commands; }
E.myfunc() { commands; }
AnswersA, C, E

Combined syntax, also valid in Bash.

Why this answer

The correct options are A, C, and E. In Bash, a function can be defined using any of these three syntaxes: 'function name { commands; }' (option C), 'name() { commands; }' (option E), or 'function name() { commands; }' (option A). Option B is invalid because it uses an equals sign after the function name, which is not part of any valid Bash function definition.

Option D is invalid because 'def' is not a Bash keyword; it is used in Python and other languages, but not in Bash.

65
MCQeasy

Which of the following Bash variable expansions will result in the string "Hello World"?

A.${GREETING:+World} when GREETING=Hello
B.${GREETING:-World} when GREETING=Hello
C.${GREETING:-'Hello World'} when GREETING is unset
D.${GREETING:=World} when GREETING is unset
AnswerC

Since GREETING is unset, the default value 'Hello World' is used.

Why this answer

${var:-default} expands to the value of var if var is set and not null; otherwise, it expands to default. In option C, GREETING is unset, so ${GREETING:-'Hello World'} expands to 'Hello World'. Option A: ${GREETING:+World} with GREETING=Hello expands to 'World' because + uses alternate value when var is set.

Option B: ${GREETING:-World} with GREETING=Hello expands to 'Hello' because var is set. Option D: ${GREETING:=World} with GREETING unset assigns 'World' to GREETING and expands to 'World', not 'Hello World'. Therefore, only option C yields 'Hello World'.

66
MCQmedium

In a Bash script, a variable is assigned the output of a command using: result=$(ls -l). What is the purpose of the $() syntax?

A.It runs the command and assigns its output to the variable
B.It expands the variable result
C.It checks if the command exists
D.It runs the command in a subshell and discards output
AnswerA

Correct. Command substitution runs the command and returns its stdout.

Why this answer

The $() syntax is command substitution, which captures the output of a command and stores it in a variable.

67
MCQmedium

A system administrator needs to update a configuration file on multiple servers using Ansible. The playbook must ensure the line 'MaxAuthTries 3' is present in /etc/ssh/sshd_config. Which Ansible module is most appropriate?

A.template
B.shell
C.copy
D.lineinfile
AnswerD

lineinfile is designed to manage lines in text files.

Why this answer

The lineinfile module ensures a specific line is present in a file, ideal for configuration management.

68
MCQhard

In a bash script, a variable is set as follows: myvar='Hello World'. Which of the following correctly prints the first 5 characters of the variable?

A.echo ${myvar#?????}
B.echo ${myvar:0:5}
C.echo ${myvar:0-5}
D.echo ${myvar:5}
AnswerB

Correct. Extracts substring of length 5 from start.

Why this answer

Parameter expansion ${myvar:0:5} extracts a substring starting at position 0 of length 5. The other options are incorrect syntax.

69
Multi-Selecthard

An administrator is debugging a Docker container that exits immediately after starting. Which THREE commands can help diagnose the issue? (Select THREE).

Select 3 answers
A.docker run --rm -it <image> sh
B.docker logs <container>
C.docker inspect <container>
D.docker exec -it <container> sh
E.docker ps
AnswersA, B, C

Starts a new container interactively with a shell to test manually.

Why this answer

docker logs shows the container's output; docker exec -it can run commands inside a running container (but if it exits, you may need to start it interactively); docker inspect shows low-level config and state; docker ps shows running containers, not helpful for exited ones.

70
MCQhard

A Linux administrator is troubleshooting a Bash script that unexpectedly terminates when a command fails. The script uses `#!/bin/bash`. Which of the following commands, if placed at the beginning of the script, would cause it to exit on any command failure?

A.set -u
B.trap 'exit 1' ERR
C.set -e
D.set -o pipefail
AnswerC

set -e exits the script on any command failure.

Why this answer

The `set -e` option makes the shell exit immediately if a command exits with a non-zero status.

71
MCQmedium

A DevOps engineer wants to run a Docker container with a bind mount to make a host directory /data available at /mnt/data inside the container. Which command is correct?

A.docker run -v /data:/mnt/data:bind ubuntu
B.docker run -v /data:/mnt/data ubuntu
C.docker run --mount type=bind,source=/data,target=/mnt/data ubuntu
D.docker run --mount type=volume,source=/data,target=/mnt/data ubuntu
AnswerC

Correct. The --mount syntax explicitly defines a bind mount.

Why this answer

Option C is correct because the `--mount` flag with `type=bind` explicitly creates a bind mount, mapping the host directory `/data` to the container path `/mnt/data`. This syntax is more explicit and less error-prone than the older `-v` flag, especially when dealing with bind mounts on directories that do not exist yet or when additional mount options are needed.

Exam trap

The trap here is that candidates often assume the `-v` flag always creates a bind mount, but Docker's `-v` syntax can create either a bind mount or a volume depending on whether the source is an absolute path (bind) or a name (volume), and the `--mount` flag with explicit `type=bind` is the unambiguous, recommended method for bind mounts.

How to eliminate wrong answers

Option A is wrong because the `:bind` suffix is not a valid mount option in the `-v` syntax; the `-v` flag uses a colon-separated list of host-path:container-path[:options], and while `:ro` or `:z` are valid, `:bind` is not recognized. Option B is wrong because while `-v /data:/mnt/data` does create a bind mount by default when the source is an absolute path, it is ambiguous and can be confused with a volume mount; the `--mount` syntax is the recommended modern approach for clarity. Option D is wrong because `type=volume` creates a named Docker volume, not a bind mount; using `source=/data` with a volume type would treat `/data` as a volume name rather than a host directory path, which is not what the question asks.

72
Multi-Selecthard

A DevOps engineer is writing a Bash script to automate deployment of a containerized application. The script must: (1) exit immediately if any command fails, (2) build a Docker image with tag 'myapp:latest' from the current directory, (3) run a container from that image in detached mode with port mapping 8080:80, (4) set environment variable ENV=production inside the container, and (5) bind mount a host directory /data to /app/data inside the container. Which TWO of the following snippets should be included in the script to meet these requirements? (Choose TWO.)

Select 2 answers
A.docker run -d -p 8080:80 -e ENV=production -v /data:/app/data myapp:latest
B.set -e
C.docker build -t myapp:latest .
D.docker build --tag myapp:latest .
E.docker run -d -p 8080:80 -eENV=production -v /data:/app/data myapp:latest
AnswersA, B

Correctly runs the container detached with port mapping, environment variable, and bind mount.

Why this answer

Option B (set -e) causes the script to exit immediately if any command fails, satisfying requirement 1. Option A (docker run -d -p 8080:80 -e ENV=production -v /data:/app/data myapp:latest) runs the container detached with port mapping, environment variable, and bind mount, meeting requirements 3, 4, and 5. Option C and D are Docker build commands, not related to running the container.

Option E has a syntax error (-eENV=production missing space).

73
MCQmedium

A developer is writing a Bash script that needs to test whether a file exists and is readable. Which of the following conditionals correctly performs this check?

A.if test -f $file || test -r $file; then
B.if [ -f $file -a -r $file ]; then
C.if [[ -f $file && -r $file ]]; then
D.if [ -f $file -o -r $file ]; then
AnswerC

Double brackets are preferred and allow && for logical AND.

Why this answer

The -f test checks for a regular file, and -r checks if it is readable. Both must be true, so they are combined with &&.

74
MCQmedium

A Linux administrator is writing a bash script that accepts command-line options: -a for all, -f for file, and -o for output. Which of the following correctly uses getopts to parse these options?

A.while getopts "-a -f -o" opt; do ... done
B.while getopts "a:f:o" opt; do case $opt in a) ...;; f) ...;; o) ...;; esac; done
C.getopts "afo" opt; case $opt in a) ...;; f) ...;; o) ...;; esac
D.while getopts "afo" opt; do case $opt in a) ...;; f) ...;; o) ...;; esac; done
AnswerD

Correct. The option string 'afo' defines three boolean options without arguments.

Why this answer

getopts uses a string of option characters; a colon after an option indicates it requires an argument. The correct usage is 'a:f:o' where a and f require arguments, but the stem doesn't specify arguments. Assuming -a and -f don't require arguments, the string should be 'afo'.

75
MCQmedium

A container is running and a technician needs to execute an interactive shell inside it. The container was started with 'docker run -d --name myapp myimage'. Which command will achieve this?

A.docker exec -it myapp /bin/bash
B.docker attach myapp
C.docker run -it --name myapp myimage /bin/bash
D.docker logs -f myapp
AnswerA

exec runs a new command in the running container; -it makes it interactive.

Why this answer

docker exec -it myapp /bin/bash runs an interactive shell inside the running container.

Page 1 of 2 · 106 questions totalNext →

Ready to test yourself?

Try a timed practice session using only Scripting, Containers, and Automation questions.