CSCI290 – Midterm
Grades:
Q1- Q6: 1 point for each question.
Q7 - Q13: 2 points for each question.
Note: All your answers, including codes and their results (screen capture image), shall be
written in this document. Rename this document to lastname_midterm.doc (e.g.
abla_midterm.doc) and submit. This is the only document you need to submit.
Student name:
Q1: I have following two python lists:
List1 = [1, 2, 3, 4, 5]
List2 = [10, 20, 30, 40, 50]
I’ve created List3 by dividing each element of List2 by 2.
I’ve converted List1 and List3 to numpy arrays: a,b.
Then, I did the following:
c= a*b
d =numpy.sum(c)
Converted d from floating point data type into integer.
what will be the value of d?
a. 550
b. 275
c. 1050
d. 525
Answer: b. 275
Q2: What is the output of the following code:
def mystery(value1, value2, value3):
m_value = value1
if value2 > m_value:
m_value = value2
if value3 > m_value:
m_value = value3
return m_value
print(mystery(123, 245, 36))
print(mystery(23.3, 45.6, 99.7))
print(mystery('yellow', 'red', 'blue' ))
,Answer:
245
99.7
yellow
Q3: Which of the following statements about numpy is false?
a. Arrays in this library perform computations more efficiently than standard Python lists.
b. Arrays from this library allow elements of various types within the same container.
c.The library includes built-in support for linear algebra, signal processing, and random number
generation.
d. Arrays within this library have fixed dimensions and require explicit modifications to alter
their shape.
Answer: b. Arrays from this library allow elements of various types within the
same container.
Q4. Which of the following statements about Pandas is incorrect?
a. Pandas DataFrames allow columns to have different data types
b. Pandas Series are always two-dimensional, like DataFrames.
c. Pandas provides built-in functions for handling missing data.
d. Pandas supports reading and writing data from multiple file formats, including CSV,
Excel, and SQL databases.
Answer: b. Pandas Series are always two-dimensional, like DataFrames.
Q5: Given the following class definitions and instances:
There is a base class Animal with an __init__ method that takes a name parameter and
initializes a self.name attribute.
The Animal class also has a speak method that prints "The animal speaks".
There is a derived class Dog that inherits from Animal.
The Dog class overrides the speak method to print "The dog barks".
There is another derived class Puppy that inherits from Dog, but it does not define any
new methods.
An instance dog of the Dog class is created with the name "Buddy".
An instance puppy of the Puppy class is created with the name "Rufus".
The speak method is called on both dog and puppy (as following):
dog.speak()
puppy.speak()
What is the output?
A.
The dog barks
The animal speaks
, B.
The animal speaks
The animal speaks
C.
The dog barks
The dog barks
D.
The animal speaks
The dog barks
Answer: C.
The dog barks
The dog barks
Q6: Given the following Pandas data frame
Name Age Score
0 Alice 25 75
1 Bob 32 88
2 Carol 27 92
3 Dave 29 81
4 Emily 31 67
What is the correct way to sort the DataFrame df in descending order based on the Age column?
A. df.sort_values(by='Age', ascending=False, inplace=True)
B. df.sort_values('Age', ascending=True, inplace=True)
C. df = df.sort_values('Age', ascending=False)
D. df.sort_values(['Score'], ascending=[False], inplace=True)
Answer: A. df.sort_values(by='Age', ascending=False, inplace=True)
Q7: Please write a code to count the total words and total characters, not including dash (“-“) and
space between the words, in the following variable blue_dot. Please show your code and results.
blue_dot = """ "We succeeded in taking that picture [from deep space],
and, if you look at it, you see a dot. That's here. That's home.
That's us. On it, everyone you ever heard of, every human being who
ever lived, lived out their lives. The aggregate of all our joys and
sufferings, thousands of confident religions, ideologies and economic
Grades:
Q1- Q6: 1 point for each question.
Q7 - Q13: 2 points for each question.
Note: All your answers, including codes and their results (screen capture image), shall be
written in this document. Rename this document to lastname_midterm.doc (e.g.
abla_midterm.doc) and submit. This is the only document you need to submit.
Student name:
Q1: I have following two python lists:
List1 = [1, 2, 3, 4, 5]
List2 = [10, 20, 30, 40, 50]
I’ve created List3 by dividing each element of List2 by 2.
I’ve converted List1 and List3 to numpy arrays: a,b.
Then, I did the following:
c= a*b
d =numpy.sum(c)
Converted d from floating point data type into integer.
what will be the value of d?
a. 550
b. 275
c. 1050
d. 525
Answer: b. 275
Q2: What is the output of the following code:
def mystery(value1, value2, value3):
m_value = value1
if value2 > m_value:
m_value = value2
if value3 > m_value:
m_value = value3
return m_value
print(mystery(123, 245, 36))
print(mystery(23.3, 45.6, 99.7))
print(mystery('yellow', 'red', 'blue' ))
,Answer:
245
99.7
yellow
Q3: Which of the following statements about numpy is false?
a. Arrays in this library perform computations more efficiently than standard Python lists.
b. Arrays from this library allow elements of various types within the same container.
c.The library includes built-in support for linear algebra, signal processing, and random number
generation.
d. Arrays within this library have fixed dimensions and require explicit modifications to alter
their shape.
Answer: b. Arrays from this library allow elements of various types within the
same container.
Q4. Which of the following statements about Pandas is incorrect?
a. Pandas DataFrames allow columns to have different data types
b. Pandas Series are always two-dimensional, like DataFrames.
c. Pandas provides built-in functions for handling missing data.
d. Pandas supports reading and writing data from multiple file formats, including CSV,
Excel, and SQL databases.
Answer: b. Pandas Series are always two-dimensional, like DataFrames.
Q5: Given the following class definitions and instances:
There is a base class Animal with an __init__ method that takes a name parameter and
initializes a self.name attribute.
The Animal class also has a speak method that prints "The animal speaks".
There is a derived class Dog that inherits from Animal.
The Dog class overrides the speak method to print "The dog barks".
There is another derived class Puppy that inherits from Dog, but it does not define any
new methods.
An instance dog of the Dog class is created with the name "Buddy".
An instance puppy of the Puppy class is created with the name "Rufus".
The speak method is called on both dog and puppy (as following):
dog.speak()
puppy.speak()
What is the output?
A.
The dog barks
The animal speaks
, B.
The animal speaks
The animal speaks
C.
The dog barks
The dog barks
D.
The animal speaks
The dog barks
Answer: C.
The dog barks
The dog barks
Q6: Given the following Pandas data frame
Name Age Score
0 Alice 25 75
1 Bob 32 88
2 Carol 27 92
3 Dave 29 81
4 Emily 31 67
What is the correct way to sort the DataFrame df in descending order based on the Age column?
A. df.sort_values(by='Age', ascending=False, inplace=True)
B. df.sort_values('Age', ascending=True, inplace=True)
C. df = df.sort_values('Age', ascending=False)
D. df.sort_values(['Score'], ascending=[False], inplace=True)
Answer: A. df.sort_values(by='Age', ascending=False, inplace=True)
Q7: Please write a code to count the total words and total characters, not including dash (“-“) and
space between the words, in the following variable blue_dot. Please show your code and results.
blue_dot = """ "We succeeded in taking that picture [from deep space],
and, if you look at it, you see a dot. That's here. That's home.
That's us. On it, everyone you ever heard of, every human being who
ever lived, lived out their lives. The aggregate of all our joys and
sufferings, thousands of confident religions, ideologies and economic