Computer Science
What will be the output of the following code ?
c = 10
def add():
global c
c = c + 2
print(c, end = '#')
add()
c = 15
print(c, end = '%')
- 12%15#
- 15#12%
- 12#15%
- 12%15#
Answer
12#15%
Reason —
- The variable
c
is initially set to 10. - The function
add()
is defined. Inside this function, theglobal
keyword is used to indicate that the variablec
refers to the global variable defined outside the function. - Inside
add()
: Whenadd()
is called:- The value of
c
(which is 10) is accessed. - It is then incremented by 2, making
c
equal to 12. - The new value of
c
(12) is printed, followed by a#
symbol.
- The value of
- After calling the function, the global
c
is now 12. However, it is then explicitly set to 15. - Finally, the new value of
c
(15) is printed, followed by a%
symbol.
Related Questions
Write the missing statement to complete the following code:
file = open("example.txt", "r") data = file.read(100) ............... #Move the file pointer to the beginning of the file next_data = file.read(50) file.close()
State whether the following statement is True or False:
The
finally
block in Python is executed only if no exception occurs in the try block.Which SQL command can change the degree of an existing relation ?
What will be the output of the query ?
SELECT * FROM products WHERE product_name LIKE 'App%';
- Details of all products whose names start with 'App'
- Details of all products whose names end with 'App'
- Names of all products whose names start with 'App'
- Names of all products whose names end with 'App'