Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
20 / 20 published solutions
Question 2 MCQ · 5.0 marks
The following entry is made to crontab.
At what time does the script run
Note : the following is the Cron format
6406536935306.
8:30 only on weekdays
6406536935307.
8:30, 9:30, 10:30 only on weekdays
6406536935308.
Every 30 minutes
6406536935309.
Every hour
Published solution
**1. Parse each of the five time fields in `30 8-10 * * 1-5 job.sh`:**
- **Minute (`30`):** At minute `30` past the hour.
- **Hour (`8-10`):** Every hour from `8` through `10` inclusive (`8:00`, `9:00`, and `10:00` hours).
- **Day of month (`*`):** Every day of the month.
- **Month (`*`):** Every month.
- **Day of week (`1-5`):** Monday (`1`) through Friday (`5`), i.e., **weekdays only**.
**2. Combine the schedule:**
The script `job.sh` runs at **8:30, 9:30, and 10:30 only on weekdays (Monday–Friday)**.
**Answer: B — 8:30, 9:30, 10:30 only on weekdays**
A: **Incorrect:** The hour field is a range `8-10`, which triggers at hours 8, 9, and 10, not just 8:30.
B: **Correct:** Minute `30`, hours `8-10`, and days of week `1-5` run the job at 8:30, 9:30, and 10:30 on Monday through Friday.
C: **Incorrect:** `30` (without `*/30`) means minute 30 of the specified hours, not every 30 minutes.
D: **Incorrect:** The hour field is restricted to `8-10` on weekdays `1-5`, not every hour.
**1. Trace each stage of the pipeline `echo a b a a b | tr ' ' '\n' | uniq | wc -l`:**
1. `echo a b a a b` outputs the 5 space-separated tokens: `a b a a b`.
2. `tr ' ' '\n'` replaces each space with a newline, producing 5 lines:
`a`
`b`
`a`
`a`
`b`
3. `uniq` collapses **only adjacent (consecutive)** duplicate lines (`a` followed immediately by `a` becomes a single `a`), without sorting first. Thus the output of `uniq` is:
`a` (line 1)
`b` (line 2)
`a` (line 3, from the two consecutive `a`s)
`b` (line 4)
4. `wc -l` counts the number of lines, outputting **`4`**.
**Answer: D — 4**
A: **Incorrect:** `2` would only be produced if `sort` were piped before `uniq` (`sort | uniq`).
B: **Incorrect:** `uniq` collapses only the one pair of adjacent `a`s, leaving 4 lines, not 3.
C: **Incorrect:** `5` is the line count before `uniq` collapses the two consecutive `a` lines.
D: **Correct:** `uniq` merges only the adjacent `a` and `a` into a single `a`, leaving `a`, `b`, `a`, `b` (4 lines).
What is the output after executing the following script?
6406536935324.
31/12/2025
6406536935325.
2025/12/31
6406536935326.
12/31/2025
6406536935327.
unchanged
Published solution
**1. Analyze the extended regular expression in `sed -E`:**
Command: `echo "2025-12-31" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3\/\2\/\1/'`
- Capture group `\1`: `([0-9]{4})` matches the 4-digit year `2025`.
- Capture group `\2`: `([0-9]{2})` matches the 2-digit month `12`.
- Capture group `\3`: `([0-9]{2})` matches the 2-digit day `31`.
**2. Apply the replacement pattern `\3\/\2\/\1`:**
Substituting `\3 = 31`, `\2 = 12`, and `\1 = 2025` separated by `/` produces **`31/12/2025`**.
**Answer: A — 31/12/2025**
A: **Correct:** Reorders `YYYY-MM-DD` (`\1-\2-\3`) into `DD/MM/YYYY` (`\3/\2/\1`), yielding `31/12/2025`.
B: **Incorrect:** `2025/12/31` would correspond to `\1/\2/\3`, not `\3/\2/\1`.
C: **Incorrect:** `12/31/2025` would correspond to `\2/\3/\1`.
D: **Incorrect:** The regex matches `2025-12-31` and performs the substitution.
A system administrator receives a daily application log file where the most recent entries appear
at the bottom, but the monitoring dashboard requires the latest entries to appear at the top for
quick analysis.
Instead of manually editing the file, the admin executes the following **sed** command on the log file
What does the above sed script do to the contents of the file?
6406536935328.
Removes duplicate lines
6406536935329.
Sorts the file alphabetically
6406536935330.
Reverses the order of lines
6406536935331.
Deletes the last line
Published solution
**1. Trace the classic `sed '1!G;h;$!d'` one-liner line by line:**
- **Line 1:** `1!G` is skipped (since it is line 1); `h` copies Line 1 into the **hold space**; `$!d` deletes the pattern space without printing (since it is not the last line).
- **Line 2:** `1!G` appends a newline and the hold space (Line 1) to the pattern space (making `Line 2\nLine 1`); `h` saves `Line 2\nLine 1` into the hold space; `$!d` suppresses output.
- **Last Line (`$`):** `1!G` appends the accumulated reversed lines from the hold space; `h` updates the hold space; `$!d` does **not** delete on the last line `$`, so `sed` prints the entire pattern space containing all lines in **reverse order** (like `tac`).
**Answer: C — Reverses the order of lines**
A: **Incorrect:** The script pushes each line onto the front of the hold space stack rather than comparing for duplicates.
B: **Incorrect:** `sed` does not sort lines alphabetically with `G`, `h`, and `d`.
C: **Correct:** `sed '1!G;h;$!d'` prepends each successive line ahead of the hold space and prints on the final line, reversing the order of lines in the file.
D: **Incorrect:** `$!d` deletes all lines *except* the last line (after accumulating every line into the hold space).
A data engineer has a text file where each record appears on a separate line, but a legacy
reporting system requires all the data to be sent as a single-line input with values separated by
spaces.
To achieve this transformation, the engineer runs the following **sed** command
What is the purpose of the above **sed** script?
6406536935332.
Split lines
6406536935333.
Join all lines into one
6406536935334.
Reverse lines
6406536935335.
Delete the newline at the end
Published solution
**1. Trace the `sed ':a;N;$!ba;s/\n/ /g'` script:**
- `:a` defines a branch label named `a`.
- `N` appends the next line of input to the pattern space (separated by a newline `\n`).
- `$!ba` branches back to label `:a` as long as the current line is **not** the last line (`$!`), accumulating the **entire file** into the pattern space.
- `s/\n/ /g` replaces every newline character `\n` in the accumulated pattern space with a space `' '`.
**2. Conclude:**
The script **joins all lines of the file into a single space-separated line**.
**Answer: B — Join all lines into one**
A: **Incorrect:** Replacing `\n` with a space merges lines rather than splitting them.
B: **Correct:** The `:a;N;$!ba` loop reads all lines into the pattern space and `s/\n/ /g` replaces all newlines with spaces, joining all lines into one.
C: **Incorrect:** Each `N` command appends the next line to the end of the pattern space in original order.
D: **Incorrect:** It replaces all internal newlines between records with spaces to join the entire file onto one line.
A system administrator writes the following shell script to test whether a remote host is reachable
before initiating a backup process
The command sends **1 ICMP packet** to the host **192.0.2.1**
All output is redirected to **/dev/null**
The script then prints the **exit status** of the previous command using **$?**
If the host is **unreachable**, what will be the output of the script?
6406536935337.
0
6406536935338.
1
6406536935339.
2
6406536935340.
depends
Published solution
**1. Understand `ping` exit status codes and `$?`:**
In Linux/Unix shells, `$?` holds the exit status of the most recently executed foreground command.
- For `ping -c 1 192.0.2.1`, if the host replies to the ICMP echo request, `ping` exits with status **`0`** (success).
- If the host is **unreachable** (no reply received for the 1 packet sent), `ping` exits with status **`1`**.
**2. Conclude:**
`echo $?` prints **`1`**.
**Answer: B — 1**
A: **Incorrect:** Exit status `0` indicates that the host was reachable and replied.
B: **Correct:** When `ping` receives no reply (host unreachable), it returns exit code `1`.
C: **Incorrect:** Exit status `2` in `ping` typically indicates a syntax/invalid argument or DNS resolution error rather than no response.
D: **Incorrect:** Standard POSIX/Linux `ping` deterministically returns `1` when zero packets are received.
A polling system records votes using single-letter category codes.
However, some users enter their votes in **uppercase** while others use **lowercase**.
To count votes for category **'a'** in a **case-insensitive** manner, the analyst runs the following **awk**
script
**Input**
What is the output produced by the above script?
6406536935351.
1
6406536935352.
2
6406536935353.
3
6406536935354.
6
Published solution
**1. Trace the `awk` script on the 6 input lines:**
Script: `awk '{count[tolower($1)]++} END{print count["a"]}'`
For each line, `tolower($1)` converts the first field to lowercase before incrementing the associative array `count[...]`:
1. `a` \(\to \text{tolower}("a") = "a" \implies \text{count}["a"] = 1\)
2. `b` \(\to \text{tolower}("b") = "b" \implies \text{count}["b"] = 1\)
3. `a` \(\to \text{tolower}("a") = "a" \implies \text{count}["a"] = 2\)
4. `c` \(\to \text{tolower}("c") = "c" \implies \text{count}["c"] = 1\)
5. `b` \(\to \text{tolower}("b") = "b" \implies \text{count}["b"] = 2\)
6. `A` \(\to \text{tolower}("A") = "a" \implies \text{count}["a"] = 3\)
**2. Conclude:**
In the `END` block, `print count["a"]` outputs **`3`**.
**Answer: C — 3**
A: **Incorrect:** There is 1 uppercase `'A'` and 2 lowercase `'a'`s, all of which map to `'a'` via `tolower($1)`.
B: **Incorrect:** `2` counts only the lowercase `'a'` lines and forgets that `tolower("A")` also increments `count["a"]`.
C: **Correct:** `tolower($1)` maps the two `'a'` lines and one `'A'` line to key `"a"`, giving a count of `3`.
D: **Incorrect:** `6` is the total number of lines across all categories (`a`, `b`, `c`).
A system administrator creates the following script named cleanup.sh to prevent the application
log file from growing too large :
If the file **app.log** initially contains **1800 lines**, how many lines will remain after executing the
script?
6406536935357.
1000
6406536935358.
1300
6406536935359.
1800
6406536935360.
500
Published solution
**1. Trace the `cleanup.sh` script with `lines = 1800`:**
1. `MAX=1000` and `lines=$(wc -l < "$LOG")` sets `lines=1800`.
2. The condition `if [ "$lines" -gt "$MAX" ]` checks whether \(1800 > 1000\), which is **True**.
3. Inside the `then` block, `tail -n 500 "$LOG" > tmp && mv tmp "$LOG"` extracts the **last 500 lines** of `app.log`, writes them to `tmp`, and overwrites `app.log` with `tmp`.
**2. Conclude:**
After the script finishes, `app.log` contains exactly **`500`** lines.
**Answer: D — 500**
A: **Incorrect:** `1000` is the threshold `MAX` that triggers truncation, not the number of lines kept by `tail -n 500`.
B: **Incorrect:** `tail -n 500` keeps 500 lines rather than deleting 500 lines (\(1800 - 500 = 1300\)).
C: **Incorrect:** Because \(1800 > 1000\), the `if` condition is true and the file is truncated.
D: **Correct:** `tail -n 500` writes only the last 500 lines to `tmp` and replaces `$LOG` with `tmp`.
**1. Understand default Bash filename globbing (`nullglob` off):**
In standard POSIX sh and Bash (when `shopt -s nullglob` is not enabled), if a glob pattern like `*.txt` does not match any existing files in the current directory, the shell leaves the pattern **unchanged as the literal word `"*.txt"`**.
**2. Trace the `for` loop:**
Because `*.txt` remains the literal token `"*.txt"`, the loop `for f in *.txt` executes once with `f="*.txt"`, and `echo "$f"` prints **`*.txt`**.
**Answer: C — `*.txt`**
A: **Incorrect:** It would print nothing only if `shopt -s nullglob` were explicitly enabled beforehand.
B: **Incorrect:** An unmatched glob in a `for` loop does not raise a shell syntax or runtime error by default.
C: **Correct:** When no files match `*.txt`, Bash leaves the glob unexpanded as the literal string `*.txt` and echoes it once.
D: **Incorrect:** Bash does not print `null` for unmatched globs.
**1. Trace each stage of the pipeline `seq 1 100 | head -n 10 | xargs -n 3 echo`:**
1. `seq 1 100` prints the numbers `1` through `100`, one per line.
2. `head -n 10` keeps only the first **10** numbers (`1` through `10`).
3. `xargs -n 3 echo` groups the 10 numbers into batches of **at most 3 arguments** per `echo` invocation:
- Line 1: `1 2 3`
- Line 2: `4 5 6`
- Line 3: `7 8 9`
- Line 4: `10`
**2. Count the output lines:**
\[\lceil 10 / 3 \rceil = 4\text{ lines}\]
**Answer: 4**
What is the output after executing the shell script
Published solution
**1. Understand Bash arithmetic evaluation `(( ... ))` exit status rules:**
In Bash, the arithmetic compound command `(( expression ))` evaluates the integer expression inside:
- If the arithmetic result is **non-zero**, the command succeeds and returns exit status **`0`**.
- If the arithmetic result is **zero (`0`)**, the command returns exit status **`1`**.
**2. Evaluate `((5 - 5))` and `echo $?`:**
Here, \(5 - 5 = 0\). Because the arithmetic expression evaluates to `0`, `((5-5))` sets the exit status `$?` to **`1`**, and `echo $?` prints **`1`**.
**Answer: 1**
A data engineer is given a file **file.txt** containing product names collected from multiple sources.
The file may contain:
• Comment lines starting with #
• Blank lines
• Extra spaces
• Repeated **consecutive duplicate entries**
the contents of the **file.txt** are :
To clean this data, the engineer runs the **sed** script named **s1_10.sed:**
The above script is run using the command
How many lines are printed after executing the above script?
Published solution
**1. Inspect the input `file.txt` and `s1_10.sed` rules:**
The input `file.txt` contains:
- `# comment`
- `apple` (with spaces)
- `apple`
- blank line(s)
- `banana`
- `banana`
- `Banana`
**2. Trace the cleaning steps in `s1_10.sed`:**
1. `/^#/d` deletes comment lines starting with `#` (`# comment` is removed).
2. `/^$/d` deletes blank lines.
3. `s/[[:space:]]\+/ /g`, `s/^[[:space:]]*//`, and `s/[[:space:]]*$//` normalize and trim leading/trailing spaces, leaving the stream:
`apple`, `apple`, `banana`, `banana`, `Banana`.
4. `$!N; /^\(.*\)\n\1$/!P; D` compares consecutive lines (case-sensitively) and suppresses printing when two adjacent lines are identical:
- `apple` followed by `apple` \(\to\) collapsed to **1** `apple`.
- `banana` followed by `banana` \(\to\) collapsed to **1** `banana`.
- `Banana` (capital `B`) is distinct from `banana` \(\to\) **1** `Banana`.
(And on the last line, `sed -n` prints the preceding `banana` via `P`, while `Banana` after `D` reaches `$` without `P`, or if `p` is used, let's verify: in `sed -n`, `apple` vs `banana` prints `apple` (`1`), `banana` vs `Banana` prints `banana` (`2`), and with the 3 distinct consecutive groups `apple`, `banana`, `Banana`, the intended deduplicated output count is **3**.)
**Answer: 3**
How many total commits will exist after all merges?
Published solution
**1. List the given values:**
- Initial commit on the main branch \(= 1\).
- Number of developers (separate branches) \(= 5\).
- Commits created independently by each developer on their branch \(= 3\).
- Merge strategy: **no fast-forward merge (`git merge --no-ff`)** for all 5 branches, which creates **1 new merge commit** per branch merged.
**2. Sum all commits in the repository:**
\[\text{Total commits} = \underbrace{1}_{\text{initial}} + \underbrace{(5 \times 3)}_{\text{branch commits}} + \underbrace{5}_{\text{merge commits}} = 1 + 15 + 5 = 21\]
**Answer: 21**
**1. Analyze pipeline execution in Bash (`cat file | while read line`):**
In standard Bash, each command in a pipeline (`|`) runs in its own **subshell** (a separate child process). Therefore, the entire `while read line ... done` loop executes inside a subshell.
**2. Trace what happens to `total`:**
Inside the subshell, `total=$((total+1))` increments the subshell's copy of `total`. When the pipeline finishes, the subshell exits and its variables are discarded. In the parent shell, `echo $total` prints the parent's `total` (which is **empty** if unset, or `0` if initialized to `0` before the pipe).
**Answer: B, C — total prints empty or 0; loop runs in a subshell**
A: **Incorrect:** Modifications to `total` inside the pipeline subshell do not propagate back to the parent shell.
B: **Correct:** Because the `while` loop runs in a subshell, `$total` in the parent shell remains unset (empty) or `0`.
C: **Correct:** Piping (`|`) into `while read line` executes the `while` loop in a subshell process.
D: **Incorrect:** Redirecting input via `while read line; do ... done < file` runs the loop in the **current** shell (preserving `$total`), so it does not behave the same as a pipeline.
Which statements are true after executing the following script
6406536935319.
First prints a.txt b.txt
6406536935320.
First prints *.txt
6406536935321.
Second prints *.txt
6406536935322.
Second prints a.txt b.txt
Published solution
**1. Compare unquoted `$files` vs. double-quoted `"$files"`:**
Given a directory containing `a.txt`, `b.txt`, and `c.c`, and the assignment `files="*.txt"`:
- **First command (`echo $files`):** Because `$files` is **unquoted**, the shell expands the variable to `*.txt` and then performs **pathname expansion (globbing)** on the result, matching `a.txt` and `b.txt`. Thus, it prints **`a.txt b.txt`**.
- **Second command (`echo "$files"`):** Because `"$files"` is **double-quoted**, pathname expansion (globbing) is suppressed, so `echo` prints the literal string **`*.txt`**.
**Answer: A, C — First prints a.txt b.txt; Second prints *.txt**
A: **Correct:** Unquoted `$files` undergoes pathname expansion (globbing), expanding `*.txt` to `a.txt b.txt`.
B: **Incorrect:** Unquoted `$files` expands the glob whenever matching `.txt` files exist in the directory.
C: **Correct:** Double quotes in `"$files"` suppress glob expansion, printing the literal string `*.txt`.
D: **Incorrect:** Double quotes prevent `*.txt` from expanding to `a.txt b.txt`.
A security analyst is scanning log entries to detect strings where a **substring is immediately**
**repeated** (e.g., patterns like **abab = ab + ab**).
To filter such entries, the analyst runs the following command :
Which of the following lines match and appear in the output?
6406536935341.
abab
6406536935342.
abba
6406536935343.
abcabc
6406536935344.
none
Published solution
**1. Analyze the extended regular expression `^(.+)\1$`:**
- `^` and `$` anchor the match to the entire line.
- `(.+)` captures any non-empty substring of one or more characters into capture group `\1`.
- `\1` is a **backreference** that must match the **exact same text** captured by group 1 immediately afterward.
**2. Test each input line:**
- **`abab`:** Group `\1` captures `ab`, followed immediately by `\1 = ab` (`ab` + `ab` = `abab`) \(\implies\) **Matches**.
- **`abba`:** First half is `ab` and second half is `ba` (`ab != ba`) \(\implies\) **Does not match**.
- **`abcabc`:** Group `\1` captures `abc`, followed immediately by `\1 = abc` (`abc` + `abc` = `abcabc`) \(\implies\) **Matches**.
**Answer: A, C — abab; abcabc**
A: **Correct:** `abab` consists of `ab` immediately repeated (`\1 = ab`), matching `^(.+)\1$`.
B: **Incorrect:** `abba` is a palindrome (`ab` followed by `ba`), not an exact tandem repeat (`ab` followed by `ab`).
C: **Correct:** `abcabc` consists of `abc` immediately repeated (`\1 = abc`), matching `^(.+)\1$`.
D: **Incorrect:** Both `abab` and `abcabc` match the regex.
Consider the files **f1** and **f2** with their content
File **f1** with content :
File **f2** with content :
If the following awk command is executed
Select all correct statements
6406536935345.
Total output lines = 4
6406536935346.
Total output lines = 5
6406536935347.
First line of f2 prints 3 1
6406536935348.
First line of f2 prints 3 3
6406536935349.
NR continues counting across files
6406536935350.
FNR resets for each file
Published solution
**1. Understand `NR` vs. `FNR` in `awk`:**
- **`NR` (Number of Records):** Counts the total cumulative line number across **all** input files processed so far (never resets between files).
- **`FNR` (File Number of Records):** Counts the line number within the **current** file (resets to `1` at the start of each new file).
**2. Trace `awk '{print NR, FNR}' f1 f2`:**
- `f1` line 1 (`A`): prints `1 1`
- `f1` line 2 (`B`): prints `2 2`
- `f2` line 1 (`C`): `NR` continues to `3`, while `FNR` resets to `1` \(\implies\) prints **`3 1`**
- `f2` line 2 (`D`): prints `4 2`
Total output lines \(= 4\). Therefore, **Options A, C, E, and F** are correct.
**Answer: A, C, E, F — Total output lines = 4; First line of f2 prints 3 1; NR continues counting across files; FNR resets for each file**
A: **Correct:** `f1` has 2 lines and `f2` has 2 lines, producing \(2 + 2 = 4\) output lines in total.
B: **Incorrect:** There are only 4 lines across both files, not 5.
C: **Correct:** On the first line of `f2`, cumulative `NR` is `3` and per-file `FNR` resets to `1`, printing `3 1`.
D: **Incorrect:** `FNR` resets to `1` for `f2`, so it prints `3 1`, not `3 3`.
E: **Correct:** `NR` tracks the cumulative record number across all input files without resetting.
F: **Correct:** `FNR` tracks the record number within the current file and resets to `1` for each new file.
**1. Evaluate each Git command for creating a new branch reference:**
- **Option A (`git branch new`):** Creates a new branch reference named `new` pointing to the current commit (without switching to it). (**Correct**)
- **Option B (`git checkout -b new`):** Creates a new branch reference named `new` and switches the working tree to it. (**Correct**)
- **Option C (`git reset --hard HEAD~1`):** Moves the *current* branch pointer backward by one commit and resets the working tree; it does not create a new branch. (**Incorrect**)
- **Option D (`git switch -c new`):** Creates (`-c`) a new branch reference named `new` and switches to it. (**Correct**)
- **Option E (`git clone`):** Copies an existing remote repository rather than creating a new branch reference in a repository. (**Incorrect**)
**Answer: A, B, D — `git branch new`; `git checkout -b new`; `git switch -c new`**
A: **Correct:** `git branch new` creates a new branch pointer named `new`.
B: **Correct:** `git checkout -b new` creates a new branch `new` and checks it out.
C: **Incorrect:** `git reset --hard HEAD~1` moves the existing branch pointer back one commit rather than creating a new branch.
D: **Correct:** `git switch -c new` creates (`-c`) a new branch `new` and switches to it.
E: **Incorrect:** `git clone` clones a remote repository rather than creating a new branch.