OOP final

Ace your homework & exams now with Quizwiz!

Composition

- "has-a" relationship- ownership (e.g. Car has an engine) - represented by filled in diamond

What does the code below print? #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area () { return 0; } }; class Rectangle: public Polygon { public: int area () { return width * height; } }; class Triangle: public Polygon { public: int area () { return (width * height / 2); } }; int main () { Rectangle rect; Triangle trgl; Polygon poly; Polygon * ppoly1 = &rect; Polygon * ppoly2 = &trgl; Polygon * ppoly3 = &poly; ppoly1->set_values (4,5); ppoly2->set_values (4,5); ppoly3->set_values (4,5); cout << ppoly1->area() << ','; cout << ppoly2->area() << ','; cout << ppoly3->area(); return 0; }

20,10,0

When writing a test for the code below, what would be the boundary values for customer.age? def serve_drink(self, customer): if customer.age < 21: customer.give("water") else: customer.give("wine")

21 and 20

What does this code print? lst = [1,2,3,4,5] it1 = iter(lst) it2 = iter(lst) next(it2) next(it1) next(it1) next(it2) next(it1) print(next(it1))

4

All exceptions in Python inherit from this class. A) BaseException B) Exception C) Error D) ValueError

A

If you create a tkinter.StringVar() and want to tie its value to a label on a button, which of these arguments would you use when creating the button? A) textvariable B) command C) value D) text E) variable

A

What types of coverage are achieved if any in the example below? def foo(a): if a == 5: return a else: return 0 def test_foo(): assert foo(5) == 5

None

T or F The strategy pattern can be simplified in Python using first-class functions.

True

T or F The yield keyword causes a function to return a Generator object.

True

T or F When using multiple except clauses, they should be ordered from most specific to most general.

True

T or F super is not a built in keyword in C++

True

T or F tkinter is a cross-platform GUI toolkit.

True

We talked briefly about a type of diagram to show classes and objects. What is the 3 letter abbreviation for these?

UML

With the unittest framework, what method is run automatically before each test method?

setUp

What types of coverage are achieved if any in the example below? def baz(x, y): z = 0 [ if x > 0 and y > 0: [ if x < y: z = x else: z = y ]] return x + y + z

statement and branch

Aggregation

- "has-a" relationship- can object exist on its own (e.g. Car has a passenger, but passenger can exist on their own)- represented by an open diamond

Inheritance

- "is-a" relationship - e.g. driver is a person - represented by an open arrow

Association

- e.g. Siblings - represented by solid line

#include <iostream> using namespace std; int main () { int a = 5; int* b = &a; int*& c = b; (*c) = 10; cout << a << endl; return 0; } The code above will print...

10

In the ZipProcessor example, we modified the behavior applied to the unzipped files by using... A) Inheritance B) Composition C) getters and setters D) magic methods

A

In the auction example, the auctioneer is... A) the subject B) an observer C) a strategy D) a decorator

A

In the diamond pattern UML diagram, what would be the first item in Subclass.__mro__ ? A) <class '__main__.Subclass'> B) <class '__main__.LeftSubclass'> C) <class '__main__.BaseClass'> D) <class 'object'>E) <class '__main__.RightSubclass'>

A

In the state pattern, the states are represented by... A) state objects B) a string attribute in the context object C) a transition table

A

Multiple inheritance causes problems when... A) the parent classes share the same methods or attributes. B) one of the base classes is a built-in class. C) you add a new method to the subclass. D) you use multiple mixins.

A

What category does the Decorator pattern fall into? A) Structural B) Creational C) Concurrency D) Behavioral

A

What category does the template method pattern fall into? A) Behavioral B) Concurrency C) Structural D) Creational

A

What does "monkey patching" refer to? A) replacing methods on an object at runtime B) updating a module in a project to match the latest version C) the type system that doesn't complain until you use an undefined attribute or method D) a style of testing where modules are intentionally broken to see if the failure is handled gracefully

A

What is the difference between structs and classes? A) default visibility is public for structs and private for classes B) structs cannot have methods C) structs cannot inherit from other structs D) structs are stored on the stack and classes are stored on the heap

A

What was the expectation for the ComparableMixin? A) Subclasses should implement __lt__ B) Subclasses should implement __gt_ C) Subclasses can only inherit from ComparableMixin

A

Which of the following is true about this abstract class Shape2D. class Shape2D: def area(self): raise NotImplementedError() A) Subclasses of Shape2D will inherit this area method B) Python will not allow instantiation of a Shape2D object C) Subclasses of Shape2D are forced to override area

A

Which of the following are ways to decorate a function foo() with the log_calls decorator above? # A @log_calls def foo(): # B def foo(log_calls): # C log_calls(foo()) # D foo = log_calls(foo) # E @log_calls(foo)

A and D

In the Java examples in class, which of the following lines would lead to a compilation error? (Why?) A) Shape2D b = new Shape2D(5, 7); B) Shape2D rect = new Rectangle(5, 7); C) Rectangle rect = new Rectangle(5, 7);

A) Shape2D b = new Shape2D(5, 7); because Shape2D is an abstract class and you can't make an instance of an abstract class (in ANY language)

How many classes can you inherit from in Python?

Any number

A test case that creates an object from a class and calls one of its public methods would be an example of... A) black box testing B) white box testing

B

In which of the following cases would a Singleton be most problematic? A) objects with no state B) objects with mutable state C) logging object D) configuration object

B

What category does the singleton pattern fall into? A) Behavioral B) Creational C) Concurrency D) Structural

B

What category does the state pattern fall into? A) Concurrency B) Behavioral C) Creational D) Structural

B

What category does the strategy pattern fall into? A) Concurrency B) Behavioral C) Creational D) Structural

B

Which of the following is an important aspect of event-driven programming? A) Dynamic types B) Callback functions C) Return-oriented programming D) Graphics

B

Which of the following is not an advantage of decorators over subclasses? A) Decorators can be layered in different combinations B) Decorators do not involve writing new classes C) New functionality can be added to an object at runtime

B

Which of the following is true about inheritance? A) A child class must re-implement the parent's methods. B) A child class can use attributes defined in the parent class. C) Child classes are more general than their parents.

B

Which of these code segments is correct? A class Dummy {int x;Dummy(int x) {this.x = x;}}; B class Dummy {int x;Dummy(int x) {this->x = x;}};

B

(x for x in [1,2,3,4]) This line above returns a... A) Tuple B) List C) Generator D) Set E) Dictionary

C

What category does the adapter pattern fall into? A) Creational B) Behavioral C) Structural D) Concurrency

C

What category does the composite pattern fall into? A) Concurrency B) Creational C) Structural D) Behavioral

C

What category does the facade pattern fall into? A) Creational B) Behavioral C) Structural D) Concurrency

C

What category does the iterator pattern fall into? A) Structural B) Creational C) Behavioral D) Concurrency

C

What category does the observer pattern fall into? A) Creational B) Concurrency C) Behavioral D) Structural

C

What is the difference between these two lines of code? Person* tim = new Person("Tim"); Person* tim = (Person*)malloc(sizeof(Person)); A) The second one will only compile in C, but not C++ B) The second one will not compile in either C or C++ C) The second one does not call the constructor D) The second one allocates memory on the heap while the first one uses the stack

C

When is the initializer list preferred over initializing in the constructor body? A) When the constructor is not overloaded B) When there are many instance variables to set up C) When initializing an instance variable that is another object

C

Which of the following languages does not support multiple inheritance? A) Python B) C++ C) Java

C

Which of these options allows you to access the following mangled name from outside its class? class MyClass:def __init__(self):self.__secret = 10obj1 = MyClass() A) MyClass.__secret B) obj1.__secret C) obj1._MyClass__secret D) obj1.__secret__

C

In the Notebook example, the most accurate relationship between Notes and Notebook is...

Composition

What relationship would exist between a chess board and the spaces on the board?

Composition

A decorator must... A) be defined using the @decorator syntax. B) not be used more than once on the same object. C) hold a reference to the original subject. D) maintain the same public interface as the original subject.

D

In Python, how do you know when an iterator has finished iterating? A) calling __next__ returns False B) calling __done__ returns True C) calling __next__ returns None D) calling __next__ raises a StopIteration exception

D

What category does the command pattern fall into? A) Structural B) Creational C) Concurrency D) Behavioral

D

What method on a session object do we use to update the database? A) sync() B) close() C) save() D) commit()

D

With a log level of logging.WARNING, which log types will be ignored?

Debug and Info

Object relationships from weakest to strongest

Dependency < Association < Aggregation < Composition < Inheritance

In the State pattern which of these could be converted to singletons? A) User B) Context C) Context D) Parser E) States

E

What takes the place of the _notes array in the code we looked at? A) dict B) Table C) Column D) set E) relationship

E

Which of the following patterns that we have already talked about is most similar to the template method pattern? A) Observer B) Iterator C) Adapter D) Facade E) Decorator F) Strategy

F

T or F Automated testing is a waste of time because software only needs to be tested once when it is finished.

False

T or F Each unit test must have only one assertion.

False

T or F In Python, functions can be passed as arguments to other functions, but a function cannot return another function.

False

T or F When creating a widget with tkinter, we must specify the x,y-coordinates where we wish to draw it.

False

T or F You can only test code if you know its internal implementation.

False

T or F .pack() allows us to set the relative layout for our widgets.

False

T or F A subject can have only one observer attached to it.

False

T or F Const methods cannot be used by non-const objects.

False

T or F If a method calls super() it must be the first line in that method.

False

T or F In python, all data attributes must be defined in the __init__ method.

False

T or F In the XmlParser example we needed to add the singleton code to each state class separately so that each would have its own singleton instance.

False

T or F The adapter pattern cannot be used to change the number of arguments to a method.

False

T or F The concept of Polymorphism describes the ability to modify public data attributes.

False

T or F The default destructor will delete all objects pointed to by instance variables.

False

T or F floats are a good way to store variables representing money.

False

Code coverage from least specific to most specific

Function coverage < Statement coverage < Branch coverage < Edge coverage < Condition coverage

How can I recognize that a function is an instance method?

It takes "self" as the first argument

Rank log levels

NOTSET < DEBUG < INFO < WARNING < ERROR < CRITICAL

Dependency

One object "uses" another- temporary, e.g. taking an object in as a parameter- represented by a dotted arrow

T or F When creating Columns on a SQLAlchemy model, we define them as class level variables rather than instance variables.

True

T or F When inside a context manager (set up by a with clause), the context's __exit__() method will be called even if the code inside the with block is interrupted by an exception.

True

T or F const int* const foo(const int* const bar) const; The code above could be a valid method signature.

True

T or F with Foo(a, b) as name: In the line above, the value returned by Foo's __enter__() method is assigned to the variable, name.

True

T or F A static method in ExampleClass cannot access instance variables for objects of ExampleClass.

True

T or F Abstract methods are meant to be overriden.

True

T or F An observer can be removed at runtime.

True

T or F By default the coverage tool will not count branch coverage.

True

T or F Decorators, Adapters, and Facades are all types of Wrappers.

True

T or F If an exception is never handled, then the program will terminate.

True

T or F State transitions can be handled in either the context class or the state classes.

True

T or F The iterator pattern is built into Python's for loops.

True

T or F The method with the header below is implemented for MyClass. This defines the behavior of < when used to compare instances of MyClass.def __lt__(self, other):

True

In the following code, what method must be defined on foo? [x for x in foo]

__iter__

Enter the code that replaces XXX in this example. def log_calls(func): def wrapper(*args, **kwargs): now = time.time() print( "Calling {0} with {1} and {2}".format( func.__name__, args, kwargs ) ) return_value = XXXX print( "Executed {0} in {1}ms".format( func.__name__, time.time() - now ) ) return return_value return wrapper

func(*args, **kwargs)

In a UML sequence diagram, the arrows indicate...

method calls

Which of the following operators cannot be overloaded? sizeof == [] = + new ->

sizeof

With the unittest framework, what method is run automatically after each test method?

tearDown


Related study sets

Web Apps I (File extension terminology and definitions)

View Set

Mastering Assignment 3 : lipids and membranes

View Set

Evolve: Maternity - Women's Health/Disorders

View Set

Insurance Pre-licensing : Chapter 4 quiz

View Set