Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Code generation in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Code Generation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple code generation model snippet
What is the output of this Python code simulating a simple code generation model's token prediction?
Prompt Engineering / GenAI
tokens = ['def', ' ', 'add', '(', 'a', ',', ' ', 'b', ')', ':', '\n', '    ', 'return', ' ', 'a', ' ', '+', ' ', 'b']
predicted = ''.join(tokens)
print(predicted)
A
defadd(a,b):
returna+b
Bdef add(a, b): return a + b
C
def add(a, b):
    return a + b
D
def add(a, b):
return a + b
Attempts:
2 left
💡 Hint
Look carefully at the tokens and how they join with spaces and newlines.
Model Choice
intermediate
1:30remaining
Choosing the right model for code generation
Which model architecture is best suited for generating code snippets given a prompt?
ATransformer-based model
BConvolutional Neural Network (CNN)
CRecurrent Neural Network (RNN) with LSTM
DK-Nearest Neighbors (KNN)
Attempts:
2 left
💡 Hint
Consider models that handle long-range dependencies and context well.
Hyperparameter
advanced
1:30remaining
Effect of temperature on code generation output
In a code generation model, what is the effect of increasing the temperature parameter during sampling?
AImproves the syntax correctness of generated code
BGenerates more deterministic and repetitive code outputs
CDecreases the length of generated code
DGenerates more random and diverse code outputs
Attempts:
2 left
💡 Hint
Think about how temperature affects randomness in sampling.
Metrics
advanced
2:00remaining
Evaluating code generation quality
Which metric is most appropriate to evaluate the functional correctness of generated code snippets?
ANumber of tokens generated per second
BCode execution accuracy on test inputs
CPerplexity of the language model on generated code
DBLEU score comparing generated code to reference code
Attempts:
2 left
💡 Hint
Think about measuring if the code actually works as intended.
🔧 Debug
expert
2:30remaining
Debugging a code generation output error
A code generation model outputs this Python code snippet: "def multiply(a, b):\nreturn a * b" When running, it raises an IndentationError. What is the cause?
Prompt Engineering / GenAI
def multiply(a, b):
return a * b
AThe return statement is not indented inside the function
BThe function name is invalid
CThe multiplication operator is incorrect
DThe function is missing a colon at the end
Attempts:
2 left
💡 Hint
Python requires code blocks to be indented properly.

Practice

(1/5)
1. What is the main purpose of code generation in AI?
easy
A. Manually write code faster
B. Automatically create code from instructions
C. Run code without errors
D. Delete unnecessary code

Solution

  1. Step 1: Understand code generation meaning

    Code generation means creating code automatically from instructions or examples.
  2. Step 2: Match purpose with options

    Automatically create code from instructions correctly states this purpose, others describe different tasks.
  3. Final Answer:

    Automatically create code from instructions -> Option B
  4. Quick Check:

    Code generation = automatic code creation [OK]
Hint: Code generation means automatic code writing [OK]
Common Mistakes:
  • Confusing code generation with manual coding
  • Thinking code generation fixes errors automatically
  • Believing code generation deletes code
2. Which of the following is the correct Python syntax to define a function named generate_code?
easy
A. generate_code def():
B. function generate_code()
C. def generate_code[]:
D. def generate_code():

Solution

  1. Step 1: Recall Python function syntax

    Python functions start with def, followed by name and parentheses, then colon.
  2. Step 2: Check each option

    def generate_code(): matches correct syntax; A, B and D have syntax errors (A wrong order, B JavaScript style, D brackets).
  3. Final Answer:

    def generate_code(): -> Option D
  4. Quick Check:

    Python function = def name(): [OK]
Hint: Python functions start with def and parentheses [OK]
Common Mistakes:
  • Using JavaScript function keyword in Python
  • Missing parentheses after function name
  • Using brackets instead of parentheses
3. What will be the output of this Python code generated by AI?
def add_numbers(a, b):
    return a + b

result = add_numbers(3, 4)
print(result)
medium
A. 7
B. 34
C. TypeError
D. None

Solution

  1. Step 1: Understand function behavior

    The function adds two numbers and returns the sum.
  2. Step 2: Calculate add_numbers(3, 4)

    3 + 4 equals 7, so result is 7 and printed.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Adding numbers returns their sum [OK]
Common Mistakes:
  • Thinking + concatenates numbers as strings
  • Expecting error from simple addition
  • Confusing return value with print output
4. Identify the error in this AI-generated Python code:
def multiply(x, y):
return x * y

print(multiply(2, 3))
medium
A. Missing indentation for return statement
B. Wrong function name
C. Missing parentheses in print
D. Using * instead of + operator

Solution

  1. Step 1: Check Python indentation rules

    Python requires the return line inside function to be indented.
  2. Step 2: Identify error in code

    Return is not indented, causing IndentationError; other options are incorrect.
  3. Final Answer:

    Missing indentation for return statement -> Option A
  4. Quick Check:

    Python needs indented blocks [OK]
Hint: Indent inside functions in Python [OK]
Common Mistakes:
  • Ignoring indentation errors
  • Thinking print needs no parentheses in Python 3
  • Confusing operators without context
5. You want to generate Python code that creates a dictionary from a list of keys ["a", "b", "c"] with values as their lengths. Which code snippet correctly uses dictionary comprehension?
hard
A. result = {len(k): k for k in ["a", "b", "c"]}
B. result = [k: len(k) for k in ["a", "b", "c"]]
C. result = {k: len(k) for k in ["a", "b", "c"]}
D. result = {k, len(k) for k in ["a", "b", "c"]}

Solution

  1. Step 1: Understand dictionary comprehension syntax

    It uses curly braces with key:value pairs inside a for loop.
  2. Step 2: Check each option

    result = {k: len(k) for k in ["a", "b", "c"]} correctly creates dict with keys and their lengths; B uses list brackets wrongly; C swaps key and value; D uses comma instead of colon.
  3. Final Answer:

    result = {k: len(k) for k in ["a", "b", "c"]} -> Option C
  4. Quick Check:

    Dict comprehension = {key: value for item} [OK]
Hint: Dict comprehension uses {key: value for item} [OK]
Common Mistakes:
  • Using list brackets [] instead of {}
  • Swapping keys and values
  • Using comma instead of colon in dict