MIS 433 - Programming for Analytics Quiz questions and answers.
What is the correct way to create a single-line comment in Python?
# This is a comment
How do you correctly enter your full name using a multi-line comment in Python?
''' Jon Doe'''
What syntax is used to display the data types of all columns in the DataFrame?
.dtypes
What syntax is used to display the last five rows of a DataFrame?
.tail()
Below is a set of scrambled code for a histogram using matplotlib to display the distribution of total bill amounts from the tips dataset. Provide the correct order of code to display the plot. Scrambled Code:A. plt.hist(tips["total_bill"], bins=10) B. import matplotlib.pyplot as plt C. import seaborn as sns D. tips = sns.load_dataset("tips") E. plt.show()
B → C → D → A → E
A data scientist receives a dataset of customer names, all in uppercase (e.g., "JOHN DOE", "JANE SMITH"). For consistency, they need to convert all names to lowercase for analysis. Which approach would be the most efficient for processing a list of these names?
Using a for statement loop to iterate through the list and apply the .lower() method to each name, storing the result in a new list.
What does the len() function return for a Python list that contains four elements?
The number of elements in the list.
After melting a DataFrame, how does the number of rows typically change in relation to the original DataFrame, assuming you have multiple value columns being melted?
The number of rows increases.
What is true about the code below? total = 0 scores = [100, 100, 50, 50] for value in scores: total = total + 1 print(total)
The output will display: 4
You are building a linear regression model to predict 'sales' based on 'advertising_spend'. Which of the following correctly defines the feature (X) and target (y) for sklearn's LinearRegression model?
X = df[['advertising_spend']], y = df['sales']
A data analyst wrote the following Python code to analyze the df DataFrame from the Seaborn 'tips' dataset: day = input("Enter day: ") day_df = df[df["day"] == day] day_df["tip"].mean()
To calculate the average tip amount for a user-specified day
What is the purpose of the str() function?
To convert a variable or value to a string.
What is the primary purpose of the indentation in the print statement within the following Python code snippet? grades = [95, 73, 85]new_grades = []for grade in grades: new_grades.append(grade + 5) print(new_grades)
To ensure the new_grades list is printed only once, after all elements have been processed and the for loop has completed.
In supervised machine learning, what is the purpose of splitting a dataset into training and testing sets, as done with train_test_split?
To evaluate the model's performance on unseen data and prevent overfitting.
What is the primary purpose of Seaborn's `FacetGrid`?
To map a dataset onto multiple axes organized in a grid, where each subplot shows a subset of the data.
When creating a grid of subplots using plt.subplots(), what is plt.tight_layout() used for?
To prevent labels from overlapping.
What is the primary purpose of using `train_test_split` in machine learning workflows?
To prevent overfitting by ensuring the model is evaluated on unseen data.
When train_test_split() is called, and its results are unpacked into multiple variables, what is the standard sequence of data types returned? X_train, X_test, y_train, y_test = train_test_split(X, y)
Training features, Testing features, Training target, Testing target
What is the output of the code below? schedule = [ ["Monday", "MIS 303: Introduction to Business Information Systems"], ["Tuesday", "MIS 310: Database Management Systems"], ["Wednesday", "MIS 320: Networks and Security"], ["Thursday", "MIS 330: Systems Analysis and Design"], ["Friday", "MIS 412: E-Business Systems Development"] ] for day in schedule: if "Thursday" in day[0]: print(day)
["Thursday", "MIS 330: Systems Analysis and Design"]
What will this code output? spam = ['cat', 'bat', 'rat', 'elephant'] spam[1:3]
['bat', 'rat']
What will this be the output of following user defined function? def letter_filter(input_string): vowels = "aeiou" filtered_letters = [] for letter in input_string.lower(): if letter not in vowels: filtered_letters.append(letter.lower()) return filtered_letters input_text = "HELLO-WORLD!" letter_filter(input_text)
['h', 'l', 'l', '-', 'w', 'r', 'l', 'd', '!']
Which of the following subplot index positions corresponds to the scatter plot of total bill vs tip placed in the 2×2 grid below?
[0,1]
What will this code output? my_list = [10, 20, 30] new_list = my_list.append(40) print(new_list)
[10, 20, 30, 40]
If you have a Pandas Series `day`, how do you get an array of its unique values?
day.unique()
A statement used to define a function, which is a block of reusable code that performs a specific task.
def
Which of the following is a valid way to select a column from a Pandas DataFrame, specifically shown in the documents?
df.column_name
Refer to the DataFrame df below. To calculate the total 'Sales' for each 'Region', what is the correct syntax to use?
df.groupby('Region')['Sales'].sum()
Below is a sample of the df DataFrame. Which of the following code snippets is used to retrieve the first three rows?
df.iloc[0:3]
You have a DataFrame df and want to change the column name old_name to new_name. Which of the following code snippets correctly renames the column in place?
df.rename(columns={'old_name': 'new_name'}, inplace=True)
Given a DataFrame `df` with a column named 'Price', how can you select this column?
df["Price"]
Which pandas method is used to output the following: time Dinner 176 Lunch 68 Name: count, dtype: int64
df["time"].value_counts()
Refer to the DataFrame df, which is loaded from the Seaborn 'tips' dataset, shown below. To retrieve only the rows where the sex is 'Male', what is the correct syntax to use? total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3 3 23.68 3.31 Male No Sun Dinner 2 4 24.59 3.61 Female No Sun Dinner 4 ... ... ... ... ... ... ... ... 155 29.85 5.14 Female No Sun Dinner 5 156 48.33 9.00 Male No Sat Dinner 4 157 25.88 2.00 Male Yes Sat Dinner 3 158 30.46 2.00 Male No Sun Dinner 2 159 14.00 1.50 Male No Sun Dinner 2
df[df["sex"]=="Male"]
Which of the following is the correct way to select rows from a DataFrame named `df` where the 'price' column is greater than 20.00?
df[df['price'] > 20.00]
A control flow statement that allows code to be executed repeatedly based on a condition, iterating over a sequence (such as a list).
for
If you want to concatenate a string and an integer in Python, what must you do explicitly?
Convert the integer to a string
In Python, which data type is typically used to represent currency values or measurements requiring decimal precision?
Decimal
What does the following code snippet do? def greet(name): print(f"Hello, {name}!") greet("Alice")
Defines a function called greet and calls it with the argument "Alice"
Consider the following Python code snippet, which determines a customer's discount level based on their purchase_amount. Analyze the code's flow, paying close attention to nested conditional statements and their indentation. purchase_amount = 1500 # Customer's total purchase amount if purchase_amount > 1000: print("Eligible for Standard Discount") if purchase_amount < 2000: print("Receive Silver Tier Discount") elif purchase_amount < 2500: print("Receive Bronze Tier Discount") else: print("Receive Gold Tier Discount") else: print("No Discount Applied")
Standard member, Silver membership
What does the random_state parameter in the train_test_split function ensure?
That the split is reproducible, meaning you get the same train/test sets every time with the same random_state value.
In scikit-learn, what does the term features refer to in a dataset?
The input variables used to make predictions
Which of the following Python libraries is commonly used for data manipulation and analysis, as demonstrated by loading datasets into DataFrames?
pandas
Consider two DataFrames, df1 with a product_id column and df2 also with a product_id column. Which method is typically used to combine these DataFrames based on their common product_id column?
pd.merge(df1, df2, on='product_id')
How do you read a CSV file named 'data.csv' into a Pandas DataFrame?
pd.read_csv('data.csv')
To convert a column of dates from a string format to a datetime object in Pandas, which function should be used?
pd.to_datetime()
In Matplotlib, how do you create a figure and a set of subplots (e.g., 2 rows, 1 column)?
plt.subplots(2, 1)
To label the x-axis "Time" and the y-axis "Value" on a Matplotlib plot, what functions would you use?
plt.xlabel("Time"); plt.ylabel("Value")
A Python for loop is ideal for which of the following tasks?
Iterating over elements in a sequence (like a list or string) a predetermined number of times.
ow many else blocks can be used in a single if-elif-else statement?
One
When using train_test_split() and training a linear model with model.fit(), which data should be passed to model.fit()?
Only the training data: X_train, y_train
A junior developer has written a Python program that calculates the average of five numbers. However, the program prints the running average after each number is added, instead of just the final average. Which common programming error related to loop structure is most likely causing this issue?
Placing the print statement inside the for the for statement loop instead of after it.
What will this code output? numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(numbers[0] + numbers[-1])
10
What is the output of the code below? grades = [100, 105, 110, 115, 120] new_grades = [] for grade in grades: new_grades.append(grade+5) print(new_grades[1])
110
What will this code output? course = {"MIS": 433, "Course Name": "Programming for Analytics", 202440: 43336} print(len(course_dict))
3
In an Scikit-learn function call like train_test_split(X, y, test_size=0.2, random_state=42), what type of argument is test_size=0.2?
A keyword argument
An online store wants to categorize products based on their unique 3-digit product ID. If the ID is '101', it's "Electronics"; if '202', it's "Apparel"; otherwise, it's "General Merchandise". Which control flow structure is ideal for implementing this product categorization logic?
An if-elif-else chain for mutually exclusive conditions.
What happens if you try to use the + operator between a string and an integer value without explicit conversion in Python?
It raises a TypeError
In Python, a for statement is used to iterate over a sequence of elements. This means the for loop can process each item, one by one, from a collection of data. Based on your understanding of Python's built-in data types, select the three iterable data types that can be directly used as the sequence in a for statement:
List, Dictionary and String
What is the output for the following syntax: course = "MIS 433" print(course + "C01")
MIS 433C01
Based on Problem 5, which line of code correctly prompts the user for their age and converts it to an integer?
age = int(input("Enter your age: "))
Given `fig, ax = plt.subplots()`, which variable represents the axes object where you would typically plot data?
ax
If you have an array of axes objects `axes` from `plt.subplots()`, how would you set the title of the first subplot (at index 0)?
axes[0].set_title("Subplot 1")
Which blocks of code contain syntax errors?
course = [MIS, "433", MIS, "410"] print(course) and course = {MIS: 433, MIS: 410} print(course)
Which line of code correctly defines a dictionary?
course_dict = {"MIS": 433}
In Seaborn, which parameter is commonly used to color plot elements based on a categorical variable (e.g., 'Gender')?
hue
A conditional statement that executes different blocks of code based on multiple conditions, allowing for more complex decision-making.
if-elif-else
A conditional statement that executes a block of code if a condition is true, and another block of code if the condition is false.
if-else
You want to find the average 'price' for each unique 'category' in a DataFrame named items_df. Which combination of methods would achieve this?
items_df.groupby('category').price.mean()
You need to gather a state name from a user and then check if that state exists in a predefined list of U.S. states. If it does, you also need to report the number of characters in the state name. Which Python function is essential for getting the character count of the input string?
len()
How do you find the number of key-value pairs in the dictionary course_dict?
len(course_dict)
What is the output? temperature = 99.99 if temperature >= 100: print("high fever") elif temperature >= 99: print("low-grade fever") else: print("normal") print("Retake temperature?")
low-grade fever
After fitting a LinearRegression model (named model), how can you access the learned coefficients (slopes) of the linear equation?
model.coef_
To remove a column named 'redundant_column' from a DataFrame my_df permanently, which of the following is the correct approach?
my_df = my_df.drop(['redundant_column'], axis=1)
After training a LinearRegression model named my_model, you want to assess how well it performs on your X_test and y_test data. Which method would you use?
my_model.score(X_test, y_test)
If the purpose of the sayHello() function is to greet a person by their name as shown in the output below, what syntax should be in place of the first blank (---)? def sayHello(name): print('Hello ' + ---) sayHello(---) Output: Hello Al
name
Which of the following statements correctly uses the input() function to prompt the user for their name?
name = input("Enter your name: ")
Which of the following code snippets will correctly display a histogram of the tip column using seaborn?
sns.histplot(df["tip"], bins=8)plt.show()
What syntax is used for the chart output below?
sns.scatterplot(x='total_bill', y='tip', hue='time', data=df)
What type of data does the input() function return by default?
string
Which code snippet correctly defines a list of summer sessions and prints it?
summer_sessions = ["Session A", "Session B", "Session C", "Session D"] print(summer_sessions)
Given the list summer_sessions = ["Session A", "Session B", "Session C"], what is the correct way to access the first element?
summer_sessions[0]
In the function call below, which argument is passed as a keyword argument, and what is the key difference between positional and keyword arguments? X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_s
test_size and random_state are keyword arguments because they are assigned explicitly by name
Determine if the variable name is allowed or not allowed in Python.
total_sum = Allowed totalSum = Allowed Total Sum = Not allowed total_$um = Not allowed
You need to predict a new value using a trained LinearRegression model named trained_model. If your model was trained with a single feature, 'feature_A', and you want to predict for a new value of 15.0, which is the correct syntax?
trained_model.predict([[15.0]])
Given a variable mis_int with a value of 433, how do you check its data type?
type(mis_int)
You have defined a function check_state(state_name) that determines if a given state_name exists in a states_list and prints a message. After defining this function, how would you typically call it to test with a user's input?
user_input = input("Enter state: ") check_state(user_input)
Consider the following Python list. What code should be placed in the blank to print the data type of each item as the program iterates through the cases list? cases = ["US", 23.3, "Russia", 4, "Peru", 1.2, {"Russia" : 39.5}] for value in cases: print(type(_______)) # Blank to fill
value
Match the data type.
✅ {"Session A": "5 Week"}Correct match: Dictionary This is a key-value pair enclosed in {}. ✅ "Session A: 5 Week"Correct match: String This is a sequence of characters inside double quotes. ("Session A", "5 Week") Correct match: Tuple This is a pair of values enclosed in parentheses (). ✅ ["Session A", "5 Week"] Correct match: List This is a list of elements enclosed in square brackets [].
