List and string are different MCQ

Here are 1000 MCQs on Python [Chapterwise].

1. Who developed Python Programming Language? a] Wick van Rossum b] Rasmus Lerdorf c] Guido van Rossum d] Niene Stom

View Answer

Answer: c
Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the Netherlands.

2. Which type of Programming does Python support? a] object-oriented programming b] structured programming c] functional programming d] all of the mentioned

View Answer

Answer: d
Explanation: Python is an interpreted programming language, which supports object-oriented, structured, and functional programming.

3. Is Python case sensitive when dealing with identifiers? a] no b] yes c] machine dependent d] none of the mentioned

View Answer

Answer: a
Explanation: Case is always significant.

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: b
Explanation: Many languages have been implemented using both compilers and interpreters, including C, Pascal, and Python.

6. All keywords in Python are in _________ a] Capitalized b] lower case c] UPPER CASE d] None of the mentioned

View Answer

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?

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.

8. Which of the following is used to define a block of code in Python language? a] Indentation b] Key c] Brackets d] All of the mentioned

View Answer

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: None.

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 Numero sign [#] at the beginning of the line. 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: a
Explanation: The function sys.version can help us to find the version of python that we are currently working on. 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?

a] 4 b] 2 c] 1 d] 8

View Answer

Answer: a
Explanation: The binary form of 1 is 0001. The expression x>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 class.

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

    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.

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

    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’}

    39. What arithmetic operators cannot be used with strings in Python? a] * b] – c] + d] All of the mentioned

    View Answer

    Answer: b
    Explanation: + is used to concatenate and * is used to multiply strings.

    40. 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

    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.

    42. What will be the value of ‘result’ in following Python program?

    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].

    43. To add a new element to a list we use which Python command? a] list1.addEnd[5] b] list1.addLast[5] c] list1.append[5] d] list1.add[5]

    View Answer

    Answer: c
    Explanation: We use the function append to add an element to the list.

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

    print['*', "abcde".center[6], '*', sep='']

    a] * abcde * b] *abcde * c] * abcde* d] * abcde *

    View Answer

    Answer: b
    Explanation: Padding is done towards the right-hand-side first when the final string is of even length.

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

    1. >>>list1 = [1, 3]
    2. >>>list2 = list1
    3. >>>list1[0] = 4
    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.

    46. Which one of the following is the use of function in python? a] Functions don’t provide better modularity for your application b] you can’t also create your own functions c] Functions are reusable pieces of programs d] All of the mentioned

    View Answer

    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].

    48. What is the maximum possible length of an identifier in Python? a] 79 characters b] 31 characters c] 63 characters d] none of the mentioned

    View Answer

    Answer: d
    Explanation: Identifiers can be of any length.

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

    i = 0 while i

    Chủ Đề