Python Exam 5: Classes, Objects, and File Handling (10 mixed exercises)

Python Exam 5: OOP and Files

Test your skills in building solid class structures, using inheritance, and reading/writing files.

Exercise 1 Create a Class

Create a simple class:

  1. Define a class named `MyClass`.
  2. It contains a property `x = 5`.
  3. Create an object `p1` and print `x`.
Solution
class MyClass:
  x = 5

p1 = MyClass()
print(p1.x)
Exercise 2 __init__ Method

Use a constructor:

  1. Define a `Person` class with an `__init__` method.
  2. The method takes `name` and `age`.
  3. Create an object named "John" with age 36.
  4. Print the name and age.
Solution
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age)
Exercise 3 Object Methods

Add behavior to a class:

  1. In the `Person` class, add a `myfunc` method.
  2. The method prints "Hello my name is " followed by the name.
  3. Call the method from object `p1`.
Solution
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self):
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc()
Exercise 4 The `self` Parameter

Understand the `self` parameter:

  1. Must the first parameter always be named `self`?
  2. Try renaming it to `mysillyobject` in `myfunc` and confirm it works.
Solution
# Yes, you can rename it, but it must be the first parameter.
class Person:
  def __init__(mysillyobject, name, age):
    mysillyobject.name = name
    mysillyobject.age = age

  def myfunc(abc):
    print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()
Exercise 5 Inheritance

Create a child class:

  1. Define a `Student` class that inherits from `Person`.
  2. Do not add any new properties (use `pass`).
  3. Create a `Student` object and use `Person` properties.
Solution
class Student(Person):
  pass

x = Student("Mike", 24)
x.myfunc()
Exercise 6 Add Child Properties

Customize the child class:

  1. In `Student`, add an `__init__` method.
  2. Use `super().__init__(name, age)` to keep the parent behavior.
  3. Add a new property `graduationyear`.
Solution
class Student(Person):
  def __init__(self, name, age, year):
    super().__init__(name, age)
    self.graduationyear = year

x = Student("Mike", 24, 2019)
print(x.graduationyear)
Exercise 7 Iterators

Create an iterator:

  1. You have a tuple `mytuple = ("apple", "banana", "cherry")`.
  2. Get the iterator using `iter()`.
  3. Print the next item using `next()`.
Solution
mytuple = ("apple", "banana", "cherry")
myit = iter(mytuple)

print(next(myit))
print(next(myit))
Exercise 8 File Open

Read a text file:

  1. Open `demofile.txt` for reading (`"r"`).
  2. Print its contents using `read()`.
Solution
f = open("demofile.txt", "r")
print(f.read())
Exercise 9 File Write

Append content to a file:

  1. Open `demofile2.txt` in append mode (`"a"`).
  2. Write "Now the file has more content!".
  3. Close the file.
Solution
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
Exercise 10 Delete File

Delete a file from the system:

  1. Import the `os` module.
  2. Check if `demofile.txt` exists.
  3. If it exists, delete it with `os.remove()`.
Solution
import os
if os.path.exists("demofile.txt"):
  os.remove("demofile.txt")
else:
  print("The file does not exist")
Smart Editor

Write code and see the result instantly

Try it free