Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
Specification and Abstraction — Exercise Solutions
1. How to Use This Page
This page provides complete, exam-focused solutions for the Specification and Abstraction chapter from the TN State Board Class 11 Computer Science textbook (Samacheer Kalvi).
Tips for best results:
- Read the Key Concepts section first for a quick refresher before reviewing answers.
- Try each question on your own before reading the solution.
- Pay attention to the explanations — they contain the reasoning examiners look for.
- Use the Exam Checklist at the end for last-minute revision.
2. Key Concepts (Quick Recap)
2.1 Problem Specification
A specification is a precise, unambiguous description of what a function or program should do. It defines:
| Component | Description | Example |
|---|---|---|
| Input | What the function receives | A list of integers |
| Output | What the function produces | The maximum integer |
| Pre-condition | Condition that must hold before execution | List is non-empty |
| Post-condition | Condition that must hold after execution | Output equals the largest element |
Example — Specification of a maximum function:
# Specification: find_max
# Input: A non-empty list of integers L
# Output: An integer m such that m is an element of L
# and for every element x in L, m >= x
# Pre-condition: L is non-empty
# Post-condition: m is the maximum value in L
def find_max(L):
m = L[0]
for x in L:
if x > m:
m = x
return m
2.2 Abstraction
Abstraction is the process of hiding unnecessary implementation details and exposing only what is needed to use a function or module.
| Type | What It Hides | What It Exposes |
|---|---|---|
| Procedural Abstraction | How a function works internally | Function name, parameters, return value |
| Data Abstraction | How data is stored/represented | Operations that can be performed on the data |
Example — Abstraction in everyday programming:
# You use print() without knowing how it internally
# communicates with the display hardware.
# This is abstraction — you know WHAT it does,
# not HOW it does it.
print("Hello, World!") # Abstraction at work
2.3 Decomposition
Decomposition (also called stepwise refinement) is the process of breaking a complex problem into smaller, more manageable sub-problems.
# Problem: Calculate the average of squares of even numbers in a list
# Decomposed into sub-problems:
# 1. Filter even numbers from the list
# 2. Square each filtered number
# 3. Calculate the average of the squared values
def is_even(n):
return n % 2 == 0
def square(n):
return n * n
def average(numbers):
return sum(numbers) / len(numbers)
def avg_of_squares_of_evens(lst):
evens = list(filter(is_even, lst))
squares = list(map(square, evens))
return average(squares)
2.4 Algorithmic Notation
Algorithms are expressed using a structured notation that includes:
- Name: Identifies the algorithm
- Input: What is given to the algorithm
- Output: What the algorithm produces
- Steps: Sequential, conditional, and iterative statements
# Algorithm: Swap two numbers
# Input: Two numbers a and b
# Output: a and b with values exchanged
# Steps:
# 1. temp = a
# 2. a = b
# 3. b = temp
def swap(a, b):
temp = a
a = b
b = temp
return a, b
2.5 Function Specifications
A function specification consists of:
# Function: area_of_circle
# Input: radius — a positive real number
# Output: area — a positive real number
# Pre-condition: radius > 0
# Post-condition: area = pi * radius * radius
import math
def area_of_circle(radius):
return math.pi * radius * radius
3. I. Choose the Best Answer (MCQs)
Question 1
Which of the following best defines specification in programming?
(a) The actual code that implements a function (b) A precise description of what a function does, including inputs, outputs, and conditions (c) The hardware requirements for running a program (d) A list of variable names used in a program
Question 2
Abstraction in computer science refers to:
(a) Making programs run faster (b) Hiding implementation details and showing only essential features (c) Writing code without any comments (d) Increasing the number of variables in a program
Question 3
Which of the following is an example of procedural abstraction?
(a) Using a class to store data (b) Calling a built-in function like len() without knowing its internal code (c) Declaring a variable as an integer (d) Compiling a Python program
Question 4
The pre-condition of a function specification refers to:
(a) The output produced by the function (b) The condition that must be true before the function starts executing (c) The loop condition inside the function (d) The return statement of the function
Question 5
Which of the following correctly distinguishes specification from implementation?
(a) Specification is written in Python; implementation is written in C (b) Specification describes what a function does; implementation describes how it does it (c) Specification includes loops; implementation includes conditions (d) There is no difference between them
Question 6
Decomposition in problem solving means:
(a) Removing unnecessary parts of a program (b) Breaking a complex problem into smaller, manageable sub-problems (c) Combining multiple programs into one (d) Writing code in a single function
Question 7
In a function specification, the post-condition describes:
(a) The condition that must hold before the function is called (b) What the function guarantees about its output after execution (c) The loop termination condition (d) The variable declarations inside the function
Question 8
Which of the following is NOT a benefit of abstraction?
(a) Reduces complexity for the user of a module (b) Allows implementation changes without affecting the interface (c) Makes the program execute faster (d) Enables modular programming
Question 9
Consider the specification: "Given a non-empty list of integers, return the smallest element." Which of the following is the pre-condition?
(a) The output is the smallest integer (b) The list contains at least one element (c) The list contains only positive integers (d) The function uses a comparison operator
Question 10
In the context of algorithmic problem solving, which step comes first?
(a) Writing the code (b) Understanding and specifying the problem (c) Testing the program (d) Optimizing the algorithm
4. II. Fill in the Blanks
1. A specification describes what a function does, while an implementation describes how it does it.
2. The process of breaking down a complex problem into smaller sub-problems is called decomposition.
3. The condition that must be satisfied before a function is called is called the pre-condition.
4. The condition that the function guarantees after execution is called the post-condition.
5. Procedural abstraction hides the details of how a function works and exposes only its interface.
6. Data abstraction hides the representation of data and exposes only the operations that can be performed on it.
7. The three parts of a function specification are input, output, and conditions (pre-condition and post-condition).
8. A function's interface defines what data goes in and what data comes out, without revealing the internal process.
9. In stepwise refinement, each sub-problem can be solved independently and then combined to form the complete solution.
10. The principle of abstraction allows a programmer to use a function without knowing its internal code.
11. A post-condition is a guarantee about the relationship between inputs and outputs after a function completes.
12. If a function is supposed to compute the square root of a number, the pre-condition would typically require the input to be non-negative.
5. III. True or False
Statement 1
True or False: Specification includes the exact code used to implement a function.
Correction: Specification describes what a function does (inputs, outputs, conditions), not the actual code or algorithm. The code belongs to implementation.
Statement 2
True or False: Abstraction helps reduce complexity by hiding unnecessary details.
Statement 3
True or False: A function can have only one possible implementation for a given specification.
Correction: A single specification can have multiple valid implementations. For example, a sorting specification can be implemented using bubble sort, merge sort, quick sort, or insertion sort — all producing the same result but using different algorithms.
Statement 4
True or False: Decomposition is also known as stepwise refinement.
Statement 5
True or False: The pre-condition of a function is checked by the function itself during execution.
Correction: A pre-condition is an assumption or requirement that the caller must satisfy before calling the function. It is not necessarily checked by the function internally. It is the responsibility of the caller to ensure pre-conditions are met, though some functions may validate inputs as a defensive measure.
Statement 6
True or False: Data abstraction hides how data is stored and only shows what operations can be performed on the data.
6. IV. Match the Following
Match the items in Column A with the correct items in Column B.
| # | Column A | # | Column B |
|---|---|---|---|
| 1 | Specification | (a) | Breaking a problem into sub-problems |
| 2 | Abstraction | (b) | Condition that must hold after execution |
| 3 | Pre-condition | (c) | Describes what a function does |
| 4 | Post-condition | (d) | Hiding implementation details |
| 5 | Decomposition | (e) | Condition that must hold before execution |
| 6 | Procedural abstraction | (f) | Hiding how a function works internally |
| Column A | Column B |
|---|---|
| 1. Specification | (c) Describes what a function does |
| 2. Abstraction | (d) Hiding implementation details |
| 3. Pre-condition | (e) Condition that must hold before execution |
| 4. Post-condition | (b) Condition that must hold after execution |
| 5. Decomposition | (a) Breaking a problem into sub-problems |
| 6. Procedural abstraction | (f) Hiding how a function works internally |
7. V. Answer Briefly (2-Mark Questions)
Question 1
What is specification? Give an example.
Specification is a precise description of what a function or program does, including its inputs, outputs, and conditions, without describing how it is implemented.
Example:
# Specification: is_palindrome
# Input: A string s
# Output: True if s reads the same forwards and backwards, False otherwise
# Pre-condition: s is a string
# Post-condition: Output is True if and only if s is a palindrome
The specification states the input, output, and conditions clearly, without revealing whether the function compares characters, reverses the string, or uses any other technique internally.
Question 2
What is abstraction? Why is it important?
Abstraction is the process of hiding unnecessary implementation details and exposing only the essential features needed to use a function or module.
Importance:
- Reduces complexity: Users can work with a simple interface without understanding internal details.
- Enables modularity: Different modules can be developed independently.
- Facilitates maintenance: Implementation can be changed without affecting users of the function.
- Promotes reuse: Well-abstracted functions can be reused in different programs.
For example, when we use math.sqrt(25), we get the result 5.0 without needing to know the Newton-Raphson method or any other algorithm used internally.
Question 3
Differentiate between pre-condition and post-condition.
| Aspect | Pre-condition | Post-condition |
|---|---|---|
| When | Must be true before the function is called | Guaranteed to be true after the function finishes |
| Purpose | States what the function requires from the caller | States what the function guarantees to the caller |
| Example | "Input list must be non-empty" | "Output is the maximum element of the list" |
| Responsibility | Caller must ensure it is satisfied | The function ensures it on completion |
Question 4
What is decomposition? Give a simple example.
Decomposition is the process of breaking a complex problem into smaller, more manageable sub-problems. Each sub-problem can be solved independently and then combined to solve the original problem.
Example:
To compute the average score of students who passed an exam:
1. Sub-problem 1: Filter students who scored above the pass mark. 2. Sub-problem 2: Sum the scores of the filtered students. 3. Sub-problem 3: Divide the sum by the count of filtered students.
def passed_students(scores, pass_mark):
return [s for s in scores if s >= pass_mark]
def average(score_list):
return sum(score_list) / len(score_list)
def avg_passing_score(scores, pass_mark):
passed = passed_students(scores, pass_mark)
return average(passed)
Question 5
Distinguish between procedural abstraction and data abstraction.
| Aspect | Procedural Abstraction | Data Abstraction |
|---|---|---|
| What is hidden | How a function/procedure works internally | How data is stored and represented |
| What is exposed | Function name, parameters, and return value | Operations that can be performed on the data |
| Example | Using sorted() without knowing the sorting algorithm | Using a stack's push/pop without knowing if it uses an array or linked list |
| Focus | Actions/operations | Data representation |
8. VI. Answer in Detail (5-Mark Questions)
Question 1
Explain the concept of specification in detail with examples. Why is specification important in programming?
Specification is a formal, precise description of what a function or program does. It acts as a contract between the user of a function and its implementer. A complete specification includes:
1. Function name: Identifies the function. 2. Input (parameters): Describes what data the function receives and its type. 3. Output (return value): Describes what the function produces. 4. Pre-condition: The condition that must be true before the function is called. 5. Post-condition: The condition that is guaranteed to be true after the function completes.
Detailed Example — Specification of a factorial function:
# Specification: factorial
# Input: n — a non-negative integer
# Output: An integer equal to the product of all positive
# integers from 1 to n
# Pre-condition: n >= 0
# Post-condition: output = 1 * 2 * 3 * ... * n (for n > 0)
# output = 1 (for n = 0)
def factorial(n):
result = 1
for i in range(1, n + 1):
result = result * i
return result
Note that the specification and the implementation are separate. The specification says what the function does, while the code shows how it does it.
Another Example — Specification of a search function:
# Specification: linear_search
# Input: A list L and a target value v
# Output: Index of v in L if found, -1 otherwise
# Pre-condition: L is a list
# Post-condition: If v is in L, output is the first index
# where L[index] == v; otherwise output is -1
def linear_search(L, v):
for i in range(len(L)):
if L[i] == v:
return i
return -1
Importance of Specification:
1. Clarity: It eliminates ambiguity about what a function should do, ensuring both the implementer and the user have the same understanding.
2. Correctness: By stating pre-conditions and post-conditions explicitly, we can verify whether a function behaves as expected.
3. Modularity: Specifications allow different programmers to work on different parts of a program independently, as long as they agree on the interface.
4. Testing: Specifications provide the basis for writing test cases. Test cases check whether the implementation satisfies the specification.
5. Documentation: Specifications serve as documentation, helping programmers understand how to use a function correctly without reading its source code.
6. Multiple implementations: A clear specification allows the same function to be implemented in different ways (e.g., different sorting algorithms) while guaranteeing the same result.
Question 2
Explain the different types of abstraction with examples. How does abstraction help in writing better programs?
Abstraction is the process of hiding unnecessary details and exposing only the essential features. In computer science, there are two main types:
Question 3
Explain decomposition with a detailed example. What are the advantages of decomposition in problem solving?
Decomposition (also called stepwise refinement or top-down design) is the process of breaking a complex problem into smaller, more manageable sub-problems. Each sub-problem can be solved, tested, and debugged independently, and the solutions are then combined to solve the original problem.
Detailed Example — Student Grade Management System:
Suppose we need to build a program that:
- Takes student marks in five subjects
- Calculates the total and average
- Determines the grade based on the average
- Displays a report card
Step 1: Identify sub-problems (Decomposition)
Step 2: Solve each sub-problem independently
# Sub-problem 1: Input student marks
def input_marks():
marks = []
subjects = ["Tamil", "English", "Maths", "Science", "Social Science"]
for subject in subjects:
m = int(input(f"Enter marks for {subject}: "))
marks.append(m)
return marks
# Sub-problem 2: Calculate total marks
def calculate_total(marks):
return sum(marks)
# Sub-problem 3: Calculate average
def calculate_average(total, count):
return total / count
# Sub-problem 4: Determine grade
def determine_grade(average):
if average >= 90:
return "A+"
elif average >= 80:
return "A"
elif average >= 70:
return "B+"
elif average >= 60:
return "B"
elif average >= 50:
return "C"
else:
return "F"
# Sub-problem 5: Display report card
def display_report(marks, total, average, grade):
subjects = ["Tamil", "English", "Maths", "Science", "Social Science"]
print("--- Report Card ---")
for subject, mark in zip(subjects, marks):
print(f"{subject}: {mark}")
print(f"Total: {total}")
print(f"Average: {average:.2f}")
print(f"Grade: {grade}")
Step 3: Combine sub-problems
# Main function combining all sub-problems
def generate_report_card():
marks = input_marks()
total = calculate_total(marks)
average = calculate_average(total, len(marks))
grade = determine_grade(average)
display_report(marks, total, average, grade)
generate_report_card()
Advantages of Decomposition:
1. Manageability: Large, complex problems become a set of smaller, solvable problems. Each sub-problem is easier to understand and solve.
2. Independent development: Different programmers can work on different sub-problems simultaneously, speeding up development.
3. Easier testing: Each sub-problem can be tested independently. For example, we can test determine_grade() with various average values without needing the full program.
4. Easier debugging: When an error occurs, it is easier to locate which sub-problem caused it, rather than searching through one large monolithic program.
5. Reusability: Solved sub-problems can be reused in other programs. For instance, calculate_average() can be used in any program that needs an average calculation.
6. Better understanding: Decomposing a problem helps programmers understand the problem better. The process of identifying sub-problems clarifies the overall structure and requirements.
7. Maintainability: When changes are needed, only the relevant sub-problem needs modification, without affecting others. For example, changing the grading scale only requires modifying determine_grade().
Question 4
Explain the relationship between specification, abstraction, and decomposition with suitable examples.
Specification, abstraction, and decomposition are three fundamental concepts in algorithmic problem solving that work together to produce well-structured programs.
Their Relationship:
1. Decomposition breaks a complex problem into smaller sub-problems. 2. Specification precisely defines what each sub-problem should do (inputs, outputs, conditions). 3. Abstraction hides the internal details of each sub-problem, exposing only the specification.
Together, they form a disciplined approach to programming:
Detailed Example — Temperature Conversion System:
Suppose we need a program that converts temperatures between Celsius, Fahrenheit, and Kelvin, and displays a comparison table.
Step 1: Decomposition
Step 2: Specification
# Specification: celsius_to_fahrenheit
# Input: c — a real number representing temperature in Celsius
# Output: A real number representing temperature in Fahrenheit
# Pre-condition: c is a valid real number
# Post-condition: output = (c * 9/5) + 32
# Specification: fahrenheit_to_celsius
# Input: f — a real number representing temperature in Fahrenheit
# Output: A real number representing temperature in Celsius
# Pre-condition: f is a valid real number
# Post-condition: output = (f - 32) * 5/9
# Specification: celsius_to_kelvin
# Input: c — a real number representing temperature in Celsius
# Output: A real number representing temperature in Kelvin
# Pre-condition: c >= -273.15 (absolute zero)
# Post-condition: output = c + 273.15
Step 3: Abstraction — Implementation Hidden
# Users of these functions only need to know the specifications.
# They don't need to know the formula or how it's computed.
temp_f = celsius_to_fahrenheit(100) # Returns 212.0
temp_c = fahrenheit_to_celsius(212) # Returns 100.0
temp_k = celsius_to_kelvin(0) # Returns 273.15
The user calls celsius_to_fahrenheit(100) and gets 212.0. They do not need to know whether the implementation uses (c 9/5) + 32 or (c 1.8) + 32 — that is hidden by abstraction.
How They Complement Each Other:
| Concept | Role | Benefit |
|---|---|---|
| Decomposition | Breaks problem into parts | Makes complex problems manageable |
| Specification | Defines each part precisely | Ensures correctness and clarity |
| Abstraction | Hides internal details | Reduces complexity for users |
Without decomposition, we would try to solve the entire problem at once, leading to complex, hard-to-manage code. Without specification, we would not have a clear understanding of what each part should do. Without abstraction, users of each part would need to understand all the internal details, making the program harder to use and maintain.
9. Common Mistakes to Avoid
1. Confusing specification with implementation: Specification describes WHAT a function does; implementation describes HOW it does it. Do not include code or algorithm details in a specification. For example, saying "uses a for loop to find the maximum" is implementation, not specification.
2. Omitting pre-conditions: Always state the pre-conditions clearly. If your function assumes the input list is non-empty, state it explicitly. Missing pre-conditions can lead to incorrect results or runtime errors.
3. Confusing pre-condition and post-condition: Remember — pre-condition is what must be true BEFORE the function runs (the caller's responsibility); post-condition is what is guaranteed AFTER the function runs (the function's responsibility).
4. Thinking abstraction makes programs faster: Abstraction is about managing complexity, not improving performance. In some cases, abstraction layers can add a small overhead. The primary benefit is code clarity, modularity, and maintainability.
5. Incomplete decomposition: When decomposing a problem, ensure each sub-problem is well-defined and has clear inputs and outputs. Vague or overlapping sub-problems lead to confusion. Each sub-problem should have a single, clear purpose.
6. Writing specifications that are too vague: A specification like "this function sorts things" is too vague. A proper specification should be precise: "Given a list of integers, returns a new list containing the same elements in non-decreasing order."
7. Forgetting edge cases in specifications: When writing specifications, consider edge cases such as empty lists, zero values, negative numbers, and boundary conditions. For example, specifying that a function finds the "minimum of a list" should include the pre-condition that the list is non-empty.
8. Treating decomposition as just dividing code into functions: Decomposition is not merely about splitting code into functions. It requires thoughtful analysis of the problem to identify logical sub-problems that can be solved independently and then composed correctly.
9. Mixing "what" and "how" in specifications: A specification should never include implementation details like "iterates through the list using a for loop" or "uses recursion." The specification should only describe the relationship between input and output.
10. Ignoring the contract nature of specifications: A specification is a contract. If a caller violates the pre-condition, the function is not obligated to produce the correct result. Students often forget this and try to handle all invalid inputs inside the function rather than clearly stating the pre-condition.
10. Exam Checklist
Before your exam, make sure you can confidently address the following points:
- [ ] Define specification — a precise description of what a function does (inputs, outputs, conditions)
- [ ] Define abstraction — hiding implementation details, exposing only essential features
- [ ] Differentiate specification vs. implementation — specification = WHAT, implementation = HOW
- [ ] Define decomposition — breaking a complex problem into smaller sub-problems
- [ ] Explain pre-condition — what must be true before a function is called
- [ ] Explain post-condition — what is guaranteed after a function completes
- [ ] Give examples of procedural abstraction — using
len(),sorted(),print()without knowing internal code - [ ] Give examples of data abstraction — using Stack, Queue, or List operations without knowing internal storage
- [ ] Distinguish procedural abstraction vs. data abstraction — hiding "how it works" vs. hiding "how data is stored"
- [ ] Write a complete function specification — including function name, input, output, pre-condition, and post-condition
- [ ] Decompose a given problem into sub-problems — identify, specify, and combine sub-solutions
- [ ] Explain why abstraction reduces complexity — users interact with simple interfaces, not internal details
- [ ] Explain why decomposition aids in testing and debugging — each sub-problem can be tested independently
- [ ] Describe the relationship between specification, abstraction, and decomposition — how they work together in problem solving
- [ ] Identify common mistakes — confusing pre/post conditions, mixing "what" and "how," vague specifications
- [ ] Write Python code examples demonstrating each concept — specifications with comments, decomposed functions, abstracted interfaces
These solutions cover the key topics from the TN State Board Class 11 Computer Science chapter on Specification and Abstraction. Practice writing your own specifications and decomposing problems to build confidence for the exam.
