Quiz: Variables and Data Types

To View Tricks: Login Required

Number of Questions: 9

Question: 6 -

What is the data type of print(type(10))

Options:
  1. integer

  2. text

  3. float

  4. int

  5. Answer:

    int

    Solution not available.

Question: 7 -

What is the output of the following variable assignment?

x = 75
def myfunc():
    x = x + 1
    print(x)

myfunc()
print(x)

Options:
  1. None of these

  2. 1

  3. 76

  4. Error

  5. Answer:

    Error

    Solution:

    Here we have not used a global keyword to reassign a new value to global variable x into myfunc() so Python assumes that x is a local variable.

    It means you are accessing a local variable before defining it. that is why you received a UnboundLocalError: local variable 'x' referenced before assignment

    The correct way to modify the global variable inside a function:

    x = 75
    
    def myfunc():
        global x
        x = x + 1
        print(x)
        
    myfunc()
    print(x)


Question: 8 -

What is the output of the following code?

x = 50
def fun1():
    x = 25
    print(x)
    
fun1()
print(x)

Options:
  1. Error

  2. 25
    55

  3. 25
    50

  4. 25
    25

  5. Answer:

    25
    50

    Solution:

    A variable declared outside of all functions has a GLOBAL SCOPE. Thus, it is accessible throughout the file. And variable declared inside a function is a local variable whose scope is limited to its function.


Question: 9 -

Select the right way to create a string literal Ault'Kelly

Options:
  1. str1 = 'Ault\\'Kelly'

  2. str1 = """Ault'Kelly"""

  3. None of these

  4. str1 = 'Ault\'Kelly'

  5. Answer:

    str1 = 'Ault\'Kelly'

    Solution not available.