Page | 1
CSC148 Questions with Detailed
Verified Answers
Question: the only mutable types
Answer: list and dictionaries and user defined classes
Question:Where is all data stored in?
Answer: objects that have 3 components -> id, type and value
Question:What is special about id
Answer: The id of an object is a unique identifier, meaning that no other
object has the same identifier.
Question:difference between object and variable?
Answer: a variable stores an object with value, id and type
Variables are not objects because it doesn't store data but only storing the id.
it REFERS to an object
Question:What is special about the type of object
Answer: it defines what operators you can use on it
Question:3 + 'hello'
Answer: TypeError: unsupported operand type(s) for +: 'int' and 'str'
Question:>>> prof = 'Diane'
, Page | 2
>>> id(prof)
4405312456
>>> prof = prof + ' Horton'
>>> prof
'Diane Horton'
>>> id(prof)
Answer: >>> # The old str object couldn't change, so Python made a new
>>> # str object for the variable prof to refer to. Since it's
>>> # a new object, it has a different id.
>>> id(prof)
4405308016
Question:what is aliasing?
Answer: referring to the same object so they have the same id
Question:phrase this "z = x" in english
Answer: "make z refer to the object that x refers to."
Question:y = y
id value changes?
Answer: no, just like how x= y means they now have the same id,
y = y will also have the same id as before
Question:y = [1,2,3]
x = [1,2,3]
id(y)
id(x)
, Page | 3
Answer: >>> id(y)
2493677724232
>>> id(x)
2493677724296
Question:>>> x = [1, 2, 3]
>>> z = x
>>> z[0] = -999
>>> x
Answer: x = [-999, 2,3]
Question:what if you want to check if two variables value are exactly the
same?
Answer: "== " compares the values stored in the objects reference. This is
called value equality.
Question:what if you want to check if the variables are referring to the exact
same object
Answer: "is" like x is y, which will check the id
Question:special case for immutable stuff?
Answer: >>> x = 'foo'
>>> y = 'foo'
>>> x is y
True
HOWEVER
>>> x = "ice cream"
CSC148 Questions with Detailed
Verified Answers
Question: the only mutable types
Answer: list and dictionaries and user defined classes
Question:Where is all data stored in?
Answer: objects that have 3 components -> id, type and value
Question:What is special about id
Answer: The id of an object is a unique identifier, meaning that no other
object has the same identifier.
Question:difference between object and variable?
Answer: a variable stores an object with value, id and type
Variables are not objects because it doesn't store data but only storing the id.
it REFERS to an object
Question:What is special about the type of object
Answer: it defines what operators you can use on it
Question:3 + 'hello'
Answer: TypeError: unsupported operand type(s) for +: 'int' and 'str'
Question:>>> prof = 'Diane'
, Page | 2
>>> id(prof)
4405312456
>>> prof = prof + ' Horton'
>>> prof
'Diane Horton'
>>> id(prof)
Answer: >>> # The old str object couldn't change, so Python made a new
>>> # str object for the variable prof to refer to. Since it's
>>> # a new object, it has a different id.
>>> id(prof)
4405308016
Question:what is aliasing?
Answer: referring to the same object so they have the same id
Question:phrase this "z = x" in english
Answer: "make z refer to the object that x refers to."
Question:y = y
id value changes?
Answer: no, just like how x= y means they now have the same id,
y = y will also have the same id as before
Question:y = [1,2,3]
x = [1,2,3]
id(y)
id(x)
, Page | 3
Answer: >>> id(y)
2493677724232
>>> id(x)
2493677724296
Question:>>> x = [1, 2, 3]
>>> z = x
>>> z[0] = -999
>>> x
Answer: x = [-999, 2,3]
Question:what if you want to check if two variables value are exactly the
same?
Answer: "== " compares the values stored in the objects reference. This is
called value equality.
Question:what if you want to check if the variables are referring to the exact
same object
Answer: "is" like x is y, which will check the id
Question:special case for immutable stuff?
Answer: >>> x = 'foo'
>>> y = 'foo'
>>> x is y
True
HOWEVER
>>> x = "ice cream"