Question: 6 -
What is the data type of print(type(10))
-
integer
-
text
-
float
-
int
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)
-
None of these
-
1
-
76
-
Error
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)
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)
-
Error
-
25
55 -
25
50 -
25
25
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.
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
-
str1 = 'Ault\\'Kelly'
-
str1 = """Ault'Kelly"""
-
None of these
-
str1 = 'Ault\'Kelly'
Answer:
str1 = 'Ault\'Kelly'