Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Answer Key

[A] Multiple Choice Questions: (1 x 10 = 10 Marks)

Q1. User defined name is known as:


(a) Identifier

Q2. Which of the following is not a valid variable name?


(d) 7flag

Q3. Which of the following is not a comment?

(d) "This is comment"

Q4. Which of the following input statement is correct?


(b) a = input("enter a number")

Q5. Which of the following print statement is correct?


(d) print (9)

Q6. What will be the output on executing the following set of commands in the shell?
>>>str= "Hello"

>>>str[:2]
(a) He

Q7. In order to store values in terms of key and value pairs, which core data type you
should use?
(d) Dictionary
Q8. Which of the following data type is not supported in Python?
(d) Slice

Q9. Which of the following operation results in 8?


(c) 2**4

Q10. Choose the correct output of the following code:


a = 100

b = 30
print(a%b)
(c) 10

These answers correspond to the options provided in each multiple-choice question. Each
correct answer is marked with its respective letter (a, b, c, or d).

[B] Fill in the Blanks: (1 x 10 = 10 Marks)

Ans11. The output of (10==10) will be True.

Ans12. The operators that are used to compare two operands are called relational
operators.

Ans13. The output of (20>5 and 50<80) will be True.

Ans14. Python is a high-level and interpreted language.

Ans15. Python is a case sensitive language.

Ans16. Python files are saved with .py extensions.


Ans17. Python supports single-line and multi-line comments.

Ans18. Python variables have two different scopes global and local.

Ans19. Implicit and explicit are types of Python conversion.

Ans20. Python was developed by Guido van Rossum.

[C] Very short questions: (2 x 5 = 10 Marks)

Ans21. What are some common data types in Python?


- Common data types in Python include integers (`int`), floating-point numbers (`float`),
strings (`str`), lists (`list`), tuples (`tuple`), dictionaries (`dict`), and booleans (`bool`).

Ans22. What are variables in Python?

- Variables in Python are used to store data values. They are created when you assign a value
to them using the assignment operator (`=`).

Ans23. What are identifiers in Python?


- Identifiers in Python are names given to entities like variables, functions, classes, etc. They
help in identifying and referring to them in the code.

Ans24. Why is proper indentation important in Python?

- Proper indentation is important in Python because it defines the structure and hierarchy
of the code. Incorrect indentation can lead to syntax errors or alter the logical flow of the
program.

Ans25. What are the various types of operators in Python?


- Various types of operators in Python include arithmetic operators (`+`, `-`, `*`, `/`, `//`, `%`,
`**`), comparison operators (`==`, `!=`, `>`, `<`, `>=`, `<=`), logical operators (`and`, `or`, `not`),
assignment operators (`=`, `+=`, `-=`, `*=`, `/=`, `//=`, `%=`), bitwise operators (`&`, `|`, `^`, `~`,
`<<`, `>>`), membership operators (`in`, `not in`), and identity operators (`is`, `is not`).

OR

Ans24. What is meant by input and output in Python?

- Input in Python refers to the process of accepting data or information from the user or
external sources. Output in Python refers to the process of displaying results, messages, or
data to the user or external destinations.

Q25. Explain the role of logical operators in Python?


- Logical operators in Python (`and`, `or`, `not`) are used to combine conditional statements.
They are used to evaluate multiple expressions and determine the logical relationship
between them (e.g., conjunction, disjunction, negation). They are essential for controlling the
flow of logic in programs and making decisions based on conditions.

[D] Short questions: (4 x 5 = 20 Marks)

Q26. What is operator precedence in Python? Explain with the help of example.

Operator precedence in Python determines the order in which operations are evaluated in
an expression. Operators with higher precedence are evaluated first. For example,
multiplication (`*`) has a higher precedence than addition (`+`).

Example:
result = 10 + 2 * 3
print(result) # Output: 16

Explanation:
# Multiplication (2 * 3) is evaluated first due to higher precedence,
# then addition (10 + 6) is performed.
Q27. What are the features of Python that make it suitable for diverse applications?

Python is suitable for diverse applications due to the following features:


- Easy to Learn and Read: Python's syntax is simple and easy to understand, making it
accessible for beginners and experienced developers alike.
- Versatility: Python supports multiple programming paradigms (procedural, object-
oriented, functional) and is used in various domains such as web development, data science,
machine learning, etc.
- Extensive Libraries: Python has a vast standard library and numerous third-party libraries,
providing solutions for different tasks without reinventing the wheel.
- Interpreted Nature: Python's interpreted nature allows for rapid development and
prototyping.
- Platform Independence: Python code runs on various platforms (Windows, macOS, Linux)
without modification.

Q28. Explain the difference between = and == operators in Python with examples.

= (Assignment Operator): Used to assign a value to a variable.


x = 10 # Assigns the value 10 to variable x

== (Equality Operator): Used to compare whether two values are equal.

a=5
b=5
if a == b:
print("a and b are equal") # This condition is true, so this line will be printed

Q29.Discuss the importance of comments in Python programming. Provide examples of


different types of comments.

Comments in Python are essential for:


- Documentation: Providing explanations and documentation within the code.
- Clarity: Making the code more readable and understandable.
- Debugging: Temporarily disabling code or adding debugging notes.

Examples of different types of comments:

# This is a single-line comment

"""
This is a multi-line comment.
It can span multiple lines.
"""

Q30. What is type conversion? Explain with the help of example.

Type conversion (or type casting) refers to the process of converting one data type to
another in Python. It can be implicit (automatically handled by Python) or explicit (done by
the programmer using built-in functions).

Example of explicit type conversion:

# Convert integer to string


num = 10
num_str = str(num)
print(num_str) # Output: "10"

OR
Q29. How are keywords different from variable names?

Keywords in Python are reserved words that have special meanings and purposes within the
language (e.g., `if`, `else`, `for`, `while`, `def`). They cannot be used as variable names.

Variable names, on the other hand, are identifiers used to store values or data in memory.
They must follow certain rules (e.g., start with a letter or underscore, not start with a number).

Q30. Write a Python program to find the perimeter of a rectangle by taking user input (Hint:
Use input() function for taking value).

# Python program to find perimeter of a rectangle using user input

# Take length and width from user


length = float(input("Enter length of rectangle: "))
width = float(input("Enter width of rectangle: "))

# Calculate perimeter
perimeter = 2 * (length + width)

# Print the perimeter


print(f"Perimeter of rectangle with length {length} and width {width} is {perimeter}")

[E] Long questions: (5 x 4 = 20 Marks)

Q31.Explain the role and usage of bitwise operators in Python. Provide scenarios where
bitwise operators are commonly used in programming?
Bitwise operators in Python (`&`, `|`, `^`, `~`, `<<`, `>>`) manipulate bits of integers at the
binary level. They are used in scenarios such as:
- Bitwise AND (`&`): Used for bit masking and checking specific bits.
- Bitwise OR (`|`): Used for setting bits or combining flags.
- Bitwise XOR (`^`): Used for flipping bits.

- Bitwise NOT (`~`): Used for bitwise negation.


- Left Shift (`<<`) and Right Shift (`>>`): Used for shifting bits left or right, effectively
multiplying or dividing by powers of two.

Example:
# Example of bitwise operators

a = 5 # Binary: 0101
b = 3 # Binary: 0011

print(a & b) # Output: 1 (Binary: 0001)


print(a | b) # Output: 7 (Binary: 0111)

print(a ^ b) # Output: 6 (Binary: 0110)


print(~a) # Output: -6 (Binary: ...11111010, depends on the integer size in bits)
print(a << 1) # Output: 10 (Binary: 1010)
print(b >> 1) # Output: 1 (Binary: 0001)

Bitwise operators are commonly used in low-level programming, cryptography,


networking, and optimization tasks where bit-level manipulation is required.

Q32.Describe the concept of Python's string operators. Provide examples of string


concatenation?

String operators in Python primarily involve concatenation (`+`) and repetition (`*`).
- String Concatenation (`+`): Joins two or more strings together.

- String Repetition (`*`): Repeats a string multiple times.


Example:
# Example of string concatenation

str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result) # Output: "Hello World"

# Example of string repetition


str3 = "Python"
repeated_str = str3 * 3
print(repeated_str) # Output: "PythonPythonPython"

String operators are fundamental for manipulating textual data in Python, such as
combining strings, formatting output, or constructing messages dynamically.

Q33. Differentiate between arithmetic and logical operators with examples?

- Arithmetic operators** (`+`, `-`, `*`, `/`, `//`, `%`, `**`): Used for performing mathematical
calculations on numerical values.

a = 10
b=3

print(a + b) # Output: 13

print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333...
print(a // b) # Output: 3 (Floor division)
print(a % b) # Output: 1 (Modulus)
print(a ** b) # Output: 1000 (Exponentiation)

- Logical operators (`and`, `or`, `not`): Used for combining conditional statements.
x = True
y = False

print(x and y) # Output: False


print(x or y) # Output: True
print(not x) # Output: False

Arithmetic operators work with numerical data to perform calculations, while logical
operators work with boolean values to evaluate conditions and make decisions in the
program.

Q34.Discuss the operator precedence rules in Python. Explain how operator precedence
affects the evaluation of expressions with examples.

Operator precedence in Python determines the order in which operators are evaluated in
an expression. Operators with higher precedence are evaluated first. For example:
- Multiplication (`*`), division (`/`), and modulus (`%`) have higher precedence than
addition (`+`) and subtraction (`-`).
- Operators within parentheses `()` are evaluated first.

Example:
result = 10 + 2 * 3
print(result) # Output: 16

# Explanation:
# Multiplication (2 * 3) is evaluated first due to higher precedence,
# then addition (10 + 6) is performed.

Understanding operator precedence is crucial for writing correct and efficient Python
code, as it affects how expressions are evaluated and can alter the result if not used
correctly.

OR

Q33. Write a Python program that asks the user to enter the radius of a circle and calculates
its area?

# Python program to calculate area of a circle based on user input

import math

# Take radius input from user

radius = float(input("Enter the radius of the circle: "))

# Calculate area
area = math.pi * (radius ** 2)

# Print the area


print(f"Area of the circle with radius {radius} is {area:.2f}")

Q34. Suppose a person wants to do an addition but doesn’t know about operators and
operands in Python. They take a number 30 and a string "world". Justify your answer as to
what is wrong with adding 30 and the string "world".
In Python (and in most programming languages), addition (`+`) is defined for adding
numerical values together or concatenating strings. Mixing different data types like a
number (`30` as an integer) and a string (`"world"`), without explicit conversion, would
result in a `TypeError`. This is because Python does not implicitly convert types when
performing operations that expect homogeneous types.

Example:
num = 30
string = "world"

result = num + string


# This would raise a TypeError: unsupported operand type(s) for +: 'int' and 'str'

To perform the addition correctly, either convert the number to a string or vice versa,
depending on the desired outcome:

num = 30
string = "world"

result = str(num) + string


print(result) # Output: "30world"

This explicitly converts `num` to a string before concatenating it with `string`.

You might also like