0% found this document useful (0 votes)
4 views15 pages

CH 2

The document contains a series of questions and answers related to Python programming concepts, including data types, operators, and error handling. It covers topics such as string manipulation, list and dictionary operations, and the characteristics of tuples. Additionally, it includes true/false questions, fill-in-the-blank statements, and assertions with explanations regarding Python's data structures and their properties.

Uploaded by

himanknirman63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

CH 2

The document contains a series of questions and answers related to Python programming concepts, including data types, operators, and error handling. It covers topics such as string manipulation, list and dictionary operations, and the characteristics of tuples. Additionally, it includes true/false questions, fill-in-the-blank statements, and assertions with explanations regarding Python's data structures and their properties.

Uploaded by

himanknirman63
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 2

The numbered position of a letter in a string is called ...............

1. position
2. integer position
3. index
4. location

Answer

index
Reason — The index is the numbered position of a letter in the string.

Question 2

The operator ............... tells if an element is present in a sequence or not.

1. exists
2. in
3. into
4. inside

Answer
in
Reason — in is membership operator which returns True if a character or a substring
exists in the given string else returns False.

Question 3

The keys of a dictionary must be of ............... types.

1. integer
2. mutable
3. immutable
4. any of these

Answer
immutable
Reason — Dictionaries are indexed by keys and its keys must be of any immutable
type.

Question 4
Following set of commands is executed in shell, what will be the output?
>>>str = "hello"
>>>str[:2]
>>>

1. he
2. lo
3. olleh
4. hello

Answer

he
Reason — str[:2] here slicing operation begins from index 0 and ends at index 1.
Hence the output will be 'he'.

Question 5

What data type is the object below ?


L = [1, 23, 'hello', 1]

1. list
2. dictionary
3. array
4. tuple

Answer
list
Reason — A list can store a sequence of values belonging to any data type and they
are depicted through square brackets.

Question 6

What data type is the object below ?


L = 1, 23, 'hello', 1

1. list
2. dictionary
3. array
4. tuple

Answer
tuple
Reason — For creating a tuple, enclosing the elements inside parentheses is optional.
Even if parentheses are omitted as shown here, still this statement will create a tuple.
Question 7

To store values in terms of key and value, what core data type does Python provide ?

1. list
2. tuple
3. class
4. dictionary

Answer

dictionary
Reason — Dictionaries are mutable with elements in the form of a key:value pair that
associate keys to values.

Question 8

What is the value of the following expression ?


3 + 3.00, 3**3.0

1. (6.0, 27.0)
2. (6.0, 9.00)
3. (6, 27)
4. [6.0, 27.0]
5. [6, 27]

Answer
(6.0, 27.0)
Reason — The value of expression is in round brackets because it is tuple.
3 + 3.00 = 6.0
3**3.0 = 3 x 3 x 3 = 27.0

Question 9

List AL is defined as follows : AL = [1, 2, 3, 4, 5]


Which of the following statements removes the middle element 3 from it so that the list
AL equals [1, 2, 4, 5] ?

1. del AL[2]
2. AL[2:3] = []
3. AL[2:2] = []
4. AL[2] = []
5. AL.remove(3)

Answer
del AL[2]
AL[2:3] = []
AL.remove(3)
Reason — del AL[2] — The del keyword deletes the element from the list AL from
index 2.
AL[2:3] = [] — The slicing of the list AL[2:3] begins at index 2 and ends at index 2.
Therefore, the element at index 2 will be replaced by an empty list [].
AL.remove(3) — The remove() function removes an element from the list AL from index
3."

Question 10

Which two lines of code are valid strings in Python ?

1. This is a string
2. 'This is a string'
3. (This is a string)
4. "This is a string"

Answer

'This is a string'
"This is a string"
Reason — Strings are enclosed within single or double quotes.

Question 11

You have the following code segment :


String1 = "my"
String2 = "work"
print(String1 + String2)

What is the output of this code?

1. my work
2. work
3. mywork
4. my

Answer

Output

mywork
Reason — The + operator creates a new string by joining the two operand strings.

Question 12

You have the following code segment :


String1 = "my"
String2 = "work"
print(String1+String2.upper())

What is the output of this code?

1. mywork
2. MY Work
3. myWORK
4. My Work

Answer

Output

myWORK
Reason — string.upper() method returns a copy of the string converted to uppercase.
The + operator creates a new string by joining the two operand strings.

Question 13

Which line of code produces an error ?

1. "one" + 'two'
2. 1+2
3. "one" + "2"
4. '1' + 2

Answer
'1' + 2
Reason — The + operator has to have both operands of the same type either of
number type (for addition) or of string type (for concatenation). It cannot work with one
operand as string and one as a number.

Question 14

What is the output of this code ?


>>> int("3" + "4")

1. "7"
2. "34"
3. 34
4. 24

Answer

Output
34
Reason — The + operator concatenates two strings and int converts it to integer type.
int("3" + "4")
= int("34")
= 34

Question 15

Which line of code will cause an error ?


1. num= [5, 4, 3, [2], 1]
2. print(num[0])
3. print(num[3][0])
4. print(num[5])

1. Line 3
2. Line 2
3. Line 4
4. Line 1

Answer
Line 4
Reason — Index 5 is out of range because list has 5 elements which counts to index
4.

Question 16

Which is the correct form of declaration of dictionary ?

1. Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'}


2. Day = {1;'Monday', 2;'Tuesday', 3;'wednesday'}
3. Day = [1:'Monday', 2:'Tuesday', 3:'wednesday']
4. Day = {1'monday', 2'tuesday', 3'wednesday'}

Answer
Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'}
Reason — The syntax of dictionary declaration is:
<dictionary-name> = {<key>:<value>, <key>:<value>}
According this syntax, Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'} is the correct
answer.

Question 17

Identify the valid declaration of L:


L = ['Mon', '23', 'hello', '60.5']
1. dictionary
2. string
3. tuple
4. list

Answer
list
Reason — A list can store a sequence of values belonging to any data type and
enclosed in square brackets.

Question 18

Suppose a tuple T is declared as T = (10, 12, 43, 39), which of the following is
incorrect ?

1. print(T[1])
2. T[2] = -29
3. print(max(T))
4. print(len(T))

Answer

T[2] = -29
Reason — Tuples are immutable. Hence we cannot perform item-assignment in
tuples.

Fill in the Blanks

Question 1

Strings in Python store their individual letters in Memory in contiguous location.

Question 2

Operator + when used with two strings, gives a concatenated string.

Question 3

Operator + when used with a string and an integer gives an error.

Question 4

Part of a string containing some contiguous characters from the string is called string
slice.

Question 5

The * operator when used with a list/string and an integer, replicates the list/string.
Question 6

Tuples or Strings are not mutable while lists are.

Question 7

Using list() function, you can make a true copy of a list.

Question 8

The pop() function is used to remove an item from a list/dictionary.

Question 9

The del statement can remove an individual item or a slice from a list.

Question 10

The clear() function removes all the elements of a list/dictionary.

Question 11

Creating a tuple from a set of values is called packing.

Question 12

Creating individual values from a tuple's elements is called unpacking.

Question 13

The keys() method returns all the keys in a dictionary.

Question 14

The values() function returns all values from Key : value pair of a dictionary.

Question 15

The items() function returns all the Key : value pairs as (key, value) sequences.

True/False Questions

Question 1

Do both the following represent the same list.


['a', 'b', 'c']
['c', 'a', 'b']
Answer
False
Reason — Lists are ordered sequences. In the above two lists, even though the
elements are same, they are at different indexes (i.e., different order). Hence, they are
two different lists.

Question 2

A list may contain any type of objects except another list.

Answer
False
Reason — A list can store any data types and even list can contain another list as
element.

Question 3

There is no conceptual limit to the size of a list.


Answer

True
Reason — The list can be of any size.

Question 4

All elements in a list must be of the same type.

Answer
False
Reason — A list is a standard data type of python that can store a sequence of values
belonging to any data type.

Question 5

A given object may appear in a list more than once.

Answer
True
Reason — List can have duplicate values.

Question 6

The keys of a dictionary must be of immutable types.


Answer

True
Reason — Dictionaries are indexed by keys. Hence its keys must be of any non-
mutable type.

Question 7

You can combine a numeric value and a string by using the + symbol.
Answer
False
Reason — The + operator has to have both operands of the same type either of
number type (for addition) or both of string type (for concatenation). It cannot work with
one operand as string and one as a number.

Question 8

The clear( ) removes all the elements of a dictionary but does not delete the empty
dictionary.

Answer
True
Reason — The clear() method removes all items from the dictionary and the dictionary
becomes empty dictionary post this method. del statement removes the complete
dictionary as an object.

Question 9

The max( ) and min( ) when used with tuples, can work if elements of the tuple are all
of the same type.

Answer
True
Reason — Tuples should contain same type of elements for max() and min() method
to work.

Question 10

A list of characters is similar to a string type.


Answer
False
Reason — In Python, a list of characters and a string type are not similar. Strings are
immutable sequences of characters, while lists are mutable sequences that can
contain various data types. Lists have specific methods and behaviors that differ from
strings, such as append(), extend(), pop() etc.
Question 11

For any index n, s[:n] + s[n:] will give you original string s.
Answer
True
Reason — s[:n] — The slicing of a string starts from index 0 and ends at index n-1.
s[n:] — The slicing of a string starts from index n and continues until the end of the
string.
So when we concatenate these two substrings we get original string s.

Question 12

A dictionary can contain keys of any valid Python types.


Answer
False
Reason — The keys of a dictionary must be of immutable types.

Assertions and Reasons

Question 1

Assertion. Lists and Tuples are similar sequence types of Python, yet they are two
different data types.
Reason. List sequences are mutable and Tuple sequences are immutable.

Answer

(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
Lists and tuples are similar sequence types in python, but they are distinct data types.
Both are used to store collections of items, but they have different properties and use
cases. Lists are mutable, meaning you can add, remove, or modify elements after the
list is created. Tuples, on the other hand, are immutable, meaning once a tuple is
created, its contents cannot be changed.

Question 2

Assertion. Modifying a string creates another string internally but modifying a list does
not create a new list.
Reason. Strings store characters while lists can store any type of data.
Answer

(b)
Both Assertion and Reason are true but Reason is not the correct explanation of
Assertion.
Explanation
In Python, strings are immutable, meaning once they are created, their contents
cannot be changed. Whenever we modify a string, python creates a new string object
to hold the modified contents, leaving the original string unchanged. On the other
hand, lists in python are mutable, meaning we can modify their contents after they
have been created. When we modify a list, python does not create a new list object.
Instead, it modifies the existing list object in place. Strings are sequences of
characters, and each character in a string can be accessed by its index. Lists, on the
other hand, can store any type of data, including integers, floats, strings.

Question 3

Assertion. Modifying a string creates another string internally but modifying a list does
not create a new list.
Reason. Strings are immutable types while lists are mutable types of python.

Answer

(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
In Python, strings are immutable, meaning once they are created, their contents
cannot be changed. Whenever we modify a string, python creates a new string object
to hold the modified contents, leaving the original string unchanged. On the other
hand, lists in python are mutable, meaning we can modify their contents after they
have been created. When we modify a list, python does not create a new list object.
Instead, it modifies the existing list object in place.

Question 4

Assertion. Dictionaries are mutable, hence its keys can be easily changed.
Reason. Mutability means a value can be changed in place without having to create
new storage for the changed value.
Answer
(d)
Assertion is false but Reason is true.
Explanation
Dictionaries are indexed by keys and each key must be immutable and unique.
However, the dictionary itself is mutable, meaning that we can add, remove, or modify
key-value pairs within the dictionary without changing the identity of the dictionary
object itself. Mutability refers to the ability to change a value in place without creating a
new storage location for the changed value.

Question 5

Assertion. Dictionaries are mutable but their keys are immutable.


Reason. The values of a dictionary can change but keys of dictionary cannot be
changed because through them data is hashed.

Answer
(a)

Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
A dictionary is a unordered set of key : value pairs and are indexed by keys. The
values of a dictionary can change but keys of dictionary cannot be changed because
through them data is hashed. Hence dictionaries are mutable but keys are immutable
and unique.

Question 6

Assertion. In Insertion Sort, a part of the array is always sorted.


Reason. In Insertion sort, each successive element is picked and inserted at an
appropriate position in the sorted part of the array.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of
Assertion.
Explanation
Insertion sort is a sorting algorithm that builds a sorted list, one element at a time from
the unsorted list by inserting the element at its correct position in sorted list. In
Insertion sort, each successive element is picked and inserted at an appropriate
position in the previously sorted array.

Type A: Short Answer Questions/Conceptual Questions

Question 1

What is the internal structure of python strings ?


Answer

Strings in python are stored as individual characters in contiguous memory locations,


with two-way index for each location. The index (also called subscript) is the numbered
position of a letter in the string. Indices begin 0 onwards in the forward direction up to
length-1 and -1,-2, .... up to -length in the backward direction. This is called two-way
indexing.

Question 2

Write a python script that traverses through an input string and prints its characters in
different lines - two characters per line.
Answer
name = input("Enter name:")
for i in range(0, len(name), 2):
print(name[i:i+2])

Output

Enter name:python
py
th
on

Question 3

Discuss the utility and significance of Lists, briefly.


Answer
A list is a standard data type of python that can store a sequence of values belonging
to any type. Lists are mutable i.e., we can change elements of a list in place. Their
dynamic nature allows for flexible manipulation, including appending, inserting,
removing, and slicing elements. Lists offer significant utility in data storage, iteration,
and manipulation tasks.

Question 4

What do you understand by mutability ? What does "in place" task mean ?
Answer
Mutability means that the value of an object can be updated by directly changing the
contents of the memory location where the object is stored. There is no need to create
another copy of the object in a new memory location with the updated values.
Examples of mutable objects in python include lists, dictionaries.
In python, "in place" tasks refer to operations that modify an object directly without
creating a new object or allocating additional memory. For example, list methods like
append(), extend(), and pop() perform operations in place, modifying the original list,
while string methods like replace() do not modify the original string in place but instead
create a new string with the desired changes.

You might also like