Loading...
Please wait while we prepare your content
Please wait while we prepare your content
Solutions for Computer Science, Class 11, CBSE
Assertion (A): Each component of a programming statement is known as a token.
Reasoning (R): Token is not executed by Python interpreter, only used in the program coding.
A is true but R is false.
Explanation
A token is the smallest element of a Python script that is meaningful to the interpreter. Each component of a programming statement is referred to as a token. Comments are not executed by the interpreter, but tokens are executed by it.
Assertion (A): Python is a dynamically typed language.
Reasoning (R): The data type of a variable is declared as per the type of value assigned to it.
Both A and R are true and R is the correct explanation of A.
Explanation
In Python, the data type of a variable is determined by the type of value assigned to it, and it can change if the value assigned to the variable changes. This behavior is known as dynamic typing, meaning the type of the variable is not fixed and is decided at runtime based on the assigned value. Hence, Python is a dynamically typed language.
Assertion (A): In Python, strings, lists and tuples are called sequences.
Reasoning (R): Sequence is referred to as an ordered collection of values having similar or different data types.
Both A and R are true and R is the correct explanation of A.
Explanation
A sequence is an ordered collection of items in Python, and these items can have similar or different data types. They are indexed by integers. The three main types of sequence data types in Python are strings, lists, and tuples.
Assertion (A): Comments are non-executable statements that are ignored by the interpreter.
Reasoning (R): Comments are used to explain the code and to make it more informative for the users. They are basically statements used to put remarks.
Both A and R are true and R is the correct explanation of A.
Explanation
Comments are non-executable statements in a script that are ignored by the Python interpreter and, therefore, have no effect on the actual output of the code. They are used to explain Python code and make it more readable and understandable for humans. Essentially, comments are used to include remarks within the code.
Assertion (A): A set of valid characters recognized by Python constitutes a character set.
Reasoning (R): Character set in Python is a subset of Python tokens.
A is true but R is false.
Explanation
Character set is a set of valid characters recognized by Python. A character represents any letter, digit or any other symbol present on the keyword. On the other hand, a token is the smallest element of a Python script that is meaningful to the interpreter which includes keywords, identifiers, literals, operators and delimiters/punctuators. A character set can be used to form tokens, but not all tokens are part of the character set.
Assertion (A): 'Rollnumber' and 'rollnumber' are same identifiers.
Reasoning (R): Python is a case-sensitive language.
A is false but R is true.
Explanation
In Python, 'Rollnumber' and 'rollnumber' are not the same identifiers because Python is a case-sensitive language. This means Python differentiates between uppercase and lowercase letters. For example, 'Rollnumber' (with an uppercase 'R') and 'rollnumber' (with a lowercase 'r') are considered distinct identifiers in Python.
Assertion (A): Literals are same as identifiers.
Reasoning (R): A variable is a label for a location in memory.
A is false but R is true.
Explanation
Literals and identifiers are not the same. Literals are fixed numeric or non-numeric value. While identifiers are the name of any variable, constant, function or module. A variable is a label for a location in memory, which is used to store and access values.
Tokens
Reason — Tokens are the fundamental building blocks of a Python program. They are the smallest elements of a Python script that are meaningful to the interpreter.
Underscore (_)
Reason — In Python, an identifier name can only be composed of letters (both uppercase and lowercase), digits, and underscores (_). Special characters like #, hyphen (-), and are not allowed in identifier names.
K = complex(2,3)
Reason — Complex numbers are made up of pairs of real and imaginary numbers. They take the form 'x+yj' or 'x+yi', where x is the real part and y is the imaginary part. Therefore, according to this, K = complex(2,3) is not a complex number.
1,2,3,4,5,6
Reason — The order of precedence of arithmetic operators in Python is Parenthesis (), Exponential (**), Multiplication (*), Division (/), Addition (+), Subtraction (-).
6 4
Reason — Initially, x
is assigned the value 2, and y
is assigned 6 using multiple assignments. Then, the expression y, x + 2
is evaluated, where y
is 6, and x + 2
calculates to 4. After evaluating the right-hand side, x
is reassigned to y
, making x
equal to 6, and y
is reassigned to x + 2
, making y
equal to 4. Thus, when print(x, y)
executes, it outputs 6 4, showing the final values after the assignments.
Images: Vector
Reason — "Images: Vector" is not part of the Python character set because Python supports the following character set:
None of these
Reason — There is no demarcation or symbol in Python to indicate the termination of a statement. When we end typing a statement in Python by pressing the Enter key, the statement is considered terminated by default.
Keywords
Reason — Keywords are the reserved words used by Python interpreter to recognize the structure of a program.
abc = 20 30 40
Reason — In Python, the statement abc = 20 30 40
is invalid because it attempts to assign multiple values (20
, 30
, and 40
) to a single variable (abc
) in a single assignment statement.
False
Reason — The expression evaluates as follows: 10 > 5
is True because 10 is greater than 5. 7 > 12
is False because 7 is not greater than 12. 18 > 3
is True because 18 is greater than 3, and not True results in False. Combining these using Python's operator precedence, 10 > 5
and 7 > 12
evaluates to False (True and False). Then, False or not True evaluates to False (False or False). Therefore, the entire expression 10 > 5 and 7 > 12 or not 18 > 3
ultimately evaluates to False.
13
Reason — The expression 6*3+4**2//5-8
follows Python's operator precedence rules, where exponentiation (4**2) is evaluated first resulting in 16. Then, floor division (16//5) is calculated as 3. Next, multiplication (6*3) gives 18. Adding these results (18 + 3) yields 21. Finally, subtracting 8 from 21 results in 13.
82
Reason — In the above code, a
is assigned the floating-point number 72.55, and b
is assigned the integer 10. The variable c
is assigned the expression a + b
resulting in 72.55 + 10 = 82.55. Then, the int()
function is used to convert the result of a + b
to an integer. Therefore, int(82.55) results in 82.
(a) (2 + 3) ** 3 - 6/2
= 5**3 - 6/2
= 125 - 6/2
= 125 - 3.0
= 122.0
(b) (2 + 3) * 5//4 + (4 + 6)/2
= 5 * 5//4 + 10/2
= 25 // 4 + 10/2
= 6 + 10/2
= 6 + 5.0
= 11.0
(c) 12 + (3 * 4 - 6)/3
= 12 + (12 - 6)/3
= 12 + 6/3
= 12 + 2.0
= 14.0
(d) 12 + (3 ** 4 - 6)//2
= 12 + (81 - 6)//2
= 12 + 75//2
= 12 + 37
= 49
(e) 12 * 3 % 5 + 2 * 6//4
= 36 % 5 + 2 * 6//4
= 1 + 2 * 6//4
= 1 + 12 // 4
= 1 + 3
= 4
(f) 12 % 5 * 3 + (2 * 6)//4
= 2 * 3 + (2 * 6)//4
= 6 + 12 // 4
= 6 + 3
= 9
Group — This is a valid variable name.
if — This is an invalid variable name because if
is a reserved keyword in Python and cannot be used as a variable name.
total marks — This is an invalid variable name because it contains a space, which is not allowed in variable names. A variable name must be a single continuous string without spaces.
S.I. — This is an invalid variable name because it contains full stops, which are not allowed in variable names. Variable names can only include letters, digits, and underscores.
volume — This is a valid variable name.
tot_strength — This is a valid variable name.
#tag — This is an invalid variable name because it starts with a hash symbol (#), which is not allowed in variable names. Variable names must start with a letter or underscore.
tag — This is an invalid variable name because it contains a dollar sign (), which is not allowed in variable names. Variable names can only include letters, digits, and underscores.
9a — This is an invalid variable name because it starts with a digit, which is not allowed in variable names. Variable names must start with a letter or underscore.
for — This is an invalid variable name because for
is a reserved keyword in Python and cannot be used as a variable name.
8 5
x = 3
y = x + 2
x
is initialized to 3.y
is set to x + 2
, which means y = 3 + 2 = 5.x += y
The +=
operator adds the value of y
to x
and assigns the result back to x
.
x
: x += 5 means x = x + 5print(x, y)
The final values of the variables are:
Hence, the output of the program is:
8 5
0 2
x += y
The +=
operator adds the value of y
to x
and assigns the result back to x
.
x
: x += 2 means x = x + 2y -= x
The -=
operator subtracts the value of x
from y
and assigns the result back to y
.
y
: y -= 0 means y = y - 0print(x, y)
The final values of the variables are:
Hence, the output of the program is:
0 2
20 300
a = 5
b = 2 * a
a
is initialized to 5.b
is set to 2 * a
, which means b = 2 * 5 = 10.a += a + b
The +=
operator adds the value on the right to a
and assigns the result back to a
.
a
: a += 15 means a = a + 15b *= a + b
The *=
operator multiplies b
by the value on the right and assigns the result back to b
.
b
: b *= 30 means b = b * 30print(a, b)
The final values of the variables are:
Hence, the output of the program is:
20 300
66.66666666666667 126.66666666666667
p *= q / 3
The *=
operator multiplies p
by the value on the right and assigns it to p
again.
q += p + q * 2
The +=
operator adds the value on the right to q
and assigns it to q
again.
q
: q += 106.6667 means q = q + 106.6667print(p, q)
The variables now have these final values:
The precision difference is due to floating-point arithmetic. For simplicity of explanation we have taken lesser precision values.
4 -10 7
p = 5 % 2
The modulo %
operator calculates the remainder of the division.
q = p ** 4
The exponent **
operator raises p
to the power of 4.
r = p // q
The floor division //
operator divides and returns the integer quotient of the division.
p += p + q + r
The +=
operator adds the value on the right to the variable and assigns it to the variable again.
r += p + q + r
The +=
operator adds the value on the right to the variable and assigns it to the variable again.
q -= p + q * r
The -=
operator subtracts the value on the right from the variable and assigns it to the variable again.
print(p, q, r)
The variables now have these final values:
Hence, the output of the program is:
4 -10 7
8 8 0
p = 21 // 5
The floor division //
operator divides and returns the integer quotient of the division.
q = p % 4
Here, the modulo %
operator calculates the remainder of the division.
r = p * q
Now it performs a multiplication.
p += p + q - r
The +=
operator adds the value on the right to the variable and assigns it to the variable again.
r *= p - q + r
The *=
operator multiplies the variable by the value on the right and assigns it to the variable again.
q += p + q
The +=
operator adds the value on the right to the variable and assigns it to the variable again.
print(p, q, r)
The variables now have these final values:
Hence, the output of the program is:
8 8 0
(a) (a + b) / 2
(b) (3**2 + 9**3) / 2
(c) 3**2 + 9**3 / 5
(d) a**0.5 + (a + 2) / b
(e) 8 - 6 + 6 * sum / 7 - var**0.5
(f) u * t + 1 / 2 * a * t**2
Write Python expressions to represent the following situations:
(a) Add remainder of 8/3 to the product of 8 and 3.
(b) Find the square root of the sum of 8 and 43.
(c) Find the sum of the square roots of 8 and 43.
(d) Find the integral part of the quotient when 100 is divided by 32.
(a) 8 * 3 + 8 % 3
(b) (8 + 43)**(1/2)
(c) 8**(1/2) + 43**(1/2)
(d) 100//32
Operators are tokens that trigger some computation/action when applied to variables and other objects in an expression. Unary plus (+), Unary minus (-) are a few examples of unary operators. Examples of binary operators are Addition (+), Subtraction (-), Multiplication (*), Division (/).
An expression is any legal combination of symbols that represents a value. For example, 2.9, a + 5, (3 + 5) / 4.
A statement is a programming instruction that does something i.e. some action takes place. For example:
print("Hello")
a = 15
b = a - 10
Write a function called calculate_area() that takes base and height as an input argument and returns an area of a triangle as an output. The formula used is:
Area of a Triangle = ½*base*height
def calculate_area(base, height):
area = (1/2) * base * height
return area
base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
triangle_area = calculate_area(base_value, height_value)
print("Area of the triangle:", triangle_area)
Enter the base value: 10
Enter the height value: 5
Area of the triangle: 25.0
Modify the above function to take a third parameter called shape type. Shape type should be either triangle or rectangle. Based on the shape, it should calculate the area. Formula used:
Area of a Rectangle = length * width.
def calculate_area(base, height, shape_type):
if shape_type == "triangle":
area = (1/2) * base * height
elif shape_type == "rectangle":
area = base * height
else:
area = None
print("Invalid shape type. Please specify either 'triangle' or 'rectangle'.")
return area
shape_type = input("Enter the shape type, triangle or rectangle: ")
base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
area = calculate_area(base_value, height_value, shape_type)
print("Area of the", shape_type, "is ", area)
Enter the shape type, triangle or rectangle: triangle
Enter the base value: 10
Enter the height value: 5
Area of the triangle is 25.0
Enter the shape type, triangle or rectangle: rectangle
Enter the base value: 8
Enter the height value: 9
Area of the rectangle is 72
Write a function called print_pattern() that takes integer number as argument and prints the following pattern if the input number is 3:
*
* *
* * *
If input is 4, then it should print:
*
* *
* * *
* * * *
def print_pattern(num):
for i in range(1, num + 1):
print("*" * i)
num = int(input("Enter a number: "))
print("Pattern for input", num, ":")
print_pattern(num)
Enter a number: 4
Pattern for input 4 :
*
**
***
****
Enter a number: 5
Pattern for input 5 :
*
**
***
****
*****
Write a program that takes amount in dollars, converts it into rupees. It then displays the converted amount.
amount = float(input("Enter amount in dollars "))
conversion_rate = float(input("Enter conversion rate "))
amount_in_rupees = amount * conversion_rate
print("Converted amount in rupees:", amount_in_rupees)
Enter amount in dollars 70
Enter conversion rate 81
Converted amount in rupees: 5670.0
There are two errors in this code fragment:
The line c, b, a + a, b, c
attempts to unpack values but is incomplete and incorrect. It lacks a right-hand side (RHS) to assign values from.
In the line print(a; b; c)
, the print function should use commas (,) to separate arguments, not semicolons (;).
15
13 22
X = 10
⇒ assigns an initial value of 10 to X.X = X + 10
⇒ X = 10 + 10 = 20. So value of X is now 20.X = X - 5
⇒ X = 20 - 5 = 15. X is now 15.print (X)
⇒ print the value of X which is 15.X, Y = X - 2, 22
⇒ X, Y = 13, 22.print (X, Y)
⇒ prints the value of X which is 13 and Y which is 22.2 3 6
2 3 6
first = 2
⇒ first
is assigned the value 2.second = 3
⇒ second
is assigned the value 3.third = first * second
⇒ third
is calculated as first * second
, which is 2 * 3 = 6.print(first, second, third)
⇒ The print statement outputs 2 3 6, showing the values of first
, second
, and third
.third = second * first
⇒ third
is reassigned with second * first
, which is 3 * 2 = 6.print(first, second, third)
⇒ The second print statement outputs 2 3 6 again, reflecting the updated value of third
.Comments are statements in a script that are ignored by the Python interpreter and therefore, have no effect on the actual output of the code. Comments make the code more readable and understandable for human beings. This makes the revision/modification of the code easier by the original author of the code and also by some new author who has been assigned the responsibility to modify it. They are highly recommended if the script is too complex or if it is to be reviewed by multiple people over an interval of time.
For example,
# Calculate the area of a rectangle
length = 5
width = 3
# Formula: area = length * width
area = length * width
print("Area of the rectangle is:", area)
The error in the above code is in the print statement. We cannot concatenate a string (Name) and an integer (Age) directly using the '+' operator. The corrected code is:
Name = "Prateek"
Age = 26
print("Your name & age are", Name + str(Age))
The error in the given code fragment occurs in the Q = A / 10
line, where it attempts to divide the string "New" by the integer 10. This is not a valid operation and will raise a TypeError.
20 81
x = 40
y = x + 1
x
is initialized to 40.y
is set to x + 1
, which means y = 40 + 1 = 41.x, y = 20, y + x
The values of x
and y
are updated simultaneously. This means both assignments are evaluated first and then applied.
x, y
.20, y + x
.x
is set to 20.y
is set to y + x
, which means y = 41 + 40 = 81.print(x, y)
The final values of the variables are:
Hence, the output of the program is:
20 81
50 30
x, y = 20, 60
⇒ assigns an initial value of 20 to x and 60 to y.y, x, y = x, y - 10, x + 10
⇒ y, x, y = 20, 60 - 10, 20 + 10 ⇒ y, x, y = 20, 50, 30 First RHS value 20 is assigned to first LHS variable y. After that second RHS value 50 is assigned to second LHS variable x. Finally third RHS value 30 is assigned to third LHS variable which is again y. After this assignment, x becomes 50 and y becomes 30.print (x, y)
⇒ prints the value of x and y as 50 and 30 respectively.a and b, : 25 13 16
In the given code, using multiple assignment, three variables a
, b
, and c
are initially assigned values 10, 20, and 30 respectively and variables p
, q
, and r
are assigned new values: p
is assigned c - 5
(resulting in 25), q
is assigned a + 3
(resulting in 13), and r
is assigned b - 4
(resulting in 16). Finally, the print
statement outputs the values of p
, q
, and r
along with the string "a and b, :", resulting in the output: a and b, : 25 13 16
.
The error in the above code fragment occurs when trying to perform arithmetic operation on the result of the input()
function. The input()
function returns a string, even if the user enters a number. Therefore, attempting to subtract 1 from cl
(which is a string) will raise a TypeError. The corrected code is:
cl = int(input("Enter your class: "))
print("Last year you were in class", cl - 1)
Write a Python program that accepts marks in five subjects and outputs average marks.
m1 = int(input("Enter first subject marks: "))
m2 = int(input("Enter second subject marks: "))
m3 = int(input("Enter third subject marks: "))
m4 = int(input("Enter fourth subject marks: "))
m5 = int(input("Enter fifth subject marks: "))
avg = (m1 + m2+ m3+ m4 + m5) / 5;
print("Average Marks =", avg)
Enter first subject marks: 65
Enter second subject marks: 78
Enter third subject marks: 79
Enter fourth subject marks: 80
Enter fifth subject marks: 85
Average Marks = 77.4
Write a program to find the area of a triangle.
h = float(input("Enter height of the triangle: "))
b = float(input("Enter base of the triangle: "))
area = 0.5 * b * h
print("Area of triangle = ", area)
Enter height of the triangle: 2.5
Enter base of the triangle: 5
Area of triangle = 6.25
Write a program to read details like name, class, age of a student and then print the details, firstly in the same line and then in separate lines.
name = input("Enter name of student: ")
c = int(input("Enter class of student: "))
age = int(input("Enter age of student: "))
print("Name:", name, "Class:", c, "Age:", age)
print()
print("Name:", name)
print("Class:", c)
print("Age:", age)
Enter name of student: Kavya
Enter class of student: 9
Enter age of student: 14
Name: Kavya Class: 9 Age: 14
Name: Kavya
Class: 9
Age: 14
Write a program to read three numbers in three variables and swap first two variables with the sums of first and second, second and third numbers, respectively.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("The three number are", a, b, c)
a, b = a + b, b + c
print("Numbers after swapping are", a, b, c)
Enter first number: 10
Enter second number: 15
Enter third number: 20
The three number are 10 15 20
Numbers after swapping are 25 35 20
False
Reason — In Python, variables are not initialized with any default values or types. The type of a variable is determined by the value assigned to it, and Python does not default to initializing variables as string literals. Instead, we must explicitly assign a value to a variable, whether it's a string, integer, or any other data type.
False
Reason — In Python, assignment statements follow the syntax variable = value
, where the variable (L-value) is on the left side and the value (R-value) is on the right side of the equal sign. Therefore, 3+C = A is an invalid statement in Python because the left side (3+C) is not a valid variable name.
False
Reason — In Python, variable names cannot contain hyphens. They can only contain letters (both uppercase and lowercase), digits, and underscores, but they cannot start with a digit. Therefore, "AISSCE-2020" is invalid string variable name because it contains a hyphen.