Docsity

Introduction to Programming for Finance Practice Exam, Exams of Technology

Toggle sidebar
bg1
Introduction to Programming for Finance
Practice Exam
Question 1. Which Python data type is immutable?
A) list
B) dict
C) tuple
D) set
Answer: C
Explanation: Tuples cannot be altered after creation, unlike lists, dicts, and sets.
Question 2. What is the output of `print(3 ** 2)`?
A) 6
B) 9
C) 8
D) 12
Answer: B
Explanation: The `**` operator performs exponentiation; 3 squared equals 9.
Question 3. Which keyword is used to define a function in Python?
A) function
B) def
C) lambda
D) func
Answer: B
Explanation: `def` introduces a function definition.
Question 4. In Python, what does the expression `5 / 2` evaluate to?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56

Partial preview of the text

Download Introduction to Programming for Finance Practice Exam and more Exams Technology in PDF only on Docsity!

Practice Exam

Question 1. Which Python data type is immutable? A) list B) dict C) tuple D) set Answer: C Explanation: Tuples cannot be altered after creation, unlike lists, dicts, and sets. Question 2. What is the output of print(3 ** 2)? A) 6 B) 9 C) 8 D) 12 Answer: B Explanation: The ** operator performs exponentiation; 3 squared equals 9. Question 3. Which keyword is used to define a function in Python? A) function B) def C) lambda D) func Answer: B Explanation: def introduces a function definition. Question 4. In Python, what does the expression 5 / 2 evaluate to?

Practice Exam

A) 2

B) 2.

C) 3

D) 2.

Answer: B Explanation: The / operator performs true division, returning a float. Question 5. Which of the following statements correctly creates a dictionary with keys “a” and “b” and values 1 and 2? A) d = {a:1, b:2} B) d = ["a":1, "b":2] C) d = {"a":1, "b":2} D) d = ("a":1, "b":2) Answer: C Explanation: Dictionaries use curly braces with key‑value pairs separated by colons. Question 6. What is the result of bool(0)? A) True B) False C) 0 D) None Answer: B Explanation: In Python, zero is considered falsy, so bool(0) returns False. Question 7. Which loop will execute at least once regardless of the condition?

Practice Exam

Question 10. Which of the following creates a lambda function that adds 5 to its input? A) lambda x: x + 5 B) def add5(x): return x + 5 C) add5 = lambda x + 5 D) lambda(x): x + 5 Answer: A Explanation: The correct lambda syntax is lambda arguments: expression. Question 11. Which list method removes and returns the last element? A) pop() B) remove() C) del D) clear() Answer: A Explanation: list.pop() without an index removes the last item and returns it. Question 12. What will len("Finance") return? A) 6 B) 7 C) 8 D) 5 Answer: B Explanation: The string “Finance” contains 7 characters.

Practice Exam

Question 13. Which of the following is a correct way to slice a list L to obtain elements at positions 2,3,4? A) L[2:5] B) L[2:4] C) L[3:5] D) L[:3] Answer: A Explanation: Slicing is inclusive of the start index and exclusive of the stop index; 2:5 gives indices 2,3,4. Question 14. What does the in operator test for in a list? A) Equality of two lists B) Membership of an element C) Index of an element D) Length of the list Answer: B Explanation: in returns True if the element exists in the list. Question 15. Which of the following statements creates a set containing the numbers 1,2,3? A) {1,2,3} B) [1,2,3] C) (1,2,3) D) set[1,2,3] Answer: A Explanation: Curly braces without key‑value pairs define a set.

Practice Exam

Question 19. How can you compute the mean of a NumPy array arr? A) arr.mean() B) np.mean(arr) C) Both A and B D) np.average(arr, axis=0) only Answer: C Explanation: Both the method and the function return the arithmetic mean. Question 20. Which NumPy function returns the square root of each element in an array? A) np.sqrt() B) np.power(0.5) C) np.log() D) np.exp() Answer: A Explanation: np.sqrt computes element‑wise square roots. Question 21. What does np.percentile(data, 90) compute? A) The 90th percentile value of data B) 90% of the mean C) The 90th element after sorting D) The value below which 90% of the data lies (same as A) Answer: A Explanation: np.percentile returns the value below which the given percentage of observations fall.

Practice Exam

Question 22. Which expression correctly computes the dot product of two 1‑D arrays x and y? A) x @ y B) np.dot(x, y) C) np.multiply(x, y).sum() D) All of the above Answer: D Explanation: All three produce the scalar dot product for 1‑D arrays. Question 23. How do you transpose a 2‑D NumPy array M? A) M.T B) np.transpose(M) C) M.transpose() D) Any of the above Answer: D Explanation: All three return the transposed matrix. Question 24. Which Pandas object is best suited for a single column of data with an index? A) DataFrame B) Series C) Panel D) Index Answer: B Explanation: A Series is a one‑dimensional labeled array.

Practice Exam

Question 28. How would you drop rows containing any NaN values from DataFrame df? A) df.dropna() B) df.fillna(0) C) df.replace(np.nan, None) D) df.isnull() Answer: A Explanation: dropna removes rows (or columns) with missing data. Question 29. Which method fills missing values with the forward‑filled method? A) df.fillna(method='ffill') B) df.fillna(method='bfill') C) df.fillna(0) D) df.replace(np.nan, 0) Answer: A Explanation: ffill propagates the last valid observation forward. Question 30. How do you select rows where the column “Price” is greater than 100 using boolean indexing? A) df.loc[df['Price'] > 100] B) df.iloc[df['Price'] > 100] C) df.where(df['Price'] > 100) D) df.select(df['Price'] > 100) Answer: A Explanation: loc with a boolean mask returns matching rows.

Practice Exam

Question 31. Which Pandas method returns a view of rows 10 through 20 (inclusive) based on integer location? A) df.loc[10:20] B) df.iloc[10:21] C) df.slice(10,20) D) df[10:20] Answer: B Explanation: iloc uses zero‑based integer positions; the stop index is exclusive, so 21 is needed. Question 32. Which function creates a DateTimeIndex from a column named “Date”? A) pd.to_datetime(df['Date']) then assign to index B) df.set_index('Date', inplace=True) after converting to datetime C) Both A and B are needed together D) df.index = pd.DatetimeIndex(df['Date']) Answer: B Explanation: Converting the column to datetime and setting it as the index creates a DateTimeIndex. Question 33. How would you resample daily price data to weekly average using Pandas? A) df.resample('W').mean() B) df.groupby('week').mean() C) df.resample('7D').average() D) df.rolling('W').mean() Answer: A Explanation: resample('W') groups by week, and mean() computes the average.

Practice Exam

Question 37. How do you calculate log returns from a price series P? A) np.log(P / P.shift(1)) B) (P.diff() / P.shift(1)) C) P.pct_change() D) np.exp(P / P.shift(1)) Answer: A Explanation: Log return = ln(P_t / P_{t-1}). Question 38. Which method computes cumulative product of simple returns to obtain cumulative growth? A) returns.cumprod() B) returns.cumsum() C) np.cumprod(returns) D) Both A and C are correct Answer: D Explanation: Both the Series method and NumPy function calculate cumulative product. Question 39. How do you annualize a daily volatility sigma_daily assuming 252 trading days per year? A) sigma_daily * np.sqrt(252) B) sigma_daily / np.sqrt(252) C) sigma_daily * 252 D) sigma_daily ** 252 Answer: A

Practice Exam

Explanation: Volatility scales with the square root of time. Question 40. Given asset returns DataFrame R and weight vector w, which expression yields portfolio returns? A) (R * w).sum(axis=1) B) R.dot(w) C) np.dot(R, w) (both A and B are valid) D) R @ w.T Answer: C Explanation: Multiplying returns by weights and summing across assets gives portfolio return; both dot products work. Question 41. How can you compute the covariance matrix of asset returns in DataFrame R? A) R.cov() B) np.cov(R) C) R.corr() D) np.corrcoef(R) Answer: A Explanation: DataFrame.cov() returns the covariance matrix. Question 42. Which of the following computes the correlation matrix of returns? A) R.corr() B) np.corrcoef(R) C) R.cov() D) Both A and B (but A is the pandas method)

Practice Exam

Answer: A Explanation: RSI = 100 – 100/(1 + RS) where RS = avg gain / avg loss. Question 46. Which statement correctly generates a binary buy/sell signal when a short‑term SMA crosses above a long‑term SMA? A) signal = np.where(short_sma > long_sma, 1, - 1) B) signal = (short_sma > long_sma).astype(int) * 2 - 1 (both produce 1/-1) C) signal = (short_sma > long_sma).replace({True:1, False:-1}) D) Any of the above (choose simplest) Answer: A Explanation: np.where directly assigns 1 for buy, - 1 for sell based on the crossover condition. Question 47. Which NumPy function generates 1000 random draws from a normal distribution with mean 0 and std 1? A) np.random.normal(0,1,1000) B) np.random.randn(1000) C) Both A and B (they are equivalent) D) np.random.uniform(-1,1,1000) Answer: C Explanation: randn returns standard normal draws; specifying mean and std also works. Question 48. In a Geometric Brownian Motion simulation, which formula updates the price S_t? A) S_{t+1} = S_t * np.exp((mu - 0.5*sigma**2)*dt + sigma*np.sqrt(dt)*Z) B) S_{t+1} = S_t + mu*dt + sigma*Z C) S_{t+1} = S_t * (1 + mu*dt + sigma*Z)

Practice Exam

D) S_{t+1} = S_t * np.exp(mu*dt + sigma*Z) Answer: A Explanation: The GBM discretization uses the exponential of drift and diffusion terms. Question 49. Which Matplotlib function creates a line plot of x versus y? A) plt.plot(x, y) B) plt.scatter(x, y) C) plt.bar(x, y) D) plt.hist(y) Answer: A Explanation: plot draws lines connecting data points. Question 50. How do you add a title “Stock Price” to a Matplotlib figure? A) plt.title('Stock Price') B) plt.xlabel('Stock Price') C) plt.legend('Stock Price') D) plt.text('Stock Price') Answer: A Explanation: title sets the figure’s main heading. Question 51. Which command adds a legend to a Matplotlib plot? A) plt.legend() B) plt.title() C) plt.xlabel() D) plt.show()

Practice Exam

Answer: A Explanation: scatter directly creates a scatter plot of x vs y. Question 55. Which method creates a figure with two subplots stacked vertically? A) fig, (ax1, ax2) = plt.subplots(2, 1) B) plt.subplot(2, 1, 1); plt.subplot(2, 1,2) C) Both A and B are valid D) plt.figure(2) Answer: C Explanation: Both approaches generate two rows, one column of subplots. Question 56. What does the expression df['Close'].pct_change() compute? A) Simple daily returns (percentage change) B) Log returns C) Cumulative returns D) Moving average Answer: A Explanation: pct_change returns (current/previous) – 1. Question 57. Which Pandas function removes duplicate rows based on all columns? A) df.drop_duplicates() B) df.unique() C) df.remove_duplicates() D) df.dropna() Answer: A

Practice Exam

Explanation: drop_duplicates eliminates repeated rows. Question 58. How can you convert a column “Price” from string type to float? A) df['Price'] = df['Price'].astype(float) B) df['Price'] = pd.to_numeric(df['Price']) C) Both A and B are correct D) df['Price'] = float(df['Price']) Answer: C Explanation: Both astype and to_numeric perform the conversion. Question 59. Which NumPy function efficiently computes the dot product of two 2‑D arrays A (3×2) and B (2×4)? A) np.dot(A, B) B) A @ B C) np.matmul(A, B) D) All of the above Answer: D Explanation: All three perform matrix multiplication when dimensions align. Question 60. In Pandas, which method returns a DataFrame with rows where column “Volume” is between 1,000 and 10,000 inclusive? A) df[(df['Volume'] >= 1000) & (df['Volume'] <= 10000)] B) df.loc[1000:10000, 'Volume'] C) df.query('1000 <= Volume <= 10000') (both A and C are valid) D) df.between('Volume', 1000, 10000)