KnowledgeBoat Logo
|
OPEN IN APP

Part II: AI — Chapter 3.4

Conditional Statements

Class 9 - Exploring Robotics & AI



Multiple Choice Questions

Question 1

What type of value is returned by math.sqrt()?

  1. int
  2. float
  3. integer
  4. double

Answer

float

Reason — The math.sqrt() function always returns a float type value for a given argument, regardless of whether the input is an integer or a float. For example, math.sqrt(49) returns 7.0, not 7.

Question 2

Which of the following syntax is true to find the square root of a number 'n'?

  1. sqrt(n)
  2. math.sqrt(n)
  3. Squareroot(n)
  4. n * n

Answer

math.sqrt(n)

Reason — The sqrt() function is defined in the math module, so it must be called as math.sqrt(n) after importing the math module.

Question 3

Choose the correct statement to call Python math module:

  1. import.Math
  2. import.maths
  3. import.math
  4. import math

Answer

import math

Reason — The math module in Python is called using the keyword import followed by the module name math (which is case-sensitive and lowercase).

Question 4

Give the output of math.sqrt(a), when a = 49.

  1. 7
  2. 7.0
  3. 7.00
  4. all

Answer

7.0

Reason — The math.sqrt() function always returns a float type value. Hence, the square root of 49 is returned as 7.0 (with a single decimal point), not 7 or 7.00.

Question 5

Which of the following is not a decision-making statement in Python?

  1. if statement
  2. if-else statement
  3. if-else-if statement
  4. if-elif-else statement

Answer

if-else-if statement

Reason — In Python, the correct keyword used between if and else for multiple condition checks is elif, not else-if. Hence, if-else-if is not a valid decision-making construct in Python, the correct form is if-elif-else.

Question 6

What symbol is used to represent "equal to" in Python's relational operator?

  1. ==
  2. =
  3. :=
  4. :=:

Answer

==

Reason — In Python, the == operator is the relational operator used to check whether two values are equal. The single = is the assignment operator, used to assign a value to a variable.

Question 7

Which of the following is a Python valid statement?

  1. if (num>=25):
  2. if (num>=25)
  3. if (num=>25)
  4. if (num=<25)

Answer

if (num>=25):

Reason — In Python, an if statement must end with a colon (:), and the correct relational operators for "greater than or equal to" and "less than or equal to" are >= and <= respectively, not => or =<.

Question 8

An else statement must be preceded by ............... statement in Python.

  1. if
  2. elif
  3. if or elif
  4. all of them

Answer

if or elif

Reason — In Python, an else statement can be preceded by either an if statement (in if-else construct) or an elif statement (in if-elif-else construct). It cannot exist on its own without a preceding if or elif.

State True or False

Question 1

State whether the following statements are True or False:

  1. A conditional statement is essentially formed by using relational operators.
  2. An if-else construct only checks the first condition of the statement.
  3. The if statement is also called as a conditional statement.
  4. The 'Run Time' error occurs due to wrong logic.
  5. Python language provides some built-in functions that can be used in codes.
  6. The square root of a negative number can be determined by using math.sqrt( ) function.

Answer

  1. True
  2. False
    Corrected Statement: An if-else construct checks a condition and executes one block of statements if the condition is true; otherwise, it executes another block of statements.
  3. True
  4. False
    Corrected Statement: The 'Run Time' error occurs due to mistakes that interrupt the correct execution of the program, such as division by zero or finding the square root of a negative number. Wrong logic results in a Logical Error, not a Run Time error.
  5. True
  6. False
    Corrected Statement: The square root of a negative number cannot be determined by using math.sqrt() function as it results in a ValueError. The square root of a negative number is an imaginary number.

Predict Output — Mathematical functions

Question 1

Predict the output of the following Mathematical functions:

Mathematical FunctionOutput
math.sqrt(10.24)
min(-25.5, -12.5)
abs(-99.4)
pow(10,-2)
max(-25.5, -12.5)

Answer

Mathematical FunctionOutputExplanation
math.sqrt(10.24)3.2The math.sqrt() function returns the square root as a float. The square root of 10.24 is 3.2.
min(-25.5, -12.5)-25.5The min() function returns the smallest value. Among the two negative values, -25.5 is smaller (higher magnitude means lower value for negative numbers).
abs(-99.4)99.4The abs() function returns the absolute (magnitude) value of the number, which is always positive.
pow(10,-2)0.01The pow(a, b) function returns a raised to the power b. Here, 10-2 = 1/100 = 0.01 (float type).
max(-25.5, -12.5)-25.5The max() function returns the largest value. Among the two negative values, -12.5 is greater than -25.5.

Predict Output — Snippets

Question 1

Predict the output of the following snippet:

n = 25
if n < 15:
    print("The number is less than 15")
if n <= 30:
    print("The number is less than or equal to 30")
else:
    print("Try Again!")

Answer

Output
The number is less than or equal to 30
Explanation

The value of n is 25. In the first if statement, the condition n < 15 (i.e., 25 < 15) is False, so its block is skipped. In the second if statement, the condition n <= 30 (i.e., 25 <= 30) is True, so it prints "The number is less than or equal to 30". Since the condition is True, the else block associated with the second if statement is ignored.

Question 2

Predict the output of the following snippet:

m, n, p = 11, 15, 18
if m > 0:
    if n < 15:
        print("Correct!")
    elif p > 15:
        print("OK")
    else:
        print("Better Next Time!")

Answer

Output
OK
Explanation

In the statement m, n, p = 11, 15, 18, multiple variable assignment is used, where the values 11, 15, and 18 are assigned to m, n, and p respectively. The first condition m > 0 (i.e., 11 > 0) is True, so the control enters the outer if block. Then the condition n < 15 (i.e., 15 < 15) is checked, which is False. Therefore, the control moves to the elif condition p > 15 (i.e., 18 > 15), which is True, so the statement print("OK") gets executed and displays OK. Since the elif condition is True, the else block is skipped.

Assertion and Reason based question

Question 1

Assertion (A): In Python, there are some built-in functions which enhance the capability of processing and also reduce the complexity of programming logic.

Reason (R): These functions are defined in Python math module where, you need to define import math in the code to use them to produce the result.

Based on the above discussion, choose an appropriate statement from the options given below:

  1. Both A and R are true and R is the correct explanation of A.
  2. Both A and R are true and R is not the correct explanation of A.
  3. A is true but R is false.
  4. A is false but R is true.
  5. Both A and R are false.

Answer

A is true but R is false.

Reason — Assertion (A) is correct because Python does provide built-in functions like max(), min(), pow(), abs(), etc., that enhance processing capability and reduce complexity. However, Reason (R) is false because not all built-in functions are defined in the math module - functions like max(), min(), pow(), and abs() are directly available without importing the math module. Only specific functions like sqrt(), ceil(), floor(), etc., require the math module to be imported.

Application based question

Question 1(a)

Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:

............... function is used to return an absolute value (i.e., only magnitude of the number) as an int data type.

  1. value()
  2. absolute()
  3. abs()
  4. val()

Answer

abs()

Reason — The abs() function returns the magnitude (absolute value) of the given number. For an integer argument, it returns an int data type.

Question 1(b)

Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:

............... function is used to find the square root of a positive number.

  1. sqr()
  2. sqrt()
  3. root()
  4. sqroot()

Answer

sqrt()

Reason — The sqrt() function is used to find the square root of a positive number. In Python, it is used as math.sqrt().

Question 1(c)

Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:

What will be the return value for math.sqrt(-49)?

  1. 7.0
  2. 7
  3. ValueError
  4. Not known

Answer

ValueError

Reason — The square root of a negative number is an imaginary number. Therefore, math.sqrt(-49) results in a ValueError.

Question 1(d)

Pawan is participating in a quiz program to be carried on Mathematical functions. He has to pick an appropriate option for a question given below:

What will be the return value for max(-91.91, -91.9)?

  1. -91.90
  2. -91.9
  3. -91.19
  4. -91.00

Answer

-91.9

Reason — The max() function returns the largest value among the given arguments. Between -91.91 and -91.9, the value -91.9 is greater.

Python Programs

Question 1

Write a program in Python to find and display the diagonal of a square taking side of the square as an input.

Solution
import math
s = int(input("Enter the side of the square: "))
d = s * math.sqrt(2)
print("Diagonal of the square:", d)
Output
Enter the side of the square: 10
Diagonal of the square: 14.142135623730951

Question 2

Write a program in Python to calculate and display the hypotenuse of a Right-Angled Triangle by taking perpendicular and base as inputs.

[Hint: h=p2+b2h = \sqrt{p^2 + b^2}]

Solution
import math
p = int(input("Enter the perpendicular: "))
b = int(input("Enter the base: "))
h = math.sqrt(p*p + b*b)
print("Hypotenuse of the right-angled triangle:", h)
Output
Enter the perpendicular: 3
Enter the base: 4
Hypotenuse of the right-angled triangle: 5.0

Question 3

Write a program in Python to accept two numbers. If the two numbers are equal, then find the sum, otherwise find the product.

Solution
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
if a == b:
    s = a + b
    print("Sum of the two numbers:", s)
else:
    p = a * b
    print("Product of the two numbers:", p)
Output
Enter the first number: 11
Enter the second number: 11
Sum of the two numbers: 22

Enter the first number: 21
Enter the second number: 12
Product of the two numbers: 252

Question 4

Write a Python program to input a number. Check whether the number is positive, negative or the number is zero. Display the message accordingly.

Solution
n = int(input("Enter a number: "))
if n > 0:
    print("The number is positive")
elif n < 0:
    print("The number is negative")
else:
    print("The number is zero")
Output
Enter a number: -25
The number is negative

Question 5

Write a program in Python to input year and check whether it is a leap year or not.

[Hint: if (Year % 400 == 0) or (Year % 100 != 0) and (Year % 4 == 0), said to be a leap year]

For example,

Input: 1600 Output: It is a leap year

Input: 2024 Output: It is a leap year

Input: 1900 Output: It is not a leap year

Solution
year = int(input("Enter a year: "))
if (year % 400 == 0) or ((year % 100 != 0) and (year % 4 == 0)):
    print("It is a leap year")
else:
    print("It is not a leap year")
Output
Enter a year: 1600
It is a leap year

Enter a year: 2024
It is a leap year

Enter a year: 1900
It is not a leap year

Question 6

Write a Python program to enter the cost price and the selling price of an article. If the selling price (SP) is more than the cost price (CP) then calculate the profit and the profit percent. Otherwise, calculate the loss and the loss percent.

Solution
cp = float(input("Enter the cost price: "))
sp = float(input("Enter the selling price: "))
if sp > cp:
    profit = sp - cp
    profit_percent = (profit / cp) * 100
    print("Profit:", profit)
    print("Profit Percent:", profit_percent)
else:
    loss = cp - sp
    loss_percent = (loss / cp) * 100
    print("Loss:", loss)
    print("Loss Percent:", loss_percent)
Output
Enter the cost price: 500
Enter the selling price: 650
Profit: 150.0
Profit Percent: 30.0

Enter the cost price: 800
Enter the selling price: 600
Loss: 200.0
Loss Percent: 25.0

Question 7

Write a program in Python to accept two angles and perform the following tasks:

(i) If the sum is equal to 90 the display "Complementary Angles"

(ii) If the sum is equal to 180 the display "Supplementary Angles"

(iii) Otherwise, display "Neither Complementary nor Supplementary Angles"

Solution
a = int(input("Enter the first angle: "))
b = int(input("Enter the second angle: "))
total = a + b
if total == 90:
    print("Complementary Angles")
elif total == 180:
    print("Supplementary Angles")
else:
    print("Neither Complementary nor Supplementary Angles")
Output
Enter the first angle: 30
Enter the second angle: 60
Complementary Angles

Enter the first angle: 120
Enter the second angle: 60
Supplementary Angles

Enter the first angle: 40
Enter the second angle: 70
Neither Complementary nor Supplementary Angles

Question 8

A shopkeeper offers 30% discount on purchasing an article whereas the other shopkeeper offers two successive discounts 20% and 10% for purchasing the same article. Write a program in Python to compute and display the discounts offered by both the shopkeepers. Take the price of the article as an input. Finally, display which is the better deal for the customer.

Solution
price = int(input("Enter price of the article:"))

# First shopkeeper
d1 = (30 / 100) * price
sp1 = price - d1

# Second shopkeeper
d2 = (20 / 100) * price
price_after_first = price - d2

d3 = (10 / 100) * price_after_first
total_discount = d2 + d3

sp2 = price - total_discount

print("Discount offered by first shopkeeper =", d1)
print("Discount offered by second shopkeeper =", total_discount)

if(d1 > total_discount):
    print("First shopkeeper offers a better deal")
elif(total_discount > d1):
    print("Second shopkeeper offers a better deal")
else:
    print("Both offers are same")
Output
Enter price of the article:1000
Discount offered by first shopkeeper = 300.0
Discount offered by second shopkeeper = 280.0
First shopkeeper offers a better deal

Question 9

A number is said to be a perfect square number, if its square root is an integer number. Write a Python program to input a positive number and check whether it is perfect square numbers or not.

[Note: The square root of a negative number cannot be determined.]

Sample Input: 81

Sample Output: A perfect square

Solution
import math
n = int(input("Enter a positive number: "))
if n < 0:
    print("Square root of a negative number cannot be determined")
else:
    sq = math.sqrt(n)
    if sq == int(sq):
        print("A perfect square")
    else:
        print("Not a perfect square")
Output
Enter a positive number: 81
A perfect square

Question 10

A shopkeeper offers 30% discount on purchasing an article whereas the other shopkeeper offers two successive discounts 20% and 10% for purchasing the same article. Write a program in Python to compute and display the discounts offered by both the shopkeepers. Take the price of the article as an input. Finally, display which is the better deal for the customer.

Solution
price = int(input("Enter price of the article:"))

# First shopkeeper
d1 = (30 / 100) * price
sp1 = price - d1

# Second shopkeeper
d2 = (20 / 100) * price
price_after_first = price - d2

d3 = (10 / 100) * price_after_first
total_discount = d2 + d3

sp2 = price - total_discount

print("Discount offered by first shopkeeper =", d1)
print("Discount offered by second shopkeeper =", total_discount)

if(d1 > total_discount):
    print("First shopkeeper offers a better deal")
elif(total_discount > d1):
    print("Second shopkeeper offers a better deal")
else:
    print("Both offers are same")
Output
Enter price of the article:1000
Discount offered by first shopkeeper = 300.0
Discount offered by second shopkeeper = 280.0
First shopkeeper offers a better deal

Question 11

Write a Python program to input the name and basic salary of an employee. Calculate and display the name, gross salary and net salary of the employee when:

da = 30% of basic

hra = 18% of basic

pf = 12.5% of basic

gross = basic + da + hra

net = gross − pf

If the annual gross salary is up to ₹5,00,000 then display the message "Not an Income Tax Payer" otherwise, display the message "An Income Tax Payer".

Solution
name = input("Enter employee name:")
basic = int(input("Enter basic salary:"))

da = (30 / 100) * basic
hra = (18 / 100) * basic
pf = (12.5 / 100) * basic

gross = basic + da + hra
net = gross - pf

annual_gross = gross * 12

print("Name:", name)
print("Gross Salary =", gross)
print("Net Salary =", net)

if(annual_gross <= 500000):
    print("Not an Income Tax Payer")
else:
    print("An Income Tax Payer")
Output
Enter employee name:Rahul
Enter basic salary:30000
Name: Rahul
Gross Salary = 44400.0
Net Salary = 40650.0
An Income Tax Payer

Question 12

Write a program that displays the results of the following evaluations based on the choice entered by the user:

  1. Absolute value of the number
  2. Square root of the number
  3. Cube root of the number
Solution
import math

num = int(input("Enter a number:"))

print("Enter 1 for Absolute value")
print("Enter 2 for Square root")
print("Enter 3 for Cube root")

choice = int(input("Enter your choice:"))

match choice:

    case 1:
        ab = abs(num)
        print("Absolute value =", ab)

    case 2:
        if(num >= 0):
            sq = math.sqrt(num)
            print("Square root =", sq)
        else:
            print("Square root of a negative number cannot be determined")

    case 3:
        cb = pow(num, 1/3)
        print("Cube root =", cb)

    case _:
        print("Invalid Choice!")
Output
Enter a number:27
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:3
Cube root = 3.0

Enter a number:100
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:2
Square root = 10.0

Enter a number:-153
Enter 1 for Absolute value
Enter 2 for Square root
Enter 3 for Cube root
Enter your choice:1
Absolute value = 153

Answer the following questions

Question 1

What is meant by the term Indentation? Explain.

Answer

Indentation refers to the alignment of a statement or a group of statements which are part of another statement. In other words, it is the addition of white space before a statement to mark a particular block of code. In Python, all lines of code that belong to the same block must have the same indentation level. The standard indentation is four spaces, but any consistent number of spaces or tabs can be used. Python is very specific about indentation, improperly indented code will result in an error.

Question 2

Name the different types of errors that occur in a code.

Answer

The three different types of errors that occur in a code are:

  1. Syntax error
  2. Logical error
  3. Run Time error

Question 3

What is meant by normal flow of control?

Answer

The sequential movement of the flow of control from one statement to the other is termed as normal flow of control. In this flow, all the statements are executed one after the other (by default). The execution starts from 'Statement 1', moves to 'Statement 2', then 'Statement 3' and so on till the last statement of the program.

Question 4

What do you understand by 'Conditional flow of control'?

Answer

When the execution of the statements is based on a given condition, the control flow is referred to as the Conditional flow of control. In this construct, the control transfers to a defined statement based on whether the specified condition is true or false, while ignoring some statements unexecuted. Python provides conditional statements like if, if-else, and if-elif-else to implement conditional flow of control.

Question 5

Explain the following with their constructs:

(i) if-else

(ii) if-elif-else

Answer

(i) if-else statement

The if-else statement is used when one of the two actions is to be carried out depending on a condition. If the condition is true, the control executes one block of indented statements; otherwise, it executes another block of indented statements.

Construct:

if condition:
    Statement(s) 1
else:
    Statement(s) 2

(ii) if-elif-else statement

The if-elif-else is a conditional construct where multiple 'if-else' statements are connected with the help of the keyword elif. It allows the user to execute a statement or a block of statements when the given condition in the first if statement is true. When the first condition is false, it checks another condition using elif and takes the necessary action. At the end, the logical construct stops with the else statement.

Construct:

if condition1:
    Statement 1
elif condition2:
    Statement 2
else:
    Statement 3

Question 6

What type of error may occur when a number is divided by zero?

Answer

When a number is divided by zero, a Run Time error known as ZeroDivisionError occurs. This is because mathematically, a number cannot be divided by zero.

Question 7

Distinguish between:

(i) '=' and '=='

(ii) Syntax Error and Logical Error

Answer

(i) '=' and '=='

'=''=='
It is the assignment operator.It is the relational (equality) operator.
It is used to assign a value to a variable.It is used to compare two values to check if they are equal.
Example: a = 10 (assigns 10 to a)Example: a == 10 (returns True if a is equal to 10)

(ii) Syntax Error and Logical Error

Syntax ErrorLogical Error
It occurs when the grammar of the programming statements is not followed properly.It occurs when the program's logic is incorrectly planned.
The code does not execute until the error is corrected.The code executes but does not produce the desired output.
Examples include incorrect punctuation, missing parenthesis, undefined terms.Examples include wrong placement of operators, improper sequencing of statements, or incorrect hierarchy of operations.

Question 8

Define the following mathematical functions:

(i) max()

(ii) min()

(iii) pow()

(iv) sqrt()

Answer

(i) max() — This function is used to find the largest of the given arguments. It returns a value depending on the input arguments (i.e., if the arguments are int types then the return value will be an int).

Example: max(41, 89) returns 89

(ii) min() — This function returns the smallest of the given arguments. The return value will depend on the input values of the arguments of the function.

Example: min(49, 68) returns 49

(iii) pow() — This function is used to find the power b raised to a specified base a (i.e., ab). It returns data of type int or float, depending upon the output.

Example: pow(5, 3) returns 125

(iv) sqrt() — This function is used to find the square root of a number p for p > 0. It always returns a float type value. The math module must be imported to use this function.

Example: math.sqrt(81) returns 9.0

PrevNext