Answer Key

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Answer Key

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

Q1. Which arithmetic operator cannot be used with strings?


(a) + (b) * (c) ** (d) None of these

Answer: (c)

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


(a) Num (b) temp (c) javascript (d) 7flag

Answer: (d) 7flag

Q3. Which of the following is not a comment?


(a) “”This is comment”” (b) # This is comment
(c) // This is comment (d) “This is comment”

Answer: (c) // This is comment

Q4. Which of the following statements about Python dictionaries is true?


(a) Dictionaries are ordered collections of items.
(b) Dictionary keys must be immutable objects.
(c) Dictionaries can only store numeric data.
(d) Dictionary values must be unique.

Answer: (b) Dictionary keys must be immutable objects.


Q5. Which of the following print statement is correct?
(a) _print “9” + “9” (b) _print int(“nine”) (c) printf 9 + 9 (d) print (9)

Answer: (d) print (9)

Q6. Which keyword is used to define a function?


(a) Fun (b) def (c) define (d) function

Answer: (b) def

Q7. The functions are used in a program:


(a) To help avoiding repeating a set of statements many times.
(b) To enhance the logical clarity of the program.
(c) To help avoiding repeated programming across programs.
(d) All of the above.

Answer: (d) All of the above.

Q8. Which of the following data types is not supported in Python?


(a) Number (b) String (c) List (d) Slice

Answer: (d) Slice

Q9. Which of the following operations results in 8?


(a) 65 // 8 (b) 16 % 9 (c) 2 ** 4 (d) 65 ** 0.5

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

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

Q11. The output of (10.0==10) will be True.


Q12. The operators that are used to compare two operands are called comparison
operators.
Q13. The output of (5>5 and 50<80) will be False.
Q14. Python is a high-level and interpreted language.
Q15. The functions which you create are called user-defined functions.
Q16. Python files are saved with .py extensions.
Q17. Python supports single-line and multi-line comments.
Q18. Python variables have two different scopes global and local.
Q19. Implicit and explicit are types of Python conversion.
Q20. In Python, which function is used to check if a key exists in a dictionary is `in`.

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

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.

Q22. What operator is used for exponentiation in Python. Give examples?


- Answer: The operator used for exponentiation in Python is `**`. Examples:
- `2 ** 3` evaluates to `8`
- `10 ** 2` evaluates to `100`
Q23. What are identifiers in Python?
- Answer: Identifiers in Python are names used to identify variables, functions, classes,
modules, or other objects. They follow certain rules:
- Must start with a letter (a-z, A-Z) or underscore (`_`)
- Can contain letters, digits (0-9), and underscores (`_`)

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.
"""

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


- Answer: 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

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


- Answer: Input in Python refers to receiving data from the user or another program, often
achieved using the `input()` function. Output refers to displaying information to the user or
writing data to files or other devices, typically done using the `print()` function.

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!

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

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.

x = 10 # Assigns the value 10 to variable x

== (Equality Operator): Compares two values to check if they are equal.


x = 10
y=5
print(x == y) # False, because 10 is not equal to 5

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`).

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


- Answer: Type conversion (or typecasting) refers to the process of converting one data type
into another in programming.
Example:
# Integer to string conversion
num = 10
num_str = str(num)
print(num_str) # Output: '10'
# String to integer conversion
num_str = '20'
num = int(num_str)
print(num) # Output: 20

OR

Q29. How are keywords different from variable names?


- Answer: Keywords in Python are reserved words that have special meaning and cannot be
used as identifiers (variable names, function names, etc.). Examples include `if`, `else`, `for`,
`while`, `def`, etc. Variable names are identifiers used to store data and can be chosen freely
(following the rules of identifiers).

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: "))

if marks >= 90:


grade = 'A'
elif marks >= 80:
grade = 'B'
elif marks >= 70:
grade = 'C'
elif marks >= 60:
grade = 'D'
else:
grade = 'F'

print(f"Your grade is {grade}")

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

You might also like