Professional Documents
Culture Documents
Answer Key
Answer Key
Answer Key
Answer: (c)
Answer: (a) 65 // 8
Q10. The function max(10,5,8,7,6) will return:
(a) 6 (b) 8 (c) 10 (d) 5
Answer: (c) 10
Q21. What are two key features of Python that make it popular for beginners and
professionals alike?
- Answer: Python's key features include its easy-to-read syntax and extensive standard
library, which provide powerful built-in functionalities.
Q24. Explain the purpose of comments in Python code. Give an example of a single-line
and a multi-line comment.
- Answer: Comments in Python are used to document code, making it easier to understand.
Examples:
- Single-line comment: `# This is a single-line comment`
- Multi-line comment:
"""
This is a multi-line comment.
It spans multiple lines for detailed explanations.
"""
OR
Q25. Describe the syntax used to define a function in Python. Provide an example.
- Answer: In Python, a function is defined using the `def` keyword followed by the function
name, parameters (if any), and a colon (`:`). Example:
def greet(name):
"""This function greets the person with the given name."""
print(f"Hello, {name}!")
greet("Alice")
Output:
Hello, Alice!
Q26. Explain why indentation is important in Python code. How does Python use
indentation to define blocks of code?
- Answer: Indentation is crucial in Python as it defines the structure and hierarchy of code
blocks. Python uses indentation (typically 4 spaces per indentation level) to indicate which
statements are grouped together in loops, conditional statements, function definitions, etc.
Incorrect indentation can lead to syntax errors or change the logical flow of the program.
Q27. You are explaining to a friend who is new to programming why Python is often
recommended as a first language to learn. Discuss Python's readability, simplicity, and
versatility, and provide examples of industries or applications where Python is heavily
used.
- Answer: Python is recommended as a first language due to its:
- Readability: Clear and expressive syntax that resembles pseudo-code, making it easier to
understand.
- Simplicity: Minimalistic design with fewer lines of code required for tasks compared to
other languages.
- Versatility: Extensive standard library and compatibility with various platforms.
Python is heavily used in industries such as web development (Django, Flask), data analysis
(Pandas, NumPy), artificial intelligence and machine learning (TensorFlow, PyTorch),
scientific computing, and automation.
Q28. Explain the difference between = and == operators in Python with examples.
- Answer:
-= (Assignment Operator): Assigns a value to a variable.
Q29. What are identifiers in Python? Provide rules for naming identifiers in Python.
- Answer: Identifiers in Python are names given to variables, functions, classes, modules, or
other entities. Rules for naming identifiers:
- Must start with a letter (a-z, A-Z) or underscore (`_`).
- Can contain letters, digits (0-9), and underscores (`_`).
- Case-sensitive (`Var` and `var` are different identifiers).
- Cannot be a keyword (e.g., `if`, `def`, `for`) or built-in function names (e.g., `print`, `len`).
OR
Q30. Write a Python program to find the circumference of a circle by taking user input
(Hint: Use the `input()` function for taking the radius).
# Program to calculate the circumference of a circle
import math
# Taking radius input from user
radius = float(input("Enter the radius of the circle: "))
# Calculating circumference
circumference = 2 * math.pi * radius
# Printing the result
print(f"The circumference of the circle with radius {radius} is: {circumference:.2f}")
[E] Long questions: (5 x 4 = 20 Marks)
Q31. Describe the purpose of the if, elif, and else statements in Python. Provide an
example of using these statements in a program.
- Answer:
if statement: Executes a block of code if a condition is true.
elif statement: Allows checking of multiple conditions if the previous conditions were not
true.
else statement: Executes a block of code if none of the previous conditions are true
(optional).
Example:
# Program to check the grade based on marks
marks = int(input("Enter your marks: "))
Q32. Describe the concept of Python's string operators. Provide examples of string
concatenation?
- Answer: Python's string operators include:
Concatenation (`+`): Combines two strings into one.
Example:
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2 # Concatenates str1 and str2 with a space in between
print(result) # Output: Hello World
Q33. Briefly explain why Python is often called a high-level programming language. How
does it compare to low-level languages like assembly?
- Answer: Python is called a high-level programming language because it abstracts
complex details from the programmer and provides high-level constructs like built-in data
types, control structures, and functions. It focuses on readability and ease of use.
Compared to low-level languages like assembly:
- Python has simpler syntax and fewer lines of code required to accomplish tasks.
- Python's code is portable and platform-independent, unlike assembly which is specific
to a particular hardware architecture.
- Python automates memory management and provides built-in functions, whereas
assembly requires manual memory management and detailed knowledge of hardware.
Q34. You are mentoring a junior developer who is new to Python and frequently
encounters indentation errors. Explain the importance of consistent indentation in Python
code and provide tips on how to avoid common indentation pitfalls.
- Answer:
- Importance of indentation: Python uses indentation to define the scope of blocks of
code (such as loops, functions, and conditional statements). Inconsistent indentation can
lead to syntax errors and affect the logical flow of the program.
- Tips to avoid common pitfalls:
- Use consistent indentation style (typically 4 spaces per indentation level).
- Avoid mixing spaces and tabs for indentation (choose one and stick with it).
- Use an IDE or editor that supports automatic indentation and highlights indentation
errors.
- Be mindful of indenting after colons (`:`) in Python (e.g., after `if`, `else`, `for`, `while`,
function definitions).
OR
Q33. Explain the significance of the colon (:) in Python code. Where and how is it typically
used with examples?
- Answer:
- The colon (`:`) in Python is used to indicate the beginning of an indented code block
after control flow statements (`if`, `else`, `elif`, `for`, `while`, `def`, `class`) and before a
code block in functions, loops, and conditional statements.
- Example:
if x > 5:
print("x is greater than 5") # Indentation after colon
Q34. Suppose a person wants to do an addition but doesn’t know about operators and
operands in Python. They take one number 30 and the string “world”. Justify your answer
as to what is wrong if that person tries to add 30 and the string “world”.
- Answer:
- In Python, addition (`+`) is used to add numeric values or concatenate strings. However,
Python does not allow addition between a number and a string directly. It will raise a
`TypeError` because Python expects operands of the same type for addition.
- Example:
num = 30
string = "world"
result = num + string # This will raise a TypeError