PE1 : Python Essentials 1 – Module 4 Test

  1. Which of the following lines properly starts a function using two parameters, both with zeroed default values?

    • def fun (a=b=0) :
    • def fun (a=0, b=0) :
    • fun fun (a=0, b) :
    • fun fun (a, b=0) :
  2. What is the output of the following snippet?

    def fun(x):
        if x % 2 == 0:
           return 1
        else:
            return
    print(fun(fun(2)) + 1)
    • 1
    • None
    • the code will cause a runtime error
    • 2
  3. Which of the following statement are true? (Select two answers)

    • The None value can be assigned to variables
    • The None value cannot be used outside functions
    • The​ None value can be used as an argument of arithmetic operators
    • The None value can be compared with variables
  4. The following snippet:

    def func_1(a):
        return a ** a
    def func_2(a):
        return func_1(a) * func_1(a)
    print(func_2(2))
    • will output 2
    • will output 16
    • will output 4
    • is erroneous
  5. What code would you insert instead of the comment to obtain the expected output?

    Expected output:

    a
    b
    c

    Code:

    dictionary = {}
    my_list = ['a', 'b', 'c', 'd']
    for i in range(len(my_list) - 1):
          dictionary[my_list[i]] = (my_list[i], )
    for i in sorted(dictionary.key()):
        k = dictionary[i]
        # Insert your code here.
    • print(k["0"])
    • print(k['0'])
    • print(k)
    • print(k[0])
  6. The following snippet:

    def func(a, b):
        return a ** a
    print(func(2))
    • is erroneous
    • will output 2
    • will output 4
    • will return None
  7. What is the output of thee following snippet?

    def fun(inp=2, out=3):
         return inp * out
    print(fn(out =2))
    • 2
    • 6
    • 4
    • the snippet is erroneous
  8. Assuming that my_tuple is a correctly created tuple, the fact that tuples are immutable means that the following instruction:

    my_tuple[1] = my_tuple[1] + my_tuple[0]
    • can be executed if and only if the tuple contains at least two elements
    • is illegal 
    • may be illegal if the tuple contains string 
    • is fully correct
  9. A function defined in the following way: (Select two answers)

    def function(x=0):
        return x
    • may be invoked with exactly one argument 
    • must be invoked with exactly one argument 
    • must be invoked without any argument
    • may be invoked without any argument
  10. What is the output o the following snippet?

    def fun(x):
        x += 1
        return x
    x = 2
    x = fun(x + 1)
    print(x)
    • 3
    • 5
    • the code erroneous 
    • 4
  11. What is the output of the following snippet?

    my_list = ['Mary', 'had', 'a', 'little', 'lamb']
    def my_list(my_list):
        del my_list[3]
        my_list[3] = 'ram'
    print(my_list(my_list))
    • [‘Mary’, ‘had’, ‘a’ ,’lamb’]
    • [‘Mary’, ‘had’, ‘a’ ,’ram’]
    • [‘Mary’, ‘had’, ‘a’ ,’little’, ‘lamb’]
    • no output, the snippet is erroneous
  12. What is the output of the following snippet?

    def f(x):
        if x == 0:
            return 0
        return x + f(x - 1)
    print(f(3))
    • 6
    • the code is erroneous 
    • 1
    • 3
  13. What is the output of the following snippet?

    tup = (1, 2, 4, 8)
    tup = tup[1:-1]
    tup = tup[0]
    print(tup)
    • the snippet is erroneous
    • (12)
    • (2, )
    • 2
  14. What is the output of the following snippet?

    def fun(x):
        global y
        y = x * x
        return y
    fun(2)
    print(y)
    • the code will cause  a runtime error
    • 4
    • None
    • 2
  15. What is the output of the following snippet?

    dictionary = {'one': 'two', 'three': 'one', 'two': 'three'}
    v = dictionary['one']
    for k in range(len(dictionary)):
        v = dictionary[v]
    print(v)
    • three
    • one
    • (‘one’, ‘two’, ‘three’)
    • two
  16. Select the true statements about the try-exception block in relation to the following example. (Select two answers.)

    try:
      # Some code is here...
    except:
      # some code is here...
    • if you suspect that a snippet may raise an exception, you should place it in the try block
    • The code that follows the try statement will be executed if the code in the except clause runs into an error.
    • The code that follows the except statement will be executed if the code in the try clause  runs into an error.
    • If there is a syntax error in code located in the try block, the except branch will not handle it, and a SyntaxError exception will be raised instead.
  17. Which one if the following lines properly starts a parameterless function definition?

    • def fun:
    • def fun():
    • function fun():
    • fun function():
  18. What is the output of the following snippet?

    def fun(x, y, x):
        return x + 2 * y + 3 * z
    print(fun(0, z=1, y=3))
    • 9
    • 0
    • 3
    • the snippet is erroneous
  19. What is the output of the following code?

    try:
      value = input("Enter a value: ")
      print(value/value)
    except:
      print("Bad input...")
    except ZeroDivisionError:
      print("Very bad input...")
    except TypeError:
      print("Very very bad input...")
    except:
      print("Booo!")
    • Booo!
    • Bad input...
    • Very very bad input...
    • Very bad input...
  20. What is the output of the following snippet?

    def ant():
       print(var + 1, end ='')
    var = 1
    any()
    print(var)
    • 12
    • 22
    • 11
    • 21
  21. The fact that tuples belong to sequence types means that:

    • they can be indexed and sliced like lists
    • they can be extended using the .append() method
    • they can be modified using the del instruction
    • they are actually lists
  22. A built-in function is a function which:

    • is hidden from programmers
    • has been placed within your code by another programmer
    • has to be imported before use
    • comes with Python, ans is an integer part of Python
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments