Bird
0
0

Which of the following code snippets has correct indentation in Python?

easy📝 Syntax Q12 of 15
Python - Basics and Execution Environment
Which of the following code snippets has correct indentation in Python?
A<pre>def greet(): print('Hello') print('World')</pre>
B<pre>def greet(): print('Hello') print('World')</pre>
C<pre>def greet(): print('Hello') print('World')</pre>
D<pre>def greet(): print('Hello') print('World')</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Check indentation inside the function

    All lines inside the function must be indented the same amount to belong to the function block.
  2. Step 2: Identify the snippet with consistent indentation

    def greet():
      print('Hello')
      print('World')
    has both print statements indented equally under the function, which is correct.
  3. Final Answer:

    def greet():\n print('Hello')\n print('World') -> Option D
  4. Quick Check:

    Consistent indentation inside function = Correct [OK]
Quick Trick: Indent all block lines equally under the header [OK]
Common Mistakes:
MISTAKES
  • Mixing indentation levels inside a block
  • Not indenting any lines inside a function
  • Indenting only some lines inconsistently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes