Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

Question answering 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
🎖️
Question Answering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
What is the main purpose of a question answering system?

Choose the best description of what a question answering system does.

AIt predicts numerical values based on historical data.
BIt generates answers to questions by understanding and processing natural language input.
CIt translates text from one language to another without understanding the meaning.
DIt classifies images into categories based on their content.
Attempts:
2 left
💡 Hint

Think about what happens when you ask a smart assistant a question.

Predict Output
intermediate
1:30remaining
Output of a simple QA model prediction

Given the following code snippet using a pretrained QA model, what will be the printed answer?

Prompt Engineering / GenAI
from transformers import pipeline
qa = pipeline('question-answering')
context = "The Eiffel Tower is located in Paris."
question = "Where is the Eiffel Tower located?"
result = qa(question=question, context=context)
print(result['answer'])
A"located"
B"Eiffel Tower"
C"The Eiffel Tower"
D"Paris"
Attempts:
2 left
💡 Hint

The model extracts the answer from the context that best fits the question.

Model Choice
advanced
2:00remaining
Choosing the best model type for open-domain QA

You want to build a system that answers questions about any topic using a large collection of documents. Which model type is best suited?

AA retrieval-augmented generation model that first finds relevant documents then generates answers.
BA simple text classification model that labels questions into categories.
CA convolutional neural network trained on images.
DA clustering algorithm that groups similar questions.
Attempts:
2 left
💡 Hint

Think about how to handle large knowledge bases and generate answers.

Metrics
advanced
2:00remaining
Evaluating a QA model with Exact Match and F1 score

A QA model predicted the answer "Paris" for the question "Where is the Eiffel Tower?" The true answer is "Paris, France". What are the Exact Match and F1 scores?

AExact Match: 0, F1: 0.67
BExact Match: 1, F1: 1
CExact Match: 0, F1: 0
DExact Match: 1, F1: 0.5
Attempts:
2 left
💡 Hint

Exact Match requires exact string equality; F1 measures overlap of tokens.

🔧 Debug
expert
2:00remaining
Why does this QA model code raise an error?

Consider this code snippet:

from transformers import pipeline
qa = pipeline('question-answering')
context = None
question = "What is AI?"
result = qa(question=question, context=context)
print(result['answer'])

Why does it raise an error?

ABecause the pipeline is not initialized, it raises an ImportError.
BBecause the question is missing, the model raises a ValueError.
CBecause the context is None, the model cannot process it and raises a TypeError.
DBecause the print statement is incorrect syntax, it raises a SyntaxError.
Attempts:
2 left
💡 Hint

Check the type and value of the context variable passed to the model.

Practice

(1/5)
1. What is the main purpose of question answering in AI?
easy
A. To find answers from given text or context
B. To generate random text without context
C. To translate languages automatically
D. To create images from descriptions

Solution

  1. Step 1: Understand the goal of question answering

    Question answering systems are designed to find specific answers from a given text or context.
  2. Step 2: Compare options with the goal

    Only To find answers from given text or context describes finding answers from text, which matches the purpose.
  3. Final Answer:

    To find answers from given text or context -> Option A
  4. Quick Check:

    Question answering = find answers [OK]
Hint: Focus on 'answer from text' meaning [OK]
Common Mistakes:
  • Confusing question answering with translation
  • Thinking it generates random text
  • Mixing it with image generation
2. Which input is essential for a question answering model to work?
easy
A. Only a context without a question
B. Only a question without any context
C. A question and a related context or passage
D. Random text unrelated to the question

Solution

  1. Step 1: Identify inputs needed for question answering

    Question answering requires both a question and some context to find the answer.
  2. Step 2: Match options with required inputs

    Only A question and a related context or passage provides both question and related context, which is necessary.
  3. Final Answer:

    A question and a related context or passage -> Option C
  4. Quick Check:

    Question + context = answer [OK]
Hint: Remember: question needs context to answer [OK]
Common Mistakes:
  • Assuming question alone is enough
  • Ignoring the need for context
  • Choosing unrelated text as input
3. Given this Python code using a question answering model:
from transformers import pipeline
qa = pipeline('question-answering')
context = "The Eiffel Tower is in Paris."
question = "Where is the Eiffel Tower located?"
result = qa(question=question, context=context)
print(result['answer'])
What will be printed?
medium
A. Location unknown
B. Eiffel Tower
C. The Eiffel Tower is in Paris
D. Paris

Solution

  1. Step 1: Understand the code's purpose

    The code uses a question answering pipeline to find the answer to the question from the context.
  2. Step 2: Identify the answer in the context

    The question asks for location; the context says "The Eiffel Tower is in Paris." So the answer is "Paris".
  3. Final Answer:

    Paris -> Option D
  4. Quick Check:

    Answer extracted = Paris [OK]
Hint: Look for direct answer in context matching question [OK]
Common Mistakes:
  • Printing the whole context instead of answer
  • Confusing object with location
  • Assuming no answer found
4. This code snippet tries to answer a question but raises an error:
from transformers import pipeline
qa = pipeline('question-answering')
context = "Python is a programming language."
question = "What is Python?"
result = qa(question, context)
print(result['answer'])
What is the error and how to fix it?
medium
A. Error: question is invalid; fix by changing question text
B. Error: missing keyword arguments; fix by using qa(question=question, context=context)
C. Error: context is empty; fix by adding text to context
D. No error; code runs fine

Solution

  1. Step 1: Identify the function call error

    The pipeline expects keyword arguments question= and context=, but code passes positional arguments.
  2. Step 2: Fix the call with correct keywords

    Change to qa(question=question, context=context) to fix the error.
  3. Final Answer:

    Error: missing keyword arguments; fix by using qa(question=question, context=context) -> Option B
  4. Quick Check:

    Use keywords for qa() args [OK]
Hint: Use keyword arguments for question and context [OK]
Common Mistakes:
  • Passing positional args instead of keywords
  • Assuming empty context causes error
  • Changing question text unnecessarily
5. You want to build a question answering system that can handle multiple paragraphs and find the best answer. Which approach is best?
hard
A. Split text into paragraphs, run QA on each, then pick highest confidence answer
B. Combine all paragraphs into one string and run QA once
C. Only use the first paragraph for QA
D. Ignore paragraphs and guess answer randomly

Solution

  1. Step 1: Understand handling multiple paragraphs

    QA models usually work best on smaller text chunks, so splitting helps.
  2. Step 2: Choose method to find best answer

    Running QA on each paragraph separately and selecting the answer with highest confidence ensures accuracy.
  3. Final Answer:

    Split text into paragraphs, run QA on each, then pick highest confidence answer -> Option A
  4. Quick Check:

    Split + score answers = best result [OK]
Hint: Split text, run QA per part, pick best answer [OK]
Common Mistakes:
  • Running QA on all text at once causing confusion
  • Ignoring paragraphs reduces accuracy
  • Guessing answers without context