Page 416 - Computer_Science_F5
P. 416

Chapter Summary


               Reflect on what you have learnt from this chapter, summarise and keep it in your    Chapter Six: Object oriented programming with Python
               portfolio.
          FOR ONLINE READING ONLY



                       Revision Exercise 6               6.  Write a Python program that reads
                                                            a text file named “grades.txt”. Each
               1.  Write Python code to calculate the       line in the file contains a student’s
                   area of a rectangle with length 5        name followed by their grade (for
                   units and width 3 units. Print the       example, “Mponjoli 97”). Your
                   result.                                  program should calculate and print
               2.  Write a Python program that asks         the average grade for all students.
                   the user to input their age. If the      Handle any possible exceptions that
                   age is greater than or equal to 18,      may occur during file reading or
                   print “You are an adult”. Otherwise,     grade calculation.
                   print “You are a minor”.              7.  Explain the concept of

               3.  Create a list of your favorite fruits.   polymorphism in Python OOP and
                   Write a Python program that uses a       provide a real-world example.
                   for loop to iterate through the list   8.  How does encapsulation enhance
                   and print each fruit name on a new       data security and privacy in Python
                   line.                                    classes? Give an example.

               4.  Define a Python function called       9.  Describe the process of inheritance
                   calculate_cube that takes an integer     in Python and discuss its advantages
                   parameter side_length and returns        in code reusability.
                   the volume of a cube with that side   10. What is the purpose of an abstract
                   length. Test the function with a side    class in Python? Provide an example
                   length of 4 units.                       of defining and implementing an
               5.  Write  a  Python  program  that          abstract class.
                   prompts the user to enter their full   11. How can you achieve method
                   name. Print the initials of their first   overriding in  Python classes?
                   name and last name in uppercase          Provide a scenario where method
                   letters. For example, if the user        overriding is useful.
                   inputs “Mtukula Jamaasoni”, the       12. Discuss the difference between
                   output should be “MJ”.                   class attributes and instance


                                                    407
               Student’s Book  Form Five



     Computer Science Form 5.indd   407                                                     23/07/2024   12:34
   411   412   413   414   415   416   417   418   419   420   421