Curated

Automation, Orchestration, and Scripting

0 votes

Questions113

  1. Question 51

    Automation, Orchestration, and Scripting

    An administrator must print each account name and its login shell from /etc/passwd, where fields are separated by colons and the shell is the seventh field. Which loop reads the file correctly?

    Show the answer

    Correct answerB. while IFS=: read -r user _ _ _ _ _ shell; do echo "$user $shell"; done < /etc/passwd

    Setting IFS to a colon for the read command splits each line on colons, so the seventh variable receives the shell. FS is an awk variable, so bash ignores it and read still splits on whitespace, leaving the whole line in the first variable.

    Open this question
  2. Question 52

    Automation, Orchestration, and Scripting

    A service wrapper script uses case "$1" in with the first pattern *) echo "Usage: $0 start|stop"; ;; followed by start) and stop) branches. Running the script with start only prints the usage message. What is the cause, and how is it fixed?

    Show the answer

    Correct answerC. The * pattern matches every value and the first match wins, so it must be moved to the last position

    A case statement runs the first pattern that matches and then stops, so a leading * catches every argument. Placing *) last makes it the default. Double brackets are not used in case patterns, and ;; ends the case rather than continuing to later patterns.

    Open this question
  3. Question 53

    Automation, Orchestration, and Scripting

    A backup script must continue only when /var/backups exists and is a directory. If the path is a regular file or missing, the script must stop. Which test in an if statement meets this requirement?

    Show the answer

    Correct answerB. if [ -d /var/backups ]; then

    The -d operator is true only when the path exists and is a directory. The -e operator is the tempting choice, but it is also true for regular files and other file types, so the script would continue when it should stop.

    Open this question
  4. Question 54

    Automation, Orchestration, and Scripting

    A script prompts for confirmation and stores the reply in $answer. A case statement must run the same branch whether the user types y, Y, yes, YES or any other capitalization of yes. Which pattern line does this?

    Show the answer

    Correct answerB. [yY]|[yY][eE][sS])

    A pipe separates alternative patterns, and bracket expressions cover both cases of each letter, so this matches y and any capitalization of yes. The comma line is tempting but commas are literal in case patterns, and the other globs fail to match a lone y.

    Open this question
  5. Question 55

    Automation, Orchestration, and Scripting

    A script contains the line: if [ $disk_usage > 90 ]; then alert; fi. Each time it runs, the alert fires regardless of usage, and a file named 90 appears in the working directory. Which change fixes the comparison?

    Show the answer

    Correct answerA. Replace > with -gt

    Inside [ ], > is a shell redirection that creates the file 90, leaving [ $disk_usage ] as a one-argument test that is true for any non-empty value. The -gt operator performs the integer comparison. Quoting 90 does not stop the redirection.

    Open this question
  6. Question 56

    Automation, Orchestration, and Scripting

    A monitoring script must run its alert function when /var/log/app.log contains the string ERROR. The script must not print any matching lines. Which if statement is correct?

    Show the answer

    Correct answerD. if grep -q ERROR /var/log/app.log; then alert; fi

    The if statement tests the exit status of a command directly, and grep -q returns 0 on a match without printing. Wrapping grep in command substitution inside brackets is tempting, but -q produces no output, so the string is always empty and the test is always false.

    Open this question
  7. Question 57

    Automation, Orchestration, and Scripting

    What does this loop print? i=1; until [ $i -ge 4 ]; do echo $i; i=$((i+1)); done

    Show the answer

    Correct answerA. 1, 2 and 3, each on its own line

    The until loop runs while [ $i -ge 4 ] is false, so it prints 1, 2 and 3 and stops once i reaches 4. Printing 4 would need the test -gt 4, since -ge 4 is already true at that point.

    Open this question
  8. Question 58

    Automation, Orchestration, and Scripting

    In Bash, when does the loop while read -r line; do echo "$line"; done < hosts.txt stop?

    Show the answer

    Correct answerA. When read returns a nonzero status at end of file, after one pass per line

    AWhy this is right: read succeeds for each line and fails at end of file, which ends the while loop.

    BWhy not this: read returns 0 for a blank line and sets line to an empty string, so the loop goes on.

    CWhy not this: The redirection after done feeds the whole file to the loop, and read takes one line per pass.

    DWhy not this: while tests the condition command, read, not the commands in the body.

    The read builtin returns 0 when it reads a line and nonzero at end of file, so the while condition fails and the loop ends. The redirection after done feeds the file to the loop's standard input.

    Open this question
  9. Question 59

    Automation, Orchestration, and Scripting

    A script uses a for loop to ping each host listed in servers.txt. When a ping fails, the script must log the host and move on to the next host without ending the script. Which statement belongs after the logging command?

    Show the answer

    Correct answerD. continue

    The continue statement skips the rest of the current iteration and starts the next host. The break statement ends the whole loop, so the remaining hosts would never be checked.

    Open this question
  10. Question 60

    Automation, Orchestration, and Scripting

    A deployment script must pause until a build process creates the file /tmp/ready, then continue with the next step. Which loop does this correctly?

    Show the answer

    Correct answerB. until [ -f /tmp/ready ]; do sleep 5; done

    An until loop repeats while its condition is false, so it sleeps until the file exists and then exits. The while version repeats only while the file exists, so it would exit at once if the file is missing.

    Open this question
  11. Question 61

    Automation, Orchestration, and Scripting

    A directory contains log files with spaces in their names, such as "app server.log". Which loop compresses each .log file as a separate item without splitting the names?

    Show the answer

    Correct answerB. for f in *.log; do gzip "$f"; done

    A glob in the for list expands to one word per file, and quoting "$f" preserves spaces. Command substitution such as $(find ...) is word-split on whitespace, so "app server.log" would still break into two items even with the quotes.

    Open this question
  12. Question 62

    Automation, Orchestration, and Scripting

    A technician is writing a script that must run under Bash and abort when any command returns a nonzero status. Which first line of the file achieves this when the script is executed directly?

    Show the answer

    Correct answerA. #!/bin/bash -e

    A shebang is #! followed by the interpreter's absolute path and at most one optional argument, here -e. The forms with the characters reversed or missing are read as ordinary comments or invalid text, so the kernel will not use them to pick an interpreter.

    Open this question
  13. Question 63

    Automation, Orchestration, and Scripting

    A script named report.sh begins with #!/bin/sh and uses Bash arrays. On a Debian server, running ./report.sh fails with a syntax error, but it works on a RHEL server where /bin/sh points to Bash. What is the best fix?

    Show the answer

    Correct answerB. Change the first line to #!/bin/bash

    On Debian, /bin/sh is typically dash, which lacks Bash arrays, so the shebang must name Bash explicitly. Renaming the file changes nothing because the kernel reads the shebang, not the extension. A shebang on line two is just a comment, and the script already executes, so permissions are not the problem.

    Open this question
  14. Question 64

    Automation, Orchestration, and Scripting

    A script begins with #!/usr/bin/env python3. The user's PATH lists /opt/py312/bin before /usr/bin, and both contain python3. Which interpreter runs the script when the user executes it?

    Show the answer

    Correct answerC. /opt/py312/bin/python3, the first match in the user's PATH

    AWhy not this: env searches every directory in PATH in order, not only system directories.

    BWhy not this: PYTHONPATH sets where Python looks for modules; it does not choose the interpreter.

    CWhy this is right: Env runs the first python3 found in PATH, which here is in /opt/py312/bin.

    DWhy not this: env has no interpreter of its own; it finds and runs the named program.

    The env utility searches the directories in PATH for python3 and runs the first match, so the interpreter's location does not need to be hard-coded. A fixed path such as /usr/bin/python3 would ignore PATH.

    Open this question
  15. Question 65

    Automation, Orchestration, and Scripting

    A team deploys the same Python 3 script to several servers. On some, python3 is in /usr/bin, and on others it is in /usr/local/bin. Which shebang lets the script run on all of them without editing the file?

    Show the answer

    Correct answerA. #!/usr/bin/env python3

    The env utility sits at a consistent path and finds python3 by searching PATH, so the script works wherever the interpreter is installed. A hard-coded path fails on servers that install python3 elsewhere. The kernel does not expand commands in a shebang, and a bare name is not resolved through PATH.

    Open this question
  16. Question 66

    Automation, Orchestration, and Scripting

    A monitoring script stores a whole-number disk usage percentage in the variable usage. The script must take the alert branch when usage is 90 or higher. Which TWO tests do this correctly in Bash? (Choose two.)

    Choose 2 answers

    Show the answer

    Correct answersC. [ "$usage" -ge 90 ]F. [ "$usage" -gt 89 ]

    The -ge and -gt operators compare integers, so -ge 90 and -gt 89 both match any whole number of 90 or more. The > operator is a string comparison, so 100 sorts before 89 and the test gives wrong results; inside single brackets it is a redirection instead.

    Open this question
  17. Question 67

    Automation, Orchestration, and Scripting

    In Bash, what are the results of the tests [ 05 -eq 5 ] and [ 05 = 5 ]?

    Show the answer

    Correct answerA. The -eq test succeeds and the = test fails

    AWhy this is right: -eq compares integer values, so 05 equals 5, while = compares strings, and "05" is not "5".

    BWhy not this: = compares character by character, and the leading zero makes the strings differ.

    CWhy not this: -eq compares numbers, and 05 has the integer value 5, so that test succeeds.

    DWhy not this: The results are reversed; -eq compares numbers and = compares strings.

    The -eq operator converts both operands to integers, so 05 and 5 are equal. The = operator compares strings, and "05" differs from "5", so the test fails. Treating the two operators as interchangeable is a common mistake.

    Open this question
  18. Question 68

    Automation, Orchestration, and Scripting

    A script sets a=9 and b=10, then runs [[ $a > $b ]] && echo "a is larger". The script prints "a is larger". What explains this result?

    Show the answer

    Correct answerB. The values are compared as strings, and the character 9 sorts after the character 1.

    Inside [[ ]], > is a lexicographic string comparison, so "9" sorts after "10" because 9 follows 1 in character order. It does not convert to integers; numeric comparison needs -gt or an arithmetic (( )) expression.

    Open this question
  19. Question 69

    Automation, Orchestration, and Scripting

    A script reads a value into the variable count and must proceed only if the value consists entirely of digits. Which test performs this check using a regular expression?

    Show the answer

    Correct answerA. [[ "$count" =~ ^[0-9]+$ ]]

    The =~ operator performs extended regular expression matching and is available only inside [[ ]]. The single-bracket form fails because test does not support =~, and == in [[ ]] does glob matching, so the caret and plus sign would be treated as literal characters.

    Open this question
  20. Question 70

    Automation, Orchestration, and Scripting

    A script stores a pattern with pattern='^[0-9]+\.[0-9]+$' and must test whether the variable ver, such as 4.18, is a major.minor version number. Which test applies the stored pattern as a regular expression?

    Show the answer

    Correct answerC. [[ $ver =~ $pattern ]]

    The =~ operator in [[ ]] with an unquoted variable expands the pattern and treats it as an extended regex. Quoting the variable makes the match literal, so a value like 4.18 would not match.

    Open this question
  21. Question 71

    Automation, Orchestration, and Scripting

    A script reads a variable named user and must accept it only if it starts with a lowercase letter and is followed by 2 to 7 lowercase letters or digits, with nothing else before or after. Which test does this in Bash?

    Show the answer

    Correct answerA. [[ $user =~ ^[a-z][a-z0-9]{2,7}$ ]]

    The ^ and $ anchors force the whole string to fit the pattern, and an unquoted right side of =~ is treated as an extended regex. Without anchors, the match succeeds on any substring, so longer or malformed names would pass.

    Open this question
  22. Question 72

    Automation, Orchestration, and Scripting

    A script runs the following: file=backup-2024-06-15.tar.gz [[ $file =~ ^backup-([0-9]{4})-([0-9]{2})-([0-9]{2}) ]] echo "${BASH_REMATCH[2]}" What does the echo command print?

    Show the answer

    Correct answerC. 06

    After a successful =~ match, BASH_REMATCH[n] holds the nth parenthesized group, so index 2 is the second group, the month 06. Index 0 would hold the entire matched text, and index 1 would hold the year.

    Open this question
  23. Question 73

    Automation, Orchestration, and Scripting

    In Bash, the test [[ $input =~ "^[0-9]+$" ]] fails even when input is 123. Why?

    Show the answer

    Correct answerA. The quoted pattern is matched as a literal string, not as a regular expression

    AWhy this is right: Quoting the right side of =~ makes Bash match it literally, so ^, + and $ lose their meaning.

    BWhy not this: Single quotes also make the pattern literal; the pattern must be unquoted or held in a variable.

    CWhy not this: =~ works only inside [[ ]]; single brackets do not support it.

    DWhy not this: =~ uses POSIX extended regular expressions, which support the + quantifier.

    Since Bash 3.2, any quoted part of the pattern on the right of =~ is matched literally, so regex operators such as ^, $ and * lose their special meaning. Regex patterns must therefore be left unquoted or held in a variable.

    Open this question
  24. Question 74

    Automation, Orchestration, and Scripting

    A startup script must load /etc/app/env.conf with source, but only when that path exists as a regular file. Which line does this correctly?

    Show the answer

    Correct answerD. [ -f /etc/app/env.conf ] && source /etc/app/env.conf

    The -f operator is true only when the path exists and is a regular file. The -n operator tests only whether the string is non-empty, which is always true for a literal path, so source would run even when the file is missing.

    Open this question
  25. Question 75

    Automation, Orchestration, and Scripting

    In a Bash script, which test returns exit status 0 both when LOGDIR is set to an empty string and when LOGDIR is unset?

    Show the answer

    Correct answerD. [ -z "$LOGDIR" ]

    AWhy not this: -n succeeds when the string is not empty, so it returns 1 in both of these cases.

    BWhy not this: -d tests whether a directory exists; an empty path is not a directory, so it returns 1.

    CWhy not this: -v succeeds when the variable is set, even to an empty string, so it returns 1 when LOGDIR is unset.

    DWhy this is right: -z succeeds for a zero-length string, and a quoted unset variable expands to an empty string.

    The -z operator succeeds when the length of the string is zero, and a quoted unset variable expands to an empty string. It does not test file existence or distinguish unset from empty.

    Open this question
  26. Question 76

    Automation, Orchestration, and Scripting

    A backup script must stop with an error if the directory /var/backups is missing. Which test belongs in the if statement that triggers the error branch?

    Show the answer

    Correct answerD. [ ! -d /var/backups ]

    The ! operator negates the -d test, so the expression succeeds only when /var/backups is not an existing directory. Using ! with -f only checks that the path is not a regular file, which is also true for a directory, so the error would fire wrongly.

    Open this question
  27. Question 77

    Automation, Orchestration, and Scripting

    A script contains the line [ -n $CONFIG ] && echo "Config set". CONFIG is empty, yet the message still prints. What is the cause?

    Show the answer

    Correct answerD. The unquoted variable vanishes, leaving [ -n ], which is a single non-empty string.

    Without quotes, an empty variable expands to nothing, so the test sees only the string -n and a lone non-empty string is true. Quoting as "$CONFIG" keeps the empty argument. The -n operator tests for non-zero string length, not zero length.

    Open this question
  28. Question 78

    Automation, Orchestration, and Scripting

    Which three statements about managing variables and aliases in Bash are correct? (Choose three.)

    Choose 3 answers

    Show the answer

    Correct answersA. unset removes a shell variable so it no longer expands to a valueB. unalias ll removes the alias named ll from the current shell sessionC. local limits a variable's scope to the function in which it is declared

    AWhy this is right: unset deletes the variable, so it expands to an empty string afterward.

    BWhy this is right: unalias removes the named alias from the current shell session.

    CWhy this is right: local makes a variable exist only for the function that declares it and the functions it calls.

    DWhy not this: export passes a variable down to child processes; nothing a script does reaches its parent shell.

    EWhy not this: A script runs in a child shell, so aliases it defines disappear when it exits.

    FWhy not this: unset -v removes PATH entirely; nothing reloads /etc/profile, so commands then fail to be found.

    unset deletes a variable, unalias removes an alias from the current session, and local confines a variable to its function. export passes a variable down to child processes only, never up to the parent shell, so that statement is wrong. A script runs in a child shell, so its aliases vanish when it ends, and unset -v PATH removes PATH rather than restoring a default.

    Open this question
  29. Question 79

    Automation, Orchestration, and Scripting

    An administrator sets BACKUP_DIR=/mnt/backup at an interactive Bash prompt and then runs ./backup.sh. The script reads $BACKUP_DIR but finds it empty. Which change makes the value available to the script?

    Show the answer

    Correct answerD. Run export BACKUP_DIR=/mnt/backup before starting the script

    export marks the variable for the environment, so child processes such as the script inherit it. set with a name=value argument only replaces the positional parameters and does not create an environment variable, so the script still sees nothing.

    Open this question
  30. Question 80

    Automation, Orchestration, and Scripting

    A script contains these lines: grep -q "ERROR" app.log echo "Search complete" if [ $? -eq 0 ]; then echo "Errors found"; fi The script prints "Errors found" even when app.log contains no errors. What is the cause?

    Show the answer

    Correct answerB. $? holds the exit status of echo, not grep, and echo returned 0

    $? always holds the status of the most recently completed command, which here is echo, and it succeeded. grep -q does return nonzero when nothing matches, but that value was overwritten before the test ran. Capturing $? right after grep fixes it.

    Open this question
  31. Question 81

    Automation, Orchestration, and Scripting

    A script is run as ./copy.sh "my file.txt" notes.txt. It loops over its arguments with for f in $@, but it treats my and file.txt as separate items. Which loop header processes each argument as exactly one item?

    Show the answer

    Correct answerD. for f in "$@"

    Quoted "$@" expands to each positional parameter as a separate word, preserving embedded spaces. Quoted "$*" joins all arguments into one single word, and unquoted $* splits on spaces as the original loop did. $# is only the argument count.

    Open this question
  32. Question 82

    Automation, Orchestration, and Scripting

    On a freshly installed Ubuntu server, an administrator runs python3 -m venv /srv/app/.venv and receives an error that ensurepip is not available. Which action resolves the problem?

    Show the answer

    Correct answerA. Install the python3-venv package with the system package manager

    Debian-based distributions split ensurepip and venv support into the python3-venv package, so installing it lets venv create the environment. Reinstalling pip with ensurepip depends on the same missing component, and --system-site-packages only changes package visibility.

    Open this question
  33. Question 83

    Automation, Orchestration, and Scripting

    A cron job must run /opt/report/run.py using packages installed only in /opt/report/.venv. No shell activation step is available in the crontab. Which command line reliably uses the virtual environment's packages?

    Show the answer

    Correct answerD. /opt/report/.venv/bin/python /opt/report/run.py

    The python executable inside the venv's bin directory uses that environment's site-packages without any activation. The system interpreter ignores the venv, and changing the working directory does not change which interpreter or packages are used.

    Open this question
  34. Question 84

    Automation, Orchestration, and Scripting

    A developer has activated a Python virtual environment in /opt/tools/venv and runs deactivate. What is the result?

    Show the answer

    Correct answerD. The shell's earlier PATH and prompt return, and /opt/tools/venv stays on disk

    AWhy not this: deactivate does not delete anything; removing an environment means deleting its directory.

    BWhy not this: deactivate leaves installed packages in place, so the environment can be activated again later.

    CWhy not this: activate is sourced into the current shell rather than starting a subshell, so deactivate does not exit it.

    DWhy this is right: Deactivate restores PATH and the prompt and leaves the environment's files intact.

    The deactivate function reverts the shell's PATH and prompt to their earlier state and leaves the environment's files intact, so it can be activated again later. Removing the environment requires deleting its directory.

    Open this question
  35. Question 85

    Automation, Orchestration, and Scripting

    An administrator on Ubuntu Server must isolate a Python tool's dependencies in ~/proj/.venv and install the packages listed in requirements.txt. Which four actions are part of this procedure? (Choose four.)

    Choose 4 answers

    Show the answer

    Correct answersA. Run python3 -m venv ~/proj/.venv to create the environment directoryB. Install the python3-venv package if the distribution does not ship ensurepip with the default PythonC. Run source ~/proj/.venv/bin/activate to place the environment's bin directory first in PATHD. Run pip install -r requirements.txt while the environment is active

    venv is built in, may need python3-venv on Debian-based systems, is created with python3 -m venv, activated by sourcing activate, and pip then installs from the file. Running activate as a script fails because a child process cannot change the parent shell's PATH.

    Open this question
  36. Question 86

    Automation, Orchestration, and Scripting

    A developer has a working virtual environment on a test server. A production server must run the same application with identical package versions. Which approach BEST reproduces the dependencies?

    Show the answer

    Correct answerD. Run pip freeze in the test venv to create requirements.txt, then create a new venv on production and run pip install -r requirements.txt

    A frozen requirements file pins exact versions, and installing it into a fresh venv rebuilds the environment on the target. Copying a venv is fragile because it embeds absolute paths to the original interpreter, and omitting versions can install different releases.

    Open this question
  37. Question 87

    Automation, Orchestration, and Scripting

    An administrator is writing a script that must quickly find the IP address assigned to a given hostname, such as web01, from a collection of 50 host entries. Which data structure is best suited to this lookup?

    Show the answer

    Correct answerB. A dictionary with hostnames as keys

    A dictionary maps keys to values, so hosts['web01'] returns the address directly. A list of IP strings is tempting, but it has no link between a name and an address, so the script would need to search by position.

    Open this question
  38. Question 88

    Automation, Orchestration, and Scripting

    A monitoring script stores two integer values, used = 50 and total = 10, and then runs ratio = used / total in Python 3. What is the data type of ratio?

    Show the answer

    Correct answerB. Floating point

    In Python 3, the / operator always returns a floating-point value, so ratio is 5.0 even though both operands are integers. Integer is tempting, but only the // floor-division operator keeps integer operands as an integer.

    Open this question
  39. Question 89

    Automation, Orchestration, and Scripting

    A script defines services = ['sshd', 'nginx', 'cron']. Which of the following expressions evaluate to a value of the Boolean data type? (Choose three.)

    Choose 3 answers

    Show the answer

    Correct answersA. len(services) > 2C. 'nginx' in servicesD. services[0] == 'sshd'

    AWhy this is right: A comparison such as 3 > 2 evaluates to True, a Boolean.

    BWhy not this: len() returns the integer 3; it is truthy in a condition but its type is int.

    CWhy this is right: The in operator tests membership and returns True or False.

    DWhy this is right: The == comparison returns a Boolean, here True.

    EWhy not this: index() returns the integer position of the item, here 2.

    FWhy not this: Slicing a list returns a new list, here the last two items.

    Membership tests and comparison operators return True or False, which are Boolean values. len(services) is tempting because it appears in a condition, but it returns the integer 3, and only the comparison against 2 yields a Boolean. services.index('cron') returns the integer 2, and services[1:] returns a new list.

    Open this question
  40. Question 90

    Automation, Orchestration, and Scripting

    A script runs port = input('Port: ') and the user types 8080. The script then evaluates port > 1024 and fails with a TypeError. What is the data type of port?

    Show the answer

    Correct answerD. String

    input() always returns a string, so port holds '8080' and cannot be compared with an integer. Integer is tempting because the user typed digits, but the value is converted only when the script calls int().

    Open this question
  41. Question 91

    Automation, Orchestration, and Scripting

    A script reads the line line = 'root:x:0:0:root:/root:/bin/bash' from /etc/passwd and runs fields = line.split(':'). What is the data type of fields?

    Show the answer

    Correct answerC. List

    The split() method breaks a string at each delimiter and returns an ordered list of strings. String is tempting because the input is a string, but split() returns a list whose elements, such as fields[2], are each strings.

    Open this question
  42. Question 92

    Automation, Orchestration, and Scripting

    During code review of a log-processing script, a reviewer asks the author to rewrite a check to test whether the list named hosts is empty. Which statement follows the PEP 8 recommendation for this check?

    Show the answer

    Correct answerB. if not hosts:

    PEP 8 recommends relying on the fact that empty sequences are false, so if not hosts is preferred. Checking len(hosts) == 0 works but is explicitly discouraged. Comparing to False is also wrong because an empty list is falsy but not equal to False.

    Open this question
  43. Question 93

    Automation, Orchestration, and Scripting

    A Linux administrator is writing a Python script that checks disk health. A module-level value holding the number of SSH retry attempts is set once and never changed. Which line follows PEP 8 naming conventions for this value?

    Show the answer

    Correct answerB. MAX_RETRIES = 5

    PEP 8 names constants in all capital letters with underscores separating words, so MAX_RETRIES is correct. Lowercase with underscores is the style for ordinary variables and functions, so it does not signal a constant.

    Open this question
  44. Question 94

    Automation, Orchestration, and Scripting

    A teammate's backup automation script is flagged by a PEP 8 linter. Which two changes bring the script into conformance with PEP 8? (Choose two.)

    Choose 2 answers

    Show the answer

    Correct answersB. Name classes using CapWords styleE. Indent each nesting level with four spaces

    PEP 8 calls for four spaces per indentation level and CapWords for class names. Functions use lowercase_with_underscores, not mixedCase, which PEP 8 allows only to retain compatibility with existing code.

    Open this question
  45. Question 95

    Automation, Orchestration, and Scripting

    A junior administrator is writing a helper function for a monitoring script. The function takes a host name and a timeout that defaults to 5 seconds. Which definition follows PEP 8 whitespace recommendations?

    Show the answer

    Correct answerA. def check(host, timeout=5):

    PEP 8 says not to put spaces around = for a default value in an unannotated parameter, and not to put spaces before the opening parenthesis or just inside the parentheses. The version with spaces around = is the most tempting wrong option, but it breaks this rule.

    Open this question
  46. Question 96

    Automation, Orchestration, and Scripting

    A developer has uncommitted edits to tracked files on a feature branch when an urgent bug is reported on main. The edits are incomplete and must not be committed. Which approach lets the developer fix main and later resume the feature work?

    Show the answer

    Correct answerD. Run git stash, switch branches, and later run git stash pop

    git stash saves the uncommitted changes and restores a clean working tree, and git stash pop reapplies them afterward. Staging with git add is tempting, but the staged changes would carry over to main rather than being set aside.

    Open this question
  47. Question 97

    Automation, Orchestration, and Scripting

    A developer wants to see the commits teammates pushed to the remote's main branch without changing the files in the current branch. Which command should the developer run first?

    Show the answer

    Correct answerC. git fetch origin

    AWhy not this: git pull fetches and then merges or rebases into the current branch, which changes its files.

    BWhy not this: git merge changes the current branch, and without a fetch first origin/main is not up to date.

    CWhy this is right: Git fetch updates remote-tracking branches such as origin/main and leaves the working tree untouched.

    DWhy not this: git rebase rewrites the current branch onto origin/main, which changes its files.

    git fetch updates remote-tracking refs such as origin/main and leaves the local branch and working tree untouched. git pull differs because it runs a fetch followed by a merge or rebase into the current branch.

    Open this question
  48. Question 98

    Automation, Orchestration, and Scripting

    A developer committed app.log to a repository by mistake. They then added *.log to .gitignore, but git status still shows app.log as modified after each run of the application. What should the developer do to stop Git from tracking the file while keeping it on disk?

    Show the answer

    Correct answerC. Run git rm --cached app.log and commit the removal

    A .gitignore rule only affects untracked files, so an already tracked file must be removed from the index with git rm --cached, then committed. git clean is tempting, but it deletes untracked files and does not change what the index tracks.

    Open this question
  49. Question 99

    Automation, Orchestration, and Scripting

    A developer has made five work-in-progress commits on the feature-login branch. The team wants these changes to appear on main as a single commit. Which procedure achieves this?

    Show the answer

    Correct answerD. Run git merge --squash feature-login on main, then git commit

    git merge --squash stages the combined changes of the branch without creating a merge commit, so one git commit records them as a single commit. A --no-ff merge is tempting, but it keeps all five commits in main's history plus a merge commit.

    Open this question
  50. Question 100

    Automation, Orchestration, and Scripting

    Before committing a Bash script, a developer wants to catch unquoted variables, unused variables and inconsistent style. Which use of AI BEST fits this task?

    Show the answer

    Correct answerC. Use AI-assisted linting to flag unquoted or unused variables and style issues, then review each suggestion

    Code linting is an AI use case that identifies quality and style problems in scripts, with a human accepting or rejecting each change. Compliance recommendations address policy alignment, not code defects such as unquoted variables.

    Open this question