2023_R_exam_translated(1) (1).ipynb

Full Transcript

{ "cells": \[ { "cell\_type": "markdown", "metadata": {}, "source": \[ "GENERAL COMMENTS ABOUT THE SAMPLE EXAM", "- It's the first year's we're doing Python in this class. So a lot is different from the previous R class. I'm doing my best to translate last year's exam part I. Last year, this part to...

{ "cells": \[ { "cell\_type": "markdown", "metadata": {}, "source": \[ "GENERAL COMMENTS ABOUT THE SAMPLE EXAM", "- It's the first year's we're doing Python in this class. So a lot is different from the previous R class. I'm doing my best to translate last year's exam part I. Last year, this part took 80 minutes for a total of 14 questions. I expect to have about 20 questions and give you 150 minutes in total for that. We don't have a part II this year.", "- I added comment if certain questions are irrelevant for this year because we didn't talk about it. So don't worry about these questions. You can make an attempt to answer other questions." \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# GBA 464: PROGRAMMING FOR ANALYTICS", "\# FINAL EXAM (Part I)", "\# Instructor: Yufeng Huang", "\# Date: August 23, 2023", "", "\# Instructions:", "\# 1. Do not start the exam until instructed.", "\# 2. The exam is divided into two parts with a total duration of 2 hours and 55 minutes.", "\# 3. This part is 80 minutes long, with 14 questions worth a total of 70 points.", "\# 4. The first six questions are multiple choice. The next eight questions require coding or predicting output.", "\# 5. This exam is closed book, but you may bring one letter-sized (or A4-sized) cheat sheet.", "\# 6. Adhere to the Simon Academic Honesty Policy.", "", "\# Please provide your responses in the cells provided below each question." \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 1 (4 pts)", "\# Which word best describes the following built-in objects in Python: `.flatten`, `.mean`, `.reshape`, `.rename`?", "\# (A) Variables", "\# (B) Expressions", "\# (C) Loops", "\# (D) Methods", "", "\# Answer:", "\# The correct answer is (D) Methods" \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 2 (4 pts)", "\# In Python, if you assign `x = \"2012-02-28\"`, what is the data type of `x`?", "\# (A) Date", "\# (B) Integer", "\# (C) String", "\# (D) Boolean", "", "\# Answer:", "\# The correct answer is (C) String" \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 3 (4 pts)", "\# Given the following Python list `a`:", "\# a = \[2, -4, 1, -5, 3, -10\]", "\# Which statement will produce the output `-5`?", "\# (A) print(a, 3)", "\# (B) print(a\[3\])", "\# (C) print(a, -5)", "\# (D) print(a\[-5\])", "", "\# Answer:", "\# The correct answer is (B) print(a\[3\])" \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 4 (4 pts)", "\# Which of the following Python statements creates a list that consists only of integer values?", "\# (A) vec = \[90, False, -199, 9\]", "\# (B) vec = \[\"2\", \"5\", \"a\", \"b\"\]", "\# (C) vec = \[90, 30, -199, 9\]", "\# (D) None of the above", "", "\# Answer:", "\# The correct answer is (C) vec = \[90, 30, -199, 9\]" \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 5 (4 pts)", "\# Consider the following function definition in Python:", "\# def f(x):", "\# z = 4", "\# def g(y):", "\# return y + z", "\# return x + g(x)", "\#", "\# Now suppose you execute the following code:", "", "\# z = 5", "\# f(2)", "", "\# What will be the output of the function call `f(2)`?", "\# (A) 8", "\# (B) 9", "\# (C) 10", "\# (D) None of the above", "", "\# Answer:", "\# The correct answer is (A) 8" \] }, { "cell\_type": "markdown", "metadata": {}, "source": \[ "Comment to Q5: We didn't talk about variable scoping, so don't worry about this question for the exam." \] }, { "cell\_type": "code", "execution\_count": null, "metadata": {}, "outputs": \[\], "source": \[ "\# Question 6 (4 pts)", "\# Suppose you have the following data in a Python pandas DataFrame `city_income`:", "\# city gender income", "\# 0 Rochester male 3000", "\# 1 Rochester female 2500", "\# 2 Buffalo male 2500", "\# 3 Buffalo female 3000", "\#", "\# You want to convert the data into a wide format, where female income and male income would appear ", "\# in two separate columns, like so:", "\# city female male", "\# 0 Buffalo 3000 2500", "\# 1 Rochester 2500 3000", "\#", "\# Which of the following Python pandas operations would achieve this transformation?", "\# (A) city\_income.pivot(index='city', columns='gender', values='income')", "\# (B) city\_income.melt(id\_vars=\['city', 'gender'\])", "\# (C) city\_income.groupby('gender').mean()", "\# (D) None of the above", "", "\# Answer:", "\# The correct answer is (A) city\_income.pivot(index='city', columns='gender', values='income')" \] }, { "cell\_type": "code", "execution\_count": 4, "metadata": {}, "outputs": \[ { "data": { "text/plain": \[ "array(\['1.0', '4.2', '9.05', '10.0'\], dtype='\

Use Quizgecko on...
Browser
Browser