Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Introduction to Python Quiz

The document provides a series of questions and answers related to Jupyter notebook commands, Python data types, operator precedence, string operations, tuples, lists, sets, and dictionaries. Each question is followed by feedback explaining the correct answer and the reasoning behind it. Additionally, there are links to resources for further learning on Python commands and operator precedence.

Uploaded by

ahmedfaraz1102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Introduction to Python Quiz

The document provides a series of questions and answers related to Jupyter notebook commands, Python data types, operator precedence, string operations, tuples, lists, sets, and dictionaries. Each question is followed by feedback explaining the correct answer and the reasoning behind it. Additionally, there are links to resources for further learning on Python commands and operator precedence.

Uploaded by

ahmedfaraz1102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Jupyter notebook commands

Which of the following commands will execute the current cell and move the navigator
to the next cell?

1. Enter
2. Shift+Enter
3. Ctrl+Cmd+Enter
4. Tab+Enter

Feedback:
Shift + Enter executes the current cell and moves the navigator to the next cell.

Which of the following commands will make the cell enter into command mode?

1. Esc
2. Shift+Esc
3. Ctrl+Enter
4. Enter

Feedback:
Esc makes the cell enter into command mode.

Which of the following keyboard shortcuts will create a cell above the current cell?

1. M
2. A
3. B
4. Z

Feedback:
Keyboard shortcut ‘A’ will create a cell above the current cell

Which of the following command displays the documentation of a selected function?

1. Ctrl+Shift
2. Shift +Tab
3. Ctrl+Tab
4. Ctrl+Delete
Feedback:
Pressing Shift+Tab command will display the documentation of that particular function

Which command is used for merging selected cells?

1. Shift+N
2. Shift+M
3. Ctrl+A
4. Alt+B

Feedback:
Shift+M shortcut is used for merging selected cells.

Click here for more commands , tips and tricks to use Python.

https://ipython.readthedocs.io/en/stable/interactive/magics.html

Data Types
Typecasting Question
What would be the output of the following code?

x = 3.5
print(float(int(x)))

1. 3.5
2. 3.0
3. 3
4. Error
Feedback:
3.0 : The int(x) will convert the float value(3.5) to an integer value 3, and then float(3) will
return the value 3.0.

What would be the output of the code given below?

name = Varma

print(type(name))
1. <class 'str'>
2. Str
3. String
4. Error

Feedback:
The above piece of code throws an error saying 'invalid syntax' because Varma isn’t
properly declared as a string. During the execution of the code, it will be considered as
two variables (name, Varma) being equated, and since they are not declared earlier, the
compiler throws an error.

Athematic Operators

Operator Precedence
What would be the output of the following code?

x=3
y=2.5
z=5
print(X+y/z)

1. 5.5
2. 1.1
3. 3.5
4. Error

Feedback:
The above code throws an error since variable X is not defined in the code but is being
used in the final print statement.

Note: Be careful with these type of errors while practising Python coding.

Follow the operator precedence rule in Python and provide the output of the following
expression:

4%(1+ 9)**2 - 60//(7+2)

1. 10
2. 75
3. -2
4. Error

Feedback:
Ans: -2 Operator precedence rule => ()>**>//>%>+>-

Click below for more information on operator precedence rule

https://docs.python.org/3.7/reference/expressions.html#operator-precedence

String Operations
String Slicing
How will you extract Python from the string word "I love Python programming"? (More
than one answers may be correct.)

1. word[7:13]
2. word[7:12]
3. word[-12:-18]
4. word[-18:-12]

word[7:13]
Feedback:
The index value of P in Python is 7 and the index value of N in Python is 12. So, while indexing, we
include the starting value, whereas the ending value is not included. So, word[7:13] will give Python,
while word[7:12] will give ‘Pytho’.

word[-18:-12]
Feedback:
Negative indexing starts from backward with the last character having an index value -1. Following
this P would have an index value of -18 and n would have an index value of -13. Hence, to extract
python, you need to give start index -18 and end index as -12. Note here that you shouldn't provide
-13 for the same reason that end index isn't included.

String operation
len() function in Python returns the length of the string when a string is passed as an
argument. In other words, len(‘Python’) would return an integer value 6.

With this in mind, what would be the output of the following code?

x = 'len'
x[len(x*2)- 5]

1. ‘n’
2. ‘‘
3. ‘e’
4. Error

'e'
Feedback:
Using a star operator on a string will result in repetition, so x*2 would give ‘lenlen’ and on passing
this to the length function, it would give us an output 6. The expression inside would finally equate
to x[1], which is ‘e’.

eBook : https://runestone.academy/ns/books/published/pythonds/index.html

Tuple
t = ("disco", 12, 4.5)
t[0][2]

What would be the output here?

1. disco
2. 'disco'
3. ‘s’
4. Error

's'
Feedback:
The output here would be ‘s’. t[0] would fetch the first element in the tuple, which is a string. Since
this string is also an iterable set of characters, [3] would give the third character of the string.

Tuple Indexing
Suppose t = (1, 2, 4, 3). Which of the following statements would result in an error?
[More than one option are correct]

1. print(t[4])
2. t[3] = 5
3. print(t[1:-4])
4. print(len(t))

print(t[4])
Feedback: As the index value 4 is higher than the maximum index value of the tuple, t[4] will return
an out of range error.

t[3] = 5
Feedback: A tuple cannot be modified.

Which of the following declarations will make x in Python tuple? [More than one option are
correct]

1. (‘2’)
2. 1,2
3. ((1,2,3), 4,5)
4. (Hello,'2','3')

1,2,
Feedback:
You can also define a tuple in this way without using parentheses.

((1,2,3), 4,5)
Feedback:
A tuple is a collection of objects. This object can also be a tuple; here, (1,2,3) is a tuple
and it acts as an object of tuple x.

List Definition
Can a tuple contain a list as an element?

1. Yes
2. No
Feedback:
Yes, tuple can contain list as an element.

List Indexing
Suppose list_1 is [2, 33, 222, 14, 25]. What is list_1[-5]?

1. 2
2. 33
3. []
4. Error
Feedback:
A list supports negative indexing with the last element starting at -1. So, list_1[-5] will
return 2.
List Manipulation
word = ['1','2','3','4']
word[ : ] = [ ]
print(word)

What would be the output of the code above?

1. Error
2. The output will have an additional space after each element.
3. Empty list
4. ['1','2','3','4']

Feedback:
Empty List, We are indexing all the elements of the list 'word' to an empty list; hence,
the final output would be an empty list. Hence, this method can also be used to delete
all the items from a list.

List Sorting
Consider the following list:

L = ['one','two','three', 'four', 'five', 'six']


print(sorted(L))
print (L)

1. ['five', 'four', 'one', 'six', 'three', 'two']

['one', 'two', 'three', 'four', 'five', 'six']

2. ['one', 'two', 'three', 'four', 'five', 'six']

['five', 'four', 'one', 'six', 'three', 'two']

3. ['five', 'four', 'one', 'six', 'three', 'two']

['five', 'four', 'one', 'six', 'three', 'two']


4. Error

['five', 'four', 'one', 'six', 'three', 'two']

['one', 'two', 'three', 'four', 'five', 'six']


Feedback:
sorted(L) will return a sorted list, and when it comes to strings, it happens based on the
character ASCII values. First, it compares the ASCII values of the first characters and sorts
the strings. If the first characters have the same value, then it proceeds to the second
characters and compares their ASCII values; if this matches again, then it proceeds to
the third characters, and this continues until you have sorted them. In this way, the
strings are sorted in a list.

Nested List
How will I extract Python from the nested list

input_list = [['SAS','R'],['Tableau','SQL'],['Python','Java']]

1. input_list[2][0]
2. input_list[2]
3. input_list[0]
4. input_list[1]

input_list[2][0]
Feedback:
Using the slicing concept we can extract Python from the nested list. Here indexing
starts from 0 and as the given list is a nested list then from index position 2 we have to
extract the first element which is Python and whose index position is 0.

input_list[2][0]
'Python'
Sets
What gets printed with the following set of instructions?

nums = set([1,1,2,3,3,3,4])
print(len(nums))

1. 7
2. 4
3. 3
4. 1

4
Feedback:
Set() removes duplicates and returns the unique values. Hence, nums would be updated
as {1, 2, 3, 4}, and when you print len(nums), it will be 4.

Quiz

List

1. Which of the following will make a list?


• L = [1, 2, 3, 4] (Correct)
• L = [i for i in range(1, 5)] (Correct)
• L = [for i in range(1, 5) i]
• L = [for i in range(1, 5): i]

2. What will be the output of the following code


L=[2, 3, 4, 2, 1, 2, 3]
print(L.index(2))

• 0 Correct: L.index(k) will return the index of the first occurrence of the element k in L
• 1
• 5
• 6

3. What is K in the following code?


L = [2, 1, 2, 4, 5, 3, 6]
K = 4 in L
1. All index's of element 4 in the list L
2. Boolean saying if 4 is in List or not (Correct: in functionality checks if the element is
in the list or not. )
3. Will give an updated list after adding 4 to the list
4. Will give an updated list after adding 4 to the list if 4 is not present in the list

4. Given L=[10,20,30,40,50,60,70,80,90,100], how will you get a list [20, 40, 60, 80]
o L[[1, 3, 5, 7]]
o L[1, 3, 5, 7]
o L[1::2]
o L[1:-1:2] (Correct)
o L[2:-1:2]
o [L[i] for i in range(1,9,2)] (Correct)

Strings
5. Strings is a data structure in python. There are two types of data structures- immutable and
mutable. Strings are under which category?
• Always Mutable
• Always Immutable (Correct: Strings are immutable.)
• Can be both depending on initialisation
6. What will be the output of the following?
print(chr(51))
• 3 (Correct: 51 is ASCII code of '3')
• Will throw an error
• a
• 'A'
• '3'
7. Given an ASCII code, how will you find the character associated with it? Suppose the given
ASCII code is 123.
• char(123)
• ord(123)
• chr(123) ✓ Correct
• ord('123')
8. What does the following line of code do to a string S
S.strip()
• Remove empty spaces from the left end of the string
• Remove empty spaces from the right end of the string
• Separate every character of the string
• Remove empty spaces from both ends of the string(Correct)

Map, Filter, Reduce


9. Will the functions map, reduce, filter work on Strings?

• None of them will work, they will work only on List and other mutable Data
structures.
• Only map will not work.
• Only filter will not work
• All three will work (Correct: They will all work on strings and any other iterable
Data structure.)
• Only reduce will not work

10. Which of the following will give the output as NUCOT?

Multiple answers may be correct.

• s = NuCot
• s = nucot
• s = s[:2] + s[2].upper() + s[3:]
• 'C'.join(['NU','OT']) (Correct: . join function will join both the strings in the list 'NU' and
'OT' with 'C' making it 'NUCOT'.
• s='aabNUCOTaab'.strip('aab') ( Correct)
• s='aabNUCOTaab'.strip('a').strip('b').strip('a') (Correct: First, all the both a on the left
side will go, then b on both sides, and then both a on the right side, making it
'NUCOT')

11. Which of the following codes can be used to find intersection of sets A, B and C.
• set.intersection(A, B, C) (Correct: Intersection function does not take in list or tuples
of sets but sets itself.)
• A.intersection(B, C)(Correct: Intersection function does not take in list or tuples of
sets but sets itself.)
• A.intersection([B, C])
• set.intersection([A, B, C])
12. Which of the following are immutable?
• Elements of sets (Correct: Sets are mutable, but the elements of sets cannot be
mutable. You can try making a set with elements as a list)
• Lists
• Dictionary
• Tuples(Correct)
• Sets
13. Which of the following will create a dictionary?
• d = {} (Correct: This will create an empty dictionary)
• d = ('a':1, 'b':2)
• d=dict(a=1, b=2) (Correct: function dict can be used to create a dictionary as well)
• d={'a'=1, 'b'=2}
• d={'a':1, 'b':2} (Correct: This is one of the methods to make a dictionary)
14. What will the following code return?
d={'a':64, 'b':65, 'c':66, 'd':67}
print(d['e'])
I. 68
II. 0
III. Nan
IV. Error(Correct: in the dictionary, there is no element 'e' in the keys of the dictionary,
trying to access value of a key that doesn't exist will give an error.)
15. What will be the output of the following code?
d={'a':64, 'b':65, 'c':66, 'd':67}
print(d.keys())
I. [a, b, c, d]
II. ('a', 'b', 'c', 'd')
III. ['a':64, 'b':65, 'c':66, 'd':67]
IV. ['a', 'b', 'c', 'd'] (Correct: d.keys() returns a list of all keys.)
V. ('a':64, 'b':65, 'c':66, 'd':67)
16. Dictionary can be seen as a map between two entities called keys and values. All the keys in
the list can be accessed by d.keys() where d is the dictionary. How are values accessed in
the dictionary?
Assume name of the dictionary is d.
I. d.vals
II. d.vals()
III. d.value()
IV. d.values()(Correct: d.values() will give you a dict_values() item which can be
typecasted as list.)
V. d.values
17. Suppose dict_1 = {"Python'':40, "R'':45}. What command should be used to delete the entry
"R"?
I. dict_1.delete('R':45)
II. del dict_1['R'](Correct )
III. dict_1.delete('R')
IV. del dict_1('R':40)

You might also like