Class, Objects, and Exceptions

2023年1月20日

[01]Which of the following code segments correctly creates a new class named MyClass?

(a)class MyClass()            (b)def MyClass:                 (c)class MyClass{}

(d)class MyClass:

              pass

A. d

[02]Which of the following code segments correctly create a new class named MyClass which inherits from the MyParent Class base class?

(a)class MyClass<MyParentClass>:

              pass

(b)class MyClass(MyParentClass):

              pass

(c)class MyClass: MyParentClass

              pass

(d)class MyClass extends MyParentClass:

              pass

A. b

[03]Given the following Python code segment:

              class MyClass:

                            x = 7

              a = MyClass()

              b = a.x

              What is the value of b?

(a)None               (b)MyClass          (c)Error is thrown              (d)7

A. d

[04]Given the following Python code segment:

              class MyClass:

                            x = 7

              a = MyClass()

              b = MyClass

              b.x = b.x + 5

              c = a.x

              What is the value of c?

(a)12     (b)5       (c)7       (d)None

A. a

[05]Given the following Python code Segment:

              class MyBaseClass:

                            x = 7

              class MyClass(MyBaseClass):

                            x = 5

              a = MyClass().x

              What is the value of a?

(a)7       (b)5       (c)None               (d)12

A. b

[06]Given the following Python code segment:

              class MyBaseClass:

              def myFunction(self):

                            return 7

              class MyClass(MyBaseClass):

                            pass

              a = MyClass().myFunction()

              What is the value of a?

(a)0       (b)Error is thrown              (c)None               (d)7

A. d

[07]Given the following Python code segment:

              class MyClass:

                            x = 7

              a = MyClass()

              a.y = 5

              b = a.y

              What is the value of b?

(a)None               (b)7       (c)5       (d)Error is thrown

A. c

[08]Given the following Python code segment:

              class MyClass:

                            def __init__(self):

                                          self.x = 5

              a = MyClass()

              b = a.x

              What is the value of b?

(a)5       (b)0       (c)None               (d)Error is thrown

A. a

[09]Given the following Python code segment:

              class MyClass:

                            def __init__(self.myNum):

                                          self.x = myNum:

              a = MyClass(40)

              b = a.x

              What is the value of b?

(a)Error is thrown              (b)0       (c)40     (d)None

A. c

[10]Given the following Python code segment:

              class MyClass:

                            __init__(self.myNum):

                                          self.x = 5

              a = MyClass(40)

              b = a.x

              What is the value of b?

(a)Error is thrown              (b)None               (c)40     (d)5

A. a

[11]Given the following Python code segment:

              class MyClass

                            def __init__(myNum):

                                          self.x = 5:

              a = MyClass(40)

              b = a.x

              What is the value of b?

(a)None               (b)5       (c)Error is thrown              (d)40

A. c

[12]Given the following Python code segment:

              class MyClass:

                            def myFunc():

                                          return 5

              a = MyClass()

              b = myFunc()

              What is the value of b?

(a)Error is thrown              (b)0       (c)5       (d)None

A. a

[13]Given the following Python code segment:

              class MyClass:

                            def myFunc():

                                          return 5:

              a = MyClass()

              b = a.myFunc()

              What is the value of b?

(a)None               (b)Error is thrown              (c)5       (d)0

A. c

[14]Given the following Python code segment:

              class MyClass:

                            def __init__(self):

                                          self.__name = “MyName"

              a = MyClass()

              b = a.__name

              What is the value of b?

(a)None               (b)Myname         (c)Error is thrown

A. c

[15]Given the following Python code segment:

              class MyClass:

                            def __init__(self):

                                          self.__name = “MyName"

              b = a.__MyClass__namme

(a)Error is thrown              (b)None               (c)MyName

A. c

[16]The __________ method is used as Python’s constructor, which initializes a new object.

(a)__init__          (b)__str__           (c)__module__   (d)__bases__

A. a

[17]The __________ method returns the string representation of an object.

(a)__bases__      (b)__module__   (c)__dict__          (d)__str__

(e)__init__

A. d

[18]The __________ attribute returns a dictionry object representing a key-value mapping of an object’s attributes.

(a)__bases__      (b)__str__           (c)__dict__          (d)__modules__

(e)__init__

A. c

[19]The __________ attribute returns the name of the modules which a function was defined in.

(a)__modules__               (b)__init__          (c)__str__

(d)__dict__                       (e)__bases__

A. a

[20]The __________ method returns a tuple of the base classes that an object inherits from.

(a)__modules__               (b)__str__           (c)__init__

(d)__bases__                    (e)__dict__

A. d

[21]The __________ method returns True if an object has an attribute with a given name.

(a)isinstance()                  (b)issubclass()    (c)super()

(d)hasattr()                       (e)type()

A. d

[22]The _________ method returns the class of an object.

(a)issubclass()                  (b)isinstance()    (c)hasattr()

(d)super()                         (e)type()

A. e

[23]The _________ method returns True if a given class is a subclass of another given type.

(a)issubclass()                  (b)type()             (c)super()

(d)hasattr()                       (e)isinstance()

A. a

[24]The __________ method returns True if an object is of a given class.

(a)hasattr()                       (b)issubclass()                  (c)type()

(d)super()                         (e)isinstance()

A. e

[25]The __________ method is used to access attributes and methods of an object’s super class.

(a)issubclass()                  (b)type()                           (c)super()

(d)isinstance()                  (e)hasattr()

A. c

[26]Which of the following codes samples correctly instantiates an array of bytes?

(Choose 2)

(a)x = bytearray(“Hello world", “utf-8")

(b)x = bytearray()

(c)x = bytearray()

(d)x = bytearray(“utf-8")

A. a, b

[27]The __________ fileaccess mode opens a file for reading text only.

(a)rb                    (b)rb+                 (c)r+                    (d)r

A. d

[28]The __________ file access mode opens a file for reading in binary format only.

(a)r                      (b)r+                   (c)rb                    (d)rb+

A. c

[29]The __________ file access mode opens a file for reading and writing text.

(a)rb                    (b)r+                   (c)rb+                  (d)r

A. b

[30]The __________ file access mode opens a file for reading and writing binary data.

(a)r                      (b)rb+                 (c)r+                    (d)rb

A. b

[31]The __________ file access mode opens a file for writing text only.

(a)w+                  (b)wb+                (c)wb                   (d)w

A. d

[32]The __________ file access mode opens a file for reading and writing text.

(a)wb                  (b)w                    (c)w+                  (d)wb+

A. d

[33]The __________ file access mode opens a file for reading and writing text.

(a)wb                  (b)w                    (c)w+                  (d)wb+

A. c

[34]The __________ file access mode opens a file for reading and writing binary data.

(a)w+                  (b)wb+                (c)w                     (d)wb

A. b

[35]The __________ file access mode opens a file for appending text only.

(a)ab                   (b)a                     (c)a+                   (d)ab+

A. b

[36]The __________ file access mode opens a file for appendin binary data only.

(a)a                     (b)a+                   (c)ab+                 (d)ab

A. d

[37]The __________ file access mode opens a file for reading and appending text.

(a)a+                   (b)ab+                 (c)a                     (d)ab

A. a

[38]The __________ file access mode opens a file for reading and appending binary data.

(a)ab                   (b)ab+                 (c)a+                   (d)a

A. b

[39]The __________ built-in IO function opens a given file and returns a file object.

(a)read()             (b)open()            (c)close()            (d)write()

(e)readline()

A. b

[40]The __________ method of the File object returns the contents of a file as a string object.

(a)write()            (b)read()             (c)close()            (d)open()

(e)readline()

A. b

[41]The __________ method of the File object reads a single line of a file and returns it as a string.

(a)close()            (b)write()            (c)open()            (d)readline()

(e)read()

A. d

[42]The __________ method of the File object writes a string into a file.

(a)close()            (b)readline()       (c)open()            (d)write()

(e)read()

A. d

[43]The __________ method of the File object releases all system resources asssociates with the file.

(a)close()            (b)open()            (c)readline()        (d)write()

(e)read()

A. a

[44]Given the following Python expression:

              x = 2 ** 3 ** 2

              What is the value of x?

(a)512   (b)12     (c)128   (d)Exception thrown

A. a

[45]Which of the following string string literals would read as the following string:

              Sammy’s name’s “Sammy"

              (Choose 2)

(a)x = 'Sammy’s name’s \"Sammy\"'

(b)x = “Sammy’s name’s \"Sammy\""

(c)x = “Sammy\’s name\’s “Sammy\""

(d)x = “Sammy\’s name\’s \"Sammy\"

A. b, d

[46]Given the following Python code:

              x = 0

              while x < 5:

                            x += 1

                            <YOUR_CODE_HERE>

              If you wanted the snippet to print the following line:

                            1            2            3            4            5

              Which of the following lines would you insert into the 4th line:

(a)print(x)           (b)print(x, sep="")            (c)print(x, “")

(d)print(x, end="")

A. d

[47]Given the following Python code:

              a = 2

              b = 3

              c = a > b

(a)None               (b)string              (c)boolian            (d)int

A. c

[48]Given the following Python code:

                            a = 0

                            b = 1

                            a = a ^ b

                            b = a ^ b

                            a = a ^ b

(a)1       (b)Exception thrown         (c)0

A. a

[49]Given the following Python code:

              for i in range(1, 9, 2):

                            print(“x")

              How many times is “x" printed?

(a)0 times           (b)3 times           (c)4 times           (d)5 times

A. c

[50]Given the following Python code:

              a = “20"

              b = “100"

              c = a < b

              What is the value of c?

(a)False               (b)True

A. a(Not b)

[51]Given the following Python code:

              a = “Hellow World"

              b = a[-11:11]

              What is the value of b?

(a)Hellow World                (b)Exception is thrown

(c)dlroW olleH                   (d)dlroW olleHHellow World

A. a

[52]Given the following Python code:

              a = [1, 2, 3, 4, 5]

              del a[0:-2]

              What is the value of a?

(a)[4, 5]              (b)[1, 2]              (c)[1, 2, 3]          (d)[1, 2, 3, 4]

A. a

[53]Given the following Python code:

              a = [[b for b in range(c)] for c in range(5)]

              for each A in a

                            for each B in reach A:

                                          print(“x")

              How many times will “x" be printed?

(a)Exception is thrown      (b)5 times           (c)25 times

(d)10 times

A. d

[54]Given the following Python code:

              x = 5

              f = lambda x: 1 + 2 * 5

              a = f(x)

              What is the value of a?

(a)5       (b)Exception is thrown      (c)11     (d)"1 + 2 * 5″

A. c

[55]Given the following Python code:

              a = 'Hello’

              def f(x)

                            return s + 'World’

              a = f(s)

              What is the value of a?

(a)HelloWorld      (b)Hello World     (c)World

(d)Exception thrown

A. a

[56]Given the following Python code:

              a = 'Hello’

              def f(x):

                            return s + 'World’

                            a = f(“Gooodbye")

              What is the value of a?

(a)HelloWorld      (b)World              (c)Exception thrown

(d)GoodbyeWorld

A. a

[57]Given the following Python code:

              s = 'Hello’

              def f(s)

                            return s + 'World’

              a = f(“Goodbye")

              What is the value of a?

(a)Exception thrown         (b)World              (c)HelloWorld

(d)GoodbyeWorld

A. d

[58]Given the following Python code:

              a = 0

              x = 0

              try:

                            a = 1 / x

              except ZeroDivisionError as z:

                            a = a + 3

              except Exception as e:

                            a = a + 5

              finlly:

                            a = a + 1

              a = a + 2

(a)3       (b)8       (c)6       (d)11

A. c

[59]Given the following Python code:

              class MyException(Exception):

                            pass:

              a = 0

              try:

                            rise MyException()

              except MyException as m:

                            a = a + 3

              except Exception as e:

                            a = a + 1

              a = a + 2

              What is the value of a?

(a)8       (b)6       (c)3       (d)11

A. b

[60]Given the following Python code:

              a = 0

              try:

                            assert a > 0

                            a = a + 2

              except Exception as e:

                            a = a + 5

              finally

                            a = a + 1

              a = a + 2

              What is the value of a?

(a)11     (b)6       (c)8       (d)3

A. c

[61]The __________ statement raises an error if a given condition evaluates to False.

(a)raise               (b)assert             (c)except             (d)lambda

A. b

[62]The following code snippet when run:

                            a = open(“myFile.txt", “w")

                            a.close()

              will(select all that apply):

              (Choose 2)

(a)create a new file if myFile.txt doesn’t exist

(b)raise an exception if myFile.txt doesn’t exist

(c)open the file myFile.txt for reading

(d)open the file myFile.txt for writing

A. a, d