w3resource

Understanding Python Indentation: PCEP Exam Practice

PCEP Certification Practice Test - Questions, Answers and Explanations

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

Question 1: What is the significance of indentation in Python?

  1. It is used to decorate the code.
  2. It is optional and used only for readability.
  3. It defines the block of code.
  4. It is used to write comments.

Answer: C) It defines the block of code.

Explanation: In Python, indentation is used to define the scope of loops, functions, classes, and other control flow constructs.

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

  1. def func():
    print("Hello")
    
  2. def func():
    	print("Hello")
    
  3. if True:
    	print("Hello")
    
  4. for i in range(5):
    	print(i)
    

Answer:

		A)
 
def func():
print("Hello")
		

Explanation: In option A, print("Hello") is not indented, causing an indentation error.

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

  1. Indentation is not necessary for single-line statements.
  2. Indentation indicates a block of code.
  3. Consistent indentation is required within a block.
  4. The amount of indentation is fixed and cannot be changed.

Answer: B) Indentation indicates a block of code. C) Consistent indentation is required within a block.

Explanation: Indentation is used to indicate blocks of code, and it must be consistent within a block.

Question 4: Arrange the following lines of code to form a correctly indented function that prints even numbers from 0 to 4.

Lines of Code Order
print(i) 1
for i in range(5): 2
print_evens() 3
def print_evens(): 4
if i % 2 == 0: 5

Answer:

  • def print_evens():
  • for i in range(5):
  • if i % 2 == 0:
  • print(i)
  • print_evens()

Explanation: The function print_evens is defined first, followed by a correctly indented for loop, if statement, and print statement, and finally the function is called.

Question 5: Fill in the blanks: In Python, indentation is used to define the ______ of code. Consistent _______ is required within a block.

Answer: In Python, indentation is used to define the block of code. Consistent indentation is required within a block.

Explanation: Indentation defines the scope and structure of code blocks in Python.

Question 6: Arrange the following lines of code to form a correctly indented function that prints numbers from 0 to 4.

Lines of Code Order
for i in range(5): 1
print(i) 2
def print_numbers(): 3
print_numbers() 4

Answer:

  • def print_numbers():
  • for i in range(5):
  • print(i)
  • print_numbers()

Explanation: The function print_numbers is defined first, followed by a correctly indented for loop and print statement, and finally the function is called.

Question 7: Fill in the code to correctly indent the following function that checks if a number is positive.

def check_positive(num):
if num > 0:
print("Positive")
else:
print("Non-positive")

Answer:

def check_positive(num):
  if num > 0:
    print("Positive")
  else:
    print("Non-positive")

Explanation: The if and else blocks, as well as their print statements, need to be indented properly.

Question 8: Insert the missing line of code to correctly indent a nested loop that prints a multiplication table.

def multiplication_table(n):
for i in range(1, n + 1):
for j in range(1, n + 1):
________
print()

multiplication_table(3)

Answer: print(i * j, end=" ")

Explanation: The print statement inside the nested loop needs to be indented to show it belongs to the inner loop.

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

  def func():
    print("Start")
        print("Middle")
    print("End")

func()
  
  1.  
    Start
    Middle
    End
    
  2. Start
        Middle
    End
    	
  3. IndentationError: unexpected indent
    
  4. Start
    End
    

Answer: C) IndentationError: unexpected indent

Explanation: The line print("Middle") has incorrect indentation, causing an IndentationError.

Question 10: In Python, what is the recommended number of spaces for indentation?

  1. 2
  2. 4
  3. 6
  4. 8

Answer: B) 4

Explanation: PEP 8, the Python style guide, recommends using 4 spaces per indentation level.

Question 11: Which of the following practices help maintain proper indentation in Python? (Select all that apply)

  1. Using tabs exclusively
  2. Mixing tabs and spaces
  3. Using a code editor with Python support
  4. Following PEP 8 guidelines

Answer: C) Using a code editor with Python support D) Following PEP 8 guidelines

Explanation: Using a code editor with Python support and following PEP 8 guidelines help maintain proper indentation.

Question 12: Match the following indentation styles with their correctness in Python.

Indentation Styles Correctness
def func():\n print("Hello") 1. Correctly indented
if True:\nprint("Yes") 2. Correctly indented
while True:\n print("Looping") 3. Incorrectly indented
for i in range(3):\nprint(i) 4. Incorrectly indented

Answer:

  • def func():
      print("Hello")
    
    -> 1. Correctly indented
  • if True:
    print("Yes")
    
    -> 3. Incorrectly indented
  • while True:
     print("Looping")
    
    -> 2. Correctly indented
  • for i in range(3):
    print(i)
    
    -> 4. Incorrectly indented

Explanation: Proper indentation is crucial for defining code blocks in Python, and incorrect indentation leads to errors.

Question 13: Fill in the blanks: To avoid indentation errors in Python, it is important to use consistent ______ and to follow ______ guidelines

Answer: To avoid indentation errors in Python, it is important to use consistent indentation and to follow PEP 8 guidelines.

Explanation: Consistent indentation and adherence to PEP 8 guidelines help prevent indentation errors.

Question 14: Arrange the following lines of code to form a correctly indented function that calculates the factorial of a number.

Lines of Code Order
def factorial(n): 1
else: 2
return 1 3
if n == 0: 4
return n * factorial(n-1) 5

Answer:

def factorial(n):
  if n == 0:
    return 1
  else:
    return n * factorial(n-1)

Explanation:The function factorial is defined first, followed by the base case, the recursive call, and their proper indentation.

Question 15: Fill in the code to correctly indent the following class definition that initializes a name attribute.

class Person:
                              def __init__(self, name):
                self.name = name
p = Person("Alice")
print(p.name)

Answer:

class Person:
def __init__(self, name):
  self.name = name
  
  p = Person("Alice")
  print(p.name)

Explanation: The __init__ method and its contents need to be indented correctly within the class definition.

Question 16: Insert the missing line of code to correctly indent a function that prints the elements of a list.

def print_list(lst):
for item in lst:
________
        
print_list([1, 2, 3])

Answer: print(item)

def print_list(lst):
  for item in lst:
    print(item)
print_list([1, 2, 3])

Explanation: The print statement needs to be indented to show it belongs to the loop.

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

for i in range(3):
    for j in range(2):
        print(i, j)
print("Done")
A)

0 0
0 1
1 0
1 1
2 0
2 1
 
 B)

0 0
1 0
2 0
0 1
1 1
2 1
 
C)

0 0
0 1
1 0
1 1
2 0
2 1
 
D)
0 0
0 1
1 0
1 1
2 0
2 1

Answer:

A)
0 0
0 1
1 0
1 1
2 0
2 1

Explanation: The nested loops correctly print the values of i and j, followed by "Done" after all iterations are complete.

Question 18: Which of the following will cause an indentation error?

A)
if True:
    print("Yes")
B)
def func():
  print("Hello")
C)
for i in range(3):
    print(i)
D)
while True:
print("Looping")

Answer:

 D)
while True:
print("Looping")

Explanation:In option D, print("Looping") is not indented, causing an indentation error.

Question 19: Which of the following statements about indentation are correct? (Select all that apply)

  1. Indentation is only required for loops.
  2. Indentation is required for function definitions.
  3. Indentation is used to define the scope of a block of code.
  4. Mixing tabs and spaces for indentation can cause errors.

Answer: B) Indentation is required for function definitions. C) Indentation is used to define the scope of a block of code. D) Mixing tabs and spaces for indentation can cause errors.

Explanation: Indentation is used to define the scope of various blocks of code, including loops and function definitions, and mixing tabs and spaces can lead to errors.

Question 20: Match the following scenarios with their indentation outcomes.

Scenarios Outcomes
def func():\n if True:\n print("Yes") 1. Correctly indented nested block
for i in range(3):\nprint(i) 2. Incorrectly indented if statement
if True:\nprint("Hello") 3. Correctly indented loop
while True:\n print("Looping") 4. Incorrectly indented loop

Answer:

  • def func():
      if True:
        print("Yes")
    
    -> 1. Correctly indented nested block
  • for i in range(3):
    print(i)
    
    -> 4. Incorrectly indented loop
  • if True:
    print("Hello")
    
    -> 2. Incorrectly indented if statement
  • while True:
    	print("Looping")
    
    -> 3. Correctly indented loop

Explanation: Proper indentation is crucial for defining nested blocks and loops in Python.

Question 21: Fill in the blanks: In Python, proper indentation is necessary for defining the _______ of code. Mixing tabs and spaces can cause _______ errors.

Answer: In Python, proper indentation is necessary for defining the scope of code. Mixing tabs and spaces can cause indentation errors.

Explanation: Indentation defines the scope, and mixing tabs and spaces can lead to indentation errors.

Question 22: Arrange the following lines of code to form a correctly indented nested if statement.

Lines of Code Order
if a > 0: 1
print("Both are positive") 2
if b > 0: 3

Answer:

  • if a > 0:
  • if b > 0:
  • print("Both are positive")

Explanation: The nested if statement is correctly indented to indicate the block structure.

Question 23: Fill in the code to correctly indent a while loop that prints "Hello" 3 times.

count = 0
while count < 3:
_____
count += 1

Answer: print("Hello")

count = 0
while count < 3:
  print("Hello")
count += 1

Explanation: The print statement needs to be indented to show it belongs to the while loop.

Question 24: Insert the missing line of code to correctly indent a function that calculates and prints the sum of a list of numbers.

def sum_list(lst):
    total = 0
    for num in lst:
        _______
    print("Sum:", total)

sum_list([1, 2, 3])

Answer: total += num

def sum_list(lst):
    total = 0
    for num in lst:
       total += num
    print("Sum:", total)

sum_list([1, 2, 3])

Explanation: The line total += num needs to be indented to show it belongs to the for loop.

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

def outer():
    print("Outer start")
    def inner():
        print("Inner")
    inner()
    print("Outer end")

outer()
A)
Outer start
Outer end
B)
Inner
C)
Outer start
Inner
Outer end
D)
Outer start

Answer:

C)
Outer start
Inner
Outer end

Explanation: The outer function prints "Outer start", calls the inner function which prints "Inner", and then prints "Outer end".

Question 26: Which of the following is true about the use of indentation in defining class methods in Python?

  1. Methods must be indented inside the class definition.
  2. Methods cannot be indented.
  3. Indentation is optional for methods.
  4. Methods should be defined without indentation.

Answer: A) Methods must be indented inside the class definition.

Explanation: In Python, methods must be indented inside the class definition to indicate that they belong to the class.

Question 27: Which of the following will result in an indentation error? (Select all that apply)

A)
def func():
print("Hello")
B)
if True:
    print("Yes")
else:
    print("No")
C)
for i in range(3):
    print(i)
D)
while True:
print("Looping")

Answer:

A)
def func():
print("Hello")
markdown
D)
 
while True:
print("Looping")

Explanation: Options A and D have incorrect indentation, resulting in indentation errors.

Question 28: Match the following code snippets with their indentation outcomes.

Code Snippets Outcomes
def func():\n print("Hello") 1. Indentation error
if True:\nprint("Yes") 2. Correctly indented
for i in range(3):\n print(i) 3. Indentation error
while True:\nprint("Looping") 4. Correctly indented

Answer:

  • def func():
    	print("Hello")
    
    -> 2. Correctly indented
  • if True:
    print("Yes")
    
    -> 1. Indentation error
  • for i in range(3):
    	print(i)
    
    -> 4. Correctly indented
  • while True:
    print("Looping")
    
    -> 3. Indentation error

Explanation: Proper indentation is necessary for defining code blocks in Python, and incorrect indentation results in errors.

Question 29: Fill in the blanks: Proper indentation is crucial for defining the _______ of code in Python. Indentation errors occur when the indentation is _______.

Answer: Proper indentation is crucial for defining the structure of code in Python. Indentation errors occur when the indentation is inconsistent.

Explanation: Indentation defines the structure of code blocks, and inconsistent indentation leads to errors.



Become a Patron!

Follow us on Facebook and Twitter for latest update.