Composition and Decomposition: book back answers
17 solved questions from the end of this chapter, Class 11 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Composition and DecompositionSECTION – A Choose the correct answer
Suppose u, v = 10 ,5 before the assignment. What are the values of u and v after the sequence of assignments? 1 u := v 2 v := u
- (a) u, v = 5 ,5
- (b) u, v = 5 ,10
- (c) u, v = 10 ,5
- (d) u, v = 10 ,10
Try it, then show the answerAnswer
(a) u, v = 5 ,5
The first assignment makes u equal to 5. The second then assigns this new value of u to v.
From the book: “Statements may be composed of other statements, leading to hierarchical structure of algorithms.”
Which of the following properties is true after the assignment (at line 3? 1 --i, j = 0, 0 2 i, j := i+1, j-1 3 -- ?
- (a) i+j >0
- (b) i+j < 0
- (c) i+j =0
- (d) i = j
Try it, then show the answerAnswer
(c) i+j =0
Starting with i = 0 and j = 0, the assignment gives i = 1 and j = -1. Their sum is zero.
From the book: “Let the input property be P, and the input-output relation be Q, for a problem.”
If C1 is false and C2 is true, the compound statement 1 if C1 2 S1 3 else 4 if C2 5 S2 6 else 7 S3 executes
- (a) S1
- (b) S2
- (c) S3
- (d) none
Try it, then show the answerAnswer
(b) S2
Since C1 is false, control goes to the else part and tests C2. Because C2 is true, S2 is executed.
From the book: “If C is true, then do S1; otherwise do S2.”
If C is false just before the loop, the control flows through 1 S1 2 while C 3 S2 4 S3
- (a) S1 ; S3
- (b) S1 ; S2 ; S3
- (c)S1 ; S2 ; S2 ; S3
- (d) S1 ; S2 ; S2 ; S2 ; S3
Try it, then show the answerAnswer
(a) S1 ; S3
The loop body S2 is not executed when C is false. Control proceeds from S1 to the statement after the loop, S3.
From the book: “If C is false, the loop ends and the control flows to the next box after the loop.”
How many times the loop is iterated? i := 0 while i ≠ 5 i := i + 1
- (a) 4
- (b) 5
- (c) 6
- (d) 0
Try it, then show the answerAnswer
(b) 5
Starting at i = 0, the loop increments i until it reaches 5. It executes once for each value from 0 through 4, for 5 iterations.
From the book: “These two steps, testing C and executing S, are repeated until C becomes false.”
SECTION-B
Distinguish between a condition and a statement.
Try it, then show the answerAnswer
A condition is a phrase that describes a test of the state and has a true or false value. A statement is a phrase that commands the computer to do an action.
A condition determines which control-flow path is taken, whereas a statement specifies an action to perform.
From the book: “A condition is a phrase that describes a test of the state.”
Draw a flowchart for conditional statement.
Try it, then show the answerAnswer
A condition box C has a true arrow to statement box S. The false arrow bypasses S; both paths then continue to the next step.
A conditional statement executes S only when C is true; if C is false, it does nothing.
From the book: “If C is true then do S; otherwise do nothing.”
Both conditional statement and iterative statement have a condition and a statement. How do they differ?
Try it, then show the answerAnswer
A conditional statement tests its condition once and executes its statement only if the condition is true. An iterative statement repeatedly tests its condition and executes its statement while the condition is true.
The iterative statement returns to test the condition after each execution of its body; a conditional statement does not repeat.
From the book: “The iterative statement is commonly known as a loop.”
What is the difference between an algorithm and a program?
Try it, then show the answerAnswer
A program is an algorithm expressed in a programming language. An algorithm can also be represented using pseudo-code or a flowchart.
A programming language allows a computer to execute the expressed algorithm, whereas pseudo-code is for human readers and flowcharts visually represent control flow.
From the book: “An algorithm expressed in a programming language is called a program.”
Why is function an abstraction?
Try it, then show the answerAnswer
A function is an abstraction because its user needs to know its specification—its name, input property and input-output relation—but not how it is implemented.
The implementation is hidden from the user, so the function can be used as a black box in solving other problems.
From the book: “users of the function need only to know what the function does, and not how it is done by the function.”
How do we refine a statement?
Try it, then show the answerAnswer
We refine a statement by expanding it into more detailed steps. Each step can in turn be expanded into still finer steps.
This process continues from a high-level action to increasingly detailed actions; the details may also use conditional or iterative statements where appropriate.
From the book: “Each subproblem can be expanded into more detailed steps. Each step can be further expanded to still finer steps, and so on.”
For the given two flowcharts write the pseudo code. C S1 true C S1 true S2 S2
Try it, then show the answerAnswer
Flowchart (1): if C S1 Flowchart (2): if C S1 else S2
The first flowchart is conditional: it performs S1 when C is true and otherwise does nothing. The second is an alternative: it performs S1 when true and S2 when false.
From the book: “The conditional control flow is depicted in the flowchart of Figure 2.5.”
If C is false in line 2, trace the control flow in this algorithm. 1 S1 2 -- C is false 3 if C 4 S2 5 else 6 S3 7 S4
Try it, then show the answerAnswer
S1, then S3, then S4.
After S1, C is false, so the else branch executes S3. Control then continues to S4.
From the book: “If C is true, then do S1; otherwise do S2.”
What is case analysis?
Try it, then show the answerAnswer
Case analysis splits a problem into an exhaustive set of disjoint cases and solves the problem independently for each case. The first true case is executed; if none of the stated conditions is true, the default case is executed.
Exhaustive cases cover every possibility, while disjoint cases ensure that only one case is selected for execution.
From the book: “Case analysis splits the problem into an exhaustive set of disjoint cases.”
Define a function to double a number in two different ways: (1) n + n, (2) 2 x n
Try it, then show the answerAnswer
double(n) -- input: n -- output: result = 2 × n Implementation 1: double(n) result := n + n Implementation 2: double(n) result := 2 × n
Both implementations return twice the input n: the first adds n to itself, and the second multiplies it by 2.
From the book: “A function is specified by the input property, and the desired input-output relation.”
Explain in detail
Exchange the contents: Given two glasses marked A and B. Glass A is full of apple drink and glass B is full of grape drink. Write the specification for exchanging the contents of glasses A and B, and write a sequence of assignments to satisfy the specification.
Try it, then show the answerAnswer
The contents of glasses A and B must be exchanged: A should contain grape drink and B should contain apple drink. Use a temporary variable to preserve one drink while assigning the other.
- Specification: Initially, A contains apple drink and B contains grape drink. The required final state is A containing grape drink and B containing apple drink.
- Use a temporary variable: Let T be a temporary variable. First assign the contents of A to T so the apple drink is preserved.
- Move grape drink to A: Assign the contents of B to A. A now contains grape drink, while the apple drink remains stored in T.
- Move apple drink to B: Assign the contents of T to B. This completes the exchange, leaving A with grape drink and B with apple drink.
- Assignment sequence: The required sequence is: T := A; A := B; B := T.
Thus, using a temporary variable and sequential assignments exchanges the contents of glasses A and B.
From the book: “A sequential statement is composed of a sequence of statements.”
Circulate the contents: Write the specification and construct an algorithm to circulate the contents of the variables A, B and C as shown below: The arrows indicate that B gets the value of A, C gets the value of B and A gets the value of C. A B C
Try it, then show the answerAnswer
Circulating the contents means assigning the original value of A to B, the original value of B to C, and the original value of C to A. A temporary variable is needed to preserve a value while the assignments are made.
- Preserve A: Store the original value of A in a temporary variable T before changing A.
- Transfer B to A: Assign the original value of C to A, after saving the original A.
- Transfer C to B: Assign the original value of A, held in T, to B.
- Transfer A to C: Assign the original value of B to C. The sequence then produces the required circulation.
Example: If the initial values are A = 1, B = 2 and C = 3, the final values are A = 3, B = 1 and C = 2.
Thus, using a temporary variable and sequential assignments circulates the contents of A, B and C as indicated.
From the book: “In order to execute the sequential statement, first do S1 and then do S2.”