Thursday, January 7, 2021

Python Tuple Interview questions - Multiple choice

 Tuple Interview questions:

========================================================================


1. Suppose t = (1, 2, 4, 3), which of the following is incorrect?

a) print(t[3])

b) t[3] = 45

c) print(max(t))

d) print(len(t))


Answer: b

Explanation: Values cannot be modified in the case of the tuple, that is, a tuple is immutable.


2. What will be the output of the following Python code?

>>>t=(1,2,4,3)

>>>t[1:3]

a) (1, 2)

b) (1, 2, 4)

c) (2, 4)

d) (2, 4, 3)


Answer: c

Explanation: Slicing in tuples takes place just as it does in strings.


3. What will be the output of the following Python code?

>>>t=(1,2,4,3)

>>>t[1:-1]

a) (2, 4, 3)

b) (1, 2, 4)

c) [2, 4]

d) (2, 4) 


Answer: d

Explanation: Slicing in tuples takes place just as it does in strings.


4. What will be the output of the following Python code?

>>>t = (1, 2)

>>> t*2

a) (1, 2, 1, 2)

b) [1, 2, 1, 2]

c) (1, 1, 2, 2)

d) [1, 1, 2, 2]


Answer: a

Explanation: * operator concatenates tuple.


5. What will be the output of the following Python code?

>>>t1 = (1, 2, 4, 3)

>>>t2 = (1, 2, 3, 4)

>>>t1 < t2

a) True

b) False

c) Error

d) None


Answer: b

Explanation: Elements are compared one by one in this case.


6. What will be the output of the following Python code?

>>>my_tuple = (1, 2, 3, 4)

>>>my_tuple.append( (5, 6, 7) )

>>>print len(my_tuple)

a) 1

b) 2

c) 5

d) Error


Answer: d

Explanation: Tuples are immutable and don’t have an append method. An exception is thrown in this case.


7. What is the data type of (1)?

a) Tuple

b) Integer

c) List

d) Both tuple and integer


Answer: b

Explanation: A tuple of one element must be created as (1,).


12. If a=(1,2,3,4), a[1:-1] is _________

a) Error, tuple slicing doesn’t exist

b) [2,3]

c) (2,3,4)

d) (2,3)


Answer: d

Explanation: Tuple slicing exists and a[1:-1] returns (2,3).


13. What will be the output of the following Python code?

>>> a=(1,2,(4,5))

>>> b=(1,2,(3,4))

>>> a<b

a) False

b) True

c) Error, < operator is not valid for tuples

d) Error, < operator is valid for tuples but not if there are sub-tuples


Answer: a

Explanation: Since the first element in the sub-tuple of a is larger than the first element in the sub-tuple of b, False is printed.


14. What will be the output of the following Python code?

>>> a=("Check")*3

>>> a

a) (‘Check’,’Check’,’Check’)

b) * Operator not valid for tuples

c) (‘CheckCheckCheck’)

d) Syntax error


Answer: c

Explanation: Here (“Check”) is a string, not a tuple because there is no comma after the element.


15. What will be the output of the following Python code?

>>> a=(1,2,3,4)

>>> del(a[2])

a) Now, a=(1,2,4)

b) Now, a=(1,3,4)

c) Now a=(3,4)

d) Error as tuple is immutable


Answer: d

Explanation: ‘tuple’ object doesn’t support item deletion.


16. What will be the output of the following Python code?

>>> a=(2, 3, 4)

>>> sum(a, 3)

a) Too many arguments for sum() method

b) The method sum() doesn’t exist for tuples

c) 12

d) 9


Answer: c

Explanation: In the above case, 3 is the starting value to which the sum of the tuple is added.


17. Is the following Python code valid?

>>> a=(1,2,3,4)

>>> del a

a) No because tuple is immutable

b) Yes, first element in the tuple is deleted

c) Yes, the entire tuple is deleted

d) No, invalid syntax for del method


Answer: c

Explanation: The command del a deletes the entire tuple.


18. What type of data is: a=[(1,1),(2,4),(3,9)]?

a) Array of tuples

b) List of tuples

c) Tuples of lists

d) Invalid type


Answer: b

Explanation: The variable has tuples enclosed in a list making it a list of tuples.


19. What will be the output of the following Python code?

>>> a=(0,1,2,3,4)

>>> b=slice(0,2)

>>> a[b]

a) Invalid syntax for slicing

b) [0,2]

c) (0,1)

d) (0,2)


Answer: c

Explanation: The method illustrated in the above piece of code is that of the naming slices.


20. Is the following Python code valid?

>>> a = (1, 2, 3)

>>> b = ('A', 'B', 'C')

>>> c = tuple(zip(a, b))

a) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))

b) Yes, c will be ((1, 'A'), (2, 'B'), (3, 'C'))

c) No because tuples are immutable

d) No because the syntax for zip function isn’t valid


Answer: b

Explanation: The zip function combines individual elements of two iterables into tuples. 


21. Is the following Python code valid?

>>> a, b, c=1, 2, 3

>>> a, b, c

a) Yes, [1,2,3] is printed

b) No, invalid syntax

c) Yes, (1,2,3) is printed

d) 1 is printed


Answer: c

Explanation: A tuple needn’t be enclosed in parenthesis.


 

23. Is the following Python code valid?

>>> a,b=1,2,3

a) Yes, this is an example of tuple unpacking. a=1 and b=2

b) Yes, this is an example of tuple unpacking. a=(1,2) and b=3

c) No, too many values to unpack

d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)


Answer: c

Explanation: For unpacking to happen, the number of values on the right-hand side must be equal to the number of variables on the left-hand side.


24. What will be the output of the following Python code?

>>> a = (1,2)

>>> b = (3,4)

>>> c = a+b

>>> c

a) (4,6)

b) (1, 2, 3, 4)

c) Error as tuples are immutable

d) None


Answer: b

Explanation: In the above piece of code, the values of the tuples aren’t being changed. 

Both the tuples are simply concatenated.


25. What will be the output of the following Python code?

>>> a, b = 6, 7

>>> a, b = b, a

>>> a, b

a) (6,7)

b) Invalid syntax

c) (7,6)

d) Nothing is printed


Answer: c

Explanation: The above piece of code illustrates the unpacking of variables.


27. Tuples can’t be made keys of a dictionary.

a) True

b) False


Answer: b

Explanation: Tuples can be made keys of a dictionary because they are hashable.


28. Is the following Python code valid?

>>> a=2,3,4,5

>>> a

a) Yes, 2 is printed

b) Yes, [2,3,4,5] is printed

c) No, too many values to unpack

d) Yes, (2,3,4,5) is printed


Answer: d

Explanation: A tuple needn’t be enclosed in parenthesis.


29. What will be the output of the following Python code?

>>> a=(2,3,1,5)

>>> a.sort()

>>> print(a)

a) (1,2,3,5)

b) (2,3,1,5)

c) None

d) Error, tuple has no attribute sort


Answer: d

Explanation: A tuple is immutable thus it doesn’t have a sort attribute.


30. Is the following Python code valid?

>>> a = (1, 2, 3)

>>> b = a.update(4,)

a) Yes, a=(1,2,3,4) and b=(1,2,3,4)

b) Yes, a=(1,2,3) and b=(1,2,3,4)

c) No because tuples are immutable

d) No because wrong syntax for update() method


Answer: c

Explanation: Tuple doesn’t have any update() attribute because it is immutable.


31. What will be the output of the following Python code?

>>> a=[(2,4),(1,2),(3,9)]

>>> a.sort()

>>> print(a)

a) [(1, 2), (2, 4), (3, 9)]

b) [(2,4),(1,2),(3,9)]

c) Error because tuples are immutable

d) Error, tuple has no sort attribute


Answer: d

Explanation: A list of tuples is a list itself. Hence items on a list can be sorted.

Request: If you find this information useful, please provide your valuable comments.

Python List Interview questions - Multiple choice

 List Interview questions:

========================================================================

1. Which of the following commands will create a list?

a) list1 = list()

b) list1 = []

c) list1 = list([1, 2, 3])

d) all of the mentioned


Answer: d

Explanation: Execute in the shell to verify


2. What is the output when we execute list(“hello”)?

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

b) [‘hello’]

c) [‘llo’]

d) [‘olleh’]


Answer: a

Explanation: Execute in the shell to verify. (Note: it's not a split() function,  type casting is happening.  i.e converting from string to list)


3. Suppose list_example is [‘h’,’e’,’l’,’l’,’o’]  what is len(list_example)?

a) 5

b) 4

c) None

d) Error


Answer: a

Explanation: Execute in the shell and verify.


4. Suppose list1 is [2445, 133, 12454, 123]  what is max(list1)?

a) 2445

b) 133

c) 12454

d) 123


Answer: c

Explanation: Max returns the maximum element in the list.


5. Suppose list_1 is [3, 5, 25, 1, 4]  what is min(list_1)?

a) 3

b) 5

c) 25

d) 1


Answer: d

Explanation: Min returns the minimum element in the list.


6. Suppose list_1 is [1, 5, 9]  what is sum(list_1)?

a) 1

b) 9

c) 15

d) Error


Answer: c

Explanation: Sum returns the sum of all elements in the list.


7. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?

a) 0

b) 4

c) 1

d) 2


Answer: d

Explanation: Execute in the shell to verify.


8. Suppose list_1 is [4, 2, 2, 4, 5, 2, 1, 0]

     Which of the following is the correct syntax for slicing operation?

a) print(list_1[0])

b) print(list_1[ :2])

c) print(list_1[ :-2])

d) all of the mentioned


Answer: d

Explanation: Slicing is allowed in lists just as in the case of strings.


9. Suppose list_1 is [2, 33, 222, 14, 25]  What is list_1[-1]?

a) Error

b) None

c) 25

d) 2


Answer: c

Explanation: -1 corresponds to the last index in the list.


10. Suppose list1 is [2, 33, 222, 14, 25] What is list1[ :-1] ?

a) [2, 33, 222, 14, 25] 

b) 25

c) [25, 14, 222, 33, 2]

d) [2, 33, 222, 14]


Answer: d

Explanation: Execute in the shell to verify


11. What will be the output of the following Python code?

>>>names = ["Narendra", "Boyina", "raj", "Mahalakshmi"]

>>>print(names[-1][-1])

a) M

b) Mahalakshmi

c) Error

d) i


Answer: d

Explanation: Execute in the shell to verify.


12. What will be the output of the following Python code?

    names_1 = ["Narendra", "Bhaskarao", "raj", "Mahalakshmi"]
    names_2 = names_1  # shallow copy
    names_3 = names_1[:] # deep copy
    names_2[0] = 'Surendra'
    names_3[1] = "Udaya Bhaskara rao"

    print(names_1)
    print(names_2)
    print(names_3)

    a)  ['Surendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
        ['Surendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
        ['Narendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
    b)  ['Surendra', 'Bhaskarao', 'raj', 'Mahalakshmi']
        ['Surendra', 'Bhaskarao', 'raj', 'Mahalakshmi']
        ['Narendra', 'Udaya Bhaskara rao', 'raj', 'Mahalakshmi']
    c) Error
    d) None of the above



Answer: b

Explanation:  names_1 and names_2 are using the same memory location reason: Shallow copy

        if you change in names_2 it will affect names_1 vice versa       reason: Shallow copy 

       But names_3 used a different memory location reason: Deep copy

      if you do changes in names_3 it will not affect the names_1reason: Deep copy


13. Suppose list_1 is [1, 3, 2] What is list_1 * 2?

a) [2, 6, 4]

b) [1, 3, 2, 1, 3]

c) [1, 3, 2, 1, 3, 2]

d) [1, 3, 2, 3, 2, 1]


Answer: c

Explanation: Execute in the shell and verify.


14. What will be the output of the following Python code?

>>>list1 = [11, 2, 23]

>>>list2 = [11, 2, 2]

>>>list1 < list2 is

a) True

b) False

c) Error

d) None


Answer: b

Explanation: Elements are compared one by one.


15. To add a new element to a list, which command we have to use?

a) list1.add(5)

b) list1.append(5)

c) list1.addLast(5)

d) list1.addEnd(5)


Answer: b

Explanation: We use the function append() function, to add an element to the list.


16. To insert 5 to the third position in list1, which command do we have to use?

a) list1.add(3, 5)

b) list1.insert(2, 5)

c) list1.insert(3, 5)

d)  list1.append(3, 5)


Answer: c

Explanation: Execute in the shell to verify.


17. To remove the string “hello” from list1, which command do we have to use?

a) list1.remove(hello) 

b)  list1.remove(“hello”)

c) list1.removeAll(“hello”)

d) list1.removeOne(“hello”)


Answer: b

Explanation:  hello is a variable, "hello" is a string, we have to remove the string, so we have to              give the element as a string


18. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?

a) 0

b) -1

c) 4

d) 2


Answer: d

Explanation: By default, the index is always considered from the left-hand side (0).


19. Suppose list_1 is [3, 4, 5, [5, 20, 15, 5],  5, 25, 1, 3], what is list_1.count(5)?

a) 4

b) 6

c) 1

d) 2


Answer: d

Explanation: Execute in the shell to verify.


20. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?

a) [3, 4, 5, 20, 5, 25, 1, 3]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 1, 25, 5, 20, 5, 4, 3] 

d) [25, 20, 5, 5, 4, 3, 3, 1]


Answer: c

Explanation: Execute in the shell to verify.


21. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5])?

a) [3,  4,  5,  20,  5,  25,  1,  3, [ 34, 5]]

b) [3,  4,  5,  20,  5,  25,  1,  3,  34,  5]

c) [25,  20,  5,  5,  4,  3,  3,  1,  34,  5]

d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5]


Answer: b

Explanation: Execute in the shell to verify.

22. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is listExample after listExample.pop()?

a) [3, 4, 5, 20, 5, 25, 1]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 5, 20, 5, 25, 1, 3]

d) [1, 3, 4, 5, 20, 5, 25]


Answer: a

Explanation: pop() function will remove the last element in the list.


23. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1)?

a) [3, 4, 5, 20, 5, 25, 1, 3]

b) [1, 3, 3, 4, 5, 5, 20, 25]

c) [3, 5, 20, 5, 25, 1, 3]

d) [1, 3, 4, 5, 20, 5, 25]


Answer: c

Explanation: Generally pop() function will remove the last element in the list. 

        But inside the pop() function, if you provide an index then it will remove based on that index.


24. What will be the output of the following Python code?

>>>"Welcome to Python".split()

a)  (“Welcome”, “to”, “Python”)

b)  [“Welcome”, “to”, “Python”]

c) {“Welcome”, “to”, “Python”}

d) “Welcome”, “to”, “Python”


Answer: b

Explanation: split() function returns the elements in a list.


25. What will be the output of the following Python code?

>>>list_1 = [1, 3]

>>>list_2 = list_1

>>>list_1[0] = 4

>>>print(list_2)

a) [1, 3]

b) [4, 3]

c) [1, 4]

d) [1, 3, 4]


Answer: b

Explanation:  list_1 and list_2 are using the same memory location reason: Shallow copy

        if you change in list_1 it will affect list_2 vice versa       reason: Shallow copy 


26. What will be the output of the following Python code?

numbers = [1, 2, 3, 4]

numbers.append([5,6,7,8])

 print(len(numbers))

a) 4

b) 5

c) 8

d) 12


Answer: b

Explanation: A list is passed in append().

            append() function considers the entire list as a single element so the length is 5.


27. To which of the following the “in” operator can be used to check if an item is in it?

a) Lists

b) Dictionary

c) Set

d) All of the mentioned


Answer: d

Explanation: membership operator (in) can be used in all data structures (Datatypes).


28. What will be the output of the following Python code?

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

print(len(list1 + list2))

a) 2

b) 4

c) 5

d) 8


Answer: d

Explanation: + appends all the elements individually into a new list.

 

29. What will be the output of the following Python code?

veggies = ['carrot', 'drumsticks', 'potato', 'asparagus']

veggies.insert(veggies.index('drumsticks'), 'ladyfinger')

print(veggies)

a) [‘carrot’, ‘ladyfinger’, ‘potato’, ‘asparagus’]

b)  [‘carrot’, ‘ladyfinger’, 'drumsticks', 'potato', 'asparagus'] 

c) [‘carrot’, ‘drumsticks’, ‘ladyfinger’, ‘potato’, ‘asparagus’]

d) [‘ladyfinger’, ‘carrot’, ‘drumsticks’, ‘potato’, ‘asparagus’]


Answer: b

Explanation: Execute in the shell to verify.


30. What will be the output of the following Python code?

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

 print(data[1][1][0])

a) 1

b) 2

c) 7

d) 5


Answer: C

Explanation: Execute in the shell to verify.


31. What will be the output of the following Python code?

points = [[1, 2], [3, 1.5], [0.5, 0.5]]

points.sort()

print(points)

a) [[1, 2], [3, 1.5], [0.5, 0.5]]

b) [[3, 1.5], [1, 2], [0.5, 0.5]]

c) [[0.5, 0.5], [1, 2], [3, 1.5]]

d) [[0.5, 0.5], [3, 1.5], [1, 2]]


Answer: c

Explanation: Execute in the shell to verify.


34. What will be the output of the following Python code?

s="a@b@c@d"

a=list(s.partition("@"))

print(a)

b=list(s.split("@"))

print(b)

a) [‘a’, ’b’, ’c’, ’d’]

[‘a’, ’b’, ’c’, ’d’]

b) [‘a’, ’@’, ’b’, ’@’, ’c’, ’@’, ’d’]

[‘a’, ’b’, ’c’, ’d’]

c) [‘a’,’@’,’b@c@d’]

[‘a’, ’b’, ’c’, ’d’]

d) [‘a’,’@’,’b@c@d’]

[‘a’, ’@’, ’b’, ’@’, ’c’, ’@’, ’d’]


Answer: c

Explanation: The partition function only splits for the first parameter along with 

the separator while the split function splits for the number of times given in the second argument but without the separator.



36. What will be the output of the following Python code?

a=[[]]*3

a[1].append(7)

print(a)

a) Syntax error

b) [[7], [7], [7]]

c) [[7], [], []]

d) [[],7, [], []]


Answer: b

Explanation: The first line of the code creates multiple reference copies of the sublist. [[], [], []] 

         Internally, the * operator uses a shallow copy (i.e., it uses the same memory location for all elements within the lists). 

when 7 is appended,  because of shallow copy, it gets appended to all the sublists.


38. What will be the output of the following Python code?

lst=[3,4,6,1,2]

lst[1:2]=[7,8]

print(lst)

a) [3, 7, 8, 6, 1, 2]

b) Syntax error

c) [3,[7,8],6,1,2]

d) [3,4,6,7,8]


Answer: a

Explanation: In the piece of code,  the slice assignment has been implemented. 

The sliced list is replaced by the assigned elements in the list. Type in Python shell to verify.


39. What will be the output of the following Python code?

a=[1,2,3]

b = a.append(4)

print(a)

print(b)

a) [1,2,3,4]

[1,2,3,4]

b) [1, 2, 3, 4]

None

c) Syntax error

d) [1,2,3]

[1,2,3,4]


Answer: b

Explanation: The append function on lists doesn’t return anything. Thus the value of b is None.

  

40. What will be the output of the following Python code?

>>> a = [14,52,7]

>>>> b = a.copy()  # deep copy

>>> b is a

         a) True

b) False


Answer: b

Explanation: here a data will be copied to b(deep copy) so it will create a separate memory location


41. What will be the output of the following Python code?

a=[13,56,17]

a.append([87])

a.extend([45,67])

print(a)

a) [13, 56, 17, [87], 45, 67]

b) [13, 56, 17, 87, 45, 67]

c) [13, 56, 17, 87,[ 45, 67]]

d) [13, 56, 17, [87], [45, 67]]


Answer: a

Explanation: The append function simply adds its arguments to the list as it is 

while extend function extends its arguments and later appends it.


42. What is the output of the following piece of code?

a=list((45,)*4)

print((45)*4)

print(a)

a) 180

[(45),(45),(45),(45)]

b) (45,45,45,45)

[45,45,45,45]

c) 180

[45,45,45,45]

d) Syntax error


Answer: c

Explanation: (45) is an int while (45,) is a tuple of one element. Thus, when a tuple is multiplied, 

It created references of itself, which are later converted into a list.


44. What will be the output of the following Python code?

word_1 = "Apple"

word_2 = "Apple"

list_1 = [1,2,3]

list_2 = [1,2,3]

print(word_1 is word_2)

print(list_1 is list_2)

a) True

True

b) False

True

c) False

False

d) True

False


Answer: d

Explanation: In the above case, both lists are equivalent but not identical as they have different objects.


45. What will be the output of the following Python code?

places = ['Bangalore', 'Mumbai', 'Delhi']

places1 = places      # shallow copy

        places1[1]="Pune"

places2 = places[ : ]   # deep copy

places2[2]="Hyderabad"

print(places)

a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’]

b) [‘Bangalore’, ‘Pune’, ‘Delhi’]

c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’]

d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]


Answer: b

Explanation: places1 is an alias for the list of places. Hence, any change made to places1 is reflected in places. 

places2 is a copy of the list of places. Thus, any change made to places2 isn’t reflected in places.


48. What will be the output of the following Python code?

a=["Apple","Ball","Cobra"]

a.sort(key = len)

print(a)

a) [‘Apple’, ‘Ball’, ‘Cobra’]

b) [‘Ball’, ‘Apple’, ‘Cobra’]

c) [‘Cobra’, ‘Apple’, ‘Ball’]

d) Invalid syntax for sort()


Answer: b

Explanation: The syntax isn’t invalid and the list is sorted according to the length of 

the strings in the list since the key is given as length.

Request: If you find this information useful, please provide your valuable comments.


Python Strings Interview questions - Multiple choice

Strings:

===============================================

 1. What will be the output of the following Python statement?

"a"+"bc"

a) a

b) bc

c) bca

d) abc

Answer: d

Explanation: + operator is concatenation operator.

 

2. What will be the output of the following Python statement?

>>>"abcd"[2:]

a) a

b) ab

c) cd

d) dc


Answer: c

Explanation: Slice operation is performed on the string.


3. What will be the output of the following Python code?

>>> str1 = 'hello'

>>> str2 = ','

>>> str3 = 'world'

>>> str1[-1:]


a) olleh

b) hello

c) h

d) o

 

Answer: d

Explanation: -1 corresponds to the last index.


 4. What arithmetic operators can be used with strings?

a) +

b) *

c) –

d) All of the mentioned

 

Answer: a

Explanation: + is used to concatenate.


5. What will be the output of the following Python code?

>>>print (r"\nhello")

a) a new line and hello

b) \nhello

c) the letter r and then hello

d) error

 

Answer: b

Explanation: When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw string and the escape sequences such as \n are not converted.

 

6. What will be the output of the following Python statement?

>>>print('new' 'line')

a) Error

b) Output equivalent to print ‘new\nline’

c) newline

d) new line

 

Answer: c

Explanation: String literal separated by whitespace are allowed. They are concatenated.


7. What will be the output of the following Python code?

>>>str1="helloworld"

>>>str1[::-1]

a) dlrowolleh

b) hello

c) world

d) helloworld

 

Answer: a

Explanation: execute in reverse

 

8. print(0xA + 0xB + 0xC):

a) 0xA0xB0xC

b) Error

c) 0x22

d) 33

 

Answer: d

Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing 

the decimal values 10, 11 and 12 respectively. There sum is 33.


9. What will be the output of the following Python code?

>>>example = "snow world"

>>>print(example[5:7])

a) wo

b) world

c) sn

d) rl

 

Answer: a

Explanation: Execute in the shell and verify.


10. What will be the output of the following Python code?

>>>example = "snow world"

>>>example[3] = 's'

>>>print(example)

a) snow

b) snow world

c) Error

d) snos world


Answer: c

Explanation: Strings cannot be modified,


11. What will be the output of the following Python code?

>>>max("what are you")

a) error

b) u

c) t

d) y

 

Answer: d

Explanation: Max returns the character with the highest ASCII value.


12. Given a string example=”hello” what is the output of example.count(‘l’)?

a) 2

b) 1

c) None

d) 0

 

Answer: a

Explanation: l occurs twice in hello.

 

13. What will be the output of the following Python code?

>>>example = "helle"

>>>example.find("e")

a) Error

b) -1

c) 1

d) 0

 

Answer: c

Explanation: Returns the lowest index.

 

14. What will be the output of the following Python code?

>>>example = "helle"

>>>example.rfind("e")

a) -1

b) 4

c) 3

d) 1

 

Answer: b

Explanation: It will start the search from the right side,  but the index will give from the 0th index.

   

15. What will be the output of the following Python code?

>>>example="helloworld"

>>>example[::-1].startswith("d")

a) dlrowolleh

b) True

c) -1

d) None

 

Answer: b

Explanation: Starts with checks if the given string starts with the parameter that is passed.

16. Which of the following statements prints hello\example\test.txt?

a) print(“hello\example\test.txt”)

b) print(“hello\\example\\test.txt”)

c) print(“hello\”example\”test.txt”)

d) print(“hello”\example”\test.txt”)

 

Answer: b

Explanation: \t consider as  escape sequence character. Here we have 3 possibilities

        1. we have to use \\"string"

        2. we have to use raw_path ("string") function

        3. we have to use r"string"  which means raw string         

17. Suppose data is “\t\tWorld\n”, what is data.strip()?

a) \t\tWorld\n

b) \t\tWorld\n

c) \t\tWORLD\n

d) World

 

Answer: d

Explanation: If we print data, we will get output as 4 spaces again 4 spaces & at the end of the string new line will print.  

strip() function will remove unwanted content on both sides (Noteby default input of strip() is space)

Ans: So it will remove spaces on both sides ---->World

18. The format function, when applied on a string returns ___________

a) Error

b) int

c) bool

d) str

 

Answer: d

Explanation: Format function returns a string.

19. What will be the output of the “hello” +1+2+3?

a) hello123

b) hello

c) Error

d) hello6

 

Answer: c

Explanation: Cannot concatenate str and int objects.

20. What will be the output of the following Python code?

>>>print("D", end = ' ')

>>>print("C", end = ' ')

>>>print("B", end = ' ')

>>>print("A", end = ' ')

a) DCBA

b) A, B, C, D

c) D C B A

d) D, C, B, A will be displayed on four lines

 

Answer: c

Explanation: Execute in the shell.

21. Say s=”hello” what will be the return value of type(s)?

a) int

b) bool

c) str

d) String

 

Answer: c

Explanation: str is used to represent strings in python.

 

22. What is “Hello”.replace(“l”, “e”)?

a) Heeeo

b) Heelo

c) Heleo

d) None

 

Answer: a

Explanation: Execute in shell to verify.

23. Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space).

a) __345.3556

b) _______345.355

c) _______345

d) ___345.3546

 

Answer: b

Explanation: Execute in the shell to verify.

24.What will be the output of the following Python statement?(python 3.xx)

>>>print(format("Welcome", "10s"), end = '#')

>>>print(format(111, "4d"), end = '#')

>>>print(format(924.656, "3.2f"))

a) Welcome# 111#924.66

b) Welcome#111#924.66

c) Welcome#111#.66

d) Welcome   # 111#924.66

 

Answer: d

Explanation: Execute in the shell to verify.

 

25. What will be the output of the following Python code?

print("abc DEF".capitalize())

a) abc def

b) ABC DEF

c) Abc def

d) Abc Def

 

Answer: c

Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.

26. What will be the output of the following Python code?

print("abcdef".center())

a) cd

b) abcdef

c) error

d) none of the mentioned

 

Answer: c

Explanation: The function center() takes at least one parameter.

27. What will be the output of the following Python code?

print("abcdef".center(0))

a) cd

b) abcdef

c) error

d) none of the mentioned

 

Answer: b

Explanation: The entire string is printed when the argument passed to center() is less than the length of the string.

"""

Regarding Pading concept:

Note1: If the width value is Even number, Fill characters will be added 1st right-hand side then remaining at left-hand
Note2: If the width value is Odd number, Fill characters will be added 1st left-hand side then remaining at right-hand
"""

28.  What will be the output of the following Python code? (padding concept)

print("abcdef".center(7, '1'))

a) 1abcdef

b) abcdef1

c) abcdef

d) error

 

Answer: a

Explanation: The character ‘1’ is used for padding instead of a space.

 

29. What will be the output of the following Python code?  (padding concept)

print("abcdef".center(7, 1))

a) 1abcdef

b) abcdef1

c) abcdef

d) error

 

Answer: d

Explanation: TypeError, the fill character must be a character, not an int.

30. What will be the output of the following Python code?

print("abcdef".center(10, '12'))

a) 12abcdef12

b) abcdef1212

c) 1212abcdef

d) error

 

Answer: d

Explanation: The fill character must be exactly one character long.

31. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('yy'))

a) 2

b) 0

c) error

d) none of the mentioned

 

Answer: a

Explanation: Counts the number of times the substring ‘yy’ is present in the given string.

 

32. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('yy', 1))

a) 2

b) 0

c) 1

d) none of the mentioned

 

Answer: a

Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.

33. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('xyy', 2, 11))

a) 2

b) 0

c) 1

d) error

 

Answer: b

Explanation: Counts the number of times the substring ‘xyy’ is present in the given string,

starting from position 2 and ending at position 11


34. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('xyy', 0, 100))

a) 2

b) 0

c) 1

d) error

 

Answer: a

Explanation: An error will not occur if the end value is greater than the length of the string itself.

35. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('yy', 2))

a) 2

b) 0

c) 1

d) none of the mentioned

 

Answer: c

Explanation: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 2.

36. What will be the output of the following Python code?

print("xyyzxyzxzxyy".count('xyy', -10, -1))

a) 2

b) 0

c) 1

d) error

 

Answer: b

Explanation: Counts the number of times the substring ‘xyy’ is present in the given string, 

starting from position 2 and ending at position 11.

 

37. What will be the output of the following Python code?

print('xyz'.encode())

a) XYZ    

b) ‘xyz’

c) b’’xyz

d) h’xyz

 

Answer: c

Explanation: By default, Data transmission from source to destination happens in the binary format (8 bits /sec == 1 byte ==utf-8 encoding), so that  A byte object is returned by encode.

38. What is the default value of encoding in encode()?

a) ascii

b) qwerty

c) utf-8

d) utf-16

 

Answer: c

Explanation: The default value of encoding is utf-8.

39. What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy"))

a) 1

b) True

c) 3

d) 2

 

Answer: b

Explanation: The function returns True if the given string ends with the specified substring.

40. What will be the output of the following Python code?

print("xyyzxyzxzxyy".endswith("xyy", 0, 2))

a) 0

b) 1

c) True

d) False


Answer: d

Explanation: The function returns False if the given string does not end with 

the specified substring.


41. What will be the output of the following Python code?

print("abcdef".find("cd") == "cd" in "abcdef")

a) True

b) False

c) Error

d) None of the mentioned

 

Answer: b

Explanation: The function find() returns the position of the substring in 

the given string whereas the in keyword returns a value of Boolean type.

42 What will be the output of the following Python code?

print("abcdef".find("cd"))

a) True

b) 2

c) 3

d) None of the mentioned


Answer: b

Explanation: The first position in the given string at which 

the substring can be found is returned.

43. What will be the output of the following Python code?

print("ccdcddcd".find("c"))

a) 4

b) 0

c) Error

d) True


Answer: b

Explanation: The first position in the given string at which 

the substring can be found is returned.

 

44. What will be the output of the following Python code?

print("Hello {0} and {1}".format('swetha', 'meera'))

a) Hello swetha and meera

b) Hello {0} and {1} swetha meera

c) Error

d) Hello 0 and 1

 

Answer: a

Explanation: The numbers 0 and 1 represent the position at which the strings are present.

45. What will be the output of the following Python code?

print("Hello {1} and {0}".format('bin', 'foo'))

a) Hello foo and bin

b) Hello bin and foo

c) Error

d) None of the mentioned

 

Answer: a

Explanation: The numbers 0 and 1 represent the position at which the strings are present.


46. What will be the output of the following Python code?

print("Hello {} and {}".format('foo', 'bin'))

a) Hello foo and bin

b) Hello {} and {}

c) Error

d) Hello and

 

Answer: a

Explanation: It is the same as Hello {0} and {1}.


47. What will be the output of the following Python code?

print("Hello {name1} and {name2}".format('foo', 'bin'))

a) Hello foo and bin

b) Hello {name1} and {name2}

c) Error

d) Hello and

 

Answer: c

Explanation: The arguments passed to the function format aren’t keyword arguments.

48. What will be the output of the following Python code?

print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))

a) Hello foo and bin

b) Hello {name1} and {name2}

c) Error

d) Hello and

 

Answer: a

Explanation: The arguments are accessed by their names.

49. What will be the output of the following Python code?

print("Hello {0!r} and {0!s}".format('foo', 'bin'))

a) Hello foo and foo

b) Hello ‘foo’ and foo

c) Hello foo and ‘bin’

d) Error

 

Answer: b

Explanation: !r -->  means  "raw string"    &   !s--> means normal string 

50. What will be the output of the following Python code?

print("Hello {0} and {1}".format(('foo', 'bin')))

a) Hello foo and bin

b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)`

c) Error

d) None of the mentioned

 

Answer: c

Explanation: IndexError, the tuple index is out of range.

51. What will be the output of the following Python code?

    print("Hello {0[0]} & {0[1]} and {1} ".format(('Nanditha', 'Venkat'), "Raahi"))                          

        a) Hello Nanditha  &  Venkat 

b) Hello Nanditha &  Venkat and Raahi 

c) Error

d) None of the mentioned

 

Answer: a

Explanation: The elements of the tuple are accessed by their indices.


52. What will be the output of the following Python code snippet?

print('The sum of {0} and {1} is {2}'.format(2, 10, 12))

a) The sum of 2 and 10 is 12

b) Error

c) The sum of 0 and 1 is 2

d) None of the mentioned

 

Answer: a

Explanation: The arguments passed to the function format can be integers also.


53. What will be the output of the following Python code snippet?

print('The sum of {0:b} and {1:x} is {2:o}'.format(2, 10, 12))

a) The sum of 2 and 10 is 12

b) The sum of 10 and a is 14

c) The sum of 10 and a is c

d) Error

 

Answer: b

Explanation: 2 is converted to binary, 10 to hexadecimal and 12 to octal.

 

54. What will be the output of the following Python code?

print('ab12'.isalnum())

a) True

b) False

c) None

d) Error


Answer: a

Explanation: The string has only letters and digits.

55. What will be the output of the following Python code?

print('ab,12'.isalnum())


a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: The character , is not a letter or a digit.

56. What will be the output of the following Python code?

print('ab'.isalpha())

a) True

b) False

c) None

d) Error


Answer: a

Explanation: The string has only letters.


57. What will be the output of the following Python code?

print('a B'.isalpha())


a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: Space is not a letter.


58. What will be the output of the following Python code snippet?

print('0xa'.isdigit())


a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: Hexadecimal digits aren’t considered as digits (a-f).


59. What will be the output of the following Python code snippet?

print(''.isdigit())


a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: If there are no characters then False is returned.


60. What will be the output of the following Python code snippet?

print('my_string'.isidentifier())

a) True

b) False

c) None

d) Error


Answer: a

Explanation: It is a valid identifier.

61. What will be the output of the following Python code snippet?

print('__foo__'.isidentifier())


a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: It is a valid identifier.

 

62. What will be the output of the following Python code snippet?

print('for'.isidentifier())


a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: Even keywords are considered as valid identifiers.

63. What will be the output of the following Python code snippet?

print('abc'.islower())

a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: There are no uppercase letters.


64. What will be the output of the following Python code snippet?

print('a@ 1,'.islower())

a) True

b) False

c) None

d) Error


Answer: a

Explanation: There are no uppercase letters.

65. What will be the output of the following Python code snippet?

print('11'.isnumeric())

a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: All the characters are numeric.


66. What will be the output of the following Python code snippet?

print('1.1'.isnumeric())

a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: The character . is not a numeric character.


67. What will be the output of the following Python code snippet?

print(''''''.isspace())

a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: None.


68. What will be the output of the following Python code snippet?

print('\t'.isspace())

a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: Tab Spaces are considered as spaces.

69. What will be the output of the following Python code snippet?

print('HelloWorld'.istitle())

a) True

b) False

c) None

d) Error

 

Answer: b

Explanation: The letter W is uppercased.

70. What will be the output of the following Python code snippet?

print('Hello World'.istitle())

a) True

b) False

c) None

d) Error

 

Answer: a

Explanation: It is in title form.


71. What will be the output of the following Python code?

print('1Rn@'.lower())

a) n

b) 1rn@

c) rn

d) r

 

Answer: b

Explanation: Uppercase letters are converted to lowercase. 

The other characters are left unchanged.

72. What will be the output of the following Python code?

print('''\tfoo'''.lstrip())

a) \tfoo

b) foo

c)   foo

d) none of the mentioned

 

Answer: b

Explanation: All leading whitespace is removed.

73. What will be the output of the following Python code?

print('xyyzxxyxyy'.lstrip('xyy'))

a) error

b) zxxyxyy

c) z

d) zxxy

 

Answer: b

Explanation: The leading characters containing xyy are removed.

74. What will be the output of the following Python code?

print('abcdef'.partition('cd'))

a) (‘ab’, ‘ef’)

b) (‘abef’)

c) (‘ab’, ‘cd’, ‘ef’)

d) 2

 

Answer: c

Explanation: The string is split into three parts by partition.

75. What will be the output of the following Python code?

print('abcdefcdgh'.partition('cd'))

a) (‘ab’, ‘cd’, ‘ef’, ‘cd’, ‘gh’)

b) (‘ab’, ‘cd’, ‘efcdgh’)

c) (‘abcdef’, ‘cd’, ‘gh’)

d) error

 

Answer: b

Explanation: The string is partitioned at the point where the separator first appears.

76. What will be the output of the following Python code?

print('abcd'.partition('cd'))

a) ('ab', 'cd', '')

b) (‘ab’, ‘cd’)

c) error

d) none of the mentioned

 

Answer: a

Explanation: The last item is a null string.

 

77. What will be the output of the following Python code snippet?

print('cd'.partition('cd'))

a) (‘cd’)

b) (”)

c) (‘cd’, ”, ”)

d) ('', 'cd', '')

 

Answer: d

Explanation: The entire string has been passed as the separator hence 

the first and the last item of the tuple returned are null strings.


78. What will be the output of the following Python code snippet?

print('abcdef12'.replace('cd', '12'))

a) ab12ef12

b) abcdef12

c) ab12efcd

d) none of the mentioned

 

Answer: a

Explanation: All occurrences of the first substring are replaced by the second substring.

79. What will be the output of the following Python code snippet?

print('abef'.replace('cd', '12'))

a) abef

b) 12

c) error

d) none of the mentioned

 

Answer: a

Explanation: The first substring is not present in the given string and hence nothing is replaced.


80. What will be the output of the following Python code snippet?

print('abcefd'.replace('cd', '12'))

a) ab1ef2

b) abcefd

c) ab1efd

d) ab12ed2

 

Answer: b

Explanation: The first substring is not present in the given string and hence nothing is replaced.

81. What will be the output of the following Python code snippet?

print('xyyxyyxyxyxxy'.replace('xy', '12', 3))

a) 12y12y12xyxxy

b) 12y12y1212x12

c) 12yxyyxyxyxxy

d) xyyxyyxyxyx12

 

Answer: a

Explanation: The first 0 occurrences of the given substring are replaced.

 

82. What will be the output of the following Python code snippet?

print('xyyxyyxyxyxxy'.replace('xy', '12', 100))

a) xyyxyyxyxyxxy

b) 12y12y1212x12

c) none of the mentioned

d) error

 

Answer: b

Explanation: The first 100 occurrences of the given substring are replaced, if 100 occurrences are not available then how many occurrences it's available those many times  it will be replaced with required string

83. What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd'))

a) [‘ab’, ‘ef’, ‘gh’]

b) [‘ab’, ‘ef’, ‘gh’, ”]

c) (‘ab’, ‘ef’, ‘gh’)

d) (‘ab’, ‘ef’, ‘gh’, ”)

 

Answer: b  

Explanation: The given string is split and a list of substrings is returned.  

Note : when we get at the ens then it will get empty string

84. What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd', 0))

a) [‘abcdefcdghcd’]

b) ‘abcdefcdghcd’

c) error

d) none of the mentioned

 

Answer: a

Explanation: The given string is split at 0 occurrences of the specified substring.

85. What will be the output of the following Python code snippet?

print('abcdefcdghcd'.split('cd', 2))

a) [‘ab’, ‘ef’, ‘ghcd’]

b) [‘ab’, ‘efcdghcd’]

c) [‘abcdef’, ‘ghcd’]

d) none of the mentioned

 

Answer: a

Explanation: The string is split into a maximum of maxsplit+1 substrings.

86. What will be the output of the following Python code snippet?

print('ab\ncd\nef'.splitlines())

a) [‘ab’, ‘cd’, ‘ef’]

b) [‘ab\n’, ‘cd\n’, ‘ef\n’]

c) [‘ab\n’, ‘cd\n’, ‘ef’]

d) [‘ab’, ‘cd’, ‘ef\n’]

 

Answer: a

Explanation: It is similar to calling split(‘\n’).

87. What will be the output of the following Python code snippet?

print('Ab!2'.swapcase())

a) AB!@

b) ab12

c) aB!2

d) aB1@

 

Answer: c

Explanation: Lowercase letters are converted to uppercase and vice-versa.

88. What will be the output of the following Python code snippet?

print('ab cd ef'.title())

a) Ab cd ef

b) Ab cd eF

c) Ab Cd Ef

d) None of the mentioned

 

Answer: c

Explanation: The first letter of every word is capitalized.

89. What will be the output of the following Python code snippet?

print('ab cd-ef'.title())

a) Ab cd-ef

b) Ab Cd-ef

c) Ab Cd-Ef

d) None of the mentioned

 

Answer: c

Explanation: The first letter of every word is capitalized. Special symbols terminate a word.


90. What will be the output of the following Python code snippet?

print('ab'.zfill(5))

a) 000ab

b) 00ab0

c) 0ab00

d) ab000

 

Answer: a

Explanation: The string is padded with zeros on the left-hand side. It is useful for formatting numbers.

91. What will be the output of the following Python code snippet?

print('+99'.zfill(5))

a) 00+99

b) 00099

c) +0099

d) +++99

 

Answer: c

Explanation: zeros are filled in between the first sign and the rest of the string.

92. What will be the output of the following Python statement?

>>>chr(ord('A'))

a) A

b) B

c) a

d) Error

 

Answer: a

Explanation: Execute in shell to verify.

93. What will be the output of the following Python statement?

>>>print(chr(ord('b')+1))

a) C

b) b

c) c

d) d

 

Answer: c

Explanation: Execute in the shell to verify.

94. What will be displayed by print(ord("g") - ord("a")) ?

a) 0

b) 6

c) 7

d) 2

 

Answer: b

Explanation:  ord() function will convert characters into ASCII values ord('a') ==>97 & ord('b') ==>98

 & char() function will convert  ASCII values into characters.

Hence the output of this code is 98-97, which is equal to 1.

95. To concatenate two strings to a third what statements are applicable?

a) s3 = s1 . s2

b) s3 = s1.add(s2)

c) s3 = s1.__add__(s2)

d) s3 = s1 * s2

 

Answer: c

Explanation: __add__ is another method that can be used for concatenation.


 96. The output of executing string.ascii_letters can also be achieved by:

a) string.ascii_lowercase_string.digits

b) abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

c) abcdefghijklmnopqrstuvwxyz

d) ABCDEFGHIJKLMNOPQRSTUVWXYZ

 

Answer: b

Explanation: Execute in the shell and check.


Request: If you find this information useful, please provide your valuable comments.