100% Accurate Answers | Data Science |
Programming | Pass Guaranteed - A+ Graded
Python Programming Fundamentals & Data Structures
Q1: You are writing a script to calculate exact financial figures. If you run the operation
in Python 3, what is the resulting data type?
A. int
B. float [CORRECT]
C. decimal
D. double
Correct Answer: B
Rationale: The correct answer is float because in Python 3, the standard division
operator (/) always returns a floating-point number, even if the operands are integers,
ensuring we don't lose precision in fractional calculations.
Q2: A student is trying to extract the middle two elements from a list my_list = [10, 20,
30, 40, 50] using slicing. Which of the following expressions will correctly return [20,
30, 40]?
A. my_list[1:4] [CORRECT]
B. my_list[2:4]
C. my_list[1:3]
D. my_list[2:5]
Correct Answer: A
Rationale: The correct answer is my_list[1:4] because Python slicing is exclusive of
the stop index, meaning this slice grabs elements at index 1, 2, and 3, exactly capturing
the middle three requested elements without including the final 50.
Q3: You have an existing list data = [1, 2] and you want to add the elements of
another list more_data = [3, 4] so that data becomes [1, 2, 3, 4]. Which method
should you use?
A. data.append(more_data)
B. data.extend(more_data) [CORRECT]
C. data.insert(more_data)
D. data.add(more_data)
Correct Answer: B
,Rationale: The correct answer is data.extend(more_data) because the extend method
iterates through the provided iterable and appends each individual element to the list,
whereas append would add the entire list as a single nested sublist.
Q4: In CIS 5450, remember that lists are mutable but tuples are immutable, so use a
tuple when you need hashable keys for a dictionary. Given this, which of the following is
a valid dictionary key?
A. ['a', 'b']
B. (1, 2) [CORRECT]
C. {1, 2}
D. [1]
Correct Answer: B
Rationale: The correct answer is (1, 2) because tuples are immutable and therefore
hashable, making them perfectly safe to use as dictionary keys, unlike lists or sets
which are mutable and will throw a TypeError.
Q5: You are accessing a dictionary user_scores = {'alice': 85, 'bob': 92} and you
aren't sure if 'charlie' is in it. Which approach will return None instead of throwing a
KeyError if 'charlie' is missing?
A. user_scores['charlie']
B. user_scores.pop('charlie')
C. user_scores.get('charlie') [CORRECT]
D. user_scores.find('charlie')
Correct Answer: C
Rationale: The correct answer is user_scores.get('charlie') because the get method
is designed to safely return a default value (which defaults to None) if the key is not
found, preventing your program from crashing.
Q6: You need to remove all duplicate integers from a list [1, 2, 2, 3, 4, 4, 5] while
preserving the unique elements. What is the most Pythonic way to achieve this?
A. Convert the list to a set [CORRECT]
B. Iterate with a for loop and use list.remove()
C. Use a list comprehension with an if statement checking the index
D. Sort the list and use list.pop()
Correct Answer: A
Rationale: The correct answer is converting the list to a set because sets inherently only
store unique elements, making set([1, 2, 2, 3, 4, 4, 5]) the fastest and most
readable way to filter out duplicates.
Q7: Given two sets, A and B
= {1, 2, 3} = {3, 4, 5}, what does the operation
A.symmetric_difference(B) return?
, A. {3}
B. {1, 2, 4, 5} [CORRECT]
C. {1, 2, 3, 4, 5}
D. {1, 2}
Correct Answer: B
Rationale: The correct answer is {1, 2, 4, 5} because the symmetric difference
operator returns a new set containing elements that are in either of the sets, but not in
their intersection (effectively the opposite of an intersection).
Q8: You are working with a nested data structure: employees = [{'name': 'Alice',
'role': 'Engineer'}, {'name': 'Bob', 'role': 'Analyst'}]. How do you extract
Bob's role?
A. employees[1]['role'] [CORRECT]
B. employees['Bob']['role']
C. employees[2].role
D. employees.get(1).get('role')
Correct Answer: A
Rationale: The correct answer is employees[1]['role'] because 'Bob' is stored in the
dictionary at index 1 of the outer list, and standard bracket notation is used to chain the
lookup into the inner dictionary.
Q9: Consider the following control flow logic meant to classify a score:
python
score = 85
if score > 90:
grade = 'A'
elif score > 80:
grade = 'B'
elif score > 70:
grade = 'C'
What value will the grade variable hold?
A. 'A'
B. 'B' [CORRECT]
C. 'C'
D. None
Correct Answer: B
Rationale: The correct answer is 'B' because Python evaluates if/elif blocks
sequentially and stops at the first condition that evaluates to True, meaning an 85
triggers the score > 80 block and skips the rest.