CSC148 Exam with Complete solutions (A Graded)
What happens between when the function is executing and when it is finished - ANSWER There
will be a call stack when it is executing. Once it finishes, the call stack is deleted, including the
local variables. The function will finish executing either when it reaches the end of the function
or if it reaches a return statement.
def mess_about(n: int, s: str) -> None:
message = s * n
print(message)
if __name__ == '__main__':
count = 13
word = 'nonsense'
mess_about(count, word)
print(n) - ANSWER name error:n is not defined because there is no return statement in
the function
passing a reference to an immutable object
,like
def emphasize(s: str) -> None:
s = s + s + '!'
if __name__ == '__main__':
word = 'moo'
emphasize(word)
print(word) - ANSWER it will have no effect outside of the funciton because you can't
change an immutable, it will just create a new object
What is special about None - ANSWER we refer to it as both a value and its type.
difference between list and List - ANSWER List is if all are one type list is not as specific
explain class -> instance -> attribute - ANSWER A class is a block of code that defines a type of
data. The built-in Python types that you're familiar with like int, str, and list are all defined by
classes. Suppose we have a class called X. An object whose type is X is called an instance of
class X; for example, the object 3 is an instance of class int.
An instance of a class does not have to contain just a single piece of data as an int does; it can
hold a collection of data bundled together. Each individual piece of data in an instance is
called an instance attribute of the object.
what actually happens when you create a class? like in the tweet example in the readings 2.1 -
ANSWER 1. Create a new Tweet object behind the scenes.
2. Call __init__ with the new object passed to the parameter self, along with the other
three arguments (for who, when, and what).
3. Return the new object. This step is where the object is returned, not directly from the call
to __init__ in Step 2.
, Once we define the Tweet class, how many Tweet objects can we construct? - ANSWER There is
no limit.
tweet.like(10) what does the dot do in this case? - ANSWER we pass one argument, yet the
method has two parameters, self and n. What dot notation does for a method call is
automatically pass the value to the left of the dot (in this case, tweet) as the method's first
parameter self.
difference between class and method - ANSWER Methods are part of the very definition of the
class, and form the basis of how others can use the class. They are bundled together with the
class, and are automatically available to every instance of the class. In contrast, functions that
operate on a class instance must be imported separately before they are used. So it sounds like
functions are "less useful" than methods because you need to do a bit of extra work to use
them.
how do you express that it can be one of two different types when writing a function or
method? - ANSWER Union[int, float] shows that the type of the value can be an int or a float
value could be a certain type, or None, how do we express that - ANSWER Optional[int] which
is equivalent Union[int, None]
Callable[[int, str], bool] what does this mean - ANSWER a type expression for a function
that takes two arguments, an integer and a string, and returns a boolean
write the code that is needed to test the doc strings - ANSWER if __name__ == '__main__':
import doctest # import the doctest library
doctest.testmod()
what is an abstract data type - ANSWER An abstract data type (or ADT) defines some kind of
data and the operations that can be performed on it. It is a pure interface. It is looking primarily
at what data is stored and what can we do with that data
What happens between when the function is executing and when it is finished - ANSWER There
will be a call stack when it is executing. Once it finishes, the call stack is deleted, including the
local variables. The function will finish executing either when it reaches the end of the function
or if it reaches a return statement.
def mess_about(n: int, s: str) -> None:
message = s * n
print(message)
if __name__ == '__main__':
count = 13
word = 'nonsense'
mess_about(count, word)
print(n) - ANSWER name error:n is not defined because there is no return statement in
the function
passing a reference to an immutable object
,like
def emphasize(s: str) -> None:
s = s + s + '!'
if __name__ == '__main__':
word = 'moo'
emphasize(word)
print(word) - ANSWER it will have no effect outside of the funciton because you can't
change an immutable, it will just create a new object
What is special about None - ANSWER we refer to it as both a value and its type.
difference between list and List - ANSWER List is if all are one type list is not as specific
explain class -> instance -> attribute - ANSWER A class is a block of code that defines a type of
data. The built-in Python types that you're familiar with like int, str, and list are all defined by
classes. Suppose we have a class called X. An object whose type is X is called an instance of
class X; for example, the object 3 is an instance of class int.
An instance of a class does not have to contain just a single piece of data as an int does; it can
hold a collection of data bundled together. Each individual piece of data in an instance is
called an instance attribute of the object.
what actually happens when you create a class? like in the tweet example in the readings 2.1 -
ANSWER 1. Create a new Tweet object behind the scenes.
2. Call __init__ with the new object passed to the parameter self, along with the other
three arguments (for who, when, and what).
3. Return the new object. This step is where the object is returned, not directly from the call
to __init__ in Step 2.
, Once we define the Tweet class, how many Tweet objects can we construct? - ANSWER There is
no limit.
tweet.like(10) what does the dot do in this case? - ANSWER we pass one argument, yet the
method has two parameters, self and n. What dot notation does for a method call is
automatically pass the value to the left of the dot (in this case, tweet) as the method's first
parameter self.
difference between class and method - ANSWER Methods are part of the very definition of the
class, and form the basis of how others can use the class. They are bundled together with the
class, and are automatically available to every instance of the class. In contrast, functions that
operate on a class instance must be imported separately before they are used. So it sounds like
functions are "less useful" than methods because you need to do a bit of extra work to use
them.
how do you express that it can be one of two different types when writing a function or
method? - ANSWER Union[int, float] shows that the type of the value can be an int or a float
value could be a certain type, or None, how do we express that - ANSWER Optional[int] which
is equivalent Union[int, None]
Callable[[int, str], bool] what does this mean - ANSWER a type expression for a function
that takes two arguments, an integer and a string, and returns a boolean
write the code that is needed to test the doc strings - ANSWER if __name__ == '__main__':
import doctest # import the doctest library
doctest.testmod()
what is an abstract data type - ANSWER An abstract data type (or ADT) defines some kind of
data and the operations that can be performed on it. It is a pure interface. It is looking primarily
at what data is stored and what can we do with that data