w3resource

Python Keywords: Essential Guide for PCEP-30-02 Exam Preparation

PCEP-30-0x questions, answers and explanations

This comprehensive set of questions and explanations covers the fundamental keywords in Python, providing a diverse set of interactions to enhance understanding and prepare for the PCEP-30-02 examination.

Question 1: Which of the following is a keyword in Python?

  1. function
  2. define
  3. class
  4. module

Answer: C) class

Explanation: 'class' is a reserved keyword in Python used to define a new class.

Question 2: What is the purpose of the import keyword in Python?

  1. To define a new function
  2. To include external modules or libraries
  3. To declare a variable
  4. To start a loop

Answer: B) To include external modules or libraries

Explanation: The import keyword is used to include external modules or libraries in a Python program.

Question 3: Which of the following are keywords in Python? (Select all that apply)

  1. return
  2. yield
  3. pass
  4. variable

Answer: A) return B) yield C) pass

Explanation: return, yield, and pass are all reserved keywords in Python, while variable is not.

Question 4: Match the following Python keywords with their descriptions.

Keywords Descriptions
def 1. Defines a function
if 2. Starts a while loop
while 3. Conditional statement
break 4. Exits the current loop

Answer:

  • def -> 1. Defines a function
  • if -> 3. Conditional statement
  • while -> 2. Starts a while loop
  • break -> 4. Exits the current loop

Explanation: Each keyword matches its respective description in the context of Python programming.

Question 5: Fill in the blanks: The ____ keyword is used to define a function, while the ____ keyword is used to define a class.

Answer: The def keyword is used to define a function, while the class keyword is used to define a class.

Explanation: def is used for defining functions and class for defining classes in Python.

Question 6: Arrange the following keywords in the order they would appear in a typical function definition.

Keywords Order
def 1
return 2
if 3
else 4

Answer:

  • def
  • if
  • else
  • return

Explanation: A typical function definition starts with def, may include if and else for conditional logic, and ends with return to return a value.

Question 7: Fill in the code to define a function that checks if a number is positive, negative, or zero.

def check_number(num):
    if num > 0:
        _______
    elif num < 0:
        _______
    else:
        _______

check_number(5)

Answer:

if num > 0:		
	print("Positive")
elif num < 0:
    print("Negative")
else:
    print("Zero")

Explanation: The code uses the if, elif, and else keywords to check the value of num and print the appropriate message.

Question 8: Insert the missing line of code to define a function that returns the square of a number.

def square(n):
    _______

result = square(4)
print(result)

Answer: return n * n

Explanation: The return keyword is used to return the square of the number n.

Question 9: Which keyword is used to define an anonymous function in Python?

  1. def
  2. lambda
  3. func
  4. anonymous

Answer: B) lambda

Explanation: The lambda keyword is used to define anonymous functions in Python.

Question 10: What does the in keyword do in Python?

  1. It checks for membership in a collection
  2. It defines a new variable
  3. It starts a for loop
  4. It ends a function

Answer: A) It checks for membership in a collection

Explanation: The in keyword is used to check if a value is present in a collection such as a list or a dictionary.

Question 11: Which of the following keywords are used in exception handling in Python? (Select all that apply)

  1. try
  2. except
  3. finally
  4. error

Answer: A) try B) except C) finally

Explanation: try, except, and finally are used for exception handling in Python, while error is not a keyword

Question 12: Match the following keywords with their roles in a loop.

Keywords Roles
for 1. Executes after the loop completes, if not terminated by break
continue 2. Exits the loop
break 3. Skips the rest of the current iteration and continues with the next iteration
else 4. Initiates a for loop

Answer:

  • for -> 4. Initiates a for loop
  • continue -> 3. Skips the rest of the current iteration and continues with the next iteration
  • break -> 2. Exits the loop
  • else -> 1. Executes after the loop completes, if not terminated by break

Explanation: Each keyword has a specific role in controlling the flow of a loop in Python.

Question 13: Fill in the blanks: The ____ keyword is used to skip the rest of the current iteration and continue with the next iteration, while the ____ keyword is used to exit the loop entirely.

Answer: The continue keyword is used to skip the rest of the current iteration and continue with the next iteration, while the break keyword is used to exit the loop entirely.

Explanation: continue is used to skip to the next iteration, and break is used to terminate the loop.

Question 14: Arrange the following keywords in the order they would appear in a typical exception handling block.

Keywords Order
try 1
else 2
except 3
finally 4

Answer:

  • try
  • except
  • else
  • finally

Explanation: A typical exception handling block starts with try, followed by except for handling exceptions, else for code that runs if no exceptions occur, and finally for code that runs regardless of whether an exception occurred or not.

Question 15: Fill in the code to handle a potential division by zero error using exception handling.

def divide(a, b):
    try:
        return a / b
    except _______:
        return "Cannot divide by zero"

result = divide(10, 0)
print(result)

Answer: ZeroDivisionError

Explanation: The except keyword is used to catch the ZeroDivisionError and handle it appropriately.

Question 16: Insert the missing line of code to define a generator function that yields the first three natural numbers.

def generate_numbers():
    yield 1
    yield 2
    _______

for number in generate_numbers():
    print(number)

Answer: yield 3

Explanation: The yield keyword is used to define a generator function that can be iterated over.

Question 17: Which keyword is used to create a new instance of a class in Python?

  1. init
  2. self
  3. new
  4. class

Answer: D) class

Explanation: The class keyword is used to define a new class, and a new instance is created using this class definition.

Question 18: What is the purpose of the with keyword in Python?

  1. To declare a new variable
  2. To define a block of code for managing resources
  3. To start a loop
  4. To handle exceptions

Answer: B) To define a block of code for managing resources

Explanation: The with keyword is used to wrap the execution of a block of code within methods defined by a context manager, commonly used for managing resources like files.

Question 19: Which of the following are valid Python keywords? (Select all that apply)

  1. assert
  2. include
  3. global
  4. switch

Answer: A) assert C) global

Explanation: assert and global are valid Python keywords, while include and switch are not.

Question 20: Match the following Python keywords with their functionalities.

Keywords Functionalities
return 1. Refers to a variable in the nearest enclosing scope
pass 2. Exits a function and returns a value
nonlocal 3. Does nothing, acts as a placeholder
raise 4. Triggers an exception

Answer:

  • return -> 2. Exits a function and returns a value
  • pass -> 3. Does nothing, acts as a placeholder
  • nonlocal -> 1. Refers to a variable in the nearest enclosing scope
  • raise -> 4. Triggers an exception

Explanation: Each keyword has a specific functionality in Python programming.

Question 21: Fill in the blanks: The ____ keyword is used to do nothing and act as a placeholder, while the ____ keyword is used to trigger an exception.

Answer: The pass keyword is used to do nothing and act as a placeholder, while the raise keyword is used to trigger an exception

Explanation: pass is used where a statement is syntactically required but no action is needed, and raise is used to raise an exception.

Question 22: Arrange the following keywords in the order they might appear in a Python program that opens a file and handles exceptions.

Keywords Order
with 1
except 2
open 3
try 4

Answer:

  • with
  • open
  • try
  • except

Explanation: A typical file handling program would use with and open to open a file, try to attempt file operations, and except to handle any exceptions.

Question 23: Fill in the code to create a context manager for reading a file using the with keyword.

def read_file(filename):
    with open(filename, 'r') as file:
        content = file.read()
        return content

result = read_file('example.txt')
print(result)

Answer: The code is already complete.

Explanation: The with keyword is used with open to ensure the file is properly closed after reading.

Question 24: Insert the missing line of code to define a function that uses the global keyword to modify a global variable.

x = 10

def modify_global():
    global x
    x = 20

modify_global()
print(x)

Answer: The code is already complete.

Explanation: The global keyword is used to modify the global variable x within the function.

Question 25: Which keyword is used to define a function in Python?

  1. func
  2. def
  3. lambda
  4. function

Answer: B) def

Explanation: The def keyword is used to define a function in Python.

Question 26: What does the elif keyword do in Python?

  1. It ends a function definition
  2. It starts a loop
  3. It checks another condition if the previous conditions were false
  4. It raises an exception

Answer: C) It checks another condition if the previous conditions were false

Explanation: The elif keyword is used in conditional statements to check another condition if the previous conditions are false.

Question 27: Which of the following are loop control keywords in Python? (Select all that apply)

  1. break
  2. pass
  3. continue
  4. return

Answer: A) break C) continue

Explanation: break and continue are used to control loops, while pass does nothing and return exits a function.

Question 28: Match the following Python keywords with their correct uses.

Keywords Uses
and 1. Inverts a boolean value
is 2. Performs a logical AND operation
not 3. Checks if two objects are the same
or 4. Performs a logical OR operation

Answer:

  • and -> 2. Performs a logical AND operation
  • is -> 3. Checks if two objects are the same
  • not -> 1. Inverts a boolean value
  • or -> 4. Performs a logical OR operation

Explanation: Each keyword has a specific logical operation or comparison function in Python. and performs a logical AND operation, is checks for object identity, not inverts a boolean value, and or performs a logical OR operation.

Question 29: Fill in the blanks: The ____ keyword is used for the logical AND operation, while the ____ keyword is used for the logical OR operation

Answer: The and keyword is used for the logical AND operation, while the or keyword is used for the logical OR operation.

Explanation: and and or are logical operators used in conditional statements.

Question 30: Arrange the following keywords in the order they would appear in a conditional statement.

Keywords Order
if 1
else 2
elif 3
print 4

Answer:

  • if
  • elif
  • else
  • print

Explanation: A conditional statement typically starts with if, followed by elif for additional conditions, else for the default case, and print for output.

Question 31: Fill in the code to check if a number is divisible by both 2 and 3.

def check_divisibility(num):
    if num % 2 == 0 and num % 3 == 0:
        _______
    else:
        _______

check_divisibility(6)

Answer:

		print("Divisible by both 2 and 3")
    else:
        print("Not divisible by both 2 and 3")
		

Explanation: The code uses the and keyword to check if the number is divisible by both 2 and 3.

Question 32: Insert the missing line of code to define a function that returns True if a variable is None.

def is_none(var):
    if var is None:
        return True
    else:
        _______

result = is_none(None)
print(result)

Answer: return False

Explanation: The is keyword checks if the variable is None.

Question 33: Which keyword is used to end a function and return a value to the caller in Python?

  1. end
  2. exit
  3. stop
  4. return

Answer: D) return

Explanation: The return keyword ends a function and returns a value to the caller.

Question 34: What is the purpose of the global keyword in Python?

  1. To declare a global variable
  2. To create a global function
  3. To modify a variable outside the current scope
  4. To define a global constant

Answer: C) To modify a variable outside the current scope

Explanation: The global keyword is used to modify a variable outside the current scope.

Question 35: Which of the following are Python keywords? (Select all that apply)

  1. finally
  2. public
  3. global
  4. elseif

Answer: A) finally C) global

Explanation: finally and global are Python keywords, while public and elseif are not. In Python, finally is used in try-except blocks, and global is used to declare global variables.

Question 36: Match the following Python keywords with their functionalities.

Keywords Functionalities
def 1. Defines a class
class 2. Imports specific parts of a module
import 3. Includes external modules
from 4. Defines a function

Answer:

  • def -> 4. Defines a function
  • class -> 1. Defines a class
  • import -> 3. Includes external modules
  • from -> 2. Imports specific parts of a module

Explanation: Each keyword has a specific functionality in defining and importing components in Python.

Question 37: Fill in the blanks: The ____ keyword is used to include external modules, while the ____ keyword is used to define a new class

Answer: The import keyword is used to include external modules, while the class keyword is used to define a new class.

Explanation: import is used to bring in external modules, and class is used to create new classes.

Question 38: Arrange the following keywords in the order they might appear in a Python class definition.

Keywords Order
class 1
self 2
def 3
return 4

Answer:

  • class
  • def
  • self
  • return

Explanation: A class definition starts with class, followed by def for methods, self to refer to instance attributes, and return to return values from methods.

Question 39: Fill in the code to define a method within a class that returns the name of the instance.

class Person:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        _______

p = Person("Alice")
print(p.get_name())

Answer: return self.name

Explanation: The return keyword is used to return the name of the instance from the get_name method.

Question 40: Insert the missing line of code to define a class with a method that returns the sum of two numbers.

class Calculator:
    def add(self, a, b):
        _______

calc = Calculator()
result = calc.add(3, 5)
print(result)

Answer: return a + b

Explanation: The return keyword is used to return the sum of a and b from the add method.

Question 41: Which keyword is used to create a loop that iterates over a sequence in Python?

  1. while
  2. for
  3. loop
  4. iterate

Answer: B) for

Explanation: The for keyword is used to create a loop that iterates over a sequence such as a list or a range.

Question 42: What is the purpose of the del keyword in Python?

  1. To delete a variable, list element, or object
  2. To define a new function
  3. To declare a global variable
  4. To raise an exception

Answer: A) To delete a variable, list element, or object

Explanation: The del keyword is used to delete a variable, list element, or object.

Question 43: Which of the following keywords are used for conditional statements in Python? (Select all that apply)

  1. if
  2. elif
  3. else
  4. endif

Answer: A) if B) elif C) else

Explanation: if, elif, and else are used for conditional statements, while endif is not a Python keyword.

Question 44: Match the following Python keywords with their descriptions.

Keywords Descriptions
yield 1. Tests if a condition is true
assert 2. Pauses a generator function and returns a value
continue 3. Declares a variable as global
global 4. Skips to the next iteration of a loop

Answer:

  • yield -> 2. Pauses a generator function and returns a value
  • assert -> 1. Tests if a condition is true
  • continue -> 4. Skips to the next iteration of a loop
  • global -> 3. Declares a variable as global

Explanation: Each keyword has a specific description based on its functionality in Python.

Question 45: Fill in the blanks: The ____ keyword is used to pause a generator function and return a value, while the ____ keyword is used to declare a variable as global.

Answer: The yield keyword is used to pause a generator function and return a value, while the global keyword is used to declare a variable as global.

Explanation: yield is used in generators, and global is used to modify global variables.

Question 46: Arrange the following keywords in the order they might appear in a generator function.

Keywords Order
def 1
yield 2
for 3
return 4

Answer:

  • def
  • for
  • yield
  • return

Explanation: A generator function starts with def, may include for for iteration, uses yield to return values, and return to exit the function.

Question 47: Fill in the code to define a generator function that yields the first three natural numbers.

def generate_numbers():
    _______
    _______
    _______

for number in generate_numbers():
    print(number)

Answer:

		yield 1
    	yield 2
   		yield 3

Explanation: The yield keyword is used to return the first three natural numbers in the generator function.

Question 48: Insert the missing line of code to define a function that asserts a condition.

def assert_condition(value):
    assert _______

assert_condition(5 > 3)

Answer: value

Explanation: The assert keyword is used to test if a condition is true and raises an AssertionError if it is not.

Question 49: Which keyword is used to test a condition in Python?

  1. check
  2. assert
  3. validate
  4. test

Answer: B) assert

Explanation: The assert keyword is used to test a condition and raise an AssertionError if the condition is false.

Question 50: What does the try keyword do in Python?

  1. It attempts to execute code that might raise an exception
  2. It defines a new function
  3. It creates a loop
  4. It declares a variable

Answer: A) It attempts to execute code that might raise an exception

Explanation: The try keyword is used to attempt to execute code that might raise an exception and handle it with except.



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/certified-entry-level-python-programmer-logic-and-structure-keywords.php