The Shell: Practice Questions
For each snippet, write the output of its final command. If it prints nothing, write “No Output”.
Each snippet starts in a new, empty directory.
For Git questions, assume git init starts on main and commits succeed.
Question 1
echo 'hello world' >> README
echo 'hola mundo' > README
echo 'salve munde' >> README
cat README | wc -lAnswer
2
Question 2
echo 'hello world' >> README
echo 'hola mundo' >> README
echo 'salve munde' >> README
wc -l READMEAnswer
3 README
Question 3
echo 'hello world' > README
echo 'hola mundo' >> README
echo 'salve munde' > README
wc -l < READMEAnswer
1
Question 4
echo 'x = 1' >> example.py
echo 'x += 1' >> example.py
echo 'print("x=", x)' >> example.py
python3 example.pyAnswer
x= 2
Question 5
echo 'xs = [1, 2]' >> example.py
echo 'xs.append(5)' >> example.py
echo 'print("xs=", xs)' >> example.py
python3 example.pyAnswer
xs= [1, 2, 5]
Question 6
python3 <<EOF
x = 1
x += 1
print("x=", x)
EOFAnswer
x= 2
Question 7
cat > example.py <<EOF
x = 1
x += 1
print("x=", x)
EOF
python3 example.pyAnswer
x= 2
Question 8
cat > example.py <<ROFL
x = 1
x += 1
print("x=", x)
ROFL
python3 example.pyAnswer
x= 2
Question 9
cat > example.py <<EOF
import sys
print("sys.argv=", sys.argv)
EOF
python3 example.py hello worldAnswer
sys.argv= ['example.py', 'hello', 'world']
Question 10
cat > example.py <<EOF
import sys
print("sys.argv=", sys.argv)
EOF
python3 example.py "hello world"Answer
sys.argv= ['example.py', 'hello world']
Question 11
cat > example.py <<EOF
import sys
print("sys.argv=", sys.argv)
EOF
python3 example.py 'hello world'Answer
sys.argv= ['example.py', 'hello world']
Question 12
cat > example.py <<EOF
import sys
num_args = len(sys.argv)
print("num_args=", num_args)
EOF
python3 example.py "salve munde" "hola mundo"Answer
num_args= 3
Question 13
cat > example.py <<EOF
import sys
num_args = len(sys.argv)
print("num_args=", num_args)
EOF
python3 example.py "salve munde' 'hola mundo"Answer
num_args= 2
Question 14
cat > example.py <<EOF
import sys
num_args = len(sys.argv)
print("num_args=", num_args)
EOF
python3 example.pyAnswer
num_args= 1
Question 15
touch hello_world
touch hola_mundo
touch salve_munde
ls | wc -lAnswer
3
Question 16
touch 'hello world'
touch "hola mundo"
touch salve munde
ls | wc -lAnswer
4
Question 17
touch 'hello world' hola "mundo salve munde"
ls | wc -lAnswer
3
Question 18
export password="hello world"
cat > quiz.py <<'EOF'
import os
print(os.getenv('password', 'hola mundo'))
EOF
python3 quiz.pyAnswer
hello world
Question 19
export password=hello
cat > quiz.py <<'EOF'
import os
print(os.getenv('password', 'hola'))
EOF
python3 quiz.pyAnswer
hello
Question 20
export PASSWORD=hello
cat > quiz.py <<'EOF'
import os
print(os.getenv('password', 'hola'))
EOF
python3 quiz.pyAnswer
hola
Question 21
touch README
echo 'API_KEY=asd' > .env
ls -1Answer
README
Question 22
touch README
echo 'API_KEY=asd' > .env
ls -1aAnswer
.
..
.env
README
Question 23
touch README
mkdir test
touch test/README
cd test
echo 'API_KEY=asd' > ../.env
ls -1aAnswer
.
..
README
Question 24
touch README
mkdir test
mkdir test/test
touch test/README
touch test/test/README
cd test/../test
ls | wc -lAnswer
2
Question 25
touch README
mkdir test
cd test
mkdir test
touch README
cd ..
touch README
ls | wc -lAnswer
2
Question 26
touch README
mkdir test
touch test/README
touch test/.env
ls -a test | wc -lAnswer
4
Question 27
touch README
mkdir test
touch test/README
touch test/.env
ls test | wc -lAnswer
1
Question 28
touch README
mkdir test
mkdir test/test
touch test/README
touch test/test/README
cd test/../test
ls . | wc -lAnswer
2
Question 29
touch README
mkdir test
mkdir test/test
touch test/README
touch test/test/README
cd test/../test
ls .. -a | wc -lAnswer
4
Question 30
git init
touch README
mkdir foo
touch foo/README2
touch README3
ls foo | wc -lAnswer
1
Question 31
git init
touch README
mkdir foo
touch foo/README2
touch README3
ls -a foo | wc -lAnswer
3
Question 32
git init
touch README
mkdir foo
touch foo/README2
touch README3
ls -a . | wc -lAnswer
6
Question 33
git init
mkdir master
echo "print('hello world')" > master/foo.py
echo "print('hola mundo')" > foo.py
git add master
git commit -m "added master"
git branch foo
git checkout foo
echo "print('hola mundo')" >> foo.py
git add foo.py
git commit -m "modified foo"
git checkout main
ls -1Answer
master
Question 34
git init
echo "print('hello world')" > foo.py
echo "print('hola mundo')" > bar.py
git add foo.py
git commit -m "added foo"
git branch foo
git checkout foo
echo "print('hola otra vez')" >> foo.py
git add foo.py
git add bar.py
git commit -m "modified foo"
git checkout main
echo "print('hello again')" >> bar.py
python3 bar.pyAnswer
hello again
Question 35
git init
echo "print('hello world')" > foo.py
git add foo.py
git commit -m "added foo"
git branch foo
git checkout foo
echo "print('hola mundo')" >> foo.py
git add foo.py
git commit -m "modified foo"
git checkout main
python3 foo.pyAnswer
hello world