Conditional and Looping Constructs
Solutions for Informatics Practices, Class 11, CBSE
Assertions And Reasons
8 questionsAssertion (A): When a set of statements is indented under the same block, starting from the same indentation, it is said to be a compound statement.
Reasoning (R): Compound Statement begins with a header ending with a colon (:) sign and the statements under the same indentation are marked as a block.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
Both A and R are true and R is the correct explanation of A.
Explanation
A compound statement in Python is formed when multiple statements are grouped together under the same block of indentation. The header line begins with a keyword and ends with a colon (:). Subsequent statements that are indented under the same level are considered part of the block.
Assertion (A): The conditional flow of control is implemented using if statement.
Reasoning (R): A statement or statements (block of code) are indented (usually 4 spaces) inside the if statement and are executed only if the condition evaluates to true, otherwise they are ignored.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
Both A and R are true and R is the correct explanation of A.
Explanation
The if statement in Python allows conditional flow of control. It evaluates a condition and executes one or more statements based on whether the condition is true or false. If the condition evaluates to true, the statement block following the if statement (indented code) gets executed, otherwise, if the condition is false, that block is skipped, and the program continues with the next statement after the if block.
Assertion (A): In an if-else statement, the if block checks the true part whereas else checks for the false part.
Reasoning (R): In a conditional construct, else block is mandatory.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
A is true but R is false.
Explanation
In an if-else statement, the if block checks if a condition is true and will execute a block of statements and if the condition is false, it won't execute that block. The else part executes a block of code when the if condition is false. In a conditional construct, the else block is not mandatory.
Assertion (A): When a Python code is unable to accept an input, it results in runtime error.
Reasoning (R): Runtime error arises due to incorrect statement that results in no output.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
A is true but R is false.
Explanation
When a Python code is unable to accept an input, it results in a runtime error. Runtime errors arise due to issues that occur while the program is running, such as attempting to use invalid operations on data types. In contrast, syntax error arises due to incorrect statement that results in no output.
Assertion (A): The break and continue statements are known as jump statements.
Reasoning (R): Jump statements are used only with looping constructs and not with conditional constructs.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
Both A and R are true and R is the correct explanation of A.
Explanation
In Python, break and continue are categorized as jump statements because they alter the flow of control in loops. Jump statements, such as break and continue, are used within loop constructs to control the flow of execution. Python does not have a switch-case conditional construct. Hence, in Python, jump statements are not used with conditional constructs.
Assertion (A): The range() method is used with both for and while loops.
Reasoning (R): By default, the values for start and step are 0 (zero) and 1 (one), respectively.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
A is false but R is true.
Explanation
The range() function is used with for loops to iterate over a sequence of numbers, not with while loops. By default, the values for start and step are 0 (zero) and 1 (one), respectively.
Assertion (A): The for loop is described as finite loop and while loop is described as unknown or indefinite iterative construct.
Reasoning (R): You cannot use while loop for menu-driven programs.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
A is true but R is false.
Explanation
The for loop is described as a finite loop because it iterates over a sequence of elements with a known length. The while loop is described as an indefinite iterative construct because it continues to execute as long as a condition is true, without a predetermined number of iterations. We can use a while loop for menu-driven programs to repeatedly display a menu and process user input until a certain condition is met.
Assertion (A): Indentation is a very important factor while writing a program in Python.
Reasoning (R): Indentation refers to the spaces at the beginning of a code line. Multiple statements with the same indent are classified as a block of code.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
Answer:
Both A and R are true and R is the correct explanation of A.
Explanation
Indentation is a very important factor while writing a program in Python because it defines the structure and grouping of code blocks. Indentation refers to the spaces at the beginning of a code line, and multiple statements with the same indent level are classified as a block of code.
Fill In The Blanks
10 questionsMultiple Choice Questions
10 questionsAnswer:
for statement
Reason — The for statement is not a decision-making statement in Python. Instead, the for loop statement is used to iterate/repeat itself over a range of values or a sequence.
Answer:
do-while
Reason — In Python, the do-while loop statement does not exist. The language provides only two primary loop constructs: the for loop and the while loop, which are used to handle different looping requirements.
Answer:
['ab', 'cd']
Reason — In the given code, the for loop iterates over the list x containing strings 'ab' and 'cd'. Inside the loop, i.upper() is called, which returns a new string in uppercase but doesn't modify i itself because strings in Python are immutable. Therefore, i.upper() doesn't change i or x in place. After the loop finishes, x remains unchanged, so the output is ['ab', 'cd'].
Answer:
1,4,7,10,13,16,19,
Reason — The above code uses a for loop to iterate over a sequence of numbers generated by range(1, 20, 3). This range starts at 1, ends before 20, and increments by 3 in each step. During each iteration, the current value of x is printed followed by a comma due to the end=',' parameter in the print function. Therefore, the output is 1,4,7,10,13,16,19, showing the sequence of numbers separated by commas.
Answer:
Conditional statements.
Reason — The if statement is a conditional statement because it is used to control flow of execution of a program depending upon the condition.
Solutions To Unsolved Questions
36 questionsConstruct a logical expression to represent each of the following conditions:
(a) Marks are greater than or equal to 70 but less than 100.
(b) Num is between 0 and 5 but not equal to 2.
(c) Answer is either 'N' OR 'n'.
(d) Age is greater than or equal to 18 and gender is male.
(e) City is either 'Kolkata' or 'Mumbai'.
Answer:
(a) (marks >= 70) and (marks < 100)
(b) (num > 0) and (num < 5) and (num != 2)
(c) (answer == 'N') or (answer == 'n')
(d) (age >= 18) and (gender == 'male')
(e) (city == 'Kolkata') or (city == 'Mumbai')
Answer:
code = input("Enter season code : ")
if code=w: #Error 1
print "winter season" #Error 2
elif code==r: #Error 3
PRINT "rainy season" #Error 4
else:
Print "summer season" # Error 5Error 1 — The assignment operator '=' is used instead of equality operator '==' in the if statement and the string 'w' should be enclosed in quotes.
Error 2 — Parentheses around the print statement are missing.
Error 3 — String 'r' should be enclosed in quotes.
Error 4 — The print statement should be in lowercase letters and there should be parentheses around the print statement.
Error 5 — The print statement should be in lowercase letters and there should be parentheses around the print statement.
The corrected code is :
code = input("Enter season code: ")
if code == "w":
print("winter season")
elif code == "r":
print("rainy season")
else:
print("summer season") Answer:
9
In the above code, num1 += num2 + num3 is a shorthand notation for num1 = num1 + num2 + num3.
Given,
num1 = 4
num2 = 3
num3 = 2
First, the expression num2 + num3 is evaluated:
num2 + num3 = 3 + 2 = 5
Now the expression becomes:
num1 = num1 + 5
which translates to:
num1 = 4 + 5 = 9
Answer:
27.2
num1 = 2 + 9 * ((3*12)-8)/10Let's calculate this step by step, following the precedence of operators in Python:
1. Evaluate the innermost parentheses first:
3 * 12 = 362. Replace and continue evaluating inside the parentheses:
((3*12)-8) => (36 - 8) = 283. Now, evaluate the multiplication and division:
9 * 28 = 2524. Continue with dividing by 10:
252 / 10 = 25.25. Finally, add 2:
2 + 25.2 = 27.2The print(num1) statement outputs 27.2
Answer:
The code will raise a Error because we cannot convert the string '3.14' to an integer directly using int() function in Python due to the presence of a decimal point. Python expects the string to represent a whole number when converting to an integer.
Answer:
True
In the expression 5 % 10 + 10 < 50 and 29 <= 29, according to operator precedence, 5 % 10 calculates to 5, then 5 + 10 equals 15. Next, 15 < 50 evaluates to True. Separately, 29 <= 29 also evaluates to True. The and operator then combines these results. Since both conditions are True, the and operator yields True, which is printed.
Answer:
The else statement is a default condition that executes when the preceding if and elif (if any) conditions evaluate to False. It does not take any conditions and is written simply as else:, followed by an indented block of code. On the other hand, elif is used to check additional conditions after the initial if statement. If the if condition is False, Python evaluates the elif condition. It is followed by a condition and ends with a colon (:), followed by a block of code.
Answer:
20
22
24
26
28
Below is a detailed explanation of this code:
range(20, 30, 2): This generates a sequence of numbers starting from 20 (inclusive) up to but not including 30 (exclusive), incrementing by 2 each time.
The general form of therangefunction isrange(start, stop, step),where:startis the starting number of the sequence.stopis the endpoint (the number is not included in the sequence).stepspecifies the increment between each number in the sequence.
for i in range(20, 30, 2):: Theforloop iterates over each number generated by therangefunction and assigns it to the variableiduring each iteration.print(i): This prints the current value ofifor each iteration.
Let's list the numbers generated by range(20, 30, 2):
- Start at 20.
- Increment by 2: 20 + 2 = 22.
- Increment by 2: 22 + 2 = 24.
- Increment by 2: 24 + 2 = 26.
- Increment by 2: 26 + 2 = 28.
- The next increment would be 28 + 2 = 30, but 30 is not included as the
stopparameter is exclusive of the value.
Therefore, the sequence of numbers is: 20, 22, 24, 26, 28.
Answer:
I
N
D
I
A
Here's the detailed explanation:
1. String Assignment:
country = 'INDIA'This line assigns the string 'INDIA' to the variable country.
2. for i in country:: The for loop iterates over each character in the string country. The variable i will take on the value of each character in the string one at a time.
3. print(i): This prints the current character stored in the variable i for each iteration.
Let’s analyze the loop iteration by iteration:
- In the first iteration,
iis'I', soprint(i)outputs:I. - In the second iteration,
iis'N', soprint(i)outputs:N. - In the third iteration,
iis'D', soprint(i)outputs:D. - In the fourth iteration,
iis'I', soprint(i)outputs:I. - In the fifth iteration,
iis'A', soprint(i)outputs:A.
Putting it all together, the output will be each character of the string 'INDIA' printed on a new line:
I
N
D
I
A
Answer:
12
Let's break down the code step-by-step:
1. Initial Assignments:
i = 0
sum = 0
Two variables are initialized: i is set to 0, and sum is set to 0.
2. while i < 9: → This loop continues to execute as long as i is less than 9.
3. Inside the while loop:
First iteration (i = 0):
if i % 4 == 0:→0 % 4equals0, so the condition is true.sum = sum + i→sumis updated tosum + 0, which is0 + 0 = 0.i = i + 2→iis updated to0 + 2 = 2.
Second iteration (i = 2):
if i % 4 == 0:→2 % 4equals2, so the condition is false.- The value of
sumremains unchanged. i = i + 2→iis updated to2 + 2 = 4.
Third iteration (i = 4):
if i % 4 == 0:→4 % 4equals0, so the condition is true.sum = sum + i→sumis updated tosum + 4, which is0 + 4 = 4.i = i + 2→iis updated to4 + 2 = 6.
Fourth iteration (i = 6):
if i % 4 == 0:→6 % 4equals2, so the condition is false.- The value of
sumremains unchanged. i = i + 2→iis updated to6 + 2 = 8.
Fifth iteration (i = 8):
if i % 4 == 0:→8 % 4equals0, so the condition is true.sum = sum + i→sumis updated tosum + 8, which is4 + 8 = 12.i = i + 2→iis updated to8 + 2 = 10.
Sixth iteration (i = 10):
- The loop condition
while i < 9:is no longer true (10 is not less than 9), so the loop terminates.
- The loop condition
4. print(sum) → After exiting the loop, the value of sum is printed.
Summarizing all updates to sum:
- After the first iteration,
sum= 0. - After the third iteration,
sum= 4. - After the fifth iteration,
sum= 12.
Therefore, the output of the code is:
12
Answer:
20
16
1. range(10, 6, -2): This generates a sequence of numbers starting from 10 (inclusive) and decreases by 2 each time, stopping before it reaches 6 (exclusive).
The general form of the range function is range(start, stop, step), where:
startis the starting number of the sequence.stopis the endpoint (the number is not included in the sequence).stepspecifies the increment (or decrement, if negative) between each number in the sequence.
2. The sequence generated by range(10, 6, -2) is:
- Start at 10.
- Decrement by 2: 10 - 2 = 8.
- The next decrement would be 8 - 2 = 6, but 6 is not included as it is exclusive.
So, the sequence generated is: 10, 8.
3. for j in range(10, 6, -2):: The for loop iterates over each number in the sequence generated by the range function.
4. print(j * 2): This prints the value of j multiplied by 2 for each iteration.
Let’s analyze the loop iteration by iteration:
First iteration (j = 10):
print(10 * 2)This prints:
20
Second iteration (j = 8):
print(8 * 2)This prints:
16
Putting it all together, the full output will be:
20
16
Answer:
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5
Below is the detailed explanation of this code:
1. Outer Loop: for x in range(1, 6):
- This loop iterates over the range of numbers starting from 1 up to but not including 6. So,
xwill take on the values: 1, 2, 3, 4, 5.
2. Inner Loop: for y in range(1, x + 1):
- For each value of
xin the outer loop,ywill iterate over the range of numbers starting from 1 up to and includingx(sincex + 1is exclusive).
3. print(x, ' ', y)
- This prints the current values of
xandyseparated by a space.
Let’s analyze the loops iteration by iteration.
Outer loop (x values ranging from 1 to 5):
When x = 1:
- The inner loop
range(1, 2)results inytaking values: 1. - Output:
1 1
When x = 2:
- The inner loop
range(1, 3)results inytaking values: 1, 2. - Output:
2 1 2 2
When x = 3:
- The inner loop
range(1, 4)results inytaking values: 1, 2, 3. - Output:
3 1 3 2 3 3
When x = 4:
- The inner loop
range(1, 5)results inytaking values: 1, 2, 3, 4. - Output:
4 1 4 2 4 3 4 4
When x = 5:
- The inner loop
range(1, 6)results inytaking values: 1, 2, 3, 4, 5. - Output:
5 1 5 2 5 3 5 4 5 5
Putting all these outputs together, we get:
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
4 3
4 4
5 1
5 2
5 3
5 4
5 5
Answer:
11
13
15
17
19
Below is the detailed explanation of this code:
for x in range(10, 20):→ This loop iterates over the range of numbers starting from 10 (inclusive) up to but not including 20 (exclusive). So,xwill take on the values: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19.if x % 2 == 0:→ For each value ofx, this condition checks ifxis an even number. The modulus operator%returns the remainder ofxdivided by 2. If the remainder is 0,xis even.continue→ If the conditionx % 2 == 0is true (i.e.,xis even), thecontinuestatement is executed. This statement skips the rest of the code inside the loop for the current iteration and moves to the next iteration.print(x)→ This line prints the value ofxif it is not even (i.e., if thecontinuestatement is not executed).
Let’s analyze the loop iteration by iteration:
When
x = 10:10 % 2 == 0is true.continueis executed, soprint(x)is skipped.
When
x = 11:11 % 2 == 0is false.print(x)outputs:11
When
x = 12:12 % 2 == 0is true.continueis executed, soprint(x)is skipped.
When
x = 13:13 % 2 == 0is false.print(x)outputs:13
When
x = 14:14 % 2 == 0is true.continueis executed, soprint(x)is skipped.
When
x = 15:15 % 2 == 0is false.print(x)outputs:15
When
x = 16:16 % 2 == 0is true.continueis executed, soprint(x)is skipped.
When
x = 17:17 % 2 == 0is false.print(x)outputs:17
When
x = 18:18 % 2 == 0is true.continueis executed, soprint(x)is skipped.
When
x = 19:19 % 2 == 0is false.print(x)outputs:19
Putting all these outputs together, the full output will be:
11
13
15
17
19
Answer:
ok
average
if x > 10:- The condition checks if
xis greater than 10. - Since
x = 50, which is greater than 10, this condition is true.
- The condition checks if
Nested
if x > 25:- Since the outer condition is true, the program proceeds to this nested condition, which checks if
xis greater than 25. - Since
x = 50, which is greater than 25, this condition is also true. - Consequently, it prints
ok
- Since the outer condition is true, the program proceeds to this nested condition, which checks if
Nested
if x > 60:- After printing 'ok', the program checks this nested condition, which verifies if
xis greater than 60. - Since
x = 50, which is not greater than 60, this condition is false. - Therefore, the program proceeds to the
elifpart.
- After printing 'ok', the program checks this nested condition, which verifies if
Nested
elif x > 40:- This condition checks if
xis greater than 40. - Since
x = 50, which is greater than 40, this condition is true. - Consequently, it prints
average
- This condition checks if
The
elseblock- The
elseblock is not executed because theelif x > 40:condition was true.
- The
Answer:
o o
Let’s break down the code step-by-step:
Outer Loop:
for i in range(2):→ This loop iterates over the range of numbers starting from 0 up to but not including 2. So,iwill take on the values: 0, 1.Inner Loop:
for j in range(1):→ For each value ofiin the outer loop,jwill iterate over the range of numbers starting from 0 up to but not including 1. So,jwill take on the only value: 0.if i + 2 == j:→ This condition checks ifi + 2is equal toj.print("+", end=" ")→ If the conditioni + 2 == jis true, this statement prints a "+", followed by a space, without moving to a new line.print("o", end=" ")→ If the conditioni + 2 == jis false, this statement prints an "o", followed by a space, without moving to a new line.
Let’s analyze the loops iteration by iteration:
- First iteration of the outer loop (i = 0):
- Inner loop (j = 0):
i + 2 == jbecomes0 + 2 == 0, which is2 == 0, is false.- Therefore, the
elseblock is executed, and the output iso
- Inner loop (j = 0):
- Second iteration of the outer loop (i = 1):
- Inner loop (j = 0):
i + 2 == jbecomes1 + 2 == 0, which is3 == 0, is false.- Therefore, the
elseblock is executed, and the output iso
- Inner loop (j = 0):
So, combining the outputs of all iterations, the full output will be:
o o
Each "o" is followed by a space, and there is no new line between them due to the end=" " parameter in the print function.
WAP to perform all the mathematical operations of a calculator.
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice (1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
result = num1 + num2
print(num1, "+", num2, "=", result)
elif choice == '2':
result = num1 - num2
print(num1, "-", num2, "=", result)
elif choice == '3':
result = num1 * num2
print(num1, "*", num2, "=", result)
elif choice == '4':
if num2 != 0:
result = num1 / num2
print(num1, "/", num2, "=", result)
else:
print("Division by zero is not allowed.")
else:
print("Invalid input")Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 1
Enter first number: 23
Enter second number: 45
23.0 + 45.0 = 68.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 2
Enter first number: 48
Enter second number: 23
48.0 - 23.0 = 25.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 3
Enter first number: 3
Enter second number: 6
3.0 * 6.0 = 18.0
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice (1/2/3/4): 4
Enter first number: 245
Enter second number: 5
245.0 / 5.0 = 49.0
Answer:
WAP to convert binary number to decimal number.
binary = input("Enter a binary number: ")
decimal = 0
for digit in binary:
decimal = decimal * 2 + int(digit)
print("The decimal equivalent of", binary, "is", decimal)Enter a binary number: 110
The decimal equivalent of 110 is 6
Answer:
WAP to find the sum of the digits of a number.
number = int(input("Enter a number: "))
sum_of_digits = 0
while number > 0:
digit = number % 10
sum_of_digits += digit
number = number // 10
print("Sum of digits:", sum_of_digits)Enter a number: 4556787
Sum of digits: 42
Answer:
WAP to print the sum of the series 1 - x1/2! + x2/3! - x3/4! ............... xn/(n + 1)! — exponential series.
x = int(input("Enter x: "))
n = int(input("Enter n: "))
sum_series = 1
sign = -1
for i in range(1, n + 1):
fact = 1
for j in range(1, i + 2):
fact *= j
term = sign * (x ** i) / fact
sum_series += term
sign *= -1
print("Sum =", sum_series)Enter x: 2
Enter n: 3
Sum = 0.3333333333333333
Answer:
WAP to print the sum of the series 1 - x2/4! + x3/6! - x4/8! + x5/10! ............... xn/(2n)! — exponential series.
x = int(input("Enter x: "))
n = int(input("Enter n: "))
sum_series = 1
sign = -1
for i in range(2, n + 1):
fact = 1
for j in range(1, 2 * i + 1):
fact *= j
term = sign * (x ** i) / fact
sum_series += term
sign *= -1
print("Sum =", sum_series)Enter x: 2
Enter n: 3
Sum = 0.8444444444444444
Answer:
WAP to display the sum of the given series:
Sum = 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3.....n)
n = int(input("Enter the value of n: "))
sum = 0
for i in range(1, n + 1):
for j in range(1, i + 1):
sum += j
print("The sum of the series is:", sum)Enter the value of n: 4
The sum of the series is: 20
Answer:
State True Or False
11 questionsAnswer:
True
Reason — In Python, the else block associated with a for loop is executed when the loop terminates normally, meaning it has iterated over all items or reached its termination condition.