Page 314 - Computer_Science_F5
P. 314

4.  If a = 7, y = 4, z = 3, and u= 2,        display roots. The program should
                   evaluate each of the following           also be able to test the type of the
                   statements, if possible. If it is not    root displayed and state whether
                   possible, state the reason.              those roots are real distinct,         Chapter Four: Object oriented programming with C++
          FOR ONLINE READING ONLY
                   (a) (a + z) % y   (b) (a + y % u         identical or imaginary.
                   (c) (y + u) % a   (d) (a + y) *u      Note:  For any quadratic equation
                                                               2
                   (e) (a % y) % z   (f) (y% z) % a          ax  bx  c   0, if
                                                              2
                   (g) (a *z) % y    (h) ((a *y) *u) *z      b   4 ac, the root is real distinct
                                                              2
               5.  Evaluate the following expressions.       b   4 ac, the root is real identical
                   (a) 15 / 3         (b) 17 - 12 / 4 * 2         b   4 ac, the root is imaginary
                                                              2
                   (c) 34 % 7       (d) 3 - 7% 7         9.  Use a function named “largest”
                   (e) 18.0 / 4      (f) 20 - 5 / 2.0       to create a C++ program that
                   (g) 13 + 5 % 2 - 3                       determines the largest number from

                   (h) 1570 + 3.0 * 2.0 / 5.0               a set of 10 numbers.
               6.  Write the syntax of the following     10. By using the concept of function,
                   C++ statements:                          write a C++ program that prompts
                   (a)  Switch case statement               the user to input three integer

                   (b)  Variable declaration and            values and find the greatest value
                                                            of the three values.
                       initialisation.
               7.  Suppose you are developing a game     11. Write a C++ program that finds
                                                            the reverse of number entered by
                   where there are various types of         the user. When the number entered
                   vehicles, such as cars, trucks, and      is ‘5261’ then the reverse value
                   motorcycles. How would you use           should be ‘1625’.
                   polymorphism to implement a
                   drive() method for each vehicle type,   12. Given the declaration orodha [100];
                   ensuring that the correct behavior is     (a) What expression represents the
                   executed based on the specific type           very first element of orodha?
                   of vehicle object? Provide a code         (b) What expression represents the
                   example demonstrating the use of              very last element of orodha?
                   polymorphism in this scenario.            (c) Write the code fragment that

               8.  Write a C++ program to find all               prints out the contents of
                   roots of a quadratic equations. The           orodha?
                   program should prompt a user to           (d) Is the expression list[3.0] legal
                   enter  the  coefficient  and  then,           or illegal?


                                                    305
               Student’s Book  Form Five



     Computer Science Form 5.indd   305                                                     23/07/2024   12:34
   309   310   311   312   313   314   315   316   317   318   319