Computer Science questions and answers
Computer Science questions and answers
KENNY
Answer: c
Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the
Netherlands.
Answer: b
Explanation: Case is always significant while dealing with identifiers in python.
4. Which of the following is the correct extension of the Python file?
a) .python
b) .pl
c) .py
d) .p
View Answer
Answer: c
Explanation: ‘.py’ is the correct extension of the Python file. Python programs can be written in
any text editor. To save these programs we need to save in files with file extension ‘.py’.
5. Is Python code compiled or interpreted?
a) Python code is both compiled and interpreted
b) Python code is neither compiled nor interpreted
c) Python code is only compiled
d) Python code is only interpreted
View Answer
Answer: a
Explanation: Many languages have been implemented using both compilers and interpreters,
including C, Pascal, and Python.
advertisement
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.
7. What will be the value of the following Python expression?
4+ 3%5
a) 7
b) 2
c) 4
d) 1
View Answer
Answer: a
Explanation: The order of precedence is: %, +. Hence the expression above, on simplification
results in 4 + 3 = 7. Hence the result is 7.
Answer: a
Explanation: In Python, to define a block of code we use indentation. Indentation refers to
whitespaces at the beginning of the line.
9. Which keyword is used for function in Python language?
a) Function
b) def
c) Fun
d) Define
View Answer
Answer: b
Explanation: The def keyword is used to create, (or define) a function in python.
10. Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
View Answer
Answer: b
Explanation: To write single-line comments in Python use the Hash character (#) at the beginning
of the line. It is also called number sign or pound sign. To write multi-line comments, close the text
between triple quotes.
Example: “”” comment
text “””
11. What will be the output of the following Python code?
i=1
while True:
if i%3 == 0:
break
print(i)
i+=1
a) 1 2 3
b) error
c) 1 2
d) none of the mentioned
View Answer
Answer: b
Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.
12. Which of the following functions can help us to find the version of python that we are currently
working on?
a) sys.version(1)
b) sys.version(0)
c) sys.version()
d) sys.version
View Answer
Answer: d
Explanation: The function sys.version can help us to find the version of python that we are
currently working on. It also contains information on the build number and compiler used. For
example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the
version.
13. Python supports the creation of anonymous functions at runtime, using a construct called
__________
a) pi
b) anonymous
c) lambda
d) none of the mentioned
View Answer
Answer: c
Explanation: Python supports the creation of anonymous functions (i.e. functions that are not
bound to a name) at runtime, using a construct called lambda. Lambda functions are restricted to a
single expression. They can be used wherever normal functions can be used.
14. What is the order of precedence in python?
a) Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
b) Exponential, Parentheses, Division, Multiplication, Addition, Subtraction
c) Parentheses, Exponential, Multiplication, Division, Subtraction, Addition
d) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
View Answer
Answer: d
Explanation: For order of precedence, just remember this PEMDAS (similar to BODMAS).
15. What will be the output of the following Python code snippet if x=1?
x<<2
a) 4
b) 2
c) 1
d) 8
View Answer
Answer: a
Explanation: The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise
left shift on x. This shift yields the value: 0100, which is the binary form of the number 4.
16. What does pip stand for python?
a) Pip Installs Python
b) Pip Installs Packages
c) Preferred Installer Program
d) All of the mentioned
View Answer
Answer: c
Explanation: pip is a package manager for python. Which is also called Preferred Installer Program.
Answer: b
Explanation: Variable names can be of any length.
18. What are the values of the following Python expressions?
2**(3**2)
(2**3)**2
2**3**2
a) 512, 64, 512
b) 512, 512, 512
c) 64, 512, 64
d) 64, 64, 64
View Answer
Answer: a
Explanation: Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as
8**2, which is equal to 64. The last expression is evaluated as 2**(3**2). This is because the
associativity of ** operator is from right to left. Hence the result of the third expression is 512.
19. Which of the following is the truncation division operator in Python?
a) |
b) //
c) /
d) %
View Answer
Answer: b
Explanation: // is the operator for truncation division. It is called so because it returns only the
integer part of the quotient, truncating the decimal part. For example: 20//3 = 6.
Answer: c
Explanation: The code shown above returns a new list containing only those elements of the list l
which do not amount to zero. Hence the output is: [1, 2, ‘hello’].
Answer: b
Explanation: The function seed is a function which is present in the random module. The functions
sqrt and factorial are a part of the math module. The print function is a built-in function which
prints a value directly to the system output.
Answer: b
Explanation: Each object in Python has a unique id. The id() function returns the object’s id.
23. The following python program can work with ____ parameters.
def f(x):
def f1(*args, **kwargs):
print("Sanfoundry")
return x(*args, **kwargs)
return f1
a) any number of
b) 0
c) 1
d) 2
View Answer
Answer: a
Explanation: The code shown above shows a general decorator which can work with any number of
arguments.
Answer: d
Explanation: The function max() is being used to find the maximum value from among -3, -4 and
false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0
(false).
25. Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
View Answer
Answer: c
Explanation: Class is a user-defined data type.
26. What will be the output of the following Python expression if x=56.236?
print("%.2f"%x)
a) 56.236
b) 56.23
c) 56.0000
d) 56.24
View Answer
Answer: d
Explanation: The expression shown above rounds off the given number to the number of decimal
places specified. Since the expression given specifies rounding off to two decimal places, the
output of this expression will be 56.24. Had the value been x=56.234 (last digit being any number
less than 5), the output would have been 56.23.
Answer: b
Explanation: A folder of python programs is called as a package of modules.
28. What will be the output of the following Python function?
len(["hello",2, 4, 6])
a) Error
b) 6
c) 4
d) 3
View Answer
Answer: c
Explanation: The function len() returns the length of the number of elements in the iterable.
Therefore the output of the function shown above is 4.
a
B
C
D
b) a b c d
c) error
d)
A
B
C
D
View Answer
Answer: d
Explanation: The instance of the string returned by upper() is being printed.
30. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the built-in namespace, then the global namespace and finally the local
namespace
b) Python first searches the built-in namespace, then the local namespace and finally the global
namespace
c) Python first searches the local namespace, then the global namespace and finally the built-in
namespace
d) Python first searches the global namespace, then the local namespace and finally the built-in
namespace
View Answer
Answer: c
Explanation: Python first searches for the local, then the global and finally the built-in namespace.
31. What will be the output of the following Python code snippet?
Answer: a
Explanation: [::-1] reverses the list.
>>>"a"+"bc"
a) bc
b) abc
c) a
d) bca
View Answer
Answer: b
Explanation: + operator is concatenation operator.
33. Which function is called when the following Python program is executed?
f = foo()
format(f)
a) str()
b) format()
c) __str__()
d) __format__()
View Answer
Answer: c
Explanation: Both str(f) and format(f) call f.__str__().
Answer: b
Explanation: eval can be used as a variable.
class tester:
def __init__(self, id):
self.id = str(id)
id="224"
>>>temp = tester(12)
>>>print(temp.id)
a) 12
b) 224
c) None
d) Error
View Answer
Answer: a
Explanation: Id in this case will be the attribute of the instance.
def foo(x):
x[0] = ['def']
x[1] = ['abc']
return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))
a) Error
b) None
c) False
d) True
View Answer
Answer: d
Explanation: The same object is modified in the function.
37. Which module in the python standard library parses options received from the command line?
a) getarg
b) getopt
c) main
d) os
View Answer
Answer: b
Explanation: getopt parses options received from the command line.
z=set('abc')
z.add('san')
z.update(set(['p', 'q']))
z
a) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’}
b) {‘abc’, ‘p’, ‘q’, ‘san’}
c) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
d) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}
View Answer
Answer: c
Explanation: The code shown first adds the element ‘san’ to the set z. The set z is then updated
and two more elements, namely, ‘p’ and ‘q’ are added to it. Hence the output is: {‘a’, ‘b’, ‘c’,
‘p’, ‘q’, ‘san’}
Answer: b
Explanation: + is used to concatenate and * is used to multiply strings.
print("abc. DEF".capitalize())
a) Abc. def
b) abc. def
c) Abc. Def
d) ABC. DEF
View Answer
Answer: a
Explanation: The first letter of the string is converted to uppercase and the others are converted to
lowercase.
41. Which of the following statements is used to create an empty set in Python?
a) ( )
b) [ ]
c) { }
d) set()
View Answer
Answer: d
Explanation: { } creates a dictionary not a set. Only set() creates an empty set.
list1 = [1,2,3,4]
list2 = [2,4,5,6]
list3 = [2,6,7,8]
result = list()
result.extend(i for i in list1 if i not in (list2+list3) and i not in result)
result.extend(i for i in list2 if i not in (list1+list3) and i not in result)
result.extend(i for i in list3 if i not in (list1+list2) and i not in result)
a) [1, 3, 5, 7, 8]
b) [1, 7, 8]
c) [1, 2, 4, 7, 8]
d) error
View Answer
Answer: a
Explanation: Here, ‘ result ’ is a list which is extending three times. When first time ‘ extend ’
function is called for ‘result’, the inner code generates a generator object, which is further used in
‘extend’ function. This generator object contains the values which are in ‘list1’ only (not in ‘list2’
and ‘list3’).
Same is happening in second and third call of ‘extend’ function in these generator object contains
values only in ‘list2’ and ‘list3’ respectively.
So, ‘result’ variable will contain elements which are only in one list (not more than 1 list).
Answer: c
Explanation: We use the function append to add an element to the list.
Answer: b
Explanation: Padding is done towards the right-hand-side first when the final string is of even
length.
>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)
a) [1, 4]
b) [1, 3, 4]
c) [4, 3]
d) [1, 3]
View Answer
Answer: c
Explanation: Lists should be copied by executing [:] operation.
Answer: c
Explanation: Functions are reusable pieces of programs. They allow you to give a name to a block
of statements, allowing you to run that block using the specified name anywhere in your program
and any number of times
47. Which of the following Python statements will result in the output: 6?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
a) A[2][1]
b) A[1][2]
c) A[3][2]
d) A[2][3]
View Answer
Answer: b
Explanation: The output that is required is 6, that is, row 2, item 3. This position is represented by
the statement: A[1][2].
Answer: d
Explanation: Identifiers can be of any length.
i=0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
a) error
b) 0 1 2 0
c) 0 1 2
d) none of the mentioned
View Answer
Answer:
Explanation: The else part is not executed if control breaks out of the loop.
x = 'abcd'
for i in range(len(x)):
print(i)
a) error
b) 1 2 3 4
c) a b c d
d) 0 1 2 3
View Answer
Answer: d
Explanation: i takes values 0, 1, 2 and 3.
Answer: c
Explanation: Built-in functions and user defined ones. The built-in functions are part of the Python
language. Examples are: dir(), len() or abs(). The user defined functions are functions created with
the def keyword.
def addItem(listParam):
listParam += [1]
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
a) 5
b) 8
c) 2
d) 1
View Answer
Answer: a
Explanation: + will append the element to the list.
Answer: d
Explanation: Tuples are represented with round brackets.
54. What will be the output of the following Python code snippet?
z=set('abc$de')
'a' in z
a) Error
b) True
c) False
d) No output
View Answer
Answer: b
Explanation: The code shown above is used to check whether a particular item is a part of a given
set or not. Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an
error in the absence of the quotes.
round(4.576)
a) 4
b) 4.6
c) 5
d) 4.5
View Answer
Answer: c
Explanation: This is a built-in function which rounds a number to give precision in decimal digits.
In the above case, since the number of decimal places has not been specified, the decimal number
is rounded off to a whole number. Hence the output will be 5.
Answer: d
Explanation: Python has a nifty feature called documentation strings, usually referred to by its
shorter name docstrings. DocStrings are an important tool that you should make use of since it
helps to document the program better and makes it easier to understand.
Answer: c
Explanation: The elements of the tuple are accessed by their indices.
Answer: a
Explanation: math.pow() returns a floating point number.
Answer: b
Explanation: Each object in Python has a unique id. The id() function returns the object’s id.
Answer: d
Explanation: (element,) is not the same as element. It is a tuple with one item.
61. The process of pickling in Python includes ____________
a) conversion of a Python object hierarchy into byte stream
b) conversion of a datatable into a list
c) conversion of a byte stream into Python object hierarchy
d) conversion of a list into a datatable
View Answer
Answer: a
Explanation: Pickling is the process of serializing a Python object, that is, conversion of a Python
object hierarchy into a byte stream.
Answer: b
Explanation: In python, power operator is x**y i.e. 2**3=8.
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.
Answer: a
Explanation: For order of precedence, just remember this PEMDAS (similar to BODMAS).
Subscribe Now: Python Newsletter | Important Subjects Newsletters.
64. What is the answer to this expression, 22 % 3 is?
a) 7
b) 1
c) 0
d) 5
View Answer
Answer: b
Explanation: Modulus operator gives the remainder. So, 22%3 gives the remainder, that is, 1.
Answer: b
Explanation: You can’t perform mathematical operation on string even if the string is in the form:
‘1234…’.
Become Top Ranker in Python Programming Now!
66. Operators with the same precedence are evaluated in which manner?
a) Left to Right
b) Right to Left
c) Can’t say
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
Answer: c
Explanation: First this expression will solve 1**3 because exponential has higher precedence than
multiplication, so 1**3 = 1 and 3*1 = 3. Final answer is 3.
68. Which one of the following has the same precedence level?
a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication
View Answer
Answer: a
Explanation: “ Addition and Subtraction ” are at the same precedence level. Similarly,
“ Multiplication and Division ” are at the same precedence level. However, Multiplication and
Division operators are at a higher precedence level than Addition and Subtraction operators.
69. The expression Int(x) implies that the variable x is converted to integer.
a) True
b) False
View Answer
Answer: a
Explanation: Int(x) converts the datatype of the variable to integer and is the example of explicit
data conversion.
70. Which one of the following has the highest precedence in the expression?
a) Exponential
b) Addition
c) Multiplication
d) Parentheses
View Answer
Answer: d
Explanation: Just remember: PEMDAS, that is, Parenthesis, Exponentiation, Division,
Multiplication, Addition, Subtraction. Note that the precedence order of Division and
Multiplication is the same. Likewise, the order of Addition and Subtraction is also the same.
Answer: a
Explanation: Case is always significant while dealing with identifiers in python.
Answer: d
Explanation: Identifiers can be of any length.
Answer: d
Explanation: All the statements will execute successfully but at the cost of reduced readability.
Answer: b
Explanation: Variable names should not start with a number.
75. 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.
Answer: a
Explanation: eval can be used as a variable.
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.
Answer: a
Explanation: Variable names can be of any length.
Answer: b
Explanation: Spaces are not allowed in variable names.
Answer: d
Explanation: Class is a user defined data type.
82. Given a function that does not return any value, What value is thrown by 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.
Answer: a
Explanation: We are printing only the 1st two bytes of string and hence the answer is “he”.
Answer: a
Explanation: Execute help(id) to find out details in python shell.id returns a integer value that is
unique.
86. 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
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) All of the mentioned
View Answer
Answer: d
Explanation: // is integer operation in python 3.0 and int(..) is a type cast operator.
advertisement
87. What error occurs when you execute the following Python code snippet?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError
View Answer
Answer: b
Explanation: Mango is not defined hence name error.
88. What will be the output of the following Python code snippet?
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.
Answer: a
Explanation: List data type can store any values within it.
90. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary
Answer: d
Explanation: Dictionary stores values in terms of keys and values.
tom
dick
harry
a)print('''tom\ndick\nharry''')
b) print(”’tomdickharry”’)
c) print(‘tom\ndick\nharry’)
d)print('tomdickharry')
View Answer
Answer: c
Explanation: The \n adds a new line.
93. What is the average value of the following Python code snippet?
>>>grade1 = 80
>>>grade2 = 90
>>>average = (grade1 + grade2) / 2
a) 85.0
b) 85.1
c) 95.0
d) 95.1
View Answer
Answer: a
Explanation: Cause a decimal value of 0 to appear as output.
Answer: c
Explanation: Execute in the shell.
Answer: b
Explanation: Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors
from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and
0.3.
Answer: c
Explanation: l (or L) stands for long.
Answer: c
Explanation: Infinity is a special case of floating point numbers. It can be obtained by float(‘inf’).
Answer: a
Explanation: ~x is equivalent to -(x+1).
Answer: a
Explanation: ~x is equivalent to -(x+1).
~~x = – (-(x+1) + 1) = (x+1) – 1 = x
~~x is equivalent to x
Extrapolating further ~~~~~~x would be same as x in the final result.
In the question, x value is given as 5 and “ ~ ” is repeated 6 times. So, the correct answer for
“~~~~~~5” is 5.
Answer: d
Explanation: Numbers starting with a 0 are octal numbers but 9 isn’t allowed in octal numbers.
Answer: a
Explanation: cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.
Answer: d
Explanation: The behavior of the round() function is different in Python 2 and Python 3. In Python
2, it 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 whereas in Python 3, it rounds off numbers towards nearest
even number when the number to be rounded off is exactly halfway through. See the below output.
i=1
while True:
print(i,end='')
i +=1
A) 12
B) 123
C) 1234
D) 124
Answer: A
T=1
while T:
print(True)
break
A) False
B) True
C) 0
D) no output
Answer: B
if <condition>_
statements-block 1
else:
statements-block 2
A) ;
B) :
C) ::
D) !
Answer: B
Answer: b
Explanation: Tuples are represented with round brackets.
Answer: c
Explanation: Slicing in tuples takes place just as it does in strings.
>>>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 in tuples takes place just as it does in strings.
Answer: c
Explanation: Execute in the shell to verify.
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.
>>>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.
>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2
a) True
b) False
c) Error
d) None
View Answer
Answer: b
Explanation: Elements are compared one by one in this case.
>>>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.
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.
Answer: b
Explanation: A tuple of one element must be created as (1,).
>>> 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
View Answer
Answer: c
Explanation: Tuple doesn’t have any update() attribute because it is immutable.
>>> a=[(2,4),(1,2),(3,9)]
>>> a.sort()
>>> 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
View Answer
Answer: a
Explanation: A list of tuples is a list itself. Hence items of a list can be sorted.
Answer: d
Explanation: A set is a mutable data type with non-duplicate, unordered values, providing the usual
mathematical set operations.
129. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}
View Answer
Answer: a
Explanation: The argument given for the set must be an iterable.
Answer: c
Explanation: A set doesn’t have duplicate items.
a = [5,5,6,7,7,7]
b = set(a)
def test(lst):
if lst in b:
return 1
else:
return 0
for i in filter(test, a):
print(i,end=" ")
a) 5 5 6
b) 5 6 7
c) 5 5 6 7 7 7
d) 5 6 7 7 7
View Answer
Answer: c
Explanation: The filter function will return all the values from list a which are true when passed to
function test. Since all the members of the set are non-duplicate members of the list, all of the
values will return true. Hence all the values in the list are printed.
Answer: b
Explanation: { } creates a dictionary not a set. Only set() creates an empty set.
>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
a) {1,2}
b) True
c) False
d) Invalid operation
View Answer
Answer: b
Explanation: a<b returns True if a is a proper subset of b.
Answer: d
Explanation: The members of a set can be accessed by their index values since the elements of the
set are unordered.
Answer: b
Explanation: There exists add method for set data type. However 5 isn’t added again as set consists
of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.
136. What will be the output of the following Python code?
>>> a={4,5,6}
>>> b={2,8,6}
>>> a+b
a) {4,5,6,2,8}
b) {4,5,6,2,8,6}
c) Error as unsupported operand type for sets
d) Error as the duplicate item 6 is present in both sets
View Answer
Answer: c
Explanation: Execute in python shell to verify.
>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b
a) {4,5}
b) {6}
c) Error as unsupported operand type for set data type
d) Error as the duplicate item 6 is present in both sets
View Answer
Answer: a
Explanation: – operator gives the set of elements in set a but not in set b.
>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b
a) {5,6,7,8,10,11}
b) {7,8}
c) Error as unsupported operand type of set data type
d) {5,6,10,11}
View Answer
Answer: d
Explanation: ^ operator returns a set of elements in set A or set B, but not in both (symmetric
difference).
Answer: a
Explanation: The multiplication operator isn’t valid for the set data type.
>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b
a) True
b) False
View Answer
Answer: a
Explanation: It is possible to compare two sets and the order of elements in both the sets doesn’t
matter if the values of the elements are the same.
>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b
a) Invalid operation
b) {3, 4, 5, 6, 7}
c) {5}
d) {3,4,6,7}
View Answer
Answer: b
Explanation: The operation in the above piece of code is union operation. This operation produces a
set of elements in both set a and set b.
a={3,4,{7,5}}
print(a[2][0])
a) Yes, 7 is printed
b) Error, elements of a set can’t be printed
c) Error, subsets aren’t allowed
d) Yes, {7,5} is printed
View Answer
Answer: c
Explanation: In python, elements of a set must not be mutable and sets are mutable. Thus, subsets
can’t exist.
Answer: b
Explanation: Book title capitalization is a phenomenon in which the first characters of the major
words in the sentence are capitalized. A button’s caption is entered using this phenomenon
144. The text contained in the identifying label is entered using __________
a) Sentence capitalization
b) Book title capitalization
c) Character capitalization
d) Word capitalization
View Answer
Answer: a
Explanation: Sentence capitalization is a phenomenon in which the first character of every word in
the sentence should be in capital letter. The text contained in is entered using this phenomenon.
145. Each button has same height and width because they are __________ the interface.
a) Stacked
b) Queued
c) Clustered
d) Heaped
View Answer
Answer: a
Explanation: Buttons have a same height and width because they are stacked in the interface. This
property mainly copies the button, thus they get equal height and width as that of the first, which is
made with accuracy.
Answer: b
Explanation: The most important point the designers should keep in mind is that the graphics
should not distract the user from his/her work. Thus the graphics should be simple enough to
emphasize a portion of the screen, mainly the top left corner.
147. An object’s __________ is used to display the type, style, and size of the font, used to display
the object’s text.
a) Font property
b) Size property
c) Text property
d) Style property
View Answer
Answer: a
Explanation: An object’s font property is used to display the type, style and size of font. The type of
the font includes- regular, bold, italics or underlined. The size is 8, 10, 14, etc and the style is
‘Times new Roman’, ‘Sans Serif’ etc.
Answer: a
Explanation: Designers generally avoid using italics and underline in a text, because they make
difficult to read; since in some font style italics is too difficult to read, and underline in some font
style looks as if an error has occurred, thus distracting the user.
149. Designers generally use __________ font style and __________ font size in an interface.
a) One, two
b) Two, one
c) Four, one
d) One, one
View Answer
Answer: a
Explanation: Designers generally use one font style in an interface because too many font sizes
make difficult to read, and distract the user. Generally, two font sizes are used one for the titles and
headings and other for the other text in the user interface.
Answer: c
Explanation: Designers limit the use of bold text to titles, headings and key items to emphasize the
text.
151. Designers generally use __________ colour for the background, and __________ for the text.
a) Blue, pink
b) White, black
c) Black, white
d) Pink, blue
View Answer
Answer: b
Explanation: Designers generally use white colour for the background and black colour for the text,
because dark colour as background is hard for eyes and light colour as text may appear blurry.
152. Designers at times use colour as the means of __________ a user interface.
a) Identification
b) Charming
c) Fascinating
d) Exquisite
View Answer
Answer: a
Explanation: Designers at times want to identify a special element, using colour. But it should not
be the only means to identify an element. Each element should have an identifying level
153. A user interact with nearly all aspects of GUI with the aid of ____
(a) scanner
(b) mouse
(c) light pen
(d) joystick.
Answer: b
154. Mouse is the pointing device that interacts with nearly all aspects of the GUI. Also, keyboard.
Answer: A
Answer: b
Explanation: Execute help(open) to get more details.
Answer: a
Explanation: a is used to indicate that data is to be appended.
Answer: d
Explanation: The program will throw an error.
159. To read two characters from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: a
Explanation: Execute in the shell to verify.
160. To read the entire remaining contents of the file as a string from a file object infile, we use
____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: b
Explanation: read function is used to read all the lines in a file.
f = None
for i in range (5):
with open("data.txt", "w") as f:
if i > 2:
break
print(f.closed)
a) True
b) False
c) None
d) Error
View Answer
Answer: a
Explanation: The WITH statement when used with open file guarantees that the file object is closed
when the with block exits.
162. To read the next line of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: c
Explanation: Execute in the shell to verify.
9. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: d
Explanation: Execute in the shell to verify.
Answer: b
Explanation: Every line is stored in a list and returned.
164. To read the next line of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: c
Explanation: Execute in the shell to verify.
165. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: d
Explanation: Execute in the shell to verify
Answer: b
Explanation: Every line is stored in a list and returned.
Answer: a
Explanation: Sets the file’s current position at the offset. The method seek() sets the file’s current
position at the offset.
Following is the syntax for seek() method:
fileObject.seek(offset[, whence])
Parameters
offset — This is the position of the read/write pointer within the file.
whence — This is optional and defaults to 0 which means absolute file positioning, other values
are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.
Answer: a
Explanation: The method truncate() truncates the file size. Following is the syntax for truncate()
method:
fileObject.truncate( [ size ])
Parameters
size — If this optional argument is present, the file is truncated to (at most) that size.
Answer: a
Explanation: remove(file_name)
6. What is the current syntax of rename() a file?
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned
View Answer
Answer: a
Explanation: This is the correct syntax which has shown below.
rename(current_file_name, new_file_name)
Answer: d
Answer:d
Explanation: Mode Meaning is as explained below:
r Reading
w Writing
a Appending
b Binary data + Updating.
171. What is the pickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned
View Answer
Answer: a
. What is the correct syntax of open() function?
a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned
View Answer
Answer: b
Answer: a
173. Which feature in OOP is used to allocate additional functions to a predefined operator in any
language?
a) Function Overloading
b) Function Overriding
c) Operator Overloading
d) Operator Overriding
View Answer
Answer: c
Explanation: The feature is operator overloading. There is not a feature named operator overriding
specifically. Function overloading and overriding doesn’t give addition function to any operator.
Answer: b
Explanation: The term “ module ” refers to the implementation of specific functionality to be
incorporated into a program.
Answer: c
Explanation: The total size of the program remains the same regardless of whether modules are
used or not. Modules simply divide the program.
177. Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity
View Answer
Answer: a
Explanation: Program code making use of a given module is called the client of the module. There
may be multiple clients for a module.
178. ______ is a string literal denoted by triple quotes for providing the specifications of certain
program elements.
a) Interface
b) Modularity
c) Client
d) Docstring
View Answer
Answer: d
Explanation: Docstring used for providing the specifications of program elements.
Answer: c
Explanation: Top-down design is an approach for deriving a modular design in which the overall
design.
Participate in Python Programming Certification Contest of the Month Now!
180. In top-down design every module is broken into same number of submodules.
a) True
b) False
View Answer
Answer: b
Explanation: In top-down design every module can even be broken down into different number of
submodules.
Answer: b
Explanation: The details of the program can be addressed before the overall design too. Hence, all
modular designs are not because of a top-down design process
#mod1
def change(a):
b=[x*2 for x in a]
print(b)
#mod2
def change(a):
b=[x*x for x in a]
print(b)
from mod1 import change
from mod2 import change
#main
s=[1,2,3]
change(s)
a) [2,4,6]
b) [1,4,9]
c)[2,4,6][1,4,9]
d) There is a name clash
View Answer
Answer: d
Explanation: A name clash is when two different entities with the same identifier become part of
the same scope. Since both the modules have the same function name, there is a name clash.
Answer: d
Explanation: Main modules are not meant to be imported into other modules.
Answer: b
Explanation: During a Python program execution, there are as many as three namespaces –
built-in namespace, global namespace and local namespace.
185. Which of the following is false about “import modulename” form of import?
a) The namespace of imported module becomes part of importing module
b) This form of import prevents name clash
c) The namespace of imported module becomes available to importing module
d) The identifiers in module are accessed as: modulename.identifier
View Answer
Answer: a
Explanation: In the “import modulename” form of import, the namespace of imported module
becomes available to, but not part of, the importing module.
Answer: b
Explanation: In the “from-import” form of import, there may be name clashes because names of
the imported identifiers aren’t specified along with the module name.
Answer: c
Explanation: In the “from-import” form of import, identifiers beginning with two underscores are
private and aren’t imported.
Answer: d
Explanation: In the “from-import” form of import, the imported identifiers (in this case factorial())
aren’t specified along with the module name.
189. What is the order of namespaces in which Python looks for an identifier?
a) Python first searches the global namespace, then the local namespace and finally the built-in
namespace
b) Python first searches the local namespace, then the global namespace and finally the built-in
namespace
c) Python first searches the built-in namespace, then the global namespace and finally the local
namespace
d) Python first searches the built-in namespace, then the local namespace and finally the global
namespace
View Answer
Answer: b
Explanation: Python first searches for the local, then the global and finally the built-in namespace.
Any text file with the .py extension containing Python code is basically a module. Different Python
objects such as functions, classes, variables, constants, etc., defined in one module can be made
available to an interpreter session or another Python script by using the import statement. Learn
modules in Python.
Others are modules in the standard library. Pillow is a third-party imaging library that needs to be
installed explicitly. Learn built-in modules in Python.
The standard reference implementation of Python is written in C. Hence, the built-in modules are
integrated with the Python interpreter because they are written in C.
193. What will print (randrange(1,10)) statement do?
a)Prints numbers from 1 to 10
b)Print numbers 1 to 10 in random order
c)Prints any one number from 1 to 9 selected randomly
d)Print numbers from 1 to 9
Answer: C
The randrange() function returns one random number from the list.
def getcol(x):
for i in range(x):
yield i
a)Void Function
b)Generator function
c)Iterator function
d)Yield function
Answer: B
Python provides a generator to create your own iterator function. A generator is a special type of
function which does not return a single value; instead, it returns an iterator object with a sequence
of values. In a generator function, a yield statement is used rather than a return statement. The
following is a simple generator function. Learn generator function.
The dir() function is different from the dir command in windows. It returns the list of attributes and
functions in a module.
The other three options are module attributes returning the name of .py file, name, and path of
module and dictionary of attributes, functions, and other definitions with values, respectively.
The dir() function is different from the dir command in windows. It returns the list of attributes and
functions in a module
For more info
08102585379