Quiz: Variables and Data Types

To View Tricks: Login Required

Number of Questions: 9

Question: 1 -

What is the data type of the following

aTuple = (1, 'Jhon', 1+3j)
print(type(aTuple[2:3]))

Options:
  1. complex

  2. list

  3. int

  4. tuple

  5. Answer:

    tuple

    Solution:

    When we access a tuple using the subscript atuple[start : end] operator, it will always return a tuple. We also call it tuple slicing. (taking a subset of a tuple using the range of indexes).


Question: 2 -

What is the data type of print(type(0xFF))

Options:
  1. hex

  2. hexint

  3. number

  4. int

  5. Answer:

    int

    Solution:

    We can represent integers in binary, octal and hexadecimal formats.

    • 0b or 0B for Binary and base is 2
    • 0o or 0O for Octal and base is 8
    • 0x or 0X for Hexadecimal and base is 16


Question: 3 -

Please select the correct expression to reassign a global variable “x” to 20 inside a function fun1()

x = 50
def fun1():
    # your code to assign global x = 20
fun1()
print(x) # it should print 20

Options:
  1. global x =20

     

  2. global x
    x = 20
  3. global var x
    x = 20
  4. global.x = 20

  5. Answer:
    global x
    x = 20
    Solution not available.

Question: 4 -

What is the output of the following code

def func1():
    x = 50
    return x
func1()
print(x)

Options:
  1. 0

  2. None

  3. NameError

  4. 50

  5. Answer:

    NameError

    Solution:

    You will get a NameError: name 'x' is not defined. To access the function’s return value we must accept it using an assignment operator like this

    def myfunc():
        x = 50
        return x
    x = myfunc()
    print(x)


Question: 5 -

In Python 3, what is the output of type(range(5)). (What data type it will return).

Options:
  1. range

  2. int

  3. list

  4. None

  5. Answer:

    range

    Solution not available.