Data Types in Python MCQ


Hello friends in this post we are going to discuss Data Types in Python MCQ | Data Types in Python Objective Type Questions | Data Types in Python Multiple Choice Questions | Data Types in Python Question Answers

Complex Data Types in Python: Working with Dictionaries & Sets in
Python:

1] Consider a dictionary of names and ages set up as below: Code Editor:
names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}
How would you look up Alice’s age in this dictionary?

Ans: names_ages[‘Alice’]

2] Consider a dictionary of names and ages set up as below:
Code Editor:
names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}
What would the output be if you were to run this code?
names_ages[‘Tim’]

Ans: KeyError: ’Tim’

3] Consider a dictionary of names and ages set up as below:
Code Editor:
names_ages = {‘John’: 35, ‘Jim’: 45, ‘Alice’: 25}
and a second dictionary as below:
Code Editor:
updated_names_ages = {‘Ella’: 29, ‘John’: 36}
How would you update the names_ages dictionary with the values in
updated_names_ages dictionary?

Ans: names_ages.update(updated_names_ages)

4] A set in Python can contain which of the following data types?

Ans: Floats, tuples, Strings

5] Consider two sets of integers:
Code Editor:
set_1 = {2, 4, 6, 8}
set_2 = {1, 2, 5, 6, 7, 8}
What operation would I run to get a result set with all of the elements from
both sets?

Ans: set_1.union(set_2)

6] Consider a nested list of names and ages:
Code Editor:
names_ages = [[‘John’, 35], [‘Jill’, 38], [‘Tim’, 27]]
How would you access Tim’s age in this nested list?

Ans: names_ages[2][1]

7] Consider a nested list of names and ages:
Code Editor:
names_ages = [[‘John’, 35], [‘Jill’, 38], [‘Tim’, 27]]
How would you convert this to a dictionary with names as the keys and ages as values?

Ans: dict(names_ages)

MCQS Complex Data Types in Python: Working with Lists & Tuples in Python:

1] Which of the following statements about Python lists are true?

Ans: Lists in python can have elements of different data types, Lists are ordered collections

2] If you wanted to insert an element at index 2 in a particular list named
names_list what is the function that you would invoke?

Ans: names_list.insert(2,”john”)

3] If you want to count the number of times the name “John” appears in the
names_list what function would you invoke?

Ans: names_list.count(“John”)

4] If you wanted to sort the elements in the list names_list in alphabetical order which of the following statements in Python are valid?

Ans: names_list = sorted(names_list), names_list.sort()

5] Consider the list:
Code Editor:
some_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
How do you slice this list to access the elements ‘c’, ‘d’?

Ans: some_lst[2:4]

6] What will be the result of this slicing operation of the names_list?
Code Editor:
names_list = [‘John’, ‘James’, ‘Lily’, ‘Emily’, ‘Nina’]
names_list[::2]

Ans: [‘John’,’Lily’,’Nina’]

7] What is the result of executing this code?
Code Editor:
some_string = “Python” a, b, c, d = some_string

Ans: This is an error, “too many values to unpack”

8] What is the result of executing this code?
Code Editor:
city = ‘Los Angeles’
city.find(‘x’)

Ans: -1

9] Which of the following lines of code will print this string in reverse i.e. print
out “olleH”
Code Editor:
some_str = “Hello”

Ans: some_str[::-1]

10] All of the following statements are ways in which lists and tuples are similar.
Which one of these is NOT true?

Ans: Both once created cannot be updated

11] All of the following statements are ways in which lists and tuples are
different. Which one of these is true?

Ans: A list can be changed once creating, a tupe is immutable and cannot be
changed

12] Which of the following are valid complex data types in Python?

Ans: Lists, Dictionaries, sets


Conditional Statements & Loops: If-else Control Structures in Python:

1] Consider three variables with values as shown:
a = 5
b = 10
c = “five”
What are the results of evaluating the conditional expression?
a == c
a >= b
not(a < b and a > b)

Ans: false, false, true

2] How is the body of an if-statement block syntactically represented in Python?

Ans: Using additional indentation from the left relative to the lines just before
and after the block

3] What is the output for this code?
if ‘bin’ in {‘float’: 1.2, ‘bin’: 0b010}:
print(‘a’)
print(‘b’)
print(‘c’)

Ans: a b c

4] What is the output for this code?
if None:
print(‘Hi’)

Ans: Nothing is printed – no output

5] Evaluate the expression provided. What does the following expression
evaluate to?
‘1’ + ‘2’ if ‘123’.isdigit() else ‘2’ + ‘3’

Ans: ‘12’

6] What is the output of the code?
a = [1, ‘one’, {2: ‘two’}, 3]
b = len(a)
if b == 4:
print(‘Length of this list is 4’)
if b == 5:
print(‘Length of this list is 5’)
else:
print(b)

Ans: Length of this list is 4 4

7] What is the value of b in the snippet of python code?
a = “six”
b = (int(a), float(a))

Ans: ValueError:invalid literal for int() with base 10: ‘six’

8] Consider the following snippet of Python code:
a = “40.6 ”
b = “60.4 ”
c = a + b
What does c evaluate to?

Ans: ’40.6 60.4’

9] What would the output of the following code snippet be?
num_one = 76

num_two = 23.4
print(“datatype of num_one:”, type(num_one))
print(“datatype of num_two:”, type(num_two))

Ans: datatype of num_one:datatype of num_two:

10] What is the output of the code snippet below?
value = 4
a = str(value)
b = a + “^” + “2”
c = a + “^” + “3”
print(value, “+”, b, “+”, c)

Ans: 4 + 4 ^ 2 + 4 ^ 3

11] What do the values of d[0], d[1], d[2], d[3] evaluate to after the execution of
the Python code below?
new_list = [“Red”, “Blue”, “White”, “Green”]
z = sorted(new_list)
d = list(z)
d[0], d[1], d[2], d[3] = d[3], d[2], d[1], d[0]

Ans: “White”,”red”,”green”,”blue”

12] What is the output of the program below?
var = “hi”
if(type(var) == int):
print(“Type of the variable is Integer”)
elif(type(var) == float):print(“Type of the variable is Float”)
elif(type(var) == complex):
print(“Type of the variable is Complex”)
else:
print(“Type of the variable is Unknown”)

Ans: Unknown

13] What is the output of the program below?
total_classes = 100
attended_classes = 67
attendance = (attended_classes/total_classes)*100
if attendance >= 75:
print (“You are eligible to appear for the test.”)
else:
print (“Sorry, you are ineligible to appear for the test.”)

Ans: sorry you are ineligible to appear for the test.

Conditional Statements & Loops: The Basics of for Loops in Python:

1] Which of these Python data types can NOT be iterated through using for
loops?

Ans: int

2] Given a variable my_dict which is a dictionary, consider you use it in a for
loop in this manner:
for x in my_dict:
print (x) What are the contents printed out?

Ans: The keys in the dictionary

3] Which TWO of the following statements about for loops in Python are TRUE?

Ans: They may have an associated else block, They can iterate over the
elements in tuples, lists, and dictionaries

4] Given the following code, what is the type of x which is printed out in each
iteration?
my_list = [[‘tiger’, ‘lion’, ‘leopard’], [‘camel’, ‘llama’, ‘alpaca’], [‘zebra’, ‘donkey’,
‘wildebeest’]]
for x in my_list:
print(x)

Ans: A list of strings

5] What is the maximum value in the sequence x?
x = range(2, 14)

Ans: 13

6] What is the correct value of x given the assignment shown?
x = list(range(-17, -7, 2))

Ans: [-17, -15, -13, -11, -9]

7] Which of the following function calls will generate the list below?
[10, 7, 4, 1, -2]

Ans: list(range(10, -3, -3)).


Leave a Reply

Your email address will not be published. Required fields are marked *