Python quiz questions

Python interview questions

  • 1.

    Given a string strtext = “Welcome”, which of the following statement would yield TypeError?

    1. print (strtext[0])

    2. strtext[1]= ‘r’

    3. print (strtext.lower())

    4. None of these

    5. print(strtext.strip())

    Answer
  • 2.

    Fill out the missing part of the code to print the following patterns:

    1 1 1 1 1
    2 2 2 2 2
    3 3 3 3 3
    4 4 4 4 4
    5 5 5 5 5

    for n in range(1, 6, 1):
        print(____ * 5)
    1. str(n)

    2. 1

    3. 2

    4. n

    5. -1

    Answer
  • 3.

    A list is given below:

    test_list = [1,5,10,19,55,30,55,99]

    Which of the following code sequence would produce a new list like the [1,5,10,99]?

    1. test_list.pop(1) test_list.pop(3) test_list.pop(4) test_list.pop(6)

    2. test_list.pop(5) test_list.remove(19) test_list.remove(55) test_list.remove(55)

    3. test_list.remove(5) test_list.remove(19) test_list.remove(55)

    4. test_list.pop(5) test_list.pop(19) test_list.pop(55)

    5. None

    Answer
  • 4.

    Which of the following is the output of the below Python code?

    def f(value, values):
        v = 1
        values[0] = 44
    t = 3
    v = [1, 2, 3]
    f(t, v)
    print(t, v[0])
    1. 3 1

    2. 1 44

    3. 1 1

    4. 3 44

    5. None

    Answer
  • 5.

    The following code fragment reads two numbers. Which of the following is the valid input for the code in Python 3?

    num1, num2 = eval(input("Enter two numbers: "))
    1. None

    2. 1 2

    3. 1, 2

    4. "1 2"

    Answer
  • 6.

    Which of the following statments is correct regarding the below Python code?

    class A:
        def __init__(self):
            self.a = 1
            self.__b = 1
        def getY(self):
            return self.__b
    obj = A()
    obj.a = 45
    print(obj.a)
    1. The program has an error because '__b' is private and can't be accessed outside of the class.

    2. The program runs fine and will print 1.

    3. The program has an error because 'a' is private and can't be accessed outside of the class.

    4. The program has an error because you can't name a variable using '__b'.

    5. The program runs fine and will print 45.

    Answer
  • 7.

    Which of the following is the output of the below Python code?

    test_list = [1,2,3,None,(),[],]
    print(len(test_list))
    1. 6

    2. 5

    3. 4

    4. 7

    5. syntax error

    Answer
  • 8.

    Which of the following is the output of the below Python code?

    class A:
        def __init__(self):
            self.x = 1
            self.__y = 1
        def getY(self):
            return self.__y
    a = A()
    a.x = 45
    print(a.x)
    1. The program runs fine and prints 1.

    2. The program has an error because x is private and cannot be access outside of the class.

    3. The program has an error because y is private and cannot be access outside of the class.

    4. The program runs fine and prints 45.

    5. The program has an error because you cannot name a variable using __y.

    Answer
  • 9.

    Which of the following is true for the below statement?

    The time.time() returns .........
    1. the current time in seconds since the epoch.

    2. the current time.

    3. the current time in milliseconds.

    4. the current time in milliseconds since midnight, January 1, 1970.

    5. the current time in milliseconds since midnight, January 1, 1970 GMT (the Unix time).

    Answer
  • 10.

    Which of the following is the output of the below Python code?

    counter = 1 
    def doLotsOfStuff():
        global counter
        for i in (1, 2, 3): 
            counter += 1
    doLotsOfStuff()
    print (counter)
    1. 1

    2. None

    3. 4

    4. 7

    5. 3

    Answer
  • 11.

    The following code reads two numbers. Which of the following is the correct input for the code?

    x, y = eval(input("Enter two numbers: "))
    print(x)
    print(y)
    1. <pre>3 4</pre>

    2. None

    3. 3 4

    4. 3, 4

    5. 3,4,

    Answer
  • 12.

    Which of the following is the output of the following Python code?

    Nums = set ([1,1,2,3,3,3,4])
    print (len(Nums))
    1. 1

    2. 2

    3. 4

    4. 5

    5. 7

    Answer
  • 13.

    Which of the following is the output of the below Python code?

    class A:
        def __str__(self):
            return"A"
    class B(A):
        def __init__(self):
            super().__init__()
    class C(B):
        def __init__(self):
            super().__init__()
    def main():
        b = B()
        a = A()
        c = C()
        print(a, b, c)
    main()
    1. C C C

    2. A B C

    3. A A A

    4. B B B

    5. None

    Answer
  • 14.

    Which of the following functions immediately terminates the program?

    1. sys.halt()

    2. sys.exit()

    3. sys.terminate()

    4. None

    5. sys.stop()

    Answer
  • 15.

    Which of the following commands would you use to start Python from the command prompt?

    1. execute python

    2. run python

    3. python

    4. go python

    Answer
  • 16.

    What is your view on the following recursive function?

    def factorial(n):
          return n * factorial(n - 1)
    1. Invoking factorial(0) returns 0.

    2. Invoking factorial(2) returns 2.

    3. Invoking factorial(3) returns 6.

    4. Invoking factorial(1) returns 1.

    5. The function runs infinitely and causes a StackOverflowError.

    Answer
  • 17.

    If PYTHONPATH is set in the environment, which directories are searched for modules?

    A) PYTHONPATH directory
    B) Current directory
    C) Home directory
    D) Installation dependent default path
    1. A only

    2. A and D

    3. A, B and D

    4. A, B and C

    5. A, B, C and D

    Answer
  • 18.

    Which of the following is the output of the following Python code?

    d = {"python":40, "developer":45}
    print(list(d.keys()))
    1. ("python":40, "developer":45)

    2. ("python", "developer")

    3. ["python", "developer"]

    4. ["python":40, "developer":45]

    Answer
  • 19.

    Which of the following is the output of the below Python code fragment?

    def f1(x = 1, y = 2):
        x = x + y
        y += 1
        print(x, y)
    f1(y = 2, x = 1)
    1. 1 3

    2. 3 2

    3. 3 3

    4. The program has a runtime error because x and y are not defined.

    5. 2 3

    Answer
  • 20.

    Which of the following is the output of the below Python code?

    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)
    1. 0

    2. 4

    3. 1

    4. 2

    5. 3

    Answer

© 2017 QuizBucket.org