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

Test for Language Fundamentals competency.

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

Image 1
data = ["A","S","D","F","G","H"]



theFile = open("SampleTextFile.txt","w")

theFile.write("First line.\n")

theFile.close()



theFile = open("SampleTextFile.txt","a")

for index in range(1,6,2):

    theFile.write(data[index])



theFile.write("Last line.\n")



theFile = open("SampleTextFile.txt","r")

for line in theFile:

    print(line,end='')



theFile.close()

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

First line.

S

F

H

Last line.
- True
- False
A: False
If wrong: The actual output is shown in Image 2. The write method does not automatically append a newline character onto the end of the text being written. If you want it there, you must explicitly put it there as shown by the code for the first and last lines in Image 1. Image 2 First line. SFHLast line.

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

Image 3
class Superclass(object):

    superclassVar = True



    def __init__(self):

        self.superclassVar = "Tom"



class Subclass(Superclass):

    def __init__(self,name):

        self.brother = name



print(Superclass.superclassVar)

ref = Subclass("Joe")

print(ref.superclassVar)

print(ref.brother)

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

True

Tom

Joe
- True
- False
A: False
If wrong: The actual output is shown in Image 4. Unlike Java and C++, when you instantiate an object of a subclass, the subclass constructor does not automatically call the constructor for the superclass. If you want that to happen, you must explicitly write code in the subclass constructor to make it happen. Image 4 True True Joe

Q: The code in the top panel of Image 5 produces the output shown in the bottom panel of Image 5 except that the hexadecimal values may change from one run to the next.

Image 5
class TestClass(object):

    classVar = [1,2,3,4]



    def modifyClassVar01(self,var):

        self.classVar.append(var)



    def printClassVar01(self):

        print(str(self)[10:40] + ": " + str(self.classVar))



ref01 = TestClass()

ref02 = TestClass()



ref01.modifyClassVar01("a")

ref02.modifyClassVar01("b")



ref01.printClassVar01()

ref02.printClassVar01()

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

TestClass object at 0x00504910: [1, 2, 3, 4, 'a']

TestClass object at 0x00542710: [1, 2, 3, 4, 'b']
- True
- False
A: False
If wrong: The actual output is shown in Image 6. Modifying a class variable at runtime impacts all of the objects that have been instantiated from that class because class variables are shared among all objects instantiated from the class. Image 6 TestClass object at 0x00504910: [1, 2, 3, 4, 'a', 'b'] TestClass object at 0x00542710: [1, 2, 3, 4, 'a', 'b']

Q: The code in the top panel of Image 7 produces the output shown in the bottom panel of Image 7 except that the hexadecimal values may change from one run to the next.

Image 7
class TestClass(object):

    classVar = 1234



    def shadowClassVariable(self):

        self.classVar = "ABCD"



    def printClassVar(self):

        print(str(self)[10:40] + ": " + str(self.classVar))



ref01 = TestClass()

ref02 = TestClass()



ref01.printClassVar()

ref02.printClassVar()



ref01.shadowClassVariable()



ref01.printClassVar()

ref02.printClassVar()



TestClass.classVar = True



ref01.printClassVar()

ref02.printClassVar()

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

TestClass object at 0x02282710: 1234

TestClass object at 0x022825F0: 1234

TestClass object at 0x02282710: ABCD

TestClass object at 0x022825F0: 1234

TestClass object at 0x02282710: ABCD

TestClass object at 0x022825F0: True
- True
- False
A: True

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

Image 8
class TestClass(object):

    def __init__(self,data):

        self.iVar01 = data



    def addInstanceVar(self,data):

        self.iVar02 = data



ref01 = TestClass("ABCD")

ref02 = TestClass("DEFG")



print(ref01.iVar01)

print(ref02.iVar01)



ref01.addInstanceVar(1234)

print(ref01.iVar02)

print(ref02.iVar02)

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

ABCD

DEFG

1234

1234
- True
- False
A: False
If wrong: The actual output is shown in Image 9. Adding a new instance variable to an object at runtime does not add that instance variable to all objects that have been instantiated from the same class. Image 9 ABCD DEFG 1234 Traceback (most recent call last): File "1359-1430r-01.py", line 20, in <module> print(ref02.iVar02) AttributeError: 'TestClass' object has no attribute 'iVar02'

Q: The code in the top panel of Image 10 produces the output shown in the bottom panel of Image 10 except that the order of the items in the dictionary may be different from one run to the next.

Image 10
class MyNewClass(object):

    myDict = {"A":{100:"M",200:"N",300:"P",400:"Q"},

              "B":{500:"S",600:"R",700:"U",800:"T"}}

    def __init__(self):

        self.myList = [0,0,0]

    def getDict(self,key):

        return self.myDict[key]

    def setMyList(self,index,val):

        self.myList[index-1] = val

    def displayListItem(self,index):

        print("Showing " + self.myList[index-1])



obj01 = MyNewClass()

dict = obj01.getDict("A")

print(dict)

obj01.setMyList(2,dict[100])

obj01.displayListItem(2)



obj02 = MyNewClass()

dict = obj02.getDict("B")

print(dict)

obj02.setMyList(2,dict[500])

obj02.displayListItem(2)



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

{200: 'N', 300: 'P', 400: 'Q', 100: 'M'}

Showing 100

{600: 'R', 700: 'U', 500: 'S', 800: 'T'}

Showing 500
- True
- False
A: False
If wrong: The actual output is shown in Image 10a. Image 10a {200: 'N', 300: 'P', 400: 'Q', 100: 'M'} Showing M {600: 'R', 700: 'U', 500: 'S', 800: 'T'} Showing S

Q: The code in the top panel of Image 11 produces the output shown in the bottom panel of Image 11 except that the order of the items in the dictionary may be different from one run to the next.

Image 11
class MyNewClass(object):

    myDict = {"A":{100:"M",200:"N",300:"P",400:"Q"},

              "B":{500:"S",600:"R",700:"U",800:"T"}

             }



    def __init__(self):

        self.myList = [0,0,0]



    def getDict(self,key):

        return self.myDict[key]



    def setMyList(self,index,val):

        self.myList[index-1] = val



    def displayListItem(self,index):

        print("Showing " + self.myList[index-1])



obj = MyNewClass()



dict = obj.getDict("A")

print(dict)



obj.setMyList(2,dict[100])

obj.displayListItem(2)

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

{200: 'N', 300: 'P', 400: 'Q', 100: 'M'}

Showing M
- True
- False
A: True

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

Image 12
def listModifier(listA,*args):

  print(listA)



  for arg in args:

    print(arg)



#End function definition



aList = ["ab","cd","ef"]

bList = ["The","old","list"]

cList = [1,2,3]



listModifier(aList,bList,cList)

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

['ab', 'cd', 'ef']

['The', 'old', 'list']

[1, 2, 3]
- True
- False
A: True

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

Image 13
def listProcessor(cList,dList=["B"],aList=["C"],bList=["D"]):

  cList.append(1.00001)

  print("cList = " + str(cList))

  print("dList = " + str(dList))

  aList.append(3.00003)

  print("aList = " + str(aList))

  print("bList = " + str(bList))



  return

#End function definition



listV = ["ab","cd","ef"]

listU = ["This old house"]



listProcessor(listV,bList=listU)



print("listV = " + str(listV))

print("listU = " + str(listU))

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

cList = ['ab', 'cd', 'ef', 1.00001]

dList = ['B']

aList = ['C', 3.00003]

bList = ['This old house']

listV = ['ab', 'cd', 'ef', 1.00001]

listU = ['This old house']
- True
- False
A: True

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

Image 14
def listProcessor(listA,listB=["B"]):

  print("listA = " + str(listA))

  print("listB = " + str(listB))



  return

#End function definition



cList = ["This old house"]

dList = ["is falling down"]

listProcessor(cList)

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

listA = ['This old house']

listB = ['is falling down']
- True
- False
A: False
If wrong: The actual output is shown in Image 15. Image 15 listA = ['This old house'] listB = ['B']