Python Mooc Assignment

22 Questions | Total Attempts: 960

SettingsSettingsSettings
Python Mooc Assignment - Quiz


Questions and Answers
  • 1. 
    You run the code below in idle.     type(5)     print(3.0-1) What's printed?
    • A. 

      Int

    • B. 

      2.0

    • C. 

      Int then 2.0

    • D. 

      Nothing

  • 2. 
    Which is allowed in Python?
    • A. 

      X + y = 2

    • B. 

      X*x = 2

    • C. 

      2 = x

    • D. 

      Xy = 2

  • 3. 
    You run the code below from the file editor. usa_gold = 46 uk_gold = 27 romania_gold = 1      total_gold = usa_gold + uk_gold + romania_gold print(total_gold)      romania_gold += 1 print(total_gold) What is printed?  
    • A. 

      74 then 74

    • B. 

      74 then 75

    • C. 

      74

    • D. 

      75

  • 4. 
    What is the value of variable `u` from the code below? once = "umbr" repeat = "ella" u = once + (repeat+" ")*4  
    • A. 

      Umbrella ella ella ella

    • B. 

      Umbrellaellaellaella

    • C. 

      Umbrella

    • D. 

      Umbrella4

  • 5. 
    What does the code below print? pset_time = 15 sleep_time = 8 print(sleep_time > pset_time) derive = True drink = False both = drink and derive print(both)
    • A. 

      False then False

    • B. 

      False then True

    • C. 

      True then False

    • D. 

      True then True

  • 6. 
    What's printed when x = 0 and y = 5? x = float(input("Enter a number for x: ")) y = float(input("Enter a number for y: ")) if x == y:     if y != 0:         print("x / y is", x/y) elif x < y:     print("x is smaller") else:     print("y is smaller")
    • A. 

      X is smaller

    • B. 

      Y is smaller

    • C. 

      X / y is 0.0

    • D. 

      None

  • 7. 
    What is printed when the below code is run? mysum = 0 for i in range(5, 11, 2):     mysum += i     if mysum == 5:         break         mysum += 1 print(mysum)
    • A. 

      5

    • B. 

      6

    • C. 

      21

    • D. 

      24

  • 8. 
    What does the code below print? s = "6.00 is 6.0001 and 6.0002" new_str = "" new_str += s[-1] new_str += s[0] new_str += s[4::30]     new_str += s[13:10:-1] print(new_str)  
    • A. 

      260000

    • B. 

      26100

    • C. 

      26 100

    • D. 

      Nothing, it will give an error

  • 9. 
    How many times will the code below print "common letter"? s1 = "mit u rock" s2 = "i rule mit" if len(s1) == len(s2):     for char1 in s1:         for char2 in s2:             if char1 == char2:                 print("common letter")                 break  
    • A. 

      4

    • B. 

      0

    • C. 

      5

    • D. 

      7

  • 10. 
    How many total lines of output will show up if you run the code below? def add(x, y):     return x+y def mult(x, y):     print(x*y) add(1,2) print(add(2,3)) mult(3,4) print(mult(4,5))
    • A. 

      0

    • B. 

      2

    • C. 

      4

    • D. 

      5

  • 11. 
    What does the code below print? def sq(func, x):     y = x**2     return func(y) def f(x):     return x**2 calc = sq(f, 2) print(calc)
    • A. 

      4

    • B. 

      8

    • C. 

      16

    • D. 

      Error

  • 12. 
    Examine the code below. What does always_sunny(('cloudy'), ('cold',)) evaluate to? def always_sunny(t1, t2):     """ t1, t2 are non empty """     sun = ("sunny","sun")     first = t1[0] + t2[0]     return (sun[0], first)
    • A. 

      ('sunny', 'cc')

    • B. 

      ('sunny', 'ccold')

    • C. 

      ('sunny', 'cloudycold')

    • D. 

      Nothing

  • 13. 
    What is the value of L after you run the code below? L = ["life", "answer", 42, 0] for thing in L:     if thing == 0:         L[thing] = "universe"     elif thing == 42:         L[1] = "everything"
    • A. 

      ["life", "answer", 42, 0]

    • B. 

      ["universe", "answer", 42, 0]

    • C. 

      ["universe", "everything", 42, 0]

    • D. 

      ["life", "everything", 42, 0]

  • 14. 
    What is the value of L3 after you execute all the operations in the code below? L1 = ['re'] L2 = ['mi'] L3 = ['do'] L4 = L1 + L2 L3.extend(L4) L3.sort() del(L3[0]) L3.append(['fa','la'])
    • A. 

      ['mi', 're', ['fa', 'la']]

    • B. 

      ['mi', 're', 'fa', 'la']

    • C. 

      ['re', 'mi', ['fa', 'la']]

    • D. 

      ['do', 'mi', ['fa', 'la']]

  • 15. 
    What is the value of brunch after you execute all the operations in the code below? L1 = ["bacon", "eggs"] L2 = ["toast", "jam"] brunch = L1 L1.append("juice") brunch.extend(L2)  
    • A. 

      ['bacon', 'eggs', 'toast', 'jam']

    • B. 

      ['bacon', 'eggs', 'juice', 'toast', 'jam']

    • C. 

      ['bacon', 'eggs', 'juice', ['toast', 'jam']]

    • D. 

      ['bacon', 'eggs', ['toast', 'jam']]

  • 16. 
    Which of the following is a good and valid definition for a class representing a car?  
    • A. 

      Def class Car(object):

    • B. 

      Class Car(object):

    • C. 

      Def Car(object):

    • D. 

      Class A(object)

  • 17. 
    Using the class definition below, which line creates a new Car object with 4 wheels and 2 doors? class Car(object):     def __init__(self, w, d):         self.wheels = w         self.doors = d         self.color = ""
    • A. 

      Car(mycar, 4, 2)

    • B. 

      Mycar = Car(4, 2, "white")

    • C. 

      Mycar = Car(4, 2)

    • D. 

      Mycar = Car(2, 4)

  • 18. 
    Which of the following methods changes the color of the car, based on the definition below? class Car(object):     def __init__(self, w, d):         self.wheels = w         self.doors = d         self.color = ""
    • A. 

      Def paint(c):     color = c

    • B. 

      Def paint(self, c):     color = c

    • C. 

      Def paint(c):     self.c = c

    • D. 

      Def paint(self, c):     self.color = c

  • 19. 
    You create a car with mycar = Car(4, 2). Which is a line of code to change the color of mycar to "red"? class Car(object):     def __init__(self, w, d):         self.wheels = w         self.doors = d         self.color = ""     def paint(self, c):         self.color = c
    • A. 

      Car.paint("red")

    • B. 

      Mycar.paint(red)

    • C. 

      Mycar.paint("red")

    • D. 

      Mycar.paint(Car, "red")

  • 20. 
    With the code below, what does the line print(mycar == yourcar) print? class Car(object):     def __init__(self, w, d):         self.wheels = w         self.doors = d         self.color = ""     def paint(self, c):         self.color = c     def __eq__(self, other):         if self.wheels == other.wheels and \             self.color == other.color and \             self.doors == other.doors:             return True         else:             return False mycar = Car(4, 2) mycar.paint("red") yourcar = Car(4,2) print(mycar == yourcar)
    • A. 

      None

    • B. 

      True

    • C. 

      False

    • D. 

      Error

  • 21. 
    Which of the below is a getter method for the number of wheels? class Car(object):     def __init__(self, w, d):         self.wheels = w         self.doors = d         self.color = "" ---------------------------------- (A)    def get_wheels():              return wheels (B)    def get_wheels():              return self.wheels (C)    def get_wheels(self):              return wheels (D)    def get_wheels(self):              return self.wheels
    • A. 

      A

    • B. 

      B

    • C. 

      C

    • D. 

      D

  • 22. 
    What line could replace ____blank____ to create a class that inherits from Animal in the code below? ____blank____     def speak(self):         print("ruff ruff") d = Dog(7) d.set_name("Ruffles") d.speak()
    • A. 

      Class Dog(Animal):

    • B. 

      Class Animal(Dog):

    • C. 

      Class Dog(object)

    • D. 

      None

Back to Top Back to top