IT 209 Midterm

¡Supera tus tareas y exámenes ahora con Quizwiz!

What's printed? class Test(object): def __init__(self, a, b): self.a = a self.b = b def __str__(self): return str(self.a) + "->" + str(self.b) for x in range(5): print (Test(x, x**2))

0->0 1->1 2->4 3->9 4->16

What's printed? class MyClass(object): __var = 0 def add(self, increment): MyClass.__var += increment print (MyClass.__var) myObject = MyClass() for x in range(1, 5): myObject.add(x)

1 3 6 10

Which of the following is not a comparison operator? 1) = 2) == 3) >= 4) <

1) =

What is the base exception class for any user-defined exception subclass? 1) exception 2) except 3) Try 4) None of the above

1) Exception

Which statement is True [select all that applies] 1) a class must have a special method named "__init__ if it is to be used to create objects of that class 2) "def constructor(self):" is a special method used as a constructor to create objects of a given class 3) By default, the special "__str__" method is used to return the actual values of all attributes in a given object "A" when the Python statement "print(A)" is executed 4) "self" is used as a name prefix when creating object attributes (e.g., self.attrName) 5) a class must have special method named "__init__" if it's to be used to create objects of that class.

1) a class must have a special method named "__init__ if it is to be used to create objects of that class 4) "self" is used as a name prefix when creating object attributes (e.g., self.attrName) 5) a class must have special method named "__init__" if it's to be used to create objects of that class.

Which of the following statements is true about class attributes? [select all that apply] 1) class/object attributes can be defined as any kind of Python data types 2) the class attributes are always defined by the constructor method (__init__) 3) attribute names must start with an upper case letter 4) object attributes can be read or updated by using "dot notation" - for example, the following statements can be used to assign a name to s1.name: s1.name = 'Mary'

1) class/object attributes can be defined as any kind of Python data types 4) object attributes can be read or updated by using "dot notation" - for example, the following statements can be used to assign a name to s1.name: s1.name = 'Mary'

Which of the following are true about class methods? [select all that apply] 1) methods can be used to execute any number of valid Python statements/commands 2) methods are often referred to as the class behaviors 3) methods may only be used by code that is executed inside the class 4) methods are defined using the same keyword used to define functions, e.g., def 5) a class may have only limited number of methods

1) methods can be used to execute any number of valid Python statements/commands 2) methods are often referred to as the class behaviors 4) methods are defined using the same keyword used to define functions, e.g., def

What would be the outcome of the following code? months = {'January':['Enero', 'Januar'], 'February':['Febrero', 'Februar'], 'March':['Marzo', 'Marz'], 'April': ['Abril', 'April'] } for k in months.keys(): print (k) 1) print all the keys 2) print all the items 3) print all the values 4) print nothing

1) print all the keys

Which of the following is a valid Python method used to read data from a file? [check all that apply] 1) read() 2) readline() 3) readall() 4) readlines() 5) all the above

1) read() 2) readline() 3) readall() 4) readlines()

Which of the following statements is true about Abstract Base Classes(ABCs)? [select all that apply] 1) they are not used to instantiate objects 2) they contain one or more abstract methods that are declared with the method definition line but have no code implementation except for 'pass' 3) they require an __init__() method as a constructor 4) there are no built-in ABC in Python. Only user defined classes can be defined as an ABC 5) a subclass that inherits an ABC class must provide an implementation for ABC's abstract methods

1) they are not used to instantiate objects 2) they contain one or more abstract methods that are declared with the method definition line but have no code implementation except for 'pass' 5) a subclass that inherits an ABC class must provide an implementation for ABC's abstract methods

What does the following code print? for num in range(1,10,3) print(num)

1, 4, 7

What is the output of the following code if the user enters 5? n = int(input('Enter a number: ')) #the user will enter 5 exp = int(12/n) + 10 print(exp) 1) 12.4 2) 12 3) 12.5 4) 0 5) None of the Above

2) 12 It's an int not a float, 12.4 rounds down to 12

What is the correct definition of a class 'ComboDevice', which has two parents named 'Scanner' and 'Copier'? 1) class Scanner(ComboDevice) 2) class ComboDevice(Scanner, Copier) 3) class ComboDevice(Scanner) 4) class ComboDevice(Copier)

2) class ComboDevice(Scanner, Copier)

Which of the following can be used to open a file for reading in Python? [check all that apply] 1) fout = open ("C:/Data?datafile.txt") 2) infile = "C:/Data?datafile.txt" f = open(infile, "r") 3) with open("C:/Data?datafile.txt", "r") as input: 4) f = open("C:/Data?datafile.txt", "w") 5) infile = "C:/Data?datafile.txt" f = open_file(infile, "r")

2) infile = "C:/Data?datafile.txt" f = open(infile, "r") 3) with open("C:/Data?datafile.txt", "r") as input:

Which of the following is NOT TRUE about inheritance in OOP? 1) child classes can access all the methods of parent classes 2) parent classes can access all the attributes in child classes 3) parent class implements generic characteristics 4) child class can have multiple parents

2) parent classes can access all the attributes in child classes

In Python, strings are ordered sequences of charachter data, and thus can be indexed similar to a list. Given the following string S = 'ALPHABETIC', which command can be used to obtain the following substring (slice) 'ALPHA' 1) s[:4] 2) s[:5] 3) s[1:5] 4) s[1:6] 5) None of the above

2) s[:5]

What is the length of the following dictionary? d = {'G01234567': 'Jane Doe', 'G2345678': 'George Washington', 'G384719374': 'George Mason'}

3

What is the correct way to concatenate two strings: 'Hello' and 'class' 1) 'Hello' - 'class' 2) 'Hello * 'class" 3) 'Hello' + 'class' 4) None of the above

3) 'Hello' + 'class'

What is the correct command to get the person's address from the following dictionary? person = {'name': 'Jane Doe', 'age': 45, 'address': '4400 University Drive, Fairfax, VA'} 1) person('address') 2) person('address') 3) person['address'] 4) All of the above

3) person['address']

The following line of code is used to write to a file (fp). Which statement is true about "outdata"? fp.writelines(outdata) 1) It's the name of the output data file being written to 2) it's a variable holding the name of the file being written to 3) it's a string variable holding the charachters to be written to the file pointed to by "fp" 4) it's a list variable consisting of one or more strings to be written to the file pointed to by 'fp' 5) None of the above

4) it's a list variable consisting of one or more strings to be written to the file pointed to by 'fp'

By default, the parent class of all classes defined in Python is: 1) exception 2) None of the above 3) parent 4) object

4) object

What would the following statement will print after question 3? print (my_mac.mnftr )

Apple

What is printed after running the following code def grade (score) if score > 90: return 'A' elif score > 80: return 'B' elif score > 70 return 'C' else: return 'F' print(grade(72))

C

An object is a specific instance of a class whose attribute values are the same as all the other objects of that class. True or False?

False

Given a class object, we cannot obtain the values of its attributes without having the __str__ method. True or False?

False

super() function can only be made in the __init__ method. True or False?

False

Assuming the 'input.txt' is a text file with many lines of data/information. The following code will read the entire content of the file and store it in the variable 'data'. True or False? fp = open('input.txt', 'r') data = fp.readline() fp.close()

False readline() reads one line at a time

Suppose you have the install_os method in the Computer class. If you execute the following commands then what will be the output: my_mac.install_os("OS X") print (my_mac.os )

OS X

A class is a custom type, or data type, created by a programmer.

True

Implementing the __contains__() method will allow a class to be treated as subclass of the Container class and enables the use of the Python 'in' operator to test whether an external object is included in that class's object. True or False

True

Method overriding is altering or replacing a method of the superclass with a new method (with the same name in the subclass). True or False?

True

The special "__str__" method can be used to return a string representation of any attributes related to a given object as specified by the programmer. True or False?

True

Using "Inheritance" is a way to reuse code that is already written in a superclass (parent class). True or False?

True

Suppose you are given the following class definition. Create an object 'c' of the class with the following attributes: a. color = black b. manufacturer = dell class Computer(object): def __init__(self, color, mnftr): self.color = color self.mnftr = mnftr self.os = ""

c = Computer('black', Dell')

Write down a Dog class where the class will have a class attribute named 'species' which needs to be set as 'mammal', an __init__ method to initialize the name and age of the Dog instance. After defining the class, instantiate three new dogs, each with a different name and age you prefer. Then write a function called, get_oldest_dog(), which returns the age of the oldest dog. The function takes a list of ages and prints the output as follows: The oldest dog is XXX years old. Where XXX = the age of the oldest one

class Dog(object): species = 'mammal' def __init__(self, name, age): self.name = name self.age = age d1 = Dog("Jake", 7) d2 = Dog("Doug", 40) d3 = Dog("William", 5) ages = [d1.age, d2.age, d3.age] def get_oldest_dog(ages): return max(ages) # Output print("The oldest dog is {} years old.".format(get_oldest_dog(ages)))

You are given a Circle class which has the following definition: class Circle(object): ... def grow(self, factor=2): """grows the circle's diameter by factor""" self.diameter = self.diameter * factor Write a class NewCircle that inherits from the Circle class and defines an overriding method that grows the circle diameter by square root of two. You can omit the __init__ method

class NewCircle(Circle): def grow(self, factor=2): self.diameter = self.diameter * math.sqrt(factor)

Add an install_os method to the Computer class. The method should have the following specification: Name of the method = install_os Should set the os of the Computer object c.

def install_os(self, new_os): self.os = new_os

Suppose you are given the following child class which inherits the Computer class: class Apple(Computer): def __init__(self, color): super().__init__(color, "Apple") self.itune_installed = False def install_itune(self): self.itune_installed = True Create an Apple object where you can choose the values for the attributes

my_mac = Apple("silver")


Conjuntos de estudio relacionados

B-05 Define & Provide Examples of Schedules of Reinforcement - Part 4 - Matching Law

View Set

Chapter 4: Building Styles and Construction

View Set

La vie scolaire: les questions ET les réponses pour avoir une conversation (préparer un dialogue, étape #3).

View Set

Chapter 10 -SAVINGS, INVESTMENT SPENDING, AND THE FINANCIAL SYSTEM

View Set

Unit 2 US and Canada Study Guide

View Set

SIE Exam (Chapter 1 , Section 1.4)

View Set