Let's break down each question and its options:
Question 1:
Which of the following is false?
-
A heap is generally faster than a stack. - False. The stack is generally faster than the heap because stack memory allocation and deallocation happen much faster (it follows LIFO) and does not require extra overhead.
-
A heap must be manually managed. - True. Memory in the heap must be manually allocated and deallocated by the programmer; otherwise, it can lead to memory leaks.
-
A stack automatically deallocates space when variables are no longer needed. - True. The stack automatically frees up memory when a function call returns, thus deallocating the space for local variables.
-
A stack uses a last in, first out (LIFO) approach. - True. This is the definition of a stack.
So the false statement is: "A heap is generally faster than a stack."
Question 2:
True or False? In Python, a file is always automatically closed for you after you are done reading it or writing to it.
Response: False. Files in Python must be explicitly closed using the close()
method, or you can use a with
statement, which ensures the file is closed after the block of code.
Question 3:
What type of file is best for storing discrete items that are stored in rows?
Response: CSV. CSV (Comma-Separated Values) files are specifically designed to store data in a tabular form (rows and columns) and can be easily used for discrete items.
Question 4:
Which of the following is the correct way to instantiate an object belonging to the user-defined class Dog that has attributes of name and breed?
-
dog = Dog(self, "Buddy", "Boxer") - Incorrect.
self
is not needed when instantiating an object. -
dog = new Dog(self, "Buddy", "Boxer") - Incorrect. In Python, you do not use the
new
keyword. -
pet = new Dog("Fido", "puppy", "Dalmatian") - Incorrect. Same reason as above; Python does not use
new
. -
pet = Dog("Spot", "Dalmatian") - Correct. This is the proper way to instantiate a Dog object in Python assuming it has a constructor that accepts two parameters (name and breed).
So the correct instantiation is: pet = Dog("Spot", "Dalmatian")
In summary, here are the answers:
- A heap is generally faster than a stack. - False
- In Python, a file is always automatically closed for you after you are done reading it or writing to it. - False
- What type of file is best for storing discrete items that are stored in rows? - CSV
- Correct way to instantiate an object belonging to the user-defined class Dog - pet = Dog("Spot", "Dalmatian")