0% found this document useful (0 votes)
84 views7 pages

Bit 121 Oop I 2021 C++

Uploaded by

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

Bit 121 Oop I 2021 C++

Uploaded by

Isaac Kigen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
COURSE CODE: COURSE TITLE: (University of Choice) MASINDE MULIRO UNIVERSITY OF SCIENCE AND TECHNOLOGY (MMUST) MAIN CAMPUS UNIVERSITY EXAMINATIONS MAIN EXAM. 2020/2021 ACADEMIC YEAR FIRST YEAR SECOND SEMESTER EXAMINATION FOR THE DEGREE OF BACHELORS OF SCIENCE IN (COMPUTER SCIENCE, INFORMATION TECHNOLOGY, ETS, KNOWLEDGE MANAGEMENT, MATHEMATICS TECHNOLOGY, STATISTICS TECHNOLOGY, ECONOMICS TECHNOLOGY & SPA) BCS 120/BIT 121 OBJECT ORIENTED PROGRAMMING 1 28/05/2021 TIME: 8:00-10:00AM Se eennnInI I InIIIENnEIIeeeeeeeeeeee ee INSTRUCTIONS TO CANDIDATES: ANSWER QUESTIONS ONE AND ANY OTHER TWO. MMUST observes ZERO tolerance to examination cheating Paper Consists of 7 Printed Pages. Please Turn Over > Page 1 of 7 ARKS QUESTION ONE: COMPULSORY QUESTION [30 MARKS} (a) A class is declared as {class Date a 3) private: Z 4 int days(12) = {31,28,31,30,31,30,31,31,30,31,30,31}; | int. day; | int month; | int year; +) public: ots (i) Write the definition of set functions such that it initializes the instance variables. The year should be initialized to a value between 2000 and 2100. ‘The month should be initialized to any value between 1 and 12. The day is initialized to a value greater than zero and less or equal to total days in the respective month. Assume all years have 28 days in February. [6 Marks] (ii) Write definition of get function for each of the instance variables. (3 Marks} (iii) Using a member initializer list, write the definition of a delegate construc- tor that receives two parameters, and invokes a constructor shown below (Assume the constructor below is appropriately defined). [3 Marks} |Date (int day = 1, int month = 1, int year - 2000){ af} (iv) Write the definition of a function declared as void increment(); such that it adds one day to the current date. (4 Marks| (b) The mathematical constant e = 2.7182818286 raised to some value x can be estimated using the series f=1+, 2 @altet+ yt where n is the number of times you improve the estim loop, write a C++ code fragment to estimate e*, from the user. Declare and initialize the n, factorial of n +1 is given by (n+ 1)! nation. Using a while Declare and read x and n constant e. Hint: Given factorial of n+l) x (n)! [5 Marks} BCS 120/BIT 121 Object Oriented Programming L SCI |, MMUST 2021. Main Examination Page 2 (c) What would be the output of the code except below [3 Marks} i) int x = 0; | for(; x <= 8; ) J cout << x; q else Y cout << "\Nno!\n"; p xt 1; ed (d) Write a function that returns the minimum value in an array passed to it. (3 Marks} (e) What would be the output of line 4, 5 and 6 of the code below is line 3 displays OX6FFE10 on a machine that uses 64 bits to store double values. _ [3 Marks] s| double x = 20.5; 2| double *xPtr = &x; 3) cout << xPtr << endl; «| cout << *kexPtr << endl; s| cout << ++xPtr << endl; «| cout << xPtr + 2 << endl; QUESTION TWO (20 MARKS] (a) If sp is an approximation of a the square root of a number A, then a better approximation s,41 can be given by atl A a= 5 (sn > Using a for loop, write a function that will calculate and return the square root s of a number A such that the difference between A and s? is less than 107%. [5 Marks} (b) Write a program that simulates the working of a three wheel casino machine with numbers 1...10. The program should work as follows: (a) The program randomly generates three integers for wheel_1, wheel_2 and wheel_3 - with each wheel taking a value in the range 1 ~ 10, 10 inclusive (b) The integers represent the numbers shown on the three wheels of a casino slot machine BCS 120/BIT 121 Object Oriented Programming 1, SCI, MMUST 2021. Main Examination Page 3 ‘The program outputs the payout for the three numbers as follows: © If the three wheels display the same number, the payout is 80 © If exactly two wheels display the same number, the payout is 3 © If the three wheels display consecutive numbers (not necessarily in order), payout is 16 e.g. wheel-1 = 2, wheel-2 = 4 and wheel.3 = 3 are consecutive and so are wheel_1 = 5, wheel_2 = 4 and wheel.3 = 6 © The payout is 0 for all other combinations. Examples of program execution are given below. Your output should match these examples as much as possible. [10 Marks| Program execution : Example i ---Three wheel values--- Wheel 1 -> 8 Wheel 2 -> 8 Wheel 3 -> 7 Payout is 3 ______ [Program execution: Example 2] ---Three wheel value a Wheel 1 -> 6 (c) Tribonacci sequence is given by the formula Fra = Frit Fn-2+ Fug Given that Fy = 0, Fi) = 1 and Fy = 1, using a while loop, write a C++ program that displays the first 10 Tribonacci numbers. (5 Marks] BCS 120/BIT 121 Object Oriented Programming 1. SCI, MMUST 2021, Main Examination Page 4 [20 MARKS} QUESTION THRE (a) Figure 1 shows the structure of an array named marks in memory. 4 | 50 | 70 [oa | 60 [on a 45 | 78 | 72 | 8 | Figure 1: Structure of an array in memory Write a + program that will read values of this array and store them in a csv file named data.csv such that the contents of the file look as shown in Figure 2 {5 Marks) Name | BITI2i | BCS112 | SIK100 | BIT113 | SUM | Mean Mary | _34 56. 76. a | 24 | 535 Kaka a | 65 67 7H | 640 John [45 78 72 8__| 253 | 68.25 Figure 2: Contents of data.csv file (b) A class is defined as shown below class Shape2D { | private: | 5} string name; .| string date; | | public: +, void setName(string n){ name =n ; } | of void setDate(string d){ date =d ; } | string getName(){ return name; } string getDate(){ return date; } string toString(){ a //wxite definition of this function | a asl virtual double area() const { return 0.0 ; } a virtual double perimeter() const { return 0.0; } ali (i) Write definition of function void toString() at line 10 such that it returns the details of shape (name and date) as a string. [3 Marks] (ii) A Rectangle is a shape that has two characteristics, length and width in ad- dition to characteristics of Shape2D. Write the definition of class Rectangle i. Write the set and get functions for Rectangle [2 Marks] BCS 120/BIT 121 Object Oriented Programming I, SCI, MMUST 2021. Main Examination Page 5 ii, Override the area() and perimeter() functions such that they return area and perimeter of a rectangle respectively. (4 Marks} iii, Override the function toString() such that it returns details of a rectangle as a string, [2 Marks} (c) You can estimate the value of using the series nig de, 4 Ab agar 3 5 kd 9 were n is the number of times you improve the estimation. Using a do...while loop, write a C++ program that estimate the value of m with n = 20 [4 Marks] QUESTION FOUR. [20 MARKS] (a) Using a loop of your choice, Write a function that calculates and returns the greatest common divisor of two integers it receives as parameters. (3 Marks] (b) With illustration, explain what is a dangling else (2 Marks} (c) Taylor series for sin(x) and cos(x) are defined as which is same as oo gins O° Grey oo in cos(x) = UVa which is same as n=0 ’ Pa an 81 nl where « is the angle in radians and n is the number of repetitions. Given an angle in degrees (deg), we can convert it into radians (rad) using the formula deg d= 9 rad = xm where ™ is a constant whose value is approximately 3.14159. Given a partial class definition on next page, BCS 120/BIT 121 Object Oriented Programming I, SCI, MMUST 2021. Main Examination Page 6 class Trig Mt | | private: | 4 double x; | public: | (i) Write the definition of the set: function for instance variable x. Valid values 1 for x range from 0 to 180. (1 Marks] (ii) Write the definition of the get: function for instance variable x. [1 Marks) (iii) Write definition a fur n that receives an angle in degrees and return the equivalent angle in radians [2 Marks} (iv) Write definition of a recursive function that receives an integer and returns the factorial of that integer (2 Marks] (v) Using a for loop, write definition of a function named sinx which receives angle in degrees and calculates and returns the sin of that angle. Re-use functions defined in (ii), (iii) and (iv) above. Let n vary from 0... 10 [3 Marks] (vi) Using a while loop, write definition of a function named cosx which receives angle in degrees and calculates and returns the cosine of that angle. Re-use functions defined in (ii), (iii) and (iv) above. Let n vary from 0... 10 [3 Marks] (vii) Write definition of the function declared as void display() such that it displays the sin and cosine of angle x. For example, if x is initialized to 30°, the output of this function should be equivalent to sample output shown below [Eranpte Output] 3 — Angle in degrees = 30 | Angle in radians: 0.523599 cos(30) = 0.866025 sin(30) = 0.5 BES 120/BIT 121 Object Oriented Programming I, SC MMUST 2021. Main Examination Page 7

You might also like