CSC201+++ by BIGTUBA
CSC201+++ by BIGTUBA
ON EACH SECTION
This set of Python Questions & Answers focuses on “Core Data Types”.
1. Which of these in not a core datatype?
a) Lists
b) Dictionary
c) Tuples
d) Class
View Answer
Answer:d
Explanation:Class is a user defined datatype.
2. Given a function that does not return any value, What value is thrown by itby default when
executed in shell.
a) int
b) bool
c) void
d) None
View Answer
Answer:d
Explanation:Python shell throws a NoneType object back.
3. Following set of commands are executed in shell, what will be the output?
>>>str="hello"
>>>str[:2]
>>>
a) he
b) lo
c) olleh
d) hello
View Answer
Answer:a
4. Which of the following will run without errors(multiple answers possible) ?
a) round(45.8)
b) round(6352.898,2)
c) round()
d) round(7463.123,2,1)
View Answer
Answer:a,b
Explanation:Execute help(round) in the shell to get details of the parameters that are passed into
the round function.
5. What is the return type of function id ?
a) int
b) float
c) bool
d) dict
View Answer
Answer:a
Explanation:Execute help(id) to find out details in python shell.id returns a integer value that is
unique.
6. In python we do not specify types,it is directly interpreted by the compiler, so consider the
following operation to be performed.
>>>x = 13 ? 2
advertisements
objective is to make sure x has a integer value, select all that apply (python 3.xx)
a) x = 13 // 2
b) x = int(13 / 2)
c) x = 13 / 2
d) x = 13 % 2
View Answer
Answer:a,b
Explanation:// is integer operation in python 3.0 and int(..) is a type cast operator.
7. What error occurs when you execute?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError
View Answer
Answer:b
Explanation:Banana is not defined hence name error.
8. Carefully observe the code and give the answer.
def example(a):
a = a + '2'
a = a*2
return a
>>>example("hello")
a) indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2
View Answer
Answer:a
Explanation:Python codes have to be indented properly.
9. What dataype is the object below ?
L = [1, 23, ‘hello’, 1] a) List
b) dictionary
c) array
d) tuple
View Answer
Answer:a
Explanation:List datatype can store any values within it.
10. In order to store values in terms of key and value we use what core datatype.
a) List
b) tuple
c) class
d) dictionary
View Answer
Answer:d
Explanation:Dictionary stores values in terms of keys and values.
11. Which of the following results in a SyntaxError(Multiple answers possible) ?
a) ‘”Once upon a time…”, she said.’
b) “He said, “Yes!””
c) ‘3\’
d) ”’That’s okay”’
View Answer
Answer:b,c
Explanation:Carefully look at the colons.
12. The following is displayed by a print function call:
tom
dick
harry
advertisements
Select all of the function calls that result in this output
a) print(”’tom
\ndick
\nharry” ’)
b) print(”’tom
dick
harry”’)
c) print(‘tom\ndick\nharry’)
d) print(‘tom
dick
harry’)
View Answer
Answer:b,c
Explanation:The \n adds a new line.
13. What is the average value of the code that is executed below ?
>>>grade1 = 80
>>>grade2 = 90
>>>average = (grade1 + grade2) / 2
a) 85
b) 85.0
c) 95
d) 95.0
View Answer
Answer:b
Explanation:Cause a decimal value to appear as output.
14. Select all options that print
hello-how-are-you
a) print(‘hello’, ‘how’, ‘are’, ‘you’)
b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
c) print(‘hello-‘ + ‘how-are-you’)
d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘-‘ + ‘you’)
View Answer
Answer:c,d
Explanation:Execute in the shell.
15. What is the return value of trunc() ?
a) int
b) bool
c) float
d) None
View Answer
Answer:a
Explanation:Executle help(math.trunc) to get details.
This section on Python aptitude questions and answers focuses on “Basic Operators”.
1. Which is the correct operator for power(x^y)?
a) X^y
b) X**y
c) X^^y
d) None of the mentioned
View Answer
Answer: b
Explanation: In python, power operator is x**y i.e. 2**3=8.
2. Which one of these is floor division?
a) /
b) //
c) %
d) None of the mentioned
View Answer
Answer: b
Explanation: When both of the operands are integer then python chops out the fraction part and
gives you the round off value, to get the accurate answer use floor division. This is floor division.
For ex, 5/2 = 2.5 but both of the operands are integer so answer of this expression in python is
2.To get the 2.5 answer, use floor division.
3. What is the order of precedence in python?
i) Parentheses
ii) Exponential
iii) Division
iv) Multiplication
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v
View Answer
Answer: a
Explanation: For order of precedence, just remember this PEDMAS.
4. What is answer of this expression, 22 % 3 is?
a) 7
b) 1
c) 0
d) 5
View Answer
Answer: b
Explanation: Modulus operator gives remainder. So, 22%3 gives 1 remainder.
advertisements
5. You can perform mathematical operation on String?
a) True
b) False
View Answer
Answer: b
Explanation: You can’t perform mathematical operation on string even if string looks like
integers.
6. Operators with the same precedence are evaluated in which manner?
a) Left to Right
b) Right to Left
View Answer
Answer: a
Explanation: None.
7. What is the output of this expression, 3*1**3?
a) 27
b) 9
c) 3
d) 1
View Answer
Answer: c
Explanation: First this expression will solve 1**3 because exponential have higher precedence
than multiplication, so 1**3 = 1 and 3*1 = 3. Final answer is 3.
8. Which one of the following have the same precedence?
a) Addition and Subtraction
b) Multiplication and Division
c) a and b
d) None of the mentioned
View Answer
Answer: c
Explanation: None.
9. Int(x) means variable x is converted to integer.
a) True
b) False
View Answer
Answer: a
Explanation: None.
advertisements
10. Which one of the following have the highest precedence in the expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses
View Answer
Answer: d
Explanation: None.
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Variable Names”.
Answer: a
Explanation: Case is always significant.
2. What is the maximum possible length of an identifier?
a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned
View Answer
Answer: d
Explanation: Identifiers can be of any length.
3. Which of the following is invalid?
a) _a = 1
b) __a = 1
c) __str__ = 1
d) none of the mentioned
View Answer
Answer: d
Explanation: All the statements will execute successfully but at the cost of reduced readability.
advertisements
4. Which of the following is an invalid variable?
a) my_string_1
b) 1st_string
c) foo
d) _
View Answer
Answer: b
Explanation: Variable names should not start with a number.
5. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution
View Answer
Answer: a
Explanation: As Python has no concept of private variables, leading underscores are used to
indicate variables that must not be accessed from outside the class.
6. Which of the following is not a keyword?
a) eval
b) assert
c) nonlocal
d) pass
View Answer
Answer: a
Explanation: eval can be used as a variable.
7. All keywords in Python are in
a) lower case
b) UPPER CASE
c) Capitalized
d) none
View Answer
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.
8. Which of the following is true for variable names in Python?
a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special charaters allowed
d) none
View Answer
Answer: a
Explanation: Variable names can be of any length.
advertisements
9. Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000
View Answer
Answer: b
Explanation: Spaces are not allowed in variable names.
10. Which of the following cannot be a variable?
a) __init__
b) in
c) it
d) on
View Answer
Answer: b
Explanation: in is a keyword.
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Regular
Expressions”.
Answer: a
Explanation: re is a part of the standard library and can be imported using: import re.
2. Which of the following creates a pattern object?
a) re.create(str)
b) re.regex(str)
c) re.compile(str)
d) re.assemble(str)
View Answer
Answer: c
Explanation: It converts a given string into a pattern object.
3. What does the function re.match do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
View Answer
Answer: a
Explanation: It will look for the pattern at the beginning and return None if it isn’t found.
4. What does the function re.search do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
View Answer
Answer: b
Explanation: It will look for the pattern at any position in the string.
5. What is the output of the following?
Answer: a
Explanation: This function returns all the subgroups that have been matched.
advertisements
6. What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group())
a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’
View Answer
Answer: d
Explanation: This function returns the entire match.
7. What is the output of the following?
Answer: c
Explanation: This function returns the particular subgroup.
8. What is the output of the following?
Answer: a
Explanation: This function returns a dictionary that contains all the mathches.
advertisements
9. What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groups())
a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’
View Answer
Answer: b
Explanation: This function returns all the subgroups that have been matched.
10. What is the output of the following?
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Numeric Types”.
advertisements
3. What is the type of inf?
a) Boolean
b) Integer
c) Float
d) Complex
View Answer
4. What does ~4 evaluate to?
a) -5
b) -4
c) -3
d) +3
View Answer
advertisements
6. Which of the following is incorrect?
a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964
View Answer
7. What is the result of cmp(3, 1)?
a) 1
b) 0
c) True
d) False
View AnswerAnswer: a
Explanation: cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.[/expand] 8. Which of the
following is incorrect? a) float('inf') b) float('nan') c) float('56'+'78') d) float('12+34') [expand
title="View Answer"]Answer: d Explanation: '+' cannot be converted to a float.[/expand] 9. What
is the result of round(0.5) - round(-0.5)? a) 1.0 b) 2.0 c) 0.0 d) None of the mentioned [expand
title="View Answer"]Answer: b Explanation: Python rounds off numbers away from 0 when the
number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is
-1.[/expand] 10. What does 3 ^ 4 evaluate to? a) 81 b) 12 c) 0.75 d) 7 [expand title="View
Answer"]Answer: d Explanation: ^ is the Binary XOR operator.[/expand]
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “While and For
Loops”.
x = ['ab', 'cd']
for i in x:
i.upper()
print(x)
a) [‘ab’, ‘cd’] b) [‘AB’, ‘CD’] c) [None, None] d) none of the mentioned
View Answer
Answer: a
Explanation: The function upper() does not modify a string in place, it returns a new string which
isn’t being stored anywhere.
2. What is the output of the following?
x = ['ab', 'cd']
for i in x:
x.append(i.upper())
print(x)
a) [‘AB’, ‘CD’] b) [‘ab’, ‘cd’, ‘AB’, ‘CD’] c) [‘ab’, ‘cd’] d) none of the
mentioned
View Answer
Answer: d
Explanation: The loop does not terminate as new elements are being added to the list in each
iteration.
3. What is the output of the following?
i=1
while True:
if i%3 == 0:
break
print(i)
i+=1
a) 1 2
b) 1 2 3
c) error
d) none of the mentioned
View Answer
i=1
while True:
if i%0O7 == 0:
break
print(i)
i += 1
advertisements
a) 1 2 3 4 5 6
b) 1 2 3 4 5 6 7
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Control exits the loop when i becomes 7.
5. What is the output of the following?
i=5
while True:
if i%0O11 == 0:
break
print(i)
i += 1
a) 5 6 7 8 9 10
b) 5 6 7 8
c) 5 6
d) error
View Answer
Answer: b
Explanation: 0O11 is an octal number.
6. What is the output of the following?
i=5
while True:
if i%0O9 == 0:
break
print(i)
i += 1
a) 5 6 7 8
b) 5 6 7 8 9
c) 5 6 7 8 9 10 11 12 13 14 15 ….
d) error
View Answer
Answer: d
Explanation: 9 isn’t allowed in an octal number.
7. What is the output of the following?
i=1
while True:
if i%2 == 0:
break
print(i)
i += 2
a) 1
b) 1 2
c) 1 2 3 4 5 6 …
d) 1 3 5 7 9 11 …
View Answer
Answer: d
Explanation: The loop does not terminate since i is never an even number.
advertisements
8. What is the output of the following?
i=2
while True:
if i%3 == 0:
break
print(i)
i += 2
a) 2 4 6 8 10 …
b) 2 4
c) 2 3
d) error
View Answer
Answer: b
Explanation: The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and
hence control exits the loop.
9. What is the output of the following?
i=1
while False:
if i%2 == 0:
break
print(i)
i += 2
a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned
View Answer
Answer: d
Explanation: Control does not enter the loop because of False.
10. What is the output of the following?
True = False
while True:
print(True)
break
a) True
b) False
c) None
d) none of the mentioned
View Answer
Answer: d
Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.
Python Questions and Answers – While and For Loops – 2
This set of Advanced Python Questions & Answers focuses on “While and For Loops”.
1. What is the output of the following?
i=0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
a) 0 1 2 0
b) 0 1 2
c) error
d) none of the mentioned
View Answer
Answer: b
Explanation: The else part is not executed if control breaks out of the loop.
2. What is the output of the following?
i=0
while i < 3:
print(i)
i += 1
else:
print(0)
a) 0 1 2 3 0
b) 0 1 2 0
c) 0 1 2
c) error
View Answer
Answer: b
Explanation: The else part is executed when the condition in the while statement is false.
3. What is the output of the following?
x = "abcdef"
while i in x:
print(i, end=" ")
a) a b c d e f
b) abcdef
c) i i i i i i …
d) error
View Answer
Answer: d
Explanation: NameError, i is not defined.
4. What is the output of the following?
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
advertisements
a) no output
b) i i i i i i …
c) a b c d e f
d) abcdef
View Answer
Answer: a
Explanation: “i” is not in “abcdef”.
5. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f
View Answer
Answer: c
Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.
6. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
print('i', end = " ")
a) no output
b) i i i i i i …
c) a a a a a a …
d) a b c d e f
View Answer
Answer: b
Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.
7. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
x = x[:-1]
print(i, end = " ")
a) i i i i i i
b) a a a a a a
c) a a a a a
d) none of the mentioned
View Answer
Answer: b
Explanation: The string x is being shortened by one charater in each iteration.
advertisements
8. What is the output of the following?
x = "abcdef"
i = "a"
while i in x[:-1]:
print(i, end = " ")
a) a a a a a
b) a a a a a a
c) a a a a a a …
d) a
View Answer
Answer: c
Explanation: String x is not being altered and i is in x[:-1].
9. What is the output of the following?
x = "abcdef"
i = "a"
while i in x:
x = x[1:]
print(i, end = " ")
a) a a a a a a
b) a
c) no output
d) error
View Answer
Answer: b
Explanation: The string x is being shortened by one charater in each iteration.
10. What is the output of the following?
x = "abcdef"
i = "a"
while i in x[1:]:
print(i, end = " ")
a) a a a a a a
b) a
c) no output
d) error
View Answer
Answer: c
Explanation: i is not in x[1:].
Python Questions and Answers – While and For Loops – 3
This set of Tough Python Questions & Answers focuses on “While and For Loops”.
x = 'abcd'
for i in x:
print(i)
x.upper()
a) a B C D
b) a b c d
c) A B C D
d) error
View Answer
Answer: b
Explanation: Changes do not happen in-place, rather a new instance of the string is returned.
2. What is the output of the following?
x = 'abcd'
for i in x:
print(i.upper())
a) a b c d
b) A B C D
c) a B C D
d) error
View Answer
Answer: b
Explanation: The instance of the string returned by upper() is being printed.
3. What is the output of the following?
x = 'abcd'
for i in range(x):
print(i)
a) a b c d
b) 0 1 2 3
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: range(str) is not allowed.
4. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
print(i)
advertisements
a) a b c d
b) 0 1 2 3
c) error
d) none of the mentioned
View Answer
Answer: b
Explanation: i takes values 0, 1, 2 and 3.
5. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
print(i.upper())
a) a b c d
b) 0 1 2 3
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type int have no attribute upper().
6. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
i.upper()
print (x)
a) a b c d
b) 0 1 2 3
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type int have no attribute upper().
7. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
x[i].upper()
print (x)
a) abcd
b) ABCD
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Changes do not happen in-place, rather a new instance of the string is returned.
advertisements
8. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
i[x].upper()
print (x)
a) abcd
b) ABCD
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type int aren’t subscriptable.
9. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
x = 'a'
print(x)
a) a
b) abcd abcd abcd
c) a a a a
d) none of the mentioned
View Answer
Answer: c
Explanation: range() is computed only at the time of entering the loop.
10. What is the output of the following?
x = 'abcd'
for i in range(len(x)):
print(x)
x = 'a'
a) a
b) abcd abcd abcd abcd
c) a a a a
d) none of the mentioned
View Answer
Answer: d
Explanation: abcd a a a is the output as x is modified only after ‘abcd’ has been printed once.
Python Questions and Answers – While and For Loops – 4
This set of Python Questions & Answers focuses on “While and For Loops” and is useful for
freshers who are preparing for their interviews.
x = 123
for i in x:
print(i)
a) 1 2 3
b) 123
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type int are not iterable.
2. What is the output of the following?
d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
print(i)
a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned
View Answer
Answer: a
Explanation: Loops over the keys of the dictionary.
3. What is the output of the following?
Answer: d
Explanation: Error, objects of type int aren’t iterable.
4. What is the output of the following?
Answer: b
Explanation: Loops over the keys and prints the values.
6. What is the output of the following?
Answer: b
Explanation: Loops over the values.
7. What is the output of the following?
Answer: d
Explanation: Causes a KeyError.
8. What is the output of the following?
d = {0, 1, 2}
for x in d.values():
print(x)
advertisements
a) 0 1 2
b) None None None
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: Objects of type set have no attribute values.
9. What is the output of the following?
d = {0, 1, 2}
for x in d:
print(x)
a) 0 1 2
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Loops over the elements of the set and prints them.
10. What is the output of the following?
d = {0, 1, 2}
for x in d:
print(d.add(x))
a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) none of the mentioned
View Answer
Answer: c
Explanation: Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.
11. What is the output of the following?
for i in range(0):
print(i)
a) 0
b) (nothing is printed)
c) error
d) none of the mentioned
View Answer
Answer: b
Explanation: range(0) is empty.
This set of Python Technical Questions & Answers focuses on “While/For Loops”.
Answer: c
Explanation: The else part is executed if control doesn’t break out of the loop.
2. What is the output of the following?
for i in range(5):
if i == 5:
break
else:
print(i)
else:
print("Here")
a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
View Answer
Answer: a
Explanation: The else part is executed if control doesn’t break out of the loop.
3. What is the output of the following?
x = (i for i in range(3))
for i in x:
print(i)
a) 0 1 2
b) error
c) 0 1 2 0 1 2
d) none of the mentioned
View Answer
Answer: a
Explanation: The first statement creates a generator object.
4. What is the output of the following?
x = (i for i in range(3))
for i in x:
print(i)
for i in x:
print(i)
advertisements
a) 0 1 2
b) error
c) 0 1 2 0 1 2
d) none of the mentioned
View Answer
Answer: a
Explanation: We can loop over a generator object only once.
5. What is the output of the following?
Answer: a
Explanation: Variable i takes the value of one character at a time.
6. What is the output of the following?
Answer: c
Explanation: Variable i takes the value of one word at a time.
7. What is the output of the following?
a = [0, 1, 2, 3]
for a[-1] in a:
print(a[-1])
a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
Answer: b
Explanation: The value of a[-1] changes in each iteration.
advertisements
8. What is the output of the following?
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])
a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
Answer: a
Explanation: The value of a[0] changes in each iteration. Since the first value that it takes is itself,
there is no visible error in the current example.
9. What is the output of the following?
a = [0, 1, 2, 3]
i = -2
for i not in a:
print(i)
i += 1
a) -2 -1
b) 0
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: SyntaxError, not in isn’t allowed in for loops.
10. What is the output of the following?
Answer: a
Explanation: Variable i takes the value of one character at a time
– Strings – 1
>>>"a"+"bc"
a) a
b) bc
c) bca
d) abc
View Answer
Answer:d
Explanation:+ operator is concatenation operator.
2. What is the output when following statement is executed ?
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
View Answer
Answer:c
Explanation:Slice operation is performed on string.
3. The output of executing string.ascii_letters can also be achieved by:
a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase
View Answer
Answer:b
Explanation:Execute in shell and check.
4. What is the output when following code is executed ?
Answer:c,d
Explanation:+ is used to concatenate and * is used to multiply strings.
6. What is the output when following code is executed ?
>>>print r"\nhello"
The output is
a) a new line and hello
b) \nhello
c) the letter r and then hello
d) Error
View Answer
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.
7. What is the output when following statement is executed ?
Answer:c
Explanation:\x is an escape sequence that means the following 2 digits are a hexadicmal number
encoding a character.
9. What is the output when following code is executed ?
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
View Answer
Answer:a
Explanation:Execute in shell to verify.
10. print 0xA + 0xB + 0xC :
a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
View Answer
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.
– Strings – 2
This set of Advanced Python Interview Questions & Answers focuses on “Strings”.
1. What is the output of the following code ?
class father:
def __init__(self, param):
self.o1 = param
class child(father):
def __init__(self, param):
self.o2 = param
>>>obj = child(22)
>>>print "%d %d" % (obj.o1, obj.o2)
a) None None
b) None 22
c) 22 None
d) Error is generated
View Answer
Answer:d
Explanation:self.o1 was never created.
2. What is the output of the following code ?
class tester:
def __init__(self, id):
self.id = str(id)
id="224"
>>>temp = tester(12)
>>>print temp.id
a) 224
b) Error
c) 12
d) None
View Answer
Answer:c
Explanation:id in this case will be the attribute of the class.
3. What is the output of the following code ?
Answer:c
Explanation:Strings cannot be modified.
5. What is the output of the following code ?
Answer:d
Explanation:Max returns the character with the highest ascii value.
6. Given a string example=”hello” what is the output of example.count(l)
a) 2
b) 1
c) None
d) 0
View Answer
Answer:a
Explanation:l occurs twice in hello.
7. What is the output of the following code ?
>>>example = "helle"
>>>example.find("e")
advertisements
a) Error
b) -1
c) 1
d) 0
View Answer
Answer:c
Explanation:returns lowest index .
8. What is the output of the following code ?
>>>example = "helle"
>>>example.rfind("e")
a) -1
b) 4
c) 3
d) 1
View Answer
Answer:b
Explanation:returns highest index.
9. What is the output of the following code ?
>>>example="helloworld"
>>>example[::-1].startswith("d")
a) dlrowolleh
b) True
c) -1
d) None
View Answer
Answer:b
Explanation:Starts with checks if the given string starts with the parameter that is passed.
10. To concatenate two strings to a third what statements are applicable (multiple answers are
allowed) ?
a) s3 = s1 + s2
b) s3 = s1.add(s2)
c) s3 = s1.__add__(s2)
d) s3 = s1 * s2
View Answer
Answer:a,c
Explanation:__add__ is another method that can be used for concatenation.
This set of Python Technical Interview Questions & Answers focuses on “Strings”.
1. What is the output when following statement is executed ?
>>>chr(ord('A'))
a) A
b) B
c) a
d) Error
View Answer
Answer:a
Explanation:Execute in shell to verify.
2. What is the output when following statement is executed ?
>>>print(chr(ord('b')+1))
a) a
b) b
c) c
d) A
View Answer
Answer:c
Explanation:Execute in the shell to verify.
advertisements
3. Which of the following statement 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”)
View Answer
Answer:b
Explanation:\is used to indicate that the next \ is not an escape sequence.
4. Suppose s is “\t\tWorld\n”, what is s.strip() ?
a) \t\tWorld\n
b) \t\tWorld\n
c) \t\tWORLD\n
d) World
View Answer
Answer:d
Explanation:Execute help(string.strip) to find details.
5. The format function returns :
a) Error
b) int
c) bool
d) str
View Answer
Answer:d
Explanation:Format function returns a string.
6. What is the output of “hello”+1+2+3 ?
a) hello123
b) hello
c) Error
d) hello6
View Answer
Answer:c
Explanation:Cannot concantenate str and int objects.
7. What is the output when following code is executed ?
Answer:c
Explanation:Execute in the shell.
advertisements
8. What is the output when following statement is executed ?(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
View Answer
Answer:d
Explanation:Execute in the shell to verify.
9. What will be displayed by print(ord(‘b’) – ord(‘a’)) ?
a) 0
b) 1
c) -1
d) 2
View Answer
Answer:b
Explanation:ascii value of b is one more than a.
10. Say s=”hello” what will be the return value of type(s) ?
a) int
b) bool
c) str
d) String
View Answer
Answer:c
Explanation:str is used to represent strings in python.
Answer:a
Explanation:Execute in shell to verify.
2. To retrieve the character at index 3 from string s=”Hello” what command do we execute
(multiple answers allowed) ?
a) s[3] b) s.getitem(3)
c) s.__getitem__(3)
d) s.getItem(3)
View Answer
Answer:a, c
Explanation:__getitem(..) can be used to get character at index specified as parameter.
3. To return the length of string s what command do we execute (multiple answers allowed) ?
a) s.__len__()
b) len(s)
c) size(s)
d) s.size()
View Answer
Answer:a,b
Explanation:Execute in shell to verify.
4. If a class defines the __str__(self) method, for an object obj for the class, you can use which
command to invoke the __str__ method.(multiple answers allowed)
a) obj.__str__()
b) str(obj)
c) print obj
d) __str__(obj)
View Answer
Answer:a,b,c
Explanation:Execute in shell to verify.
5. To check whether string s1 contains s2, use
a) s1.__contains__(s2)
b) s1 in s2
c) s1.contains(s2)
d) si.in(s2)
View Answer
Answer:a,b
Explanation:s1 in s2 works in the same way as calling the special function __contains__ .
advertisements
6. Suppose i is 5 and j is 4, i + j is same as
a) i.__add(j)
b) i.__add__(j)
c) i.__Add(j)
d) i.__ADD(j)
View Answer
Answer:b
Explanation:Execute in shell to verify.
7. What is the output of the following code ?
class Count:
def __init__(self, count = 0):
self.__count = count
c1 = Count(2)
c2 = Count(2)
print(id(c1) == id(c2), end = " ")
s1 = "Good"
s2 = "Good"
print(id(s1) == id(s2))
a) True False
b) True True
c) False True
d) False False
View Answer
Answer:c
Explanation:Execute in the shell objects cannot have same id, however in the case of strings its
different.
8. What is the output of the following code ?
class Name:
def __init__(self, firstName, mi, lastName):
self.firstName = firstName
self.mi = mi
self.lastName = lastName
firstName = "John"
name = Name(firstName, 'F', "Smith")
firstName = "Peter"
name.lastName = "Pan"
print(name.firstName, name.lastName)
advertisements
a) Peter Pan.
b) John Pan.
c) Peter Smith.
d) John Smith.
View Answer
Answer:b
Explanation:Execute in the shell to verify.
9. What function do you use to read a string?
a) input(“Enter a string”)
b) eval(input(“Enter a string”))
c) enter(“Enter a string”)
d) eval(enter(“Enter a string”))
View Answer
Answer:a
Explanation:Execute in shell to verify.
10. Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space)
a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354
View Answer
Answer:b
Explanation:Execute in the shell to verify
Python Questions and Answers – Strings – 5
print("abc DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to
lowercase.
2. What is the output of the following?
print("abc. DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to
lowercase.
3. What is the output of the following?
print("abcdef".center())
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: The function center() takes atleast one parameter.
4. What is the output of the following?
print("abcdef".center(0))
advertisements
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: The entire string is printed when the argument passed to center() is less than the
length of the string.
5. What is the output of the following?
Answer: b
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length.
Extra spaces are present since we haven’t overridden the value of sep.
6. What is the output of the following?
Answer: d
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length.
7. What is the output of the following?
Answer: c
Explanation: Padding is done towards the right-hand-side first when the final string is of even
length.
advertisements
8. What is the output of the following?
print("abcdef".center(7, 1))
a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer
Answer: d
Explanation: TypeError, the fill character must be a character, not an int.
9. What is the output of the following?
print("abcdef".center(7, '1'))
a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer
Answer: a
Explanation: The character ‘1’ is used for padding instead of a space.
10. What is the output of the following?
print("abcdef".center(10, '12'))
a) 12abcdef12
b) abcdef1212
c) 1212abcdef
d) error
View Answer
Answer: d
Explanation: The fill character must be exactly one character long.
Python Questions and Answers – Strings – 6
print("xyyzxyzxzxyy".count('yy'))
a) 2
b) 0
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: Counts the number of times the substring ‘yy’ is present in the given string.
2. What is the output of the following?
print("xyyzxyzxzxyy".count('yy', 1))
a) 2
b) 0
c) 1
d) none of the mentioned
View Answer
Answer: a
Explanation: Counts the number of times the substring ‘yy’ is present in the given string,
starting from position 1.
3. What is the output of the following?
print("xyyzxyzxzxyy".count('yy', 2))
a) 2
b) 0
c) 1
d) none of the mentioned
View Answer
Answer: c
Explanation: Counts the number of times the substring ‘yy’ is present in the given string,
starting from position 2.
advertisements
4. What is the output of the following?
print("xyyzxyzxzxyy".count('xyy', 0, 100))
a) 2
b) 0
c) 1
d) error
View Answer
Answer: a
Explanation: An error will not occur if the end value is greater than the length of the string itself.
5. What is the output of the following?
print("xyyzxyzxzxyy".count('xyy', 2, 11))
a) 2
b) 0
c) 1
d) error
View Answer
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.
6. What is the output of the following?
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.
7. What is the output of the following?
print('abc'.encode())
advertisements
a) abc
b) ‘abc’
c) b’abc’
d) h’abc’
View Answer
Answer: c
Explanation: A bytes object is returned by encode.
8. What is the default value of encoding in encode()?
a) ascii
b) qwerty
c) utf-8
d) utf-16
View Answer
Answer: c
Explanation: The default value of encoding is utf-8.
9. What is the output of the following?
print("xyyzxyzxzxyy".endswith("xyy"))
a) 1
b) True
c) 3
d) 2
View Answer
Answer: b
Explanation: The function returns True if the given string ends with the specified substring.
10. What is the output of the following?
print("xyyzxyzxzxyy".endswith("xyy", 0, 2))
a) 0
b) 1
c) True
d) False
View Answer
Answer: d
Explanation: The function returns False if the given string does not end with the specified
substring.
print('abcdefcdghcd'.split('cd', 2))
a) [‘ab’, ‘ef’, ‘ghcd’] b) [‘ab’, ‘efcdghcd’] c) [‘abcdef’, ‘ghcd’] d) none of
the mentioned
View Answer
Answer: a
Explanation: The string is split into a maximum of maxsplit+1 substrings.
2. What is the output of the following?
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’] View Answer
Answer: a
Explanation: It is similar to calling split(‘\n’).
3. What is the output of the following?
print('Ab!2'.swapcase())
a) AB!@
b) ab12
c) aB!2
d) aB1@
View Answer
Answer: c
Explanation: Lowercase letters are converted to uppercase and vice-versa.
4. What is the output of the following?
print('ab cd ef'.title())
advertisements
a) Ab cd ef
b) Ab cd eF
c) Ab Cd Ef
d) none of the mentioned
View Answer
Answer: c
Explanation: The first letter of every word is capitalized.
5. What is the output of the following?
print('ab cd-ef'.title())
a) Ab cd-ef
b) Ab Cd-ef
c) Ab Cd-Ef
d) none of the mentioned
View Answer
Answer: c
Explanation: The first letter of every word is capitalized. Special symbols terminate a word.
6. What is the output of the following?
print('abcd'.translate('a'.maketrans('abc', 'bcd')))
a) bcde
b) abcd
c) error
d) none of the mentioned
View Answer
Answer: d
Explanation: The output is bcdd since no translation is provided for d.
7. What is the output of the following?
Answer: d
Explanation: The output is bcdd since no translation is provided for d.
advertisements
8. What is the output of the following?
print('abcd'.translate({'a': '1', 'b': '2', 'c': '3', 'd': '4'}))
a) abcd
b) 1234
c) error
d) none of the mentioned
View Answer
Answer: a
Explanation: The function translate expects a dictionary of integers. Use maketrans() instead of
doing the above.
9. What is the output of the following?
print('ab'.zfill(5))
a) 000ab
b) 00ab0
c) 0ab00
d) ab000
View Answer
Answer: a
Explanation: The string is padded with zeroes on the left hand side. It is useful for formatting
numbers.
10. What is the output of the following?
print('+99'.zfill(5))
a) 00+99
b) 00099
c) +0099
d) +++99
View Answer
Answer: c
Explanation: Zeroes are filled in between the first sign and the rest of the string.
Answer:a,b,c,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’] View Answer
Answer:a
Explanation:execute in the shell to verify.
3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’], what is len(listExample)?
a) 5
b) 4
c) None
d) Error
View Answer
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
View Answer
Answer:c
Explanation:max returns the maximum element in the list.
advertisements
5. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1) ?
a) 3
b) 5
c) 25
d) 1
View Answer
Answer:d
Explanation:min returns the minimum element in the list.
6. Suppose list1 is [1, 5, 9], what is sum(list1) ?
a) 1
b) 9
c) 15
d) Error
View Answer
Answer:c
Explanation:Sum returns the sum of all elements in the list.
7. To shuffle the list(say list1) what function do we use ?
a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)
d) random.shuffleList(list1)
View Answer
Answer:c
Explanation:Execute in the shell to verify .
8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct (multiple answers
allowed) ?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) print(list1[4:6])
View Answer
Answer:a, b, c, d
Explanation:Slicing is allowed in lists just as in the case of strings.
advertisements
9. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1] ?
a) Error
b) None
c) 25
d) 2
View Answer
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] b) Error
c) 25
d) [25, 14, 222, 33, 2] View Answer
Answer:a
Explanation:Execute in the shell to verify.
This set of Python Coding Interview Questions & Answers focuses on “Lists”.
1. What is the output when following code is executed ?
>>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
>>>print names[-1][-1]
a) A
b) Daman
c) Error
d) n
View Answer
Answer:d
Explanation:Execute in the shell to verify.
2. What is the output when following code is executed ?
names2[0] = 'Alice'
names3[1] = 'Bob'
sum = 0
for ls in (names1, names2, names3):
if ls[0] == 'Alice':
sum += 1
if ls[1] == 'Bob':
sum += 10
print sum
a) 11
b) 12
c) 21
d) 22
View Answer
Answer:b
Explanation:When assigning names1 to names2, we create a second reference to the same list.
Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3,
we are creating a full copy of names1 which can be modified independently.
advertisements
3. Suppose list1 is [1, 3, 2], What is list1 * 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] View Answer
Answer:c
Explanation:Execute in the shell and verify.
4. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is :
a) [0, 1, 2, 3] b) [0, 1, 2, 3, 4] c) [0.0, 0.5, 1.0, 1.5] d) [0.0, 0.5, 1.0, 1.5, 2.0] View Answer
Answer:c
Explanation:Execute in the shell to verify.
5. What is the output when following code is executed ?
Answer:b
Explanation:Elements are compared one by one.
6. To add a new element to a list we use which command ?
a) list1.add(5)
b) list1.append(5)
c) list1.addLast(5)
d) list1.addEnd(5)
View Answer
Answer:b
Explanation:We use the function append to add an element to the list.
advertisements
7. To insert 5 to the third position in list1, we use which command ?
a) list1.insert(3, 5)
b) list1.insert(2, 5)
c) list1.add(3, 5)
d) list1.append(3, 5)
View Answer
Answer:a
Explanation:Execute in the shell to verify.
8. To remove string “hello” from list1, we use which command ?
a) list1.remove(“hello”)
b) list1.remove(hello)
c) list1.removeAll(“hello”)
d) list1.removeOne(“hello”)
View Answer
Answer:a
Explanation:Execute in the shell to verify.
9. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5) ?
a) 0
b) 1
c) 4
d) 2
View Answer
Answer:d
Explanation:Execute help(list.index) to get details.
10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5) ?
a) 0
b) 4
c) 1
d) 2
View Answer
Answer:d
Explanation:Execute in the shell to verify.
Answer:d
Explanation:Execute in the shell to verify.
2. 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) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5] c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5] d)
[1, 3, 4, 5, 20, 5, 25, 3, 34, 5] View Answer
Answer:a
Explanation:Execute in the shell to verify.
3. 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]
View Answer
Answer:c
Explanation:pop() removes the element at the position specified in the parameter.
4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 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]
View Answer
Answer:a
Explanation:pop() by default will remove the last element.
5. What is the output when following code is executed ?
>>>"Welcome to Python".split()
a) [“Welcome”, “to”, “Python”] b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”
View Answer
Answer:a
Explanation:split() function returns the elements in a list.
6. What is the output when following code is executed ?
>>>list("a#b#c#d".split('#'))
advertisements
a) [‘a’, ‘b’, ‘c’, ‘d’] b) [‘a b c d’] c) [‘a#b#c#d’] d) [‘abcd’] View Answer
Answer:a
Explanation:Execute in the shell to verify.
7. What is the output when following code is executed ?
myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
>>>print(indexOfMax)
a) 1
b) 2
c) 3
d) 4
View Answer
Answer:a
Explanation:First time the highest number is encountered is at index 1.
8. What is the output when following code is executed ?
myList = [1, 2, 3, 4, 5, 6]
for i in range(1, 6):
myList[i - 1] = myList[i]
Answer:c
Explanation:Execute in the shell to verify.
advertisements
9. What is the output when following code is executed ?
>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)
a) [1, 3] b) [4, 3] c) [1, 4] d) [1, 3, 4] View Answer
def f(values):
values[0] = 44
v = [1, 2, 3]
f(v)
print(v)
a) [1, 44] b) [1, 2, 3, 44] c) [44, 2, 3] d) [1, 2, 3] View Answer
Answer:c
Explanation:Execute in the shell to verify.
This set of Python Programming Interview Questions & Answers focuses on “Lists”.
1. What will be the output?
f(1)
f(2)
v = f(3)
print(v)
a) [1] [2] [3] b) [1] [1, 2] [1, 2, 3] c) [1, 2, 3] d) 1 2 3
View Answer
Answer:c
Explanation:execute in the shell to verify
2. What will be the output?
if 'amir' in names1:
print 1
else:
print 2
a) None
b) 1
c) 2
d) Error
View Answer
Answer:c
Explanation:execute in the shell to verify.
3. What will be the output?
print names2[2][0]
a) None
b) a
c) b
d) c
View Answer
numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print len(numbers)
advertisements
a) 4
b) 5
c) 8
d) 12
View Answer
Answer:b
Explanation:a list is passed in append so the length is 5.
5. 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 Above
View Answer
Answer:d
Explanation:in can be used in all data structures.
6. What will be the output?
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
Answer:d
Explanation:+ appends all the elements individually into a new list.
7. What will be the output?
def addItem(listParam):
listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print len(mylist)
a) 1
b) 4
c) 5
d) 8
View Answer
Answer:c
Explanation:+ will append the element to the list.
8. What will be the output?
def increment_items(L, increment):
i=0
while i < len(L):
L[i] = L[i] + increment
i=i+1
values = [1, 2, 3]
print(increment_items(values, 2))
print(values)
advertisements
a) None
[3, 4, 5] b) None
[1, 2, 3] c) [3, 4, 5] [1, 2, 3] d) [3, 4, 5] None
View Answer
Answer:a
Explanation:Execute in the shell to verify.
9. What will be the output?
def example(L):
''' (list) -> list
'''
i=0
result = []
while i < len(L):
result.append(L[i])
i=i+3
return result
a) Return a list containing every third item from L starting at index 0.
b) Return an empty list
c) Return a list containing every third index from L starting at index 0.
d) Return a list containing the items from L starting from index 0, omitting every third item.
View Answer
Answer:a
Explanation:Run the code to get a better understanding with many arguements.
10. What will be the output?
Answer:a,b,c
Explanation:Dictionaries are created by specifying keys and values
2. What are the keys?
d = {"john":40, "peter":45}
a) “john”, 40, 45, and “peter”
b) “john” and “peter”
c) 40 and 45
d) d = (40:”john”, 45:”peter”)
View Answer
Answer:b
Explanation:Dictionaries appear in the form of keys and values.
3. What will be the output?
d = {"john":40, "peter":45}
"john" in d
a) True
b) False
c) None
d) Error
View Answer
Answer:a
Explanation:In can be used to check if the key is int dictionary.
4. What will be the output?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2
advertisements
a) True
b) False
c) None
d) Error
View Answer
Answer:b
Explanation:If d2 was initialized as d2 = d1 the answer would be true.
5. What will be the output?
d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
a) True
b) False
c) Error
d) None
View Answer
Answer:c
Explanation:Arithmetic > operator cannot be used with dictionaries.
6. What is the output?
d = {"john":40, "peter":45}
d["john"]
a) 40
b) 45
c) “john”
d) “peter”
View Answer
Answer:a
Explanation:Execute in the shell to verify.
advertisements
7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we
use
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”] d) del d(“john”:40)
View Answer
Answer:c
Explanation:Execute in the shell to verify.
8. Suppose d = {“john”:40, “peter”:45}, to obtain the number of entries in dictionary what
command do we use
a) d.size()
b) len(d)
c) size(d)
d) d.len()
View Answer
Answer:b
Explanation:Execute in the shell to verify.
9. What will be the output?
d = {"john":40, "peter":45}
print(list(d.keys()))
a) [“john”, “peter”] b) [“john”:40, “peter”:45] c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
View Answer
Answer:a
Explanation:Execute in the shell to verify.
10. Suppose d = “
{ john”:40,“peter” :45}, what happens when retieving a value using d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception.
b) It is executed fine and no exception is raised, and it returns None.
c) Since “susan” is not a key in the set, Python raises a KeyError exception.
d) Since “susan” is not a key in the set, Python raises a syntax error.
View Answer
Answer:c
Explanation:Execute in the shell to verify.
Answer:b
Explanation:Tuples are characterised by their round brackets.
2. 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))
View Answer
Answer:b
Explanation:Values cannot be modified in the case of tuple.
3. What will be the output?
>>>t=(1,2,4,3)
>>>t[1:3]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
View Answer
Answer:c
Explanation:Slicing just as in the case of strings takes place in tuples.
4. What will be the output?
>>>t=(1,2,4,3)
>>>t[1:-1]
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
View Answer
Answer:c
Explanation:Slicing just as in the case of strings takes place in tuples.
5. What will be the output?
>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]
advertisements
a) [2, 3, 9] b) [1, 2, 4, 3, 8, 9] c) [1, 4, 8] d) (1, 4, 8)
View Answer
Answer:c
Explanation:Execute in the shell to verify.
6. What will be the output?
d = {"john":40, "peter":45}
d["john"]
a) 40
b) 45
c) “john”
d) “peter”
View Answer
Answer:a
Explanation:Execute in the shell to verify.
7. What will be the output?
>>>t = (1, 2)
>>>2 * t
a) (1, 2, 1, 2)
b) [1, 2, 1, 2] c) (1, 1, 2, 2)
d) [1, 1, 2, 2] View Answer
Answer:a
Explanation:* operator concatenates tuple.
8. What will be the output?
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
advertisements
a) True
b) False
c) Error
d) None
View Answer
Answer:b
Explanation:Elements are compared one by one in this case.
9. What will be the output?
>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print len(my_tuple)
a) 1
b) 2
c) 5
d) Error
View Answer
Answer:d
Explanation:Tuples are immutable and don’t have an append method. An exception is thrown in
this case..
10. What will be the output?
numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
Answer:c
Explanation:Tuples can be used for keys into dictionary. The tuples can have mixed length and the
order of the items in the tuple is considered when comparing the equality of the keys
~
J
A
Y
G
O
O
D
Y
~
CARES...................... BEST OF LUCK IN YOUR TEST