Resit questions Flashcards
Practicing resit questions
BASIC QUESTION 1 (marked correct for all):
Given the following Shell script named echo_func.sh
#!/usr/bin/env bash echo_input() { echo $0 } echo_input "World"
If we run the script with the following command, what is the expected output?
$ ./echo_func.sh Hello World
Q1 ANSWERS:
- ./echo_func.sh
- None of the above since $0 is ambiguous in this context
- Hello
- World
./echo_func.sh is the answer
- The script echo_func.sh defines a function echo_input that echoes the value of $0.
- In a bash script, $0 is the name of the script itself.
- Thus running command ./echo_func.sh Hello World, output is the name of the script, which is ./echo_func.sh.
Note: the arguments Hello and World passed in the command line are not used in this script. The function echo_input is called with the argument “World”, but within the function, this argument is not used. Instead, the script’s name ($0) is printed.
BASIC QUESTION 2 (marked correct for all):
Suppose that we have a script file named vars.sh and whose contents are shown below.
<vars.sh> foo="'$foobar'" foobar='$foo' echo "$foo"
What will be the output of the following command?
$ bash vars.sh
Q2 ANSWERS:
- ‘$foo’
- $foo
- ‘$foobar’
- ’ ‘
Output is : ‘ ‘
- In the first line, foo is assigned the value of “$foobar”.
- At this point, foobar has not been defined, so $foobar is empty.
- Therefore, foo is assigned the string ‘ ‘.
- In the second line, foobar is assigned the string ‘$foo’. This is a literal string, and variable substitution does not occur because the string is enclosed in single quotes.
-Finally, the script echoes the value of foo, which we already established is ‘ ‘.
This is because the single quotes inside double quotes are preserved as part of the string. The double quotes around $foo in the echo statement ensure that the value of foo is printed as is, including the single quotes. So, the output is a pair of single quotes.
BASIC QUESTION 3 (marked correct for all):
Assume the following directory layout. All entries except for those with .txt extensions are directories.
root ├── a │ ├── bar │ │ └── file1.txt │ └── foo └── b ├── a ├── bar └── baz
Which of the following command will move directory a into directory b? Assume that your working directory (from where you issue the command) is root.
The following are the explanations for the various options according to the man pages.
cp -r: copy directories recursively. rm -r: remove directories and their contents recursively -f: ignore nonexistent files and arguments, never prompt
Q3 ANSWERS:
- mv a b
- cp a b; rm a
- We cannot move a into b since b already has a directory named a
- cp -r a/* b; rm -rf a
CORRECT ANSWER:
mv a b
Don’t ask me why brother question is tripping me up :(
I would say cp -r a/* b; rm -rf a ??
BASIC QUESTION 4 BASIC (marked correct for all):
Which of the following options is the output for the tr command below?
$ echo "AaBbCcDd" | tr -s 'AZ' 'a-z'
Note that the flag -s modifies the command behavior as follows.
-s replace each sequence of a repeated character that is listed in the last
specified SET, with a single occurrence of that character
Q4 ANSWERS:
- aBbCcDd
- abcd
- abbccdd
- None of the above; command has an error
CORRECT ANSWER: aBbCcDd
The tr ‘AZ’ ‘a-z’ part of the command is trying to translate ‘A’ to ‘a’ and ‘Z’ to ‘z’? So, aaBbCcDd
Since -s option “deletes” reoccurrence it becomes: aBbCcDd?
Idk this question is trippy to me :(
I would think the correct answer is None, command has an error
BASIC QUESTION 5:
$ cat test cry eve shy wry zoo $ grep -E '<REGEX>' test
Which of the following regular expressions (when used in place of <REGEX> above) matches words that contain no vowels?</REGEX>
The man page lists the following for the option -E: “Interpret PATTERNS as extended regular expressions”
Q5 ANSWERS:
- [aeiou]{0}
- [^aeiou]+
- [^aeiou]
- [^aeiou]{3}
CORRECT ANSWER: [^aeiou]{3}
- [aeiou]{0}: This matches zero occurrences of any vowel, which means it matches an empty string.
- [^ aeiou]+: This matches one or more occurrences of any character that is not a vowel. This is the correct answer.
- This matches exactly one occurrence of any character that is not a vowel. It will not match words with more than one character.
- [^ aeiou]{3}: This matches exactly three occurrences of any character that is not a vowel. It will not match words with less or more than three characters.
In the context of the specific words provided in the question, which are all three letters long, the regular expression [^aeiou]{3} would be the most precise answer. This expression will match exactly three characters that are not vowels, which fits the criteria for the words given.
BASIC QUESTION 6:
Suppose the file structure on a system is the following.
root ├── a │ ├── bar │ └── foo ├── b │ ├── bar │ └── baz └── c ├── car └── far
Now, if you issue the following command from within root/c/far, which directory will you end up in?
$ cd .././car/.././../b/../../.././../
Q6 ANSWERS:
- b
- root
- Root/c/far
- None of the above; the invocation has an error
Root is the correct answer.
Here’s how the provided command works giving you are located at root/c/far at the start:
- ../ moves you up one directory, so you go from root/c/far to root/c.
- ./ keeps you in the same directory, so you stay in root/c.
- car/ moves you into the car directory, so you go to root/c/car.
- ../ moves you up one directory, so you go from root/c/car to root/c.
- ./ keeps you in the same directory, so you stay in root/c.
- ../ moves you up one directory, so you go from root/c to root.
- b/ moves you into the b directory, so you go to root/b.
- ../../../ moves you up three directories. However, since you’re already at the root directory, you stay in root.
- ./ keeps you in the same directory, so you stay in root.
- ../ moves you up one directory. However, since you’re already at the root directory, you stay in root.
So, after executing the command cd .././car/.././../b/../../.././../, you will end up in the root directory.
BASIC QUESTION 7:
The following is a simple shell script.
<args.sh> #!/usr/bin/env bash echo $#
Guess the output of the above script based on the example invocation below.
$ bash ./args.sh This,is "for testing, 'a simple input'"
Q7 ANSWERS:
- 4
- 3
- 5
- 2
Answer is 2!
When you run the command bash ./args.sh This,is “for testing, ‘a simple input’”, the arguments are:
- This,is
- “for testing, ‘a simple input’”
Even though the second argument contains spaces, it is considered as one argument because it is enclosed in double quotes. Therefore, the total number of arguments is 2, and the output of the script will be 2
BASIC QUESTION 8:
Assume you are in the test branch, and you want to merge the commits on the test branch to the master branch. What should you do?
Q8 ANSWERS:
-Checkout the master branch and run git merge test
-Run git merge master directly
-Checkout the master branch and run git merge master
-Run git merge test directly
Correct answer: Checkout the master branch and run git merge test
When you want to merge changes from one branch (in this case, test) into another branch (in this case, master), you need to be on the branch that you want to merge into. So, you first checkout the master branch with git checkout master. Then, you merge the test branch into the master branch with git merge test. This will bring all the commits from the test branch into the master branch.
BASIC QUESTION 9 (marked correct for all):
Suppose that you have three text files in a directory: first, second, and third. Assume that you want to create a fourth file (line-count.txt) containing the counts of lines in some subset of these files. Specifically, let us suppose that you want to store the line count of the file named “second” in the first line and that of the file named “first” in the second line, and you have the following partial Makefile recipe.
<Makefile> line-count.txt: first second third
What option below represents the correct command for the above Makefile recipe to compute the line counts as described above?
The man page describes option -l of the command wc as follows: “print the newline counts”
Q9 ANSWERS:
- @wc -l $(word 2, $^) > $@
@wc -l $(word 1, $^) > $@
- @wc -l $(word 1, $^) > $@
@wc -l $(word 0, $^)»_space; $@ - @wc -l $(word 2, $^) > $@
@wc -l $<»_space; $@ - @wc -l $(word 2, $^) > $@
3rd answer is correct:
@wc -l $(word 2, $^) > $@
@wc -l $<»_space; $@
- $(word 2, $^) gets the second prerequisite of the target, which is second.
- $< gets the first prerequisite of the target, which is first.
- wc -l counts the number of lines in a file.
- > redirects the output to a file, overwriting the file if it exists.
- > > appends the output to a file, creating the file if it doesn’t exist.
So, the first command counts the lines in second and writes that count to line-count.txt. The second command counts the lines in first and appends that count to line-count.txt. This matches the desired behavior of storing the line count of second in the first line of line-count.txt and the line count of first in the second line
BASIC QUESTION 10:
Suppose a Makefile contains the following lines.
P = Value for P is $(Y) Y := $(X) X := 1 Z := Value for Z is $(X) show: @echo $(P) @echo $(Z)
Which one of the options below would be the output for the following command?
$ make
Q10 ANSWERS:
- Value for P is
Value for Z is 1
- Value for P is 1
Value for Z is - Value for P is
Value for Z is - Value for P is 1
Value for Z is 1
1st answer is correct:
Value for P is
Value for Z is 1
In Makefiles, there are two types of variable assignments which are, = and :=.
- = is a recursive assignment. The right-hand side is expanded when the variable is used, not when it’s declared.
- := is a simple assignment. The right-hand side is expanded at the time of declaration.
In our Makefile, P is assigned the value Value for P is $(Y) using =. So $(Y) will be expanded when P is used, not when it’s declared. At the time of declaration, Y has no value, so $(Y) is empty.
Y is then assigned the value $(X) using :=. At this point, X has no value, so Y is assigned an empty value.
X is then assigned the value 1 using :=.
Z is assigned the value Value for Z is $(X) using :=. So $(X) is expanded at the time of declaration, and Z is assigned the value Value for Z is 1.
When you run make, it executes the show target, which echoes $(P) and $(Z). $(P) is Value for P is $(Y), and since Y is empty, it echoes Value for P is . $(Z) is Value for Z is 1, so it echoes Value for Z is 1.
BASIC QUESTION 11:
What will be the output of the following command?
$ echo 'Hello' && echos 'World' 2> /dev/null
Q11 ANSWERS:
- World
- HelloWorld
- No output, because of the typo and use of &&
- Hello
The correct answer is Hello.
The && operator in a shell command runs the second command only if the first command succeeds.
In this case, echo ‘Hello’ will succeed and print Hello.
The second command echos ‘World’ is likely a typo and will fail because echos is not a valid command. However, 2> /dev/null redirects the error message to /dev/null, effectively discarding it. So, no error message will be printed.
Therefore, the only output from this command will be Hello. The other options are not correct because World will not be printed due to the typo in echos, and there will be output (Hello) despite the typo and use of &&
BASIC QUESTION 12 BASIC:
The following are the contents of a simple shell script hello.sh.
#!/a/location/that/does/not/exist bash echo "Hello '$USER!'"
Guess the output of the above script when run by a user ‘neo’ based on the example invocation below.
$ bash ./hello.sh
Q12 ANSWERS:
- Hello’${USER!}’
- Hello ‘neo!’
- An error message as follows: “The file ‘./hello.sh’ specified the interpreter ‘/a/location/that/does/not/exit bash’ which is not an executable command.”
- Hello “
Answer is Hello ‘neo!’
When you run a shell script with bash ./script.sh, bash ignores the shebang line (#!/a/location/that/does/not/exist bash) and runs the script in a new bash shell. So, even though the path in the shebang line does not exist, it doesn’t cause an error.
The echo command prints the string Hello ‘$USER!’. In a double-quoted string in bash, variable references ($USER) and command substitutions are replaced with their values.
So $USER is replaced with the username of the current user, which is neo in this case.
Therefore, the output of the script when run by a user neo is Hello ‘neo!’
BASIC QUESTION 13:
Assuming that the following command is issued from the location /tmp, which is currently empty, how many directories does it create?
mkdir -p foo/a,{b,c}
The man page provides the following explanation for option -p: “no error if existing, make parent directories as needed”
Q13 ANSWERS:
* 4
* 3
* None; command is invalid
* 2
Answer is 3. Why though?
The command mkdir -p foo/a,{b,c} creates the following directories:
3. foo
4. foo/a
5. b
So, it creates 3 directories in total. The {b,c} part is brace expansion in bash, which will generate b. The c is not created because it’s not included in the path??
BASIC QUESTION 14 BASIC:
Suppose the current directory contains the following. Please pay attention to the file names and the last-modification timestamps.
$ ls -l .rw-r--r--@ 15 balac 5 Dec 20:07 first .rw-r--r--@ 90 balac 5 Dec 20:06 Makefile
The Makefile’s contents are shown below.
first: @echo "» First: done" &> /dev/null > second second: first @echo "» Second: done"
Now, what will be the output for the following commands in the current directory?
$ make second && make second
Q14 ANSWERS:
» First: done
make: ‘second’ is up to date.
make: ‘second’ is up to date.
> > First: done
Second: done
Second: done
> > Second: done
Second: done
> > Second: done
make: ‘second’ is up to date.
CORRECT ANSWER:
Making third.txt
Making third.txt
Here’s what happens when you run make second:
1. make looks at the second target and sees that it depends on first. So, it first executes the first target.
2. The first target has this command: @echo “» First: done” &> /dev/null > second. This command does two things:
* It prints “» First: done” to the standard output, but immediately redirects this output to /dev/null, effectively discarding it.
* It creates a new file named second. The content of this file is not specified, so it’s an empty file.
3. After executing the first target, make moves on to the second target. The second target has this command: @echo “» Second: done”. This command prints “» Second: done” to the standard output.
The make command will execute the commands associated with the second target each time it’s called, regardless of the timestamps of the files. This is because the second target does not create or modify a file named second ( it simply executes the command @echo “» Second: done”). Because this command does not change the filesystem, so make has no way of knowing whether the target is up to date and needs to be run again.
(Lowkey IDK i hate make files)
BASIC QUESTION 13:
Suppose that we use the read command to accept input from a user. The part in bold is the user input.
$ read x y
“Introduction to Computer Science”
What will be the output of the following commands?
$ echo "---"; echo $x; echo $y; echo "---"
BASIC Q14 ANSWERS:
~~~
—
Introduction to Computer
Science
—
“Introduction
to Computer Science”
—
Introduction
To Computer Science
—
“Introduction to Computer Science”
—
~~~
2nd answer is correct:
- - -
“Introduction
to Computer Science”
- - -
- “The read command in bash reads a line from the standard input and splits it into words. These words are assigned to sequential variables named as arguments”
- If there are more words than variables, read assigns the leftover words (along with the intervening whitespace) to the last variable.
- In your case, read x y assigns the first word to x and all the remaining words to y.
- Hence x=”Introduction and y = to Computer Science”
The commands do the following:
1. echo “—”; prints “—” on one line
2. echo $x; prints “Introductio on another new line
3. echo $y; prints Computer Science” on another new line
4. echo “—”; prints “—” on another new line
Note: ; is like new line /n