0% found this document useful (1 vote)
414 views4 pages

Certiport Python Exam Solutions

The document contains Python programming exam questions and answers, including code snippets for displaying prime numbers, pattern printing, Fibonacci sequence, and sum of digits. It also covers memory management, operator overloading, text replacement in files, TCP communication, pendulum animation, and a traffic light simulation using Tkinter. Each section provides a brief explanation or code implementation relevant to the topic discussed.
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 (1 vote)
414 views4 pages

Certiport Python Exam Solutions

The document contains Python programming exam questions and answers, including code snippets for displaying prime numbers, pattern printing, Fibonacci sequence, and sum of digits. It also covers memory management, operator overloading, text replacement in files, TCP communication, pendulum animation, and a traffic light simulation using Tkinter. Each section provides a brief explanation or code implementation relevant to the topic discussed.
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 - Questions and Answers

Q16(a)

Display first 50 prime numbers in 5 lines

Code:

def is_prime(n):

if n < 2:

return False

for i in range(2, int(n**0.5)+1):

if n % i == 0:

return False

return True

primes = []

num = 2

while len(primes) < 50:

if is_prime(num):

[Link](num)

num += 1

for i in range(0, 50, 10):

print(primes[i:i+10])

Q16(b)

Pattern Printing

Code:

n=5
for i in range(1, n+1):

print(" " * (n-i), end="")

for j in range(i, 0, -1):

print(j, end="")

for j in range(2, i+1):

print(j, end="")

print()

Q17(a)

Turtle Shapes

Code (Turtle)

See explanation in full document (turtle graphics for shapes)

Q17(b)

i. Fibonacci | ii. Sum of digits

Fibonacci Code:

def fibonacci(n):

if n <= 1:

return n

return fibonacci(n-1) + fibonacci(n-2)

Sum of Digits Code:

def sum_digits(n):

if n == 0:

return 0

return n % 10 + sum_digits(n // 10)

Q18(a)

Memory Management Explanation


Explanation:

Python uses reference counting and garbage collection. Memory pools manage small object storage efficiently.

Q18(b)

Operator Overloading

Code:

class Complex:

def __init__(self, r, i):

self.r = r

self.i = i

def __add__(self, o):

return Complex(self.r + o.r, self.i + o.i)

def __str__(self):

return f"{self.r} + {self.i}i"

Q19(a)

Replace Text in File

Code:

def replace_text(filename, old, new):

with open(filename) as f:

content = [Link]()

content = [Link](old, new)

with open(filename, "w") as f:

[Link](content)

Q19(b)

TCP Communication

Code:
Separate scripts needed for server and client using socket module.

Q20(a)

Pendulum Animation

Code:

Turtle animation using key bindings to control speed.

Q20(b)

Traffic Light using Tkinter

Code:

Use Tkinter labels and radio buttons to simulate light control.

You might also like