top of page

2020SpringA-X-IFT383-21462-31760 Final Exam Solutions

Regular Expressions

This first section contains questions regarding regular expressions and common UNIX tools used in this course.

 

Question 1

"[0-9][0-9][0-9]" is an equivalent expression to "[0-9]{3}"

  

True

 

  

False

 

Question 2

The expression "[[:alpha:]]+" is equivalent to "[A-Za-z]+"

  

True

  

False

 

 

Question 3

Which of the following commands will change all occurrences of dog in the file animals.txt to mutt before printing them to the terminal?

  

sed -s "dog" "mutt" animals.txt

  

sed 's/dog/mutt/g' animals.txt

  

grep -s "dog||mutt" animals.txt

  

at animals.txt | grep -c "dog" "mutt"

Question 4

Which answer is true about the following regular expression;

"(xyz)"

  

Matches "xyz" anywhere in a line

  

Matches "xyz" ONLY if it touches a word boundary

  

Matches either the letter x, y or z once

  

Matches any combination of x, y and z

BASH Scripting

The following questions are specific to BASH scripts and their syntax.

Question 5

The first line of this script is a convention unique to BASH scripts. It tells BASH that the file is a script.

@!/bin/bash echo -e "Hello, World!"

  

True

  

False

Question 6

Which of the following is not true regarding if statements in BASH scripts?

  

They can optionally contain as many elif statements as needed

  

They must contain the 'else' keyword

  

They must contain the 'then' keyword

  

They begin with an 'if' keyword and end with a 'fi' keyword

Question 7

Line 5 of this script is a 'comment' and will not be executed

     1  #!/bin/bash     

     2  declare -l MYVAR='ORB'     

     3  if [ $MYVAR == "ORB" ]; then     

    4          VAR1=3     

    5          #echo $MYVAR     

    6  fi

  

True

  

False

Awk

The following questions pertain to the Awk scripting language and awk command syntax.

Question 8

Which of these is NOT true regarding awk?

  

Awk can accept input from stdin, a named file or the terminal directly

  

The number of fields must remain constant between records

  

Data files are divided into records using the contents of the RS variable

  

Records are spereated into fields using the contents of the FS variable

Question 9

The command awk '{print $1}' demo.txt prints the first line of the file demo.txt

  

True

  

False

Question 10

Which of the following is not a built-in awk variable?

  

OFS

  

ORS

  

OFMT

  

NR

  

ORB

Question 11

When passing variables to an awk script file; the -v option must be specified if you would like to access the variable in the post-processing section of your script.

  

True

  

False

Perl

The following questions are specific to the Perl scripting language and its syntax.

Question 12

The statements in a while() code block will always be executed at least once.

  

True

  

False

Question 13

An array can be converted to a string using this function;

  

join

  

paste

  

glue

  

merge

Question 14

The following script is incomplete. Select the answer that will correctly replace the ??? and produce the desired output.

myScript.pl

# Interpreter line intentionally omitted! %cardinal = (     "N" => "North",     "E" => "East",     "S" => "South",     "W" => "West", ); ???

Invocation and output

$ ./myScript.pl East

  

print $cardinal{"East"};

  

print %cardinal["E"];

  

print $cardinal{"E"};

  

print @cardinal["E"];

Question 15

The pragma use strict; requires that variables be declared before they are used.

  

True

 

  

False

 

 

Question 28

Perl is a case sensitive language.

  

True

  

False

 

Question 16

If there is a newline character at the end of a string, this function removes it.

  

trim

  

chomp

 

  

clamp

 

  

chop

 

 

Question 17

The following script is missing its interpreter line (represented as ???). Select the correct answer to replace the ???.

??? use strict; my $VAR=100; print $VAR . "\n";

  

#!/bin/bash

  

#!/usr/bin/perl -e

  

#!/usr/bin/perl

  

use /usr/bin/perl

  

#!/bin/sh

Python

The remaining questions are related to Python and its syntax. This is the last section of this exam; keep going!

Question 18

Which of the following is correct regarding dictionaries in Python?

  

All of these are true

  

Keys must be unique

  

hey work like associative arrays of hashes found in Perl and consist of key-value pairs

  

A dictionary key can be almost any Python type, but are usually numbers or strings. Values on the other hand, can be any arbitrary Python object, but strings are most commonly used

 

Question 19

The output of the following Python statement will be "llo Wo"

myString = "Hello World" print(myString[2:7])

  

True

  

False

Question 20

In python, we can import additional functionality using;

  

packages

  

libraries

  

components

  

modules

Question 21

The operation performed on myList within line 2 is referred to as a _____________ .

myList = [0,1,2,3,4,5] subList = myList[:3]

  

subsequence

  

slice

 

  

splice

 

  

subdivide

 

 

Question 22

Like Awk and Perl; double quotes in Python can be used for string expansion, while single quotes are used for literal strings.

  

True

  

False

 

 

Question 23

Given the partial script below; select the correct statement that would read a single line from standard input.

#!/usr/bin/python import math import random import sys ...

  

sys.readline()

  

stdin.readline()

  

sys.in.readline()

  

sys.stdin.readline()

 

 

Question 24

The following Python script will not execute because there is an important statement missing. Enter the needed statement in the provided blank.

#!/usr/bin/python ???????? myVar = random.randint(0,100) print("The number of the day is %d!" % (myVar) )

  

No Answer

  

Something Else

 

  

import random

Question 25

In Python, the readline() function automatically removes the newline character at the end of a line.

  

True

 

  

False

Get Live Help And Solutions For This Final Exam. Chat Now

bottom of page