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

Python Programming Exam Guide

The document outlines a Python Programming Exam consisting of five sections: Theory Questions, Code Snippet Completion, Code Dry Runs, Code Error Correction, and Application-Based Coding, totaling 100 marks. Each section tests different skills, including theoretical knowledge, code completion, prediction of code outputs, error correction, and practical coding applications. The exam covers key Python concepts such as typing, execution methods, variable scope, and the use of libraries.

Uploaded by

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

Python Programming Exam Guide

The document outlines a Python Programming Exam consisting of five sections: Theory Questions, Code Snippet Completion, Code Dry Runs, Code Error Correction, and Application-Based Coding, totaling 100 marks. Each section tests different skills, including theoretical knowledge, code completion, prediction of code outputs, error correction, and practical coding applications. The exam covers key Python concepts such as typing, execution methods, variable scope, and the use of libraries.

Uploaded by

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

Python Programming Exam - Version 2

Section 1: Theory Questions (20 marks)


1. Explain the differences between dynamically-typed and statically-typed languages.
2. What are Python?s execution methods? Give an example for each.
3. What is the significance of indentation in Python?
4. Define and explain the purpose of docstrings in Python functions.
5. Differentiate between local and global variables in Python.
6. What are the advantages of using external Python libraries such as NumPy and Pandas?
7. Explain how inheritance and polymorphism are used in Object-Oriented Programming.

Section 2: Code Snippet Completion (20 marks)


1. Complete the function that checks if a number is prime:
def is_prime(n):
if n < ____:
return False
for i in range(2, ____):
if n % i == 0:
return False
return True
2. Complete the function that reverses a string:
def reverse_string(s):
return s[____:____]
3. Fill in the missing code in the factorial function:
def factorial(n):
if n == 0:
return ____
return ____ * factorial(n - 1)

Section 3: Code Dry Runs (20 marks)


1. Predict the output of the following:
x = [1, 2, 3]
y = x[:]
[Link](5)
print(x)
print(y)
2. Predict the output:
def modify_string(s):
s += ' World'
string = 'Hello'
modify_string(string)
print(string)

Section 4: Code Error Correction (20 marks)


1. Fix the syntax and logical errors:
def multiply(a, b)
return a * b
print(multiply(2, 3))
2. Fix the IndexError:
numbers = [5, 10, 15]
print(numbers[3])

Section 5: Application-Based Coding (20 marks)


1. Write a function `count_vowels(s: str) -> int` that counts the number of vowels in a given string.
def count_vowels(s):
# Your code here
2. Write a function `merge_sorted_lists(list1, list2)` that merges two sorted lists into one sorted list.
def merge_sorted_lists(list1, list2):
# Your code here

Common questions

Powered by AI

Dynamically-typed languages, like Python, determine the data type of a variable at runtime, allowing for more flexibility in how variables are used and changed. This can lead to faster development times as developers don't need to explicitly declare variable types, but it may introduce runtime errors that aren't caught until the code is executed . Conversely, statically-typed languages, such as Java, require variable types to be explicitly declared, enabling the compiler to catch type-related errors at compile time, which can reduce runtime errors but slow down the initial development process .

Local variables in Python are defined within a function and are accessible only within that function, meaning they cannot be accessed outside of it. Global variables, in contrast, are defined outside of any function and can be accessed by any function within the same module. This difference affects scope by limiting local variables to their function-enclosed execution, preventing them from unintentionally affecting or being affected by external code, thus aiding in data encapsulation and reducing potential side effects .

Inheritance allows a class to inherit properties and methods from another class, promoting code reusability by enabling developers to create new behaviors based on existing code. For example, a 'Dog' class can inherit from a 'Animal' class, acquiring its methods and properties without additional code duplication . Polymorphism allows methods to do different things based on the object it is acting upon, enhancing flexibility and maintenance since different classes can define their own versions of a method. This can, for example, allow a 'Dog' object to define a 'speak' method differently than a 'Bird' object, yet both can be used interchangeably under a common 'Animal' interface .

Indentation in Python is crucial as it defines the blocks of code, such as loops, conditionals, functions, etc. Unlike languages that use braces to define code blocks, Python relies on indentation levels to demarcate the start and end of these blocks, which makes the code visually neat and straightforward. Improper indentation can lead to syntax errors or logical errors by unintentionally altering the code's execution flow, such as including statements inside a loop or conditional that should not be .

Fixing syntax errors involves correcting errors that cause the Python interpreter to fail to parse the code, such as missing colons, incorrect indentation, or unmatched parentheses. For example, modifying 'def multiply(a, b) return a * b' by adding a colon after the function definition statement resolves a syntax error . Logical errors, however, occur when code runs without syntax issues but produces incorrect results, such as attempting to access an index that is out of an array’s range. Adjusting 'print(numbers[3])' to a valid index within an array helps fix an IndexError, a specific type of logical error .

Predictive reasoning involves anticipating the output of code based on logical analysis of the code’s structure and execution flow. This skill is essential as it enables programmers to foresee potential issues, such as unexpected variable modifications or sequence-related errors. For instance, understanding that 'x = [1, 2, 3]' and 'y = x[:]' creates a copy in 'y', not affecting 'x', allows one to accurately predict that 'x' remains '[1, 2, 3]' even af ter appending to 'y' . This analytical ability facilitates debugging, optimization, and the design of robust modules in larger projects .

Python supports several execution methods, including interactive and script mode. Interactive mode allows users to execute Python commands directly in the Python shell, much like a calculator, which is ideal for testing snippets of code. For instance, typing 'print("Hello, World!")' directly in the shell prints the greeting instantaneously. Script mode, on the other hand, involves writing Python code in a text file with a '.py' extension, such as 'hello.py', and executing it from the command line using 'python hello.py', which is suitable for running longer, more complex programs .

When developing application-based functions like 'count_vowels' and 'merge_sorted_lists', ensuring both functionality and performance is critical. Functionality determines whether the function correctly performs its intended operation, such as accurately counting vowels or effectively merging lists. Performance, on the other hand, relates to the efficiency of executing these operations, particularly with large datasets. For instance, 'merge_sorted_lists' should ideally achieve linear complexity relative to the total length of both lists to avoid bottlenecks, ensuring the solution remains scalable and responsive .

External Python libraries like NumPy and Pandas significantly extend Python's capabilities, especially in numerical and data manipulation tasks. NumPy provides efficient storage and operations over large arrays and matrices, allowing for high-performance scientific computing. It includes powerful tools for integrating C/C++ and Fortran code . Pandas, on the other hand, offers data structures like DataFrames for data manipulation and analysis, enabling users to easily import data, clean, process, and visualize it efficiently. These libraries reduce the complexity and length of code required for data-centric operations, facilitating faster and more sophisticated data processing tasks .

Docstrings in Python are used to provide a succinct description of a function's purpose, parameters, and return values directly within its definition. They serve as inline documentation, enhancing code maintainability and readability by allowing developers to understand the function’s intent without having to decipher the code itself. Docstrings can also be accessed via tools like 'help()' or '__doc__', serving as a form of automated documentation that supports effective collaboration and future development .

You might also like