# Python Mooc Assignment

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
| By Nitish Singh
N
Nitish Singh
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,264
Questions: 22 | Attempts: 1,264

Settings

• 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

B. 2.0
Explanation
The code first uses the `type()` function to determine the data type of the number 5, which is an integer. Then, it subtracts 1 from 3.0 and prints the result, which is 2.0.

Rate this question:

• 2.

### Which is allowed in Python?

• A.

X + y = 2

• B.

X*x = 2

• C.

2 = x

• D.

Xy = 2

D. Xy = 2
Explanation
In Python, the expression "xy = 2" is allowed because it follows the syntax rules of variable assignment. In this case, the variable "xy" is being assigned the value of 2. This is a valid operation in Python, as variables can be assigned values of any data type, including numbers.

Rate this question:

• 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

A. 74 then 74
Explanation
The code first calculates the total_gold by adding the values of usa_gold, uk_gold, and romania_gold, which are 46, 27, and 1 respectively. So, the initial value of total_gold is 74. Then, the code prints the value of total_gold, which is 74. After that, it increments the value of romania_gold by 1. However, this change in the value of romania_gold does not affect the value of total_gold because total_gold was calculated before the increment. Therefore, when the code prints the value of total_gold again, it still remains 74.

Rate this question:

• 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

A. Umbrella ella ella ella
Explanation
The value of variable `u` is "umbrella ella ella ella". This is because the code concatenates the value of `once` ("umbr") with the value of `repeat` ("ella") and adds four repetitions of the string "ella " to it.

Rate this question:

• 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

A. False then False
Explanation
The code first compares the value of sleep_time (8) with pset_time (15) using the greater-than operator (>). Since 8 is not greater than 15, the comparison evaluates to False.

Then, the code assigns the values True and False to the variables derive and drink, respectively. The code then uses the logical operator "and" to check if both drink and derive are True. Since drink is False, the expression evaluates to False.

Therefore, the code prints False for both print statements, resulting in "False then False".

Rate this question:

• 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

A. X is smaller
Explanation
When x = 0 and y = 5, the condition x < y is true. Therefore, the code block under the "x is smaller" branch will be executed. As a result, the output will be "x is smaller".

Rate this question:

• 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

A. 5
Explanation
The code initializes a variable "mysum" to 0. It then enters a for loop that iterates over the numbers 5, 7, and 9 (range(5, 11, 2)). In each iteration, the value of "mysum" is increased by the current number. However, when "mysum" reaches 5, the if statement is true and the break statement is executed, causing the loop to terminate. Therefore, the final value of "mysum" is 5.

Rate this question:

• 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

C. 26 100
Explanation
The code prints "26 100" because it concatenates different parts of the string 's' using string slicing and concatenation.

- new_str += s[-1] adds the last character of 's' which is '2' to new_str.
- new_str += s[0] adds the first character of 's' which is '6' to new_str.
- new_str += s[4::30] adds the substring starting from index 4 of 's' which is ' is' to new_str.
- new_str += s[13:10:-1] adds the substring starting from index 13 to index 10 (in reverse order) of 's' which is '0001' to new_str.

Therefore, new_str becomes '26 100' which is printed.

Rate this question:

• 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

D. 7
Explanation
The code will print "common letter" 7 times. The if statement checks if the lengths of s1 and s2 are equal, which they are. Then, it enters a nested loop where each character in s1 is compared to each character in s2. If a common letter is found, "common letter" is printed and the inner loop is broken using the break statement. Since there are 7 common letters between the two strings, "common letter" will be printed 7 times.

Rate this question:

• 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

C. 4
Explanation
The code defines two functions, "add" and "mult". The "add" function takes two arguments and returns their sum. The "mult" function takes two arguments and prints their product.

The code then calls the "add" function twice with different arguments, but only the second call is printed. Next, the "mult" function is called and the product is printed. Finally, the "mult" function is called again, but since it only prints the product and doesn't return anything, nothing is printed.

Therefore, a total of four lines of output will show up when running the code.

Rate this question:

• 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

C. 16
Explanation
The code defines a function `sq` that takes in two parameters: `func` and `x`. It calculates the square of `x` and assigns it to `y`. Then, it returns the result of calling the `func` function with `y` as an argument.

Another function `f` is defined which also calculates the square of a number.

The code then calls `sq` with `f` and `2` as arguments and assigns the result to `calc`. Finally, it prints the value of `calc`, which is the result of calling `f` with the square of `2` as an argument.

Therefore, the code will print `16`.

Rate this question:

• 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

B. ('sunny', 'ccold')
Explanation
The code defines a function called always_sunny that takes two arguments, t1 and t2. The function concatenates the first elements of t1 and t2 and assigns it to the variable first. It then creates a tuple called sun with the values "sunny" and "sun". Finally, the function returns a tuple with the first element from sun and the value of first.

In the given code, always_sunny(('cloudy'), ('cold',)) will evaluate to ('sunny', 'ccold'). This is because t1[0] is 'c' and t2[0] is 'c', so first becomes 'cc'. The first element of sun is 'sunny', so the final tuple returned is ('sunny', 'ccold').

Rate this question:

• 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.

• B.

• C.

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

• D.

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

C. ["universe", "everything", 42, 0]
Explanation
The value of L after running the code will be ["universe", "everything", 42, 0]. This is because the code iterates through each element in the list L. When it encounters the element 0, it replaces it with the string "universe" at the index 0. When it encounters the element 42, it replaces the element at index 1 with the string "everything". Therefore, the updated list becomes ["universe", "everything", 42, 0].

Rate this question:

• 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']]

A. ['mi', 're', ['fa', 'la']]
Explanation
After executing all the operations in the code, the value of L3 will be ['mi', 're', ['fa', 'la']]. This is because the code first concatenates L1 and L2 to create L4, which becomes ['re', 'mi']. Then, L4 is extended to L3, resulting in ['do', 'mi', 're']. Next, the elements in L3 are sorted in alphabetical order, giving ['do', 'mi', 're']. The first element, 'do', is then deleted using the del() function, resulting in ['mi', 're']. Finally, ['fa', 'la'] is appended to L3, resulting in ['mi', 're', ['fa', 'la']].

Rate this question:

• 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']]

B. ['bacon', 'eggs', 'juice', 'toast', 'jam']
Explanation
The value of brunch after executing all the operations in the code will be ['bacon', 'eggs', 'juice', 'toast', 'jam']. This is because initially, brunch is assigned the value of L1, which is ['bacon', 'eggs']. Then, the append() function is used to add 'juice' to L1, resulting in ['bacon', 'eggs', 'juice']. Finally, the extend() function is used to add the elements of L2 to brunch, resulting in ['bacon', 'eggs', 'juice', 'toast', 'jam'].

Rate this question:

• 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)

B. Class Car(object):
Explanation
The correct answer is "class Car(object)". This is a good and valid definition for a class representing a car because it follows the correct syntax for defining a class in Python. The "class" keyword is used to define a new class, followed by the name of the class (in this case, "Car"). The "(object)" part indicates that the class inherits from the built-in "object" class, which is the base class for all classes in Python.

Rate this question:

• 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)

C. Mycar = Car(4, 2)
Explanation
The line "mycar = Car(4, 2)" creates a new Car object with 4 wheels and 2 doors. This is because the Car class has an __init__ method that takes in two parameters, w and d, which represent the number of wheels and doors respectively. By passing in 4 and 2 as arguments when creating the Car object, we are specifying that the new car should have 4 wheels and 2 doors.

Rate this question:

• 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

D. Def paint(self, c):     self.color = c
Explanation
The correct answer is "def paint(self, c): self.color = c" because this method takes a parameter 'c' and assigns it to the 'color' attribute of the car object. This will change the color of the car to the value passed as the argument 'c'.

Rate this question:

• 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")

C. Mycar.paint("red")
Explanation
The line of code "mycar.paint("red")" is the correct answer because it calls the paint() method on the mycar object and passes "red" as the argument, which will change the color attribute of mycar to "red".

Rate this question:

• 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

C. False
Explanation
The line "print(mycar == yourcar)" will print False. This is because the __eq__ method in the Car class is defined to compare the number of wheels, color, and number of doors between two Car objects. In this case, mycar and yourcar have the same number of wheels and doors, but mycar has been painted red while yourcar has not. Therefore, the color attribute is different between the two objects, resulting in the comparison returning False.

Rate this question:

• 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

D. D
Explanation
The correct answer is option D. The method "get_wheels" is a getter method because it returns the value of the "wheels" attribute of the Car object. It uses the "self" parameter to access the attribute within the class.

Rate this question:

• 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

A. Class Dog(Animal):
Explanation
The line "class Dog(Animal):" can replace the blank to create a class that inherits from Animal. This line indicates that the class Dog is inheriting from the class Animal, allowing Dog to inherit all the attributes and methods of Animal.

Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

• Current Version
• Mar 22, 2023
Quiz Edited by
ProProfs Editorial Team
• May 06, 2019
Quiz Created by
Nitish Singh

Related Topics