0% found this document useful (0 votes)
44 views2 pages

Python Test Paper Overview

This document is a Python test paper consisting of multiple choice questions, short answer questions, coding exercises, and an advanced problem-solving section. The test is worth a total of 50 marks and covers various Python concepts such as functions, data types, and operators. Instructions emphasize answering all questions and reading carefully.

Uploaded by

bhuwaniadarsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Python Test Paper Overview

This document is a Python test paper consisting of multiple choice questions, short answer questions, coding exercises, and an advanced problem-solving section. The test is worth a total of 50 marks and covers various Python concepts such as functions, data types, and operators. Instructions emphasize answering all questions and reading carefully.

Uploaded by

bhuwaniadarsh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

WORLD OF COMPUTERS

PYTHON TEST PAPER


Instructions:

1. Answer all questions.


2. Read each question carefully before answering.
3. This test is worth a total of 50 marks.

Section A: Multiple Choice Questions (2 marks each)

1. What does the print() function do in Python? a) Reads user input


from the console b) Prints output to the console c) Performs
mathematical calculations d) Defines a variable
2. Which of the following is a correct way to comment multiple lines in
Python? a) // This is a comment b) /* This is a comment */ c) # This is
a comment d) <!-- This is a comment -->
3. What is the result of 5 + 3 * 2 in Python? a) 16 b) 11 c) 10 d) 26
4. Which data type is mutable in Python? a) Tuple b) List c) String d)
Dictionary
5. What will be the output of the following code?
python
Copy code
my_list = [ 1 , 2 , 3 , 4 , 5 ] print (my_list[ 2 ])
a) 2 b) 3 c) 4 d) 5

Section B: Short Answer Questions (5 marks each)

6. Explain the difference between == and is operators in Python.


7. What is the purpose of the __init__ method in Python classes?
8. Describe the difference between lists and tuples in Python.
Section C: Coding Exercises (10 marks each)

9. Write a Python function that takes a list of numbers as input and


returns the sum of all the numbers.
10. Write a Python function to check if a given number is prime or
not.

Section D: Advanced Problem Solving (15 marks)

11. Write a Python function to calculate the factorial of a given


number using recursion.

Common questions

Powered by AI

Typical use cases for recursion in Python include tasks like traversing hierarchical structures (such as file systems and trees), solving problems with repetitive substructure (such as Fibonacci sequence and factorial); however, recursion should be used with caution due to potential stack overflow issues. Best practices include ensuring a base case to avoid infinite recursion, limiting recursion depth to avoid high memory usage, and considering iterative methods when feasible for optimization .

In Python, the '==' operator checks for value equality, meaning it evaluates whether the values stored in the objects are the same. In contrast, the 'is' operator checks for identity, meaning it evaluates whether two references point to the same object in memory. '==' should be used when comparing values, and 'is' should be used when checking if two variables point to the same object .

Lists in Python are mutable, meaning their contents can be changed after they are created. Tuples, on the other hand, are immutable. This means once a tuple is created, its content cannot be modified. This immutability of tuples allows Python to optimize memory and performance for tuples. The choice between using lists and tuples depends on whether you need a changeable data structure (use lists) or a read-only structure (use tuples).

The primary function of the print() function in Python is to output data to the console. This makes it different from functions like input(), which read data from the console .

Recursion can be used in Python to calculate the factorial of a number by defining a function that calls itself with decremented values. The key is establishing a base case to end recursion; for factorial, this is when the number is 1 or 0, returning 1. Recursive functions should be carefully constructed to avoid infinite loops and excessive memory usage due to high recursion depths .

A common algorithmic approach to determine if a number is prime in Python is to use trial division, where the number is divided by all integers up to its square root. If none divide evenly except for 1 and the number itself, it is prime. This is computationally efficient compared to checking divisibility up to the number itself and reduces unnecessary checks, but it can be limiting for very large numbers due to computational intensity .

Python's mutable lists allow for efficient in-place modification, which can be beneficial for programs requiring dynamic dataset changes, such as during iterative processing tasks or when elements need to be frequently appended or removed. However, excessive mutation can lead to reduced performance due to potential fragmentation and increased resource consumption when growing or shrinking dynamically. Lists are preferred when flexible and dynamic structures are needed .

Mutable data types in Python are those whose contents can be changed after initialization. Lists and dictionaries are classic examples of mutable data types in Python. Unlike tuples and strings, which are immutable, lists allow modification of elements, appending, and removal of items .

Python follows the standard mathematical operator precedence rules, where multiplication (*) has higher precedence than addition (+). Therefore, in the expression 5 + 3 * 2, the multiplication is performed first, and then the result is added to 5, resulting in 11 .

The __init__ method in Python classes is a special method that serves as an initializer or constructor. It allows the class to initialize the attributes of the object with default or provided values during object creation. This method is automatically called when a new instance of the class is created, enabling the setup of initial object state .

You might also like