w3resource

PCEP Certification Practice: Name Scopes, Name Hiding, and Global Keyword

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of 25 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "name scopes, name hiding (shadowing), and the global keyword." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

Question 1: What is a scope in Python?

  1. A function that hides variables.
  2. A region of the program where a variable is accessible.
  3. A way to define global variables.
  4. A special function that returns variables.

Answer: B) A region of the program where a variable is accessible.

Explanation: A scope is the area of a program where a variable can be used. Variables can have local, global, or nonlocal scopes.

Question 2: Which of the following are true about variable scopes in Python? (Choose all that apply)

  • Variables defined inside a function are local to that function.
  • Variables defined outside a function are global.
  • Variables inside a function can only be accessed using the global keyword.
  • Global variables can be accessed from within functions unless shadowed.

Answer: A) Variables defined inside a function are local to that function.
B) Variables defined outside a function are global.
D) Global variables can be accessed from within functions unless shadowed./p>

Explanation: Variables defined inside a function are local to that function, while global variables can be accessed unless they are shadowed by local variables of the same name. Global variables do not need the global keyword for read access.

Question 3: Complete the code to access a global variable inside a function.

x = 10

def show():
    _______ x
    x = x + 5
    print(x)

show()

Answer: global

Explanation: The global keyword is used to modify the global variable x inside the function show().

Question 4: What will be the output of the following code?

x = 5

def display():
    x = 10
    print(x)

display()
print(x)
  1. 5 5
  2. 10 10
  3. 10 5
  4. Error

Answer: C) 10 5

Explanation: The variable x inside the function display() is local and shadows the global x. The global x remains unchanged when printed outside the function.

Question 5: Insert the correct code to modify the global variable count inside the function increment.

count = 0

def increment():
    ______ count
    count += 1

increment()
print(count)

Answer: global

Explanation: The global keyword is required to modify the global variable count from inside the increment() function.

Question 6: Which of the following correctly describes variable shadowing in Python?

  1. A local variable with the same name as a global variable hides the global variable inside the function.
  2. A global variable prevents access to local variables.
  3. A variable can only be shadowed using the global keyword.
  4. Shadowing prevents any access to global variables.

Answer: A) A local variable with the same name as a global variable hides the global variable inside the function.

Explanation: Shadowing occurs when a local variable has the same name as a global variable, hiding the global variable inside the local scope.

Question 7: Which of the following statements about the global keyword are true? (Choose all that apply)

  1. The global keyword allows a function to modify a global variable.
  2. Without the global keyword, functions cannot modify global variables.
  3. The global keyword is required to read global variables inside a function.
  4. You can declare multiple variables as global using the global keyword.

Answer: A) The global keyword allows a function to modify a global variable.
B) Without the global keyword, functions cannot modify global variables.
D) You can declare multiple variables as global using the global keyword.

Explanation: The global keyword allows functions to modify global variables, but it is not required to read them. Multiple variables can be declared as global using global var1, var2, ....

Question 8: Arrange the steps to correctly modify a global variable inside a function.

  1. Print the modified global variable outside the function.
  2. Declare the global variable.
  3. Use the global keyword to access the global variable inside the function.
  4. Modify the global variable inside the function.

Answer: 2, 3, 4, 1

Explanation: First, declare the global variable, then use the global keyword to access and modify it inside the function, and finally print the modified variable outside the function.

Question 9: Complete the code to prevent the local variable message from shadowing the global variable message.

message = "Hello"

def show():
    ______ message
    message = "Hi"
    print(message)

show()
print(message)

Answer: global

Explanation: The global keyword ensures that the global message variable is modified rather than shadowed by the local variable.

Question 10: What will be the output of the following code?

x = 1

def func():
    global x
    x = x + 1

func()
print(x)
  1. 1
  2. 2
  3. 0
  4. Error

Answer: B) 2

Explanation: The global keyword allows the function to modify the global variable x, incrementing it by 1. The result is 2.

Question 11: Insert the correct code to modify both global variables a and b inside the function update.

a = 5
b = 10

def update():
    ______ a, b
    a = a + 2
    b = b + 3

update()
print(a, b)

Answer: global

Explanation: The global keyword is used to declare both a and b as global variables so they can be modified inside the function.

Question 12: What is the scope of a variable defined inside a function without the global keyword?

  1. Global scope
  2. Local scope
  3. Enclosing scope
  4. Built-in scope

Answer: B) Local scope

Explanation: Variables defined inside a function without the global keyword have local scope, meaning they are only accessible within that function

Question 13: Which of the following statements are true about name scopes in Python? (Choose all that apply)

def display_info(name, age=25):
    print(name + " is " + str(age) + " years old")
  1. Variables in the global scope can be accessed inside functions without the global keyword.
  2. Local variables in functions can shadow global variables of the same name.
  3. The global keyword allows modification of global variables inside a function.
  4. Variables in the local scope can be accessed globally.

Answer:A) Variables in the global scope can be accessed inside functions without the global keyword.
B) Local variables in functions can shadow global variables of the same name.
C) The global keyword allows modification of global variables inside a function.

Explanation: Global variables can be accessed inside functions unless shadowed by local variables. The global keyword is required to modify global variables, but local variables cannot be accessed globally.

Question 14: Arrange the steps to access a global variable, modify it inside a function, and print the result.

  1. Declare the global variable.
  2. Use the global keyword inside the function.
  3. Modify the global variable inside the function.
  4. Print the global variable after the function call.

Answer: 1, 2, 3, 4

Explanation: First, declare the global variable, use the global keyword inside the function to modify it, and then print the result after the function call.

Question 15: Complete the code to prevent the local variable counter from shadowing the global variable counter.

counter = 0

def increment():
    ______ counter
    counter += 1

increment()
print(counter)

Answer: global

Explanation: The global keyword ensures that the global counter variable is modified rather than shadowed by the local variable inside the function.

Question 16: What will be the output of the following code?

x = 5

def reset():
    x = 0
    print(x)

reset()
print(x)
  1. 0 0
  2. 5 5
  3. 0 5
  4. 5 0

Answer: C) 0 5

Explanation: The variable x inside the function reset() is local and shadows the global x. Therefore, the global x remains unchanged after the function call.

17. Insert the correct keyword to modify the global variable total inside the function add.

total = 0

def add():
    ______ total
    total += 5

add()
print(total)

Answer: global

Explanation: The global keyword is required to modify the global variable total from inside the add() function.

Question 18: Which of the following will result in an error?

x = 10

def change():
    x += 5
    print(x)
  1. Using the global keyword before modifying x.
  2. Accessing x inside the function without modification.
  3. Modifying the global variable x without the global keyword.
  4. Using x as a parameter.

Answer: C) Modifying the global variable x without the global keyword.

Explanation: Modifying a global variable without using the global keyword inside a function results in an error because the function treats x as a local variable by default.

Question 19: Which of the following are valid ways to access global variables in Python? (Choose all that apply)

  1. Read a global variable without the global keyword inside a function.
  2. Modify a global variable without the global keyword.
  3. Use the global keyword to modify global variables inside a function.
  4. Declare variables as global inside and outside a function.

Answer: A) Read a global variable without the global keyword inside a function.
C) Use the global keyword to modify global variables inside a function.

Explanation: You can read global variables inside a function without the global keyword. To modify them, however, the global keyword is required. You do not need to declare variables as global outside of a function; the global keyword is only used inside functions to modify global variables.

Question 20: Arrange the steps to create and modify a global variable inside a function.

  1. Use the global keyword to modify the global variable inside the function.
  2. Declare the global variable outside the function.
  3. Modify the global variable inside the function.
  4. Print the global variable outside the function.

Answer: 2, 1, 3, 4,

Explanation: First, declare the global variable outside the function, use the global keyword inside the function, modify it, and finally print it after the function call.

Question 21: Complete the code to access the global variable counter inside the function.

counter = 10

def decrement():
    ______ counter
    counter -= 1

decrement()
print(counter)

Answer: global

Explanation: The global keyword is required to modify the global variable counter from within the function decrement().

Question 22: What will be the output of the following code?

x = 3

def multiply():
    global x
    x *= 2

multiply()
print(x)
  1. 6
  2. 3
  3. 9
  4. Error

Answer: A) 6

Explanation: The global keyword allows the function multiply() to modify the global variable x, doubling its value to 6.

Question 23: Insert the correct code to modify both global variables x and y inside the function change_values.

x = 1
y = 2

def change_values():
    ______ x, y
    x += 5
    y += 10

change_values()
print(x, y)

Answer: global

Explanation: The global keyword allows both x and y to be modified inside the function.

Question 24: What will be the result of the following code?

counter = 10

def reset():
    global counter
    counter = 0

reset()
print(counter)
  1. 10
  2. 0
  3. None
  4. Error

Answer: B) 0

Explanation: The global keyword allows the function reset() to modify the global variable counter, setting its value to 0.

Question 25: Which of the following are true about name hiding (shadowing) in Python? (Choose all that apply)

  1. Shadowing occurs when a local variable has the same name as a global variable.
  2. The global keyword prevents shadowing.
  3. Shadowing causes the local variable to hide the global variable inside the function.
  4. You cannot access the global variable if it is shadowed.

Answer: A) Shadowing occurs when a local variable has the same name as a global variable.
C) Shadowing causes the local variable to hide the global variable inside the function.

Explanation: Shadowing occurs when a local variable with the same name as a global variable hides the global variable within the local scope. You can still access the global variable if it's not modified.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/functions-and-exceptions-name-scopes-name-hiding-and-global-keyword.php