w3resource

Comprehensive Guide to Python Variables for PCEP-30-02 Exam

PCEP Certification Practice Test - Questions, Answers and Explanations

This comprehensive set of questions and explanations covers the fundamental topic of introducing literals and variables into code and using different numeral systems, focusing on variables. This diverse set of interactions enhances understanding and prepares for the PCEP-30-02 examination.

Question 1: Which of the following is a valid variable name in Python?

  1. 2ndVar
  2. my-var
  3. _myVar
  4. my var

Answer: C) _myVar

Explanation: Variable names cannot start with a digit, cannot contain hyphens or spaces, but can start with an underscore.

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

x = 10
y = x + 5
print(y)
  1. 10
  2. 5
  3. 15
  4. 105

Answer: C) 15

Explanation: The variable y is assigned the value x + 5, which is 10 + 5, resulting in 15.

Question 3: Which of the following statements about variables in Python are true? (Select all that apply)

  1. Variables can change their type after assignment.
  2. Variable names are case-sensitive.
  3. Variables must be declared before use.
  4. Variables can contain letters, digits, and underscores.

Answer: A) Variables can change their type after assignment. B) Variable names are case-sensitive. D) Variables can contain letters, digits, and underscores.

Explanation: Variables can change type, are case-sensitive, and can contain letters, digits, and underscores. Variables do not need to be declared before use.

Question 4: Match the following variable names with their validity.

Variable Names Validity
1st_place 1. Invalid
_hidden 2. Invalid
user-name 3. valid
firstName 4. Valid

Answer:

  • 1st_place -> 1. Invalid
  • _hidden -> 3. Valid
  • user-name -> 2. Invalid
  • firstName -> 4. Valid

Explanation: Variable names cannot start with a digit or contain hyphens, but can start with an underscore and use camelCase.

Question 5: Fill in the blanks: Variable names in Python can contain ________, ________, and ________, but cannot start with a ________.

Answer: Variable names in Python can contain letters, digits, and underscores, but cannot start with a digit.

Explanation: Variable names can include letters, digits, and underscores, but must not start with a digit.

Question 6: Arrange the following steps to correctly assign a value to a variable and print it.

Steps Order
my_var = 10 1
print(my_var) 2
my_var = my_var + 5 3

Answer:

  1. my_var = 10
  2. my_var = my_var + 5
  3. print(my_var)

Explanation: The sequence shows assigning an initial value to a variable, updating the variable, and then printing it.

Question 7: Fill in the code to assign the value 25 to a variable named age.

age = _______

Answer: 25

Explanation: The variable age is assigned the value 25.

Question 8: Insert the missing line of code to define a variable that holds the value 'Python'.

language = _______
print(language)

Answer: 'Python'

Explanation: The variable language is assigned the string value 'Python'.

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

a = 5
b = a * 2
a = 10
print(b)
  1. 5
  2. 10
  3. 20
  4. 15

Answer: B) 10

Explanation: The variable b is assigned the value a * 2 when a is 5, so b becomes 10. The later change to a does not affect b.

Question 10: Which of the following is not a valid variable name in Python?

  1. my_var
  2. _123
  3. varName
  4. class

Answer: D) class

Explanation: class is a reserved keyword in Python and cannot be used as a variable name.

Question 11: Which of the following are valid assignments in Python? (Select all that apply)

  1. x = 10
  2. 10 = x
  3. y = 'Hello'
  4. z == 5

Answer: A) x = 10 C) y = 'Hello'

Explanation: x = 10 and y = 'Hello' are valid assignments. 10 = x is invalid because the assignment operator = cannot be used this way, and z == 5 is a comparison, not an assignment.

Question 12: Match the following variables with their types after assignment.

Variables Types
num = 10 1. String
text = 'Hello' 2. Floating-point
flag = True 3. Integer
pi = 3.14 4. Boolean

Answer:

  1. num = 10 -> 3. Integer
  2. text = 'Hello' -> 1. String
  3. flag = True -> 4. Boolean
  4. pi = 3.14 -> 2. Floating-point

Explanation: Each variable is matched with its type based on the assigned value.

Question 13: Fill in the blanks: Variables in Python can hold values of different types, such as ________, ________, ________, and ________.

Answer: Variables in Python can hold values of different types, such as integers, strings, booleans, and floating-point numbers.

Explanation: Variables in Python can hold various types of values, including integers, strings, booleans, and floating-point numbers.

Question 14: Arrange the following steps to correctly change the type of a variable.

Steps Order
value = 100 1
print(value) 2
value = str(value) 3

Answer:

  1. value = 100
  2. value = str(value)
  3. print(value)

Explanation: The sequence shows assigning an integer value to a variable, converting it to a string, and then printing it.

Question 15: Fill in the code to assign the Boolean value True to a variable named is_valid.

is_valid = _______

Answer: True

Explanation: The variable is_valid is assigned the Boolean value True.

Question 16: Insert the missing line of code to define a variable that holds the value 3.14.

pi = _______
print(pi)

Answer: 3.14

Explanation: The variable pi is assigned the floating-point value 3.14.

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

num = 8
num = num + 2
num = num / 2
print(num)
  1. 8
  2. 10
  3. 5
  4. 4

Answer: C) 5

Explanation: The variable num is updated to 10 (8 + 2), then divided by 2, resulting in 5.

Question 18: Which of the following is a valid way to delete a variable in Python?

  1. delete x
  2. remove x
  3. del x
  4. erase x

Answer:C) del x

Explanation: The del keyword is used to delete a variable in Python.

Question 19: Which of the following statements about variable scope in Python are true? (Select all that apply)

  1. Variables defined inside a function are local to that function.
  2. Variables defined outside any function are global.
  3. Local variables can be accessed from outside their function.
  4. Global variables can be accessed from inside functions

Answer: A) Variables defined inside a function are local to that function. B) Variables defined outside any function are global. D) Global variables can be accessed from inside functions.

Explanation: Local variables are confined to their function, global variables are defined outside functions, and global variables can be accessed from within functions.

Question 20: Match the following variable scopes with their descriptions.

Variable Scopes Descriptions
Local 1. Defined inside a function
Global 2. Defined inside a nested function
Nonlocal 3. Defined outside any function

Answer:

  • Local -> 1. Defined inside a function
  • Global -> 3. Defined outside any function
  • Nonlocal -> 2. Defined inside a nested function

Explanation: Each scope is matched with its description based on where the variable is defined.

Question 21: Fill in the blanks: A variable defined inside a function is called a ________ variable, while a variable defined outside any function is called a ________ variable.

Answer: A variable defined inside a function is called a local variable, while a variable defined outside any function is called a global variable.

Explanation: Variables defined inside functions are local, and those defined outside are global.

Question 22: Arrange the following steps to correctly use a global variable inside a function.

Steps order
def my_function(): 1
global_var = 10 2
print(global_var) 3
my_function() 4

Answer:

2 global_var = 10
1 def my_function():
3 print(global_var)
4 my_function()

Explanation: The sequence shows defining a global variable, and using it within the function.

Question 23: Fill in the code to declare a global variable inside a function.

x = 5

def my_function():
    _______ x
    x = x + 1
    print(x)

my_function()

Answer: global

Explanation: The global keyword is used to declare a global variable inside a function.

Question 24: Insert the missing line of code to define a variable that holds the string 'Hello' and prints it.

greeting = _______
print(greeting)

Answer: 'Hello'

Explanation: The variable greeting is assigned the string value 'Hello'.

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

count = 0

def increment():
    global count
    count += 1

increment()
increment()
print(count)
  1. 0
  2. 1
  3. 2
  4. 3

Answer: C) 2

Explanation: The global keyword is used to modify the global variable count, which is incremented twice.



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/literals-and-variables-variables.php