Python for Data Science Cognitive Class Answers with Explanations (All Modules + Final Exam)

Last updated: 15 November 2026 · First published: 1 January 2024
In this guide
  1. Python for Data Science quiz and final exam answers
  2. Course Syllabus:
  3. General Information:
  4. Module 1: Python Basics
  5. Module 2: Python Data Structures
  6. Module 3: Python Programming Fundamentals
  7. Module 4: Working with Data in Python
  8. Module 5: Working with Numpy Arrays
  9. Python for Data Science Final Exam Answers
  10. Introduction to Python for Data Science
  11. Frequently asked questions

Python for Data Science quiz and final exam answers

These are the answers to the module quizzes and final exam of Python for Data Science, a free course from IBM’s Cognitive Class (IBM Skills Network), with a short explanation for each question so you understand the concept rather than just the option.

Course at a glance

  • Provider: Cognitive Class (IBM Skills Network)
  • Cost: Free
  • Credential: IBM digital badge after passing the final exam (check the course page for the current requirements)
  • Official course: cognitiveclass.ai
  • Answers reviewed: November 2026

IBM sometimes updates its question banks. If you see a question that isn’t listed here, leave a comment and we’ll add it. Use these explanations to learn the material; the badge is only worth something if you understand it.

Course Syllabus:

Module 1 – Python Basics

  • Your first program
  • Types
  • Expressions and Variables
  • String Operations

Module 2 – Python Data Structures

  • Lists and Tuples
  • Sets
  • Dictionaries

Module 3 – Python Programming Fundamentals

  • Conditions and Branching
  • Loops
  • Functions
  • Objects and Classes

Module 4 – Working with Data in Python

  • Reading files with open
  • Writing files with open
  • Loading data with Pandas
  • Working with and Saving data with Pandas

Module 5 – Working with Numpy Arrays and Simple APIs

  • Numpy 1D Arrays
  • Numpy 2D Arrays
  • Simple APIs
  • API Setup

General Information:

  • This course is self-paced.
  • It can be taken at any time.
  • It can be audited as many times as you wish.
  • There is only ONE chance to pass the course, but multiple attempts per question

RECOMMENDED SKILLS PRIOR TO TAKING THIS COURSE: Basic Math

Question 1: What is the result of the following operation in Python:

  • 3 + 2 * 2
  • 10
  • 7
  • 9
  • 12

Why: Multiplication runs before addition: 2*2 = 4, then 3 + 4 = 7.

Question 2: In Python, if you executed name = ‘Lizz’, what would be the output of print(name[0:2])?

  • Lizz
  • L
  • Liz
  • Li

Why: Slicing [0:2] takes the characters at index 0 and 1 (the end index is excluded), giving ‘Li’.

Question 3: In Python, if you executed var = ‘01234567’, what would be the result of print(var[::2])?

  • 0246
  • 1357
  • 1234567
  • 8903

Why: [::2] takes every second character starting at index 0: 0, 2, 4 and 6.

Question 4: In Python, what is the result of the following operation ‘1’+’2′

  • ‘2’
  • ‘3’
  • ’12’
  • 3

Why: With strings, + joins (concatenates) them, so ‘1’ + ‘2’ gives ’12’.

Question 5: Given myvar = ‘hello’, how would you convert myvar into uppercase?

  • len(myvar)
  • myvar.find(‘hello’)
  • myvar.upper()
  • myvar.sum()

Why: The string method upper() returns the text in capital letters.

Question 1: What is the syntax to obtain the first element of the tuple:

  • A=(‘a’,’b’,’c’)
  • A[1]
  • A[0]
  • A[:]

Why: Python indexing starts at 0, so A[0] is the first element.

Question 2: After applying the following method,L.append([‘a’,’b’]), the following list will only be one element longer.

  • True
  • False

Why: append() adds its argument as one single element, even when that argument is itself a list.

Question 3: How many duplicate elements can you have in a set?

  • 1
  • 0, you can only have one unique element in a set
  • 100
  • depends on the number of elements in your set.

Why: A set stores only unique values, so it cannot hold duplicates.

Question 4: >>>>Consider the following Python Dictionary:

Dict={“A”:1,”B”:”2″,”C”:[3,3,3],”D”:(4,4,4),’E’:5,’F’:6}, what is the result of the following operation: Dict[“D”]

  • 4
  • 3
  • [3,3,3]
  • (4, 4, 4)
  • error

Why: The key “D” maps to the tuple (4, 4, 4).

Question 5: What is an important difference between lists and tuples?

  • Lists can’t contain a string
  • Tuples can only have integers
  • Lists and tuples are the same.
  • Lists are mutable tuples are not
  • There is no zeros in lists

Why: Lists can be changed after they are created (mutable); tuples cannot (immutable).

Question 1: What is the output of the following lines of code:

x=1

if(x!=1):

print(‘Hello’)

else:

print(‘Hi’)

print(‘Mike’)

  • Hi Mike
  • Mike
  • Hello Mike
  • The Mike

Why: x is 1, so x != 1 is False and the else branch prints ‘Hi’; print(‘Mike’) is outside the if/else, so it always runs.

Question 2: What is the output of the following few lines of code?

A=[‘1′,’2′,’3’]

for a in A:

print(2*a)

  • 2 4 6
  • ‘2’ ‘4’ ‘6’
  • ’11’ ’22’ ’33’
  • A B C

Why: Multiplying a string by 2 repeats it, so ‘1’ becomes ’11’, ‘2’ becomes ’22’ and ‘3’ becomes ’33’.

Question 3: Consider the function Delta, when will the function return a value of 1

def Delta(x):

if x==0:

y=1;

else:

y=0;

return(y)

  • When the input is anything but 0
  • When the input in 1
  • Never
  • When the input is 0

Why: The function sets y to 1 only when the input is 0; for any other input it returns 0.

Question 4: What is the correct way to sort the list ‘B’ using a method, the result should not return a new list, just change the list ‘B’.

  • B.sort()
  • sort(B)
  • sorted(B)
  • B.sorted()

Why: B.sort() sorts the list in place; sorted(B) would return a new list instead.

Question 5: What are the keys of the of the following {‘a’:1,’b’:2}

  • 1,2
  • ;,:
  • a,b

Why: In a dictionary the keys are the values before the colons, here ‘a’ and ‘b’.

Question 1: What do the following lines of code do?

with open(“Example1.txt”,”r”) as file1:

FileContent=file1.readlines()

print(FileContent)

  • Read the file “Example1.txt”
  • Write to the file “Example1.txt”
  • Append the file “Example1.txt”

Why: Mode “r” opens the file for reading, and readlines() reads all its lines.

Question 2: What do the following lines of code do?

with open(“Example2.txt”,”w”) as writefile:

writefile.write(“This is line A\n”)

writefile.write(“This is line B\n”)

  • Read the file “Example2.txt”
  • Write to the file “Example2.txt”
  • Append the file “Example2.txt”

Why: Mode “w” opens the file for writing, replacing any existing content.

Question 3: What do the following lines of code do?

with open(“Example3.txt”,”a”) as file1:

file1.write(“This is line C\n”)

  • Read the file “Example3.txt”
  • Write to the file “Example3.txt”
  • Append the file “Example3.txt”

Why: Mode “a” opens the file for appending, so the new text is added at the end.

Question 4: What is the result of applying the following method df.head(), to the dataframe df

  • prints the first row of the dataframe
  • prints the first column of the dataframe
  • prints the first 5 rows of the dataframe
  • prints the dateframe out

Why: df.head() shows the first five rows of a DataFrame by default.

Question 1: What is the result of the following lines of code:

a=np.array([0,1,0,1,0])

b=np.array([1,0,1,0,1])

a*b

  • 0
  • array([1, 1, 1, 1, 1])
  • array([0, 0, 0, 0, 0])

Why: Corrected answer: * multiplies NumPy arrays element by element (0×1, 1×0, 0×1, 1×0, 0×1), giving array([0, 0, 0, 0, 0]).

Question 2: What is the result of the following lines of code:

a=np.array([0,1])

b=np.array([1,0])

np.dot(a,b)

  • 1
  • array([1,1])
  • 0
  • array([0,0])

Why: np.dot multiplies matching elements and adds the results: 0×1 + 1×0 = 0.

Question 3: What is the result of the following lines of code:

a=np.array([1,1,1,1,1])

a+10

  • array([10,10,10,10,10])
  • array([11, 11, 11, 11, 11])
  • array([1,1,1,1,1])

Why: Adding a number to an array adds it to every element (broadcasting), so every 1 becomes 11.

Question 4: What is the correct code to perform matrix multiplication on the matrix A and B

  • np.dot(A,B)
  • A*B
  • AxB

Why: np.dot(A, B) performs matrix multiplication; A*B would multiply element by element.

Question 1: What is the result of the following operation 3+2*2?

  • 3
  • 12
  • 9
  • 7

Why: Multiplication runs before addition: 2*2 = 4, then 3 + 4 = 7.

Question 2: What is the type of the following variable: a=True?

  • int
  • bool
  • str
  • list

Why: True and False are Boolean values, of type bool.

Question 3: What is the result of the following operation int(3.2)?

  • 3.2
  • 3
  • 4
  • ‘3.2’

Why: int() drops the decimal part, so int(3.2) is 3.

Question 4: Consider the string A=’1234567′, what is the result of the following operation: A[1::2]

  • ‘1234567’
  • ‘246’
  • ‘1357’
  • error

Why: [1::2] starts at index 1 and takes every second character: ‘2’, ‘4’ and ‘6’.

Question 5: Consider the string Name=”Michael Jackson” , what is the result of the following operation Name.find(‘el’)

  • 5
  • 4
  • 5,6
  • -1

Why: find() returns the index where ‘el’ starts; counting from 0, the ‘e’ in “Michael” is at index 5.

Question 6: The variables A=’1′ and B=’2′ ,what is the result of the operation A+B?

you cant add two strings

  • 3
  • ‘3’
  • ’12’

Why: With strings, + joins them, so ‘1’ + ‘2’ gives ’12’.

Question 7: Consider the variable F=”You are wrong”, Convert the values in the variable F to uppercase?

  • F.up()
  • F.upper
  • F.upper()

Why: upper() is a method, so it needs parentheses to run: F.upper().

Question 8: Consider the tuple tuple1=(“A”,”B”,”C” ), what is the result of the following operation tuple1[-1]?

  • “A”
  • “B”
  • “C”

Why: Index -1 refers to the last element, “C”.

Question 9: Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A[1]:

  • ((11,12),[21,22])
  • (11,12)
  • (21,22)
  • [21,22]

Why: A[1] is the second element, the list [21, 22].

Question 10: Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A[0][1]:

  • 12
  • 11
  • 22
  • 21

Why: A[0] is the tuple (11, 12), and [1] picks its second item, 12.

Question 11: What is the result of the following operation ‘1,2,3,4’.split(‘,’)

  • ‘1’,’2′,’3′,’4′
  • (‘1′,’2′,’3′,’4’)
  • [‘1′,’2′,’3′,’4’]
  • ‘1234’

Why: split(‘,’) breaks the string at each comma and returns a list of the pieces.

Question 12: Concatenate the following lists A=[1,’a’] and B=[2,1,’d’]:

  • A+B
  • A-B
  • A*B
  • A/B

Why: + joins two lists into one longer list.

Question 13: How do you cast the list ‘A’ to the set ‘a’?

  • a.set()
  • a=A.append()
  • a=A.dict()
  • a=set(A)

Why: set(A) creates a set from the list’s elements.

Question 14: Consider the Set: V={‘A’,’B’}, what is the result of V.add(‘C’)?

  • {‘A’,’B’}
  • {‘A’,’B’,’C’}
  • {‘AC’,’BC’}
  • error

Why: add() inserts ‘C’ into the set.

Question 15: Consider the Set: V={‘A’,’B’,’C’ }, what is the result of V.add(‘C’)?

  • {‘A’,’B’}
  • {‘A’,’B’,’C’}
  • {‘A’,’B’,’C’,’C’}
  • error

Why: ‘C’ is already in the set, and sets ignore duplicates, so the set is unchanged.

Question 16: What is the output of the following lines of code:

x=”Go”

if(x!=”Go”):

print(‘Stop’)

else:

print(‘Go ‘)

print(‘Mike’)

  • Go Mike
  • Mike
  • Stop Mike
  • The Mike

Why: The condition sends execution to the branch that prints ‘Go ‘, and print(‘Mike’) always runs because it sits outside the if/else.

Question 17: What is the output of the following lines of code:

x=”Go”

if(x==”Go”):

print(‘Go ‘)

else:

print(‘Stop’)

print(‘Mike’)

  • Go Mike
  • Mike
  • Stop Mike
  • The Mike

Why: The condition sends execution to the branch that prints ‘Go ‘, and print(‘Mike’) always runs because it sits outside the if/else.

Question 18: How many iterations are performed in the following loop?

for n in range(3):

print(n)

  • 1
  • 2
  • 3
  • 4

Why: range(3) produces 0, 1 and 2, so the loop runs 3 times.

Question 19: What does the following loop print?

for n in range(3):

print(n+1)

  • 0 1 2
  • 1 2 3
  • 3 2 1
  • 2 1 0

Why: n takes the values 0, 1 and 2, and the loop prints n+1: 1, 2, 3.

Question 20: What is the output of the following few lines of code ?

A=[‘1′,’2′,’3’]

for a in A:

print(2*a)

  • 2 4 6
  • ‘2’ ‘4’ ‘6’
  • ’11’ ’22’ ’33’
  • A B C

Why: Multiplying a string by 2 repeats it, so ‘1’ becomes ’11’, ‘2’ becomes ’22’ and ‘3’ becomes ’33’.

Question 21: Consider the function add, what is the result of calling the following Add(‘1′,’1’) (look closely at the return statement)

def Add(x,y):

z=y+x

return(y)

  • error
  • ‘2’
  • ’11’
  • ‘1’

Why: The function returns y, not z, and y is ‘1’.

Question 22: Consider the class Points, what are the data attributes:

class Points(object):

def __init__(self,x,y):

self.x=x

self.y=y

def print_point(self):

print(‘x=’,self.x,’y=’,self.y)

  • __init__
  • self.x self.y
  • print_point

Why: Data attributes are the values stored on each object: self.x and self.y.

Question 23: What is the result of running the following lines of code ?

class Points(object):

def __init__(self,x,y):

self.x=x

self.y=y

def print_point(self):

print(‘x=’,self.x,’ y=’,self.y)

p1=Points(1,2)

p1.print_point()

  • x=1
  • y=2
  • x=1 y=2

Why: Points(1, 2) stores x=1 and y=2, and print_point() prints both.

Question 24: What is the result of running the following lines of code?

class Points(object):

def __init__(self,x,y):

self.x=x

self.y=y

def print_point(self):

print(‘x=’,self.x,’ y=’,self.y)

p2=Points(1,2)

p2.x=2

p2.print_point()

  • x=1
  • y=2
  • x=1 y=2
  • x=2 y=2

Why: p2.x is changed to 2 before printing, so it prints x=2 y=2.

Question 25: Consider the following line of code: with open(example1,”r”) as file1:

What mode is the file object in?

  • read
  • write
  • append

Why: “r” means the file is opened in read mode.

Introduction to Python for Data Science

Python is widely recognized as one of the most versatile and powerful programming languages, particularly in the realm of data science. Its popularity stems from its simplicity and readability, making it accessible for beginners while offering robust capabilities for professionals.

Why Python for Data Science?

  1. Ease of Learning: Python’s syntax is straightforward and easy to understand, even for those new to programming.
  2. Extensive Libraries: Python boasts a rich ecosystem of libraries and tools specifically designed for data manipulation, analysis, and visualization. Key libraries include:
    • NumPy: Fundamental package for numerical computing with arrays.
    • Pandas: Data manipulation and analysis tool.
    • Matplotlib and Seaborn: Libraries for data visualization.
    • Scikit-learn: Simple and efficient tools for data mining and data analysis.
    • TensorFlow and PyTorch: Libraries for machine learning and deep learning.
  3. Community and Support: Python has a vast and active community of developers and data scientists who contribute to its growth and provide support through forums, tutorials, and open-source contributions.

Getting Started with Python for Data Science:

  1. Installation: Start by installing Python and the necessary libraries. Anaconda distribution is popular for data science as it includes many of the essential libraries pre-installed.
  2. Basic Python Concepts: Familiarize yourself with Python basics such as variables, data types (lists, tuples, dictionaries), control structures (if statements, loops), functions, and object-oriented programming if needed.
  3. Working with Libraries:
    • NumPy: Learn to create arrays, perform operations on them, and understand broadcasting and vectorization.
    • Pandas: Explore data structures like Series and DataFrame, learn data manipulation techniques such as filtering, sorting, grouping, and merging.
    • Matplotlib/Seaborn: Plotting histograms, scatter plots, line plots, and customizing plots for effective data visualization.
  4. Data Analysis and Visualization: Practice loading datasets, performing exploratory data analysis (EDA), cleaning data (handling missing values, outliers), and visualizing data to extract insights.
  5. Introduction to Machine Learning: Once comfortable with data manipulation and visualization, delve into basic machine learning concepts using libraries like Scikit-learn. Start with simple models like linear regression, decision trees, or k-nearest neighbors.
  6. Advanced Topics: As you progress, explore more advanced topics such as deep learning using TensorFlow or PyTorch, natural language processing (NLP), time series analysis, or big data processing with tools like Spark.

Resources for Learning:

  • Online Courses: Platforms like Coursera, edX, and Udemy offer courses specifically tailored to learning Python for data science.
  • Books: Recommended titles include “Python for Data Analysis” by Wes McKinney, and “Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow” by Aurélien Géron.
  • Documentation and Tutorials: Official documentation for Python and its libraries, along with community-contributed tutorials and blogs, are valuable resources.

Conclusion:

Python’s versatility and the vast array of libraries make it an ideal choice for data science projects, from data cleaning and analysis to machine learning and deep learning applications. By mastering Python fundamentals and its specialized libraries, you’ll be well-equipped to tackle real-world data challenges and advance your career in data science.

Frequently asked questions

Is Python for Data Science on Cognitive Class free?

Yes. Cognitive Class courses are free; you only need to create a free account.

Do I get a certificate?

Cognitive Class awards an IBM digital badge (issued through Credly) when you pass the course’s final exam with the required score. You can add the badge to your LinkedIn profile and résumé.

How many attempts do I get?

Attempt limits vary by course and are shown on each quiz. Read the limit before starting the final exam.

Leave a Comment