Title: Test03- Requires Respondus LockDown Browser
Course: Intro to Scripting Language: Python (APT TEMPLATE) > Section: Test03

Test for Text Processing competency.

Q: The code in the top panel of Image 1 produces the output shown in the bottom panel of Image 1.

Image 1
str = "This is a string."

print(str)

print(str[5:])

===============================================================================

This is a string.

is a string.
- True
- False
A: True

Q: The code in the top panel of Image 2 produces the output shown in the bottom panel of Image 2.

Image 2
str = "This is a string."

for char in str:

    print(char)

===============================================================================

This is a string.
- True
- False
A: False
If wrong: The output is shown in Image 3. Image 3 T h i s i s a s t r i n g .

Q: The code in the top panel of Image 4 produces the output shown in the bottom panel of Image 4.

Image 4
str = """Line 1\nLine 2"""



print(str)

===============================================================================

Line 1\nLine 2
- True
- False
A: False
If wrong: The output is shown in Image 5. Note the newline escape characters in the string. Image 5 Line 1 Line 2

Q: Given a file named 1359-1830-12-input.txt containing the text shown in the top panel of Image 6, the code in the middle panel of Image 6 produces the output shown in the bottom panel of Image 6.

Image 6
Line 1

Line 2

Line 3

===============================================================================

infile = open("1359-1830-12-input.txt")

print("Name of the file: ", infile.name)

for line in infile.readlines():

    print(line)

===============================================================================

Name of the file:  1359-1830-12-input.txt
Line 1



Line 2



Line 3
- True
- False
A: True

Q: The code in the top panel of Image 7 produces the output shown in the bottom panel of Image 7.

Image 7
str = "this is a string."



print(str.capitalize())

===============================================================================

THIS IS A STRING.
- True
- False
A: False
If wrong: The output is shown in Image 8. Image 8 This is a string.

Q: The code in the top panel of Image 9 produces the output shown in the bottom panel of Image 9.

Image 9
def prod(a,b):

    return a*b



def sum(c,d):

    return c+d



comp = lambda w,x,y,z : sum(w,x) * prod(y,z)

print(comp(3,4,5,6))

===============================================================================

210
- True
- False
A: True

Q: The code in the top panel of Image 10 produces the output shown in the bottom panel of Image 10.

Image 10
import re

str = "abcd$"



if re.search(r'[ceg$]', str):

    print("Match")

else:

    print("No Match")



if re.search(r'[^abcd$]', str):

    print("Match")

else:

    print("No Match")

===============================================================================

Match

Match
- True
- False
A: False
If right: The output is shown in Image 11. Note the complement operator ^ at the left end of the class in the second if statement in Image 11. Image 11 Match No Match

Q: The code in the top panel of Image 12 produces the output shown in the bottom panel of Image 12.

Image 12
myList = [0,1,2,3,4,5,6]

print(myList)

print("ok")

print(*myList)

===============================================================================

[0, 1, 2, 3, 4, 5, 6]

ok

[0, 1, 2, 3, 4, 5, 6]
- True
- False
A: False
If wrong: The output is shown in Image 13. Note the effect of the asterisk * in the argument list of the call to the print function in Image 12. Image 13 [0, 1, 2, 3, 4, 5, 6] ok 0 1 2 3 4 5 6

Q: The code in the top panel of Image 14 produces the output shown in the bottom panel of Image 14.

Image 14
import re

myList = ('Dick', 'TomDickHarry', 'Dick is my name')



for testString in myList:

    print("Test string:",testString)

    lambdaFunction = lambda arg: re.match(r"Dick", arg)

    matchObject = lambdaFunction(testString)

    if(matchObject!=None):

        print("  Matching subString:",matchObject.group())

===============================================================================

Test string: Dick

  Matching subString: Dick

Test string: TomDickHarry

  Matching subString: Dick

Test string: Dick is my name

  Matching subString: Dick
- True
- False
A: False
If wrong: The output is shown in Image 15. Note the difference in the behavior of the search and match functions of the re module. Image 15 Test string: Dick Matching subString: Dick Test string: TomDickHarry Test string: Dick is my name Matching subString: Dick

Q: The code in the top panel of Image 16 produces the output shown in the bottom panel of Image 16.

Image 16
import re

myList = ('Joe is your name ','Dick ','TomDickHarry ','Dick is my name ')

filterObj = filter((lambda arg: re.match(r"Dick", arg)), myList)

print(*filterObj)

===============================================================================

Dick   Dick is my name
- True
- False
A: True