System Commands · End Term · May 2026 · FN
System Commands · End Term · May 2026 FN
← Course papers · Start practice / exam
Questions and published explanations below are available without starting a test. Some questions may not have a published solution yet.
23 / 23 published solutions
Which of the following **octal permission values** grants the specified permissions?
• **User:** Read, Write, and Execute
• **Group:** Read-only
• **Others:** Read-only
**1. Convert each permission class into its 3-bit octal digit:**
In Unix/Linux file permissions, each permission class (**User**, **Group**, **Others**) is represented by an octal digit obtained by summing Read (`r = 4`), Write (`w = 2`), and Execute (`x = 1`):
- **User (Read, Write, and Execute — `rwx`):** \(4 + 2 + 1 = 7\)
- **Group (Read-only — `r--`):** \(4 + 0 + 0 = 4\)
- **Others (Read-only — `r--`):** \(4 + 0 + 0 = 4\)
**2. Combine the three digits:**
Combining User, Group, and Others in order gives the octal permission value **744**.
**Answer: D — 744.**
A: **Incorrect:** `644` gives User Read and Write (`4 + 2 = 6`), omitting Execute (`1`) permission for the User.
B: **Incorrect:** `700` gives Group and Others no permissions (`0`) instead of Read-only (`4`).
C: **Incorrect:** `755` grants Execute (`1`) permission to Group and Others (`4 + 1 = 5`) in addition to Read.
D: **Correct:** User `rwx` is \(4+2+1=7\), Group `r--` is \(4\), and Others `r--` is \(4\), yielding `744`.
Question 3 MCQ · 4.0 marks
You open a terminal session inside a directory
. Executing an
command lists the following files:
You execute the following shell command involving a single-character wildcard glob (?) and what
all files will be listed in the output?
Published solution
**1. Understand the single-character glob wildcard `?` in Bash:**
In shell filename expansion (globbing), the question mark `?` matches **any single character** (exactly 1 character, not 0 and not 2 or more).
Therefore, the pattern `a?.txt` matches any filename in the current directory that:
1. Starts with the letter `a`,
2. Followed by **exactly one** character,
3. Followed by `.txt`.
**2. Check each file in `/home/user/project`:**
- `a.txt`: Has **0** characters between `a` and `.txt` (does not match).
- `ab.txt`: Has **1** character (`b`) between `a` and `.txt` (**matches!**).
- `abc.txt`: Has **2** characters (`bc`) between `a` and `.txt` (does not match).
- `a1.txt`: Has **1** character (`1`) between `a` and `.txt` (**matches!**).
- `b.txt`: Starts with `b`, not `a` (does not match).
Sorted alphabetically by shell expansion, `echo a?.txt` outputs `a1.txt` and `ab.txt`.
**Answer: B — a1.txt ab.txt.**
A: **Incorrect:** `a.txt` has zero characters after `a` before `.txt`, whereas `?` requires exactly one character.
B: **Correct:** Both `a1.txt` and `ab.txt` have a single character after `a` followed by `.txt`.
C: **Incorrect:** `abc.txt` has two characters (`bc`) after `a`, so it does not match a single `?`.
D: **Incorrect:** `a.txt` does not match `a?.txt` because `?` cannot match zero characters.
Question 4 MCQ · 4.0 marks
Consider the following project directory layout containing regular files and subdirectories, some of
which are empty:
Which find command specifically targets and lists only zero-byte regular files, while explicitly
ignoring empty directories?
Published solution
**1. Understand the `find` flags for file type and empty size:**
To search from the current directory (`.`) for **zero-byte regular files** while ignoring empty directories, we need two predicates in `find`:
- `-type f`: Restricts matches strictly to **regular files** (excluding directories, which are `-type d`).
- `-empty`: Matches files that are empty (size 0 bytes).
**2. Compare the four `find` commands:**
- **Option A (`find . -type f -empty`):** Finds only empty regular files (0 bytes), ignoring empty directories. (Correct!)
- **Option B (`find . -type d -empty`):** Finds only empty *directories*, ignoring regular files.
- **Option C (`find . -empty`):** Finds *both* empty regular files and empty directories because `-type f` is omitted.
- **Option D (`find . -type f -mtime 0`):** Filters regular files modified within the last 24 hours (`-mtime 0`), regardless of whether they are zero-byte files.
**Answer: A — find . -type f -empty.**
A: **Correct:** Combining `-type f` (regular files only) with `-empty` (0 bytes) lists only zero-byte regular files and ignores empty directories.
B: **Incorrect:** `-type d` selects directories rather than regular files.
C: **Incorrect:** Without `-type f`, `find . -empty` returns both empty directories (`empty_dir/`) and empty files.
D: **Incorrect:** `-mtime 0` checks modification time (last 24 hours), not file size.
Question 5 MCQ · 4.0 marks
During an interactive Bash shell session, a user executes several commands over a few hours. By
default, Bash stores executed commands in an in-memory buffer during the active session. When
the user exits the terminal session gracefully (e.g., executing
or closing the shell), Bash
appends these buffered commands to a hidden history file located in the user's home directory.
Which standard environment file is automatically updated by Bash upon terminal exit to store
these previously executed commands?
Published solution
**1. Recall the standard Bash configuration and history files:**
During an interactive Bash session, executed commands are kept in memory and written out upon shell exit to the file specified by `HISTFILE`, whose default path in the user's home directory is:
`~/.bash_history`
**2. Distinguish from other startup/cleanup files:**
- `~/.bashrc`: Executed when starting an interactive non-login shell to configure aliases, functions, and environment settings.
- `~/.bash_logout`: Executed when a login shell exits to run cleanup commands (does not store the command history list).
- `~/.history`: Not the default history file name for Bash (`~/.bash_history` is).
**Answer: B — ~/.bash_history.**
A: **Incorrect:** `~/.bashrc` is the startup configuration script for interactive shells, not the command history log.
B: **Correct:** By default, Bash saves the in-memory command history list to `~/.bash_history` when an interactive session exits.
C: **Incorrect:** `~/.bash_logout` is a script executed on login shell exit, not the history storage file.
D: **Incorrect:** Bash uses `~/.bash_history` by default rather than `~/.history`.
Question 6 MCQ · 4.0 marks
As a system administrator logging into an unprivileged user session, you need to audit scheduled
automated tasks without editing or modifying the system configuration files. You run a command
to list the active crontab contents for your current user.
Which command was executed to print the scheduled cron jobs to the stdout shown above
without opening an interactive editor?
Published solution
**1. Recall the `crontab` command-line options:**
The `crontab` utility manages per-user scheduled cron tables:
- `crontab -l`: **Lists** (displays) the current user's crontab contents to standard output without opening an editor.
- `crontab -e`: Opens the current user's crontab in an interactive text editor.
- `crontab -r`: Removes the current user's crontab.
**2. Check the given options:**
`crontab -l` is the exact command that prints the active user's scheduled cron jobs to stdout.
**Answer: C — crontab -l.**
A: **Incorrect:** `cron` is the background daemon itself, not the user utility for viewing a user's crontab.
B: **Incorrect:** `-v` is not the standard POSIX/Vixie flag to list crontab entries (`-l` is).
C: **Correct:** `crontab -l` prints the current user's crontab file to standard output without modifying it.
D: **Incorrect:** There is no standard Linux command named `cronjob`.
Question 7 MCQ · 4.0 marks
Match the AWK functions with their purposes.
1-C, 2-D, 3-A, 4-B, 5-E
1-B, 2-C, 3-D, 4-E, 5-A
1-C, 2-A, 3-D, 4-B, 5-E
1-D, 2-C, 3-A, 4-B, 5-E
Published solution
**1. Match each built-in AWK string function with its purpose:**
- **1. `length(s)`:** Returns the number of characters in string `s` \(\implies\) **C**.
- **2. `substr(s, m, n)`:** Extracts and returns a substring (portion) of string `s` \(\implies\) **D**.
- **3. `index(s, t)`:** Returns the 1-based starting character position of substring `t` inside `s` (or 0 if not found) \(\implies\) **A**.
- **4. `split(s, arr, sep)`:** Divides string `s` into elements of array `arr` using separator `sep` \(\implies\) **B**.
- **5. `tolower(s)`:** Returns a copy of string `s` with all uppercase characters converted to lowercase \(\implies\) **E**.
Combining these gives **1-C, 2-D, 3-A, 4-B, 5-E**.
**Answer: A — 1-C, 2-D, 3-A, 4-B, 5-E.**
A: **Correct:** `length()` -> C, `substr()` -> D, `index()` -> A, `split()` -> B, and `tolower()` -> E.
B: **Incorrect:** Mismatches all five AWK string functions.
C: **Incorrect:** Swaps `substr()` (2-D) and `index()` (3-A).
D: **Incorrect:** Swaps `length()` (1-C) and `substr()` (2-D).
Question 8 MCQ · 4.0 marks
Suppose
shows:
What will
commit?
Only app.py
Only README.md
Both files
Nothing
Published solution
**1. Understand the Git staging area and `git commit -m`:**
In Git, `git status` separates modified working-tree files into two categories:
1. **`Changes to be committed` (Staged changes in the index):** `modified: app.py`
2. **`Changes not staged for commit` (Unstaged changes in the working directory):** `modified: README.md`
**2. Determine what `git commit -m "Update"` commits:**
When `git commit -m "Update"` is run **without** the `-a` flag (and without file path arguments), Git commits **only the changes that are currently staged** in the index (`Changes to be committed`).
Therefore, **only `app.py`** is committed, while `README.md` remains modified and unstaged.
**Answer: A — Only app.py.**
A: **Correct:** `git commit -m` commits only the staged changes (`Changes to be committed`), which is `app.py`.
B: **Incorrect:** `README.md` is under `Changes not staged for commit`, so it is not included in the commit.
C: **Incorrect:** Committing both modified tracked files without staging `README.md` first would require `git commit -a -m "Update"`.
D: **Incorrect:** Because `app.py` is staged, the commit succeeds and records the changes to `app.py`.
Question 9 MCQ · 4.0 marks
A user customizes the Bash prompt using:
The user is logged in as "alice" on host "server1", and the current directory is "/home/alice".
What will the prompt display?
Published solution
**1. Decode each Bash prompt (`PS1`) escape sequence:**
We are given `PS1="\h, \u:\w\$ "` where user is `alice`, hostname is `server1`, and current working directory is `/home/alice`:
- `\h`: Expands to the hostname up to the first dot \(\implies\) `server1`
- `, `: Literal comma and space \(\implies\) `, `
- `\u`: Expands to the username of the current user \(\implies\) `alice`
- `:`: Literal colon \(\implies\) `:`
- `\w`: Expands to the current working directory (note: in the exam options, `/home/alice` is shown literally as `/home/alice`)
- `\$`: Displays `#` if the effective UID is `0` (root), or `$` for an ordinary user (`alice`) \(\implies\) `$ `
**2. Assemble the resulting prompt string:**
`server1, alice:/home/alice$ `
**Answer: B — server1, alice:/home/alice$.**
A: **Incorrect:** This corresponds to `\u@\h:\w\$ `, which puts username before hostname separated by `@`.
B: **Correct:** `\h` expands to `server1`, `\u` expands to `alice`, `\w` shows `/home/alice`, and `\$` shows `$`.
C: **Incorrect:** This swaps `\h` (hostname `server1`) and `\u` (username `alice`).
D: **Incorrect:** The prompt format string uses `, ` between `\h` and `\u`, not `@`.
Question 10 MCQ · 4.0 marks
Match the following RAID levels with their corresponding characteristics.
Which of the following is the correct matching?
1-B, 2-A, 3-C, 4-D
1-A, 2-B, 3-D, 4-C
1-C, 2-A, 3-B, 4-D
1-B, 2-C, 3-A, 4-D
Published solution
**1. Match each RAID level with its definition:**
- **1. RAID 0:** Block-level **striping without redundancy**; if any single disk fails, data across the array is lost \(\implies\) **B**.
- **2. RAID 1:** Disk **mirroring**; duplicates data across disks and can tolerate a single disk failure \(\implies\) **A**.
- **3. RAID 5:** Block-level **striping with single distributed parity** across all disks \(\implies\) **C**.
- **4. RAID 10 (RAID 1+0):** **Combination of mirroring and striping** (a stripe of mirrors) \(\implies\) **D**.
Thus, the correct matching is **1-B, 2-A, 3-C, 4-D**.
**Answer: A — 1-B, 2-A, 3-C, 4-D.**
A: **Correct:** RAID 0 is striping without redundancy (B), RAID 1 is mirroring (A), RAID 5 is distributed single parity (C), and RAID 10 combines mirroring and striping (D).
B: **Incorrect:** Swaps RAID 0 and RAID 1, as well as RAID 5 and RAID 10.
C: **Incorrect:** RAID 0 has no parity (B, not C).
D: **Incorrect:** RAID 1 is mirroring (A, not C) and RAID 5 is distributed parity (C, not A).
Question 11 MCQ · 5.0 marks
A developer runs a multi-stage pipeline to analyze their most frequently used shell commands:
Sample output generated by the pipeline:
Which specific component of this pipeline is explicitly responsible for parsing each line of the raw
history output stream and isolating only the command name?
Published solution
**1. Trace each stage of the shell pipeline:**
`history | awk '{print $2}' | sort | uniq -c | sort -nr | head`
- `history`: Outputs numbered history lines where field `$1` is the line number (e.g., `101`) and field `$2` is the command name (e.g., `git`).
- `awk '{print $2}'`: Splits each line of `history` on whitespace and **prints only the second field (`$2`)**, which isolates the command name itself!
- `sort`: Sorts the command names alphabetically so identical commands are adjacent.
- `uniq -c`: Collapses adjacent identical command names and prefixes each with its occurrence count.
- `sort -nr`: Sorts the counted lines numerically in reverse (descending) order.
- `head`: Displays the top 10 most frequent commands.
**2. Conclude:**
The component that parses each line of `history` and extracts only the command name is `awk '{print $2}'`.
**Answer: D — awk '{print $2}'.**
A: **Incorrect:** `history` outputs the raw numbered command lines including line numbers and arguments.
B: **Incorrect:** `sort -nr` sorts the aggregated frequency counts in descending numerical order.
C: **Incorrect:** `uniq -c` counts consecutive identical lines after they have already been extracted and sorted.
D: **Correct:** `awk '{print $2}'` extracts the second whitespace-separated field (the command name) from each line of `history`.
Question 12 MCQ · 5.0 marks
Consider a file named
containing pairs of lowercase words separated by a space:
You want to use **Basic Regular Expression (BRE)** with grep to match only those lines where the
second word is exactly the same as the first word.
Which command correctly performs this pattern matching?
Published solution
**1. Understand backreferences in Basic Regular Expressions (BRE) with `grep`:**
We want to match lines consisting of two lowercase words separated by a space where the second word is identical to the first word (e.g., `abc abc`).
In **Basic Regular Expressions (`grep` without `-E`)**:
- Capturing parentheses must be escaped with backslashes: `\([a-z]*\)` captures the first word as Group 1.
- The backreference `\1` matches the exact text captured by Group 1.
- Anchoring with `^` at the start and `$` at the end ensures the entire line consists of the word, a space, and its exact repetition:
`grep '^\([a-z]*\) \1$' file.txt`
**2. Compare the options:**
- **Option A (`grep '^\([a-z]*\) \1$' file.txt`):** Properly escapes `\( ... \)` in BRE and uses `\1` to match the first captured group. (Correct!)
- **Option B (`grep -E '^([a-z]*) \2$' file.txt`):** Uses `` (Group 2), which does not exist since there is only one capturing group.
- **Option C (`grep '^[a-z]* \1$' file.txt`):** Omits the capturing parentheses `\( ... \)`, so `\1` has no group to reference.
- **Option D (`grep '^([a-z]*) \1$' file.txt`):** Without `-E`, unescaped `(` and `)` match literal parenthesis characters instead of creating a capturing group.
**Answer: A — grep '^\([a-z]*\) \1$' file.txt.**
A: **Correct:** In BRE, `\([a-z]*\)` captures the first lowercase word as Group 1, and `\1` requires the second word before `$` to be identical.
B: **Incorrect:** References `\2` even though only one capturing group `([a-z]*)` is defined.
C: **Incorrect:** Using `\1` without defining a capturing group `\( ... \)` fails to match.
D: **Incorrect:** In standard `grep` (BRE), unescaped `(` and `)` match literal parentheses rather than grouping.
Question 13 MCQ · 5.0 marks
Consider the following Bash script:
The script is executed as:
What will be the output?
Matching files: 1
Matching files: 2
Matching files: 3
Matching files: 4
Published solution
**1. Analyze the condition inside the Bash `for` loop:**
For each command-line argument `arg` in `"$@"`, the `if` statement checks:
`[[ "$arg" == *.txt && "$arg" != temp* ]]`
This condition is true if and only if:
1. The filename ends with `.txt` (`== *.txt`), **AND**
2. The filename does **not** start with `temp` (`!= temp*`).
**2. Test each of the 5 passed arguments:**
`./check.sh notes.txt temp.txt data.txt temp_data.txt report.pdf`
1. `notes.txt`: Ends with `.txt` AND does not start with `temp` \(\implies\) **Match #1** (`count = 1`).
2. `temp.txt`: Ends with `.txt`, but starts with `temp` \(\implies\) Rejected.
3. `data.txt`: Ends with `.txt` AND does not start with `temp` \(\implies\) **Match #2** (`count = 2`).
4. `temp_data.txt`: Starts with `temp` \(\implies\) Rejected.
5. `report.pdf`: Does not end with `.txt` \(\implies\) Rejected.
Thus, the script prints `Matching files: 2`.
**Answer: B — Matching files: 2.**
A: **Incorrect:** Both `notes.txt` and `data.txt` satisfy the condition, so the count is 2, not 1.
B: **Correct:** Out of the 5 arguments, only `notes.txt` and `data.txt` end with `.txt` and do not start with `temp`, incrementing `count` to 2.
C: **Incorrect:** `temp.txt` and `temp_data.txt` are excluded by `"$arg" != temp*`.
D: **Incorrect:** Only 2 of the four `.txt` files do not begin with `temp`.
Question 14 MCQ · 5.0 marks
What does the expression below mean?
Run at 8:30 AM Monday through Friday
Run at 8:30 PM Monday through Friday
Run every 30 minutes on weekdays
Run only on the 1st and 5th day of each month
Published solution
**1. Decode the 5 fields of the cron schedule expression:**
A standard crontab line has 5 time fields: `Minute Hour Day-of-Month Month Day-of-Week Command`.
For `30 8 * * 1-5 /report.sh`:
1. **Minute (`30`):** At minute 30 past the hour.
2. **Hour (`8`):** At 8 AM (in 24-hour time, `8` is 8:00 AM, whereas 8:00 PM is `20`).
3. **Day of Month (`*`):** Every day of the month (when matching the day-of-week field).
4. **Month (`*`):** Every month.
5. **Day of Week (`1-5`):** Monday (`1`) through Friday (`5`).
**2. Combine into plain English:**
Run `/report.sh` at **8:30 AM, Monday through Friday**.
**Answer: A — Run at 8:30 AM Monday through Friday.**
A: **Correct:** Minute `30`, hour `8` (8:30 AM), and day-of-week `1-5` (Monday through Friday) schedules the script for 8:30 AM on weekdays.
B: **Incorrect:** 8:30 PM in 24-hour cron format requires hour `20` (`30 20 * * 1-5`), not `8`.
C: **Incorrect:** Running every 30 minutes would use `*/30 * * * 1-5`.
D: **Incorrect:** `1-5` is in the 5th field (Day of Week), not the 3rd field (Day of Month).
Question 15 MCQ · 5.0 marks
What is the output of the following command?
Published solution
**1. Trace the `sed` substitution command with capturing groups:**
We run:
`echo "abc123" | sed 's/\([a-z]*\)\([0-9]*\)/\2-\1/'`
- **Group 1 (`\([a-z]*\)`):** Matches the sequence of lowercase letters `"abc"`, so `\1 = abc`.
- **Group 2 (`\([0-9]*\)`):** Matches the sequence of digits `"123"`, so `\2 = 123`.
- **Replacement (`\2-\1`):** Outputs Group 2, followed by a hyphen `-`, followed by Group 1:
`123-abc`
**Answer: D — 123-abc.**
A: **Incorrect:** `abc-123` would be produced by `\1-\2`, whereas the replacement string is `\2-\1`.
B: **Incorrect:** The replacement string includes a hyphen `-` between `\2` and `\1`.
C: **Incorrect:** Both captured groups are preserved and swapped around `-`.
D: **Correct:** Group 1 captures `abc` and Group 2 captures `123`, so `\2-\1` outputs `123-abc`.
Question 16 MCQ · 5.0 marks
Which of the following AWK commands will give the same output as the command above?
Note that
will replace each space with a newline character. That is, each word in the
string "To be or not to be" will be printed on a new line by the command
.
Published solution
**1. Understand what `echo To be or not to be | tr " " "\n"` does:**
The command takes the string `"To be or not to be"` and replaces every space `" "` with a newline `"\n"`, printing each word on its own line:
```
To
be
or
not
to
be
```
**2. Check the four AWK commands:**
- **Option A:**
```awk
awk 'BEGIN{
sentence="To be or not to be";
fieldsep=" "; n=split(sentence, array, fieldsep);
for(i=1; i<=n; i++){printf("%s
", array[i]);}
}'
```
Inside the `BEGIN` block, it assigns `sentence`, uses `split(sentence, array, " ")` to split the string on spaces into `array[1..n]`, and loops `i` from `1` to `n` printing each element followed by `\n`. This produces the exact same output!
- **Options B & C:** In a `BEGIN` block (before any input lines are read), `NR` is `0`, and `printf("$i\n")` prints the literal characters `$i`, not array elements. Also Option C and D use `echo "..."` inside AWK without assigning `sentence`.
**Answer: A — awk 'BEGIN{ sentence="To be or not to be"; fieldsep=" "; n=split(sentence, array, fieldsep); for(i=1; i<=n; i++){printf("%s\n", array[i]);} }'.**
A: **Correct:** `split(sentence, array, fieldsep)` splits the sentence by spaces into `array[1..n]`, and the `for` loop prints each word on a new line.
B: **Incorrect:** Inside `BEGIN`, `NR` is `0` (so the loop never runs), and `"$i"` in double quotes prints the literal string `$i`.
C: **Incorrect:** `echo` is not a valid AWK assignment statement, `NR` is `0` in `BEGIN`, and `"$i"` is literal.
D: **Incorrect:** `echo "To be or not to be"` does not assign the string to the variable `sentence`, leaving `sentence` empty.
Question 17 MCQ · 5.0 marks
Consider the following
command:
The file
contains:
What will be the output of the above
command? (The order of
iteration over an
awk array is not guaranteed by the language; consider the output correct regardless of line order.)
Published solution
**1. Trace the AWK script on `grades.csv`:**
The script uses `-F','` (comma field separator) to accumulate the sum of scores (`$3`) and count of students (`count[$2]++`) for each department (`$2`):
- **`CS`:** Alice (`90`) and Charlie (`80`) \(\implies \text{sum}[\text{"CS"}] = 90 + 80 = 170\), \(\text{count}[\text{"CS"}] = 2\).
Average \(= 170 / 2 = 85\).
- **`ECE`:** Bob (`75`) and David (`70`) \(\implies \text{sum}[\text{"ECE"}] = 75 + 70 = 145\), \(\text{count}[\text{"ECE"}] = 2\).
Average \(= 145 / 2 = 72.5\).
- **`ME`:** Eve (`85`) \(\implies \text{sum}[\text{"ME"}] = 85\), \(\text{count}[\text{"ME"}] = 1\).
Average \(= 85 / 1 = 85\).
**2. Check the condition `if (sum[dept] / count[dept] > 80)` in the `END` block:**
- `CS`: \(85 > 80\) \(\implies\) **Printed!**
- `ECE`: \(72.5 \not> 80\) \(\implies\) Not printed.
- `ME`: \(85 > 80\) \(\implies\) **Printed!**
Thus, the output contains **`ME` and `CS`** (in either line order).
**Answer: B — ME and CS.**
A: **Incorrect:** In addition to `CS` (average 85), `ME` also has an average of \(85 > 80\) and is printed.
B: **Correct:** Both `CS` (average \((90+80)/2 = 85 > 80\)) and `ME` (average \(85/1 = 85 > 80\)) satisfy the condition and are printed.
C: **Incorrect:** `CS` also has an average of \(85 > 80\) and is printed alongside `ME`.
D: **Incorrect:** `ECE` has an average of \((75+70)/2 = 72.5 \le 80\), so it is not printed.
Question 18 MSQ · 5.0 marks
A log file contains timestamps in 24-hour format and you want to use
to match only
valid 24-hour times in the format HH:MM, where HH is 00-23 and MM is 00-59.
Which of the following patterns correctly match only valid times?
Published solution
**1. Analyze the requirements for a 24-hour `HH:MM` regular expression:**
A valid 24-hour time `HH:MM` requires:
- **`HH` (00 to 23):** Either starts with `0` or `1` followed by any digit `0-9` (`[01][0-9]`, or `0[0-9]|1[0-9]`), **OR** starts with `2` followed by `0-3` (`2[0-3]`).
- **`:`:** A literal colon.
- **`MM` (00 to 59):** Starts with `0-5` followed by `0-9` (`[0-5][0-9]`).
**2. Check each `grep -E` pattern:**
- **Option A (`grep -E '^([01][0-9]|2[0-3]):[0-5][0-9]$'`):** Matches `00`–`19` or `20`–`23` for hours, and `00`–`59` for minutes. (**Correct!**)
- **Option B (`grep -E '^(0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])$'`):** Explicitly lists `0[0-9]`, `1[0-9]`, or `2[0-3]` for hours (`00`–`23`) and `[0-5][0-9]` for minutes (`00`–`59`). (**Correct!**)
- **Option C (`grep -E '^[0-2][0-9]:[0-5][0-9]$'`):** `[0-2][0-9]` allows invalid hours `24, 25, 26, 27, 28, 29`! (**Incorrect.**)
- **Option D (`grep -E '^([0-9]{2}):([0-5][0-9])$'`):** `[0-9]{2}` allows invalid hours `24` through `99`! (**Incorrect.**)
- **Option E (`grep -E '^([01][0-9]|2[0-3]):([0-5][0-9])$'`):** Same valid pattern as Option A with parentheses around the minutes group. (**Correct!**)
**Answer: A, B, E — Patterns A, B, and E accurately restrict HH to 00-23 and MM to 00-59.**
A: **Correct:** `([01][0-9]|2[0-3])` matches `00`–`23` and `[0-5][0-9]` matches `00`–`59`.
B: **Correct:** `(0[0-9]|1[0-9]|2[0-3])` is equivalent to `([01][0-9]|2[0-3])` and matches `00`–`23`.
C: **Incorrect:** `[0-2][0-9]` matches invalid hours `24` through `29`.
D: **Incorrect:** `[0-9]{2}` matches any two-digit number up to `99` for the hour.
E: **Correct:** Properly restricts hours to `00`–`23` and minutes to `00`–`59`.
Question 19 MSQ · 5.0 marks
Which of the following
commands will display **lines 4 and 5** of
?
**Assume that**
**contains at least 5 lines.**
Published solution
**1. Understand how `sed -n` prints specific line numbers:**
With `-n` (suppress automatic printing of pattern space), `sed` prints only the lines explicitly selected by the `p` command:
- **Option A (`sed -n '4,5p' file1`):** Uses the line-range address `4,5` with `p` to print lines 4 through 5. (**Correct!**)
- **Option B (`sed -n -e '4p' -e '5p' file1`):** Passes two script expressions `-e '4p'` and `-e '5p'`, printing line 4 and line 5. (**Correct!**)
- **Option C (`sed -n '1,5p' file1`):** Prints lines 1 through 5 (including lines 1, 2, 3). (**Incorrect.**)
- **Option D (`sed -n '4p;6p' file1`):** Prints line 4 and line 6, skipping line 5. (**Incorrect.**)
- **Option E (`sed -n '3,4p' file1`):** Prints lines 3 and 4, missing line 5. (**Incorrect.**)
**Answer: A, B — sed -n '4,5p' file1 and sed -n -e '4p' -e '5p' file1.**
A: **Correct:** The address range `4,5p` with `-n` prints lines 4 and 5.
B: **Correct:** Specifying `-e '4p' -e '5p'` with `-n` prints line 4 and line 5.
C: **Incorrect:** `1,5p` prints all lines from 1 to 5 rather than just lines 4 and 5.
D: **Incorrect:** `4p;6p` prints lines 4 and 6 instead of lines 4 and 5.
E: **Incorrect:** `3,4p` prints lines 3 and 4 instead of lines 4 and 5.
Question 20 MSQ · 5.0 marks
Which of the following commands will display **lines 10 through 20 (inclusive)** of the file
?
Published solution
**1. Evaluate each command for printing lines 10 through 20 (inclusive) of `report.txt`:**
Note that lines 10 through 20 inclusive consists of \(20 - 10 + 1 = 11\) lines:
- **Option A (`sed -n '10,20p' report.txt`):** Suppresses default output (`-n`) and prints lines in the inclusive range `10,20`. (**Correct!**)
- **Option B (`grep -n '10,20' report.txt`):** Searches for the literal text string `"10,20"` inside the file content rather than selecting line numbers 10 to 20. (**Incorrect.**)
- **Option C (`head -n 20 report.txt | tail -n 11`):** `head -n 20` outputs the first 20 lines (lines 1 to 20), and `tail -n 11` takes the last 11 lines of those 20 lines, which are lines \(20 - 11 + 1 = 10\) through \(20\)! (**Correct!**)
- **Option D (`awk 'NR >= 10 && NR <= 20' report.txt`):** In AWK, `NR` is the current record (line) number, and a true condition with no action block defaults to `{print $0}`, printing lines 10 through 20 inclusive. (**Correct!**)
**Answer: A, C, D — sed -n '10,20p', head -n 20 | tail -n 11, and awk 'NR >= 10 && NR <= 20'.**
A: **Correct:** `sed -n '10,20p'` prints lines 10 to 20 inclusive.
B: **Incorrect:** `grep -n '10,20'` searches for the literal substring `10,20` inside lines of the file.
C: **Correct:** Taking the last 11 lines (`tail -n 11`) of the first 20 lines (`head -n 20`) yields lines 10 through 20.
D: **Correct:** `NR >= 10 && NR <= 20` matches line numbers 10 through 20 and triggers AWK's default print action.
Question 21 MCQ · 3.0 marks
A Bash user sets the PATH variable to an empty string using **PATH=""**. What happens when the
command "ls" is executed?
The command executes normally
The shell exits
"No such file or directory" or "command not found" error occurs
The current directory is searched automatically
Published solution
**1. Understand how Bash resolves external commands using `PATH`:**
`ls` is an external executable (typically `/bin/ls` or `/usr/bin/ls`), not a shell built-in. When a user runs a command name without a slash (like `ls`), Bash searches the colon-separated directories listed in the `PATH` environment variable.
**2. Determine what happens when `PATH=""`:**
When `PATH` is set to an empty string (`PATH=""`), Bash has no directories to search for external binaries, so executing `ls` fails with a `"No such file or directory"` (or `"command not found"`) error.
**Answer: C — "No such file or directory" or "command not found" error occurs.**
A: **Incorrect:** Because `ls` is an external binary in `/bin/ls` (not a shell built-in), Bash cannot locate it when `PATH` is empty.
B: **Incorrect:** Failing to find a command prints an error message and returns exit status `127`, but does not exit the interactive shell.
C: **Correct:** With `PATH=""`, Bash cannot find `/bin/ls` and outputs a `No such file or directory` / `command not found` error.
D: **Incorrect:** An empty `PATH=` does not search the current directory unless `.` is explicitly included.
Question 22 MCQ · 3.0 marks
Which of the following statements about shell variables are correct?
Child shells can access all parent shell variables regardless of export status
Variable names may begin with a digit
All shell variables are environment variables
'export' can be used to convert a shell variable into an environment variable
Published solution
**1. Evaluate each statement about shell and environment variables in Bash:**
- **Statement A:** By default, newly defined shell variables are **local to the current shell process**; child processes (subshells) inherit only variables that have been **exported** into the environment. (Incorrect.)
- **Statement B:** In Bash/POSIX shells, valid variable names must begin with an **alphabetic letter (`a-z`, `A-Z`) or an underscore (`_`)**—they may **never** begin with a digit (`0-9` are reserved for positional parameters). (Incorrect.)
- **Statement C:** Ordinary shell variables are local to the shell unless exported, so not all shell variables are environment variables. (Incorrect.)
- **Statement D:** The `export` built-in command marks a shell variable to be passed into the environment of subsequently executed child processes, converting a local shell variable into an **environment variable**. (Correct!)
**Answer: D — 'export' can be used to convert a shell variable into an environment variable.**
A: **Incorrect:** Unexported shell variables are private to the parent shell and are not inherited by child shells.
B: **Incorrect:** Shell variable identifiers must start with a letter or underscore, never a digit.
C: **Incorrect:** Unexported variables exist only in the current shell and are not part of the process environment.
D: **Correct:** Running `export VAR` promotes a shell variable into an environment variable inherited by child processes.
Question 23 MCQ · 3.0 marks
Which command displays block devices?
Published solution
**1. Identify the Linux utilities for listing hardware devices:**
- `lsblk`: Lists information about all available **block devices** (hard disks, SSDs, partitions, loop devices) in a tree format.
- `lspci`: Lists all PCI buses and connected PCI devices.
- `lsusb`: Lists USB buses and connected USB devices.
- `fdisk`: A disk partitioning utility (requires flags like `-l` and root privileges to list partition tables).
**Answer: D — lsblk.**
A: **Incorrect:** `fdisk` is a partition table manipulation tool rather than the dedicated command for listing block device trees.
B: **Incorrect:** `lspci` lists PCI bus devices (such as graphics cards and network controllers).
C: **Incorrect:** `lsusb` lists USB buses and connected USB peripherals.
D: **Correct:** `lsblk` (list block devices) displays all block storage devices and partitions.
Question 24 NAT · 5.0 marks
Consider the following shell script:
What will be the output?
Published solution
**1. Trace the execution of the Bash `while` loop:**
We start with `x = 4` and `count = 0`. The loop runs while `$x -gt 0` (`x > 0`), decrementing `x` by `1` at the end of each iteration (`x = 4, 3, 2, 1`):
- **`x = 4`:** `$((4 % 2))` is `0` (`-eq 0` is true) \(\implies\) `count` becomes `1`. Then `x` becomes `3`.
- **`x = 3`:** `$((3 % 2))` is `1` (`-eq 0` is false) \(\implies\) `count` stays `1`. Then `x` becomes `2`.
- **`x = 2`:** `$((2 % 2))` is `0` (`-eq 0` is true) \(\implies\) `count` becomes `2`. Then `x` becomes `1`.
- **`x = 1`:** `$((1 % 2))` is `1` (`-eq 0` is false) \(\implies\) `count` stays `2`. Then `x` becomes `0`.
**2. Conclude:**
When `x` reaches `0`, the loop terminates and `echo "$count"` prints **2** (the number of even integers in `{4, 3, 2, 1}`).
**Answer: 2.**