You are on page 1of 13
_ SCHOOL OF COMPUTING & INFORMATICS TECI END OF SEMESTER II EXAMINATION 2015/2016 PROGRAMME: CS, BIT, BIS, SE ? COURSE NAME: Object-Oriented Programming COURSE CODE: CSCI214 .& TM 04 DATE: Thursday 12° May 2016 EXAMINATION INSTRUCTIONS | ATTEMPT ALL QUESTIONS IN SECTION A (40 MARKS) [ATTEMPT ANY THREE (3) QUESTIONS FROM SECTION B (60 MARKS) DO NOT OPEN THIS EXAM UNTIL YOU ARE TOLD TO DO SO ATTEMPT EACH QUESTION IN SECTION B ON A NEW PAGE ALL ROUGH WORK SHOULD BE IN YOUR ANSWER BOOKLET expressions? . Given the following declration(s), what isthe value ofthe Flowing int x3, tyes Lab cex i yennorx 2 y ° Woy ryt vy. pee Mathasqrttx*x + "YD awe? 1b. Define the following terms in relation to object oriented programming. (2 Mar LA class i, An Object i, nnertance iv. Method Overriding —_v. Polymorphism ; . Distinguish between Absiraction and Encapsulation in relation to the object-oriented programming paradigm. (2 Marks ) 1d. Consider and study the following program and answer the questions that follow: public class testmeth ( static int ¢ = 1; public static void main(String args(1) ( ‘Syetem.out.printin (ies, “); mci): System.out_printin (1) ) public static vold m(int 4) ¢ ) What is the outcome of the program once suitably compiled? (2 Marks ) |. Give a suitable reason to support your answer in d(ij above (2 Marks ) ©. Consider the following code fragment, after execution of the cade snippet below, what willbe the order of colors r7 and r2from the answers provide below? (2 Marks ) Rectangle rl = new Rectangle()}; ri.setColor (Color -blue) 7 Rectangle r2 = rl; ¥2.setColor (Color-red) ; A. Colorbiue, Colorsed B. Coler.biue, Color.blue _C, Colored, Colored D. Colored, Colorblue _E, None of the above. {Using some code example, show i. How to prevent a class from being extended? (4 marks ) How to prevent a method from being overridden? (4 marks) _‘Aratlonl number essodated wih @ numerator and a devominatr. Write down lass definition (including a constructor) for an object of ths nature. Page 2 of 5 ‘not more than three questions) 2. Define/Explain the following terms in relation to object-oriented programming: |. Type Promotion ii Type Casting _il Narrowing conversion iv. An Array (2 Marks @ ) b. Given the following are true according to the classification of arimals: mammats and reptiles are animals; dogs are mammals and can bark; animals can move; mammals are warm blooded and produce milk; and reptiles are cold blooded. Consider the class definition of Animal below and use your knowledge of Inheritance to define the classes Mammal, Reptile, and Dog with minimal code. (4 Marks @ ) public class Animal ( public void move() ( system.out.printin("animals can move") ; y Question 2: a. Briefly explain the ty, catch, throw, throws, and finally keywords in Java. ( 8 Marks ) b. Consider the following piece of Java code: 1. class myClass ( public static void main(String args{1) { System.out print ("Factors less than 10: * for (int i= 0, d= 4000; i < 10; i-+) if (d 8 i == 0) system.out-print(i + * * ) i. Identify any error(s) in this Java program clearly stating the line number(s). (5 Marks ) ji, Suggest possible ways of correcting the error(s) identifed in (a) above (if any). (5 Marks ) fi, How would you catch ail the exceptions that might be within your code without specifying the type of exception? (5 Marks )_ Page 3 of & vials when cde tern, reiraion uber, te cus ate of bith. Each time a student registers, the administrator is able to see ‘number of students who have registered. 1. Define a class that will captures the above scenario including the constructor. Pe (5 Marks ) = 1. Assuming that the data in ass you hve defined above is privat, define ; methods that would access the data. (5 Marks ) 'b. Given the class diagram in Figure 1, answer the question that follows. 7 Fruit Apple Orange Gotdende1 icious Macintosh Figue Define ll the classes as shown in the class diagram in Figure 1 (10 Marks) tion a) Define what is meant by an interface on an OOP language such as Java. ( 2 Marks ) b) Carefully study the following Singer class and answer the questions below: public abstract clase Singer ( private String naneOfSinger: U1 his abstract method is to output kind of songs singer sings public abstract void eings(); public String getsingerName() ( ) rete naneofsinger public void setSingerName(String name) ( paneofSinger = name; ) |. You are required to create three subclasses of singer which describe the following singers; jaz singers, pop singers, and opera singers. ( 6 Page 4 of § (8 Marks ete oer cm aed rentals wen nas a hzzSinger las defined in) above and which Implements the Professtonal Interface. This class is to have an extra field to represent hours spent taining ‘and methods to get and set tis (8 Marks ) Question 5: ‘8 Briefly explain wat is meant by the following terms in relation to object-oriented Programming (2 Marks @ ) i, Nested class fi, Inner class Ti, Anonymous class , Instance variable v. Class variable . Consider the following piece of Java code: 1. class Hash ( 2.” ine x: 3. Entry entry; 4. void aisplay() ¢ 5 System.out.printin(*value =" + yi; 63 7. clase mnery ¢ a int y; 3 void display() 10 Systen.out.printIn(*value = * + x); an. i 12 43 i, With your knoviledge of Nested and Inner classes, comment on the usage of the instance variables x and y. (4 Marks ) Re-write the code above replacing the display () method of the Hash class with a constructor that initializes the entry variable, (6 Marks ) Page 6 of 5 Piinieeectererememe tS An : 1b) Attempt ALL questions. ean ow ©) Answorstgevery question shouldbe stared ona ssh pags toio co Nv question one ‘4. Certain rules govern variable declarations in the Java language. State which of ‘he following are valid variable declarations and which ones arenot:. -» (2 Mark each } 4. char _aiphe > ‘et, omega = SS) sowie bn a, c= 4, d= Math WAL! inst double 2204 = 9.14: Mv! Striag( sonth days = new Steing(1217 “< acetate + OR wWx2 ~ 7 4 Given three Java language integer variables 5.3, and x with values 45.and 6 5 respectively, find the value of the following expressions (executed independently) (2Marks each) bese peg kg reer Sens rays aig 88 eee bres . Question two AS Define the following terms as used in Object Oriented Programming: he 1 Clase err eenan (Mark) fi. Object (Mork) 5 b. Consider the following class. “ition fr an integer stack that is ereated from | anarray: * St 1, class Intstack | 2 Tat) stack: : 3 int top = Vai Dn Wie ed 5) pubtie tntstecktimt 1 5) ¢ ‘ Steck = 8 Ga # 2 ' ' ed i,_ Identify any errors (if any) in this class definition. rks) ii Rewrite the cass above replacing the array on ine 2 with an object of type aerayiist and leaving the constructor argument on line 5 a8 an a array (6Marks) Waiheke, ofrealmibet CL nytmad| fis png lami ae 3\6 il jodel, {s characterized by the following attributes: Its make, m rand net weight (in Ib). Write down a class definition that — ae objects for such cars. (SMarks) — We now have another Kind of car that has all of the attributes in (a) above but, a addition is characterized by the year of manufacture and we would like to Fepresent the net weight in tons (t) instead of pounds (Ib). Write down a class definition for this car with minimal code. (Hint: 1t= 2,20016) (5 Marks) Question Four ‘a With examples, briefly explain the concepts as applied to Object Oriented + Programming. (2.5 Morks each) i. Encapsulation fi Inheritance b. The table below shows the visibility of data and methods as accessed from different places in.a java program, Study it and fill with a tick (y’) where itis visible and a cross (X) where it is not visible. (5 Marks) [oc meaiier Hesened aecened onmenbers frome roma different, fromthe Ine clos subeose packepe same eas x Vv ee _ Wa wv t —_ : 4 END ein NA PUTING AND INFORMATION SCIEN TING AND INFORMATICS TECHNOLOGY C80 2115, ppl 20M4 SEMESTER 1 : PROGRAMMING METHODOLOGY IL ve questions mane SER Tet 1 atin nts tet answer al questions There ores Tin tow 8) Write down the. nthe syntax ofthe whe statement inva 8) Explain how the wife statement works, ors Consider the following piece of Javn code: public etatie ine Teompucet jue (1) | ace ae if (ata) m0) breaks Shoe fe Gtaseah errcime; oe as Treuaay 8) Describe what the method compute! does. 3 marks 1b) Rewrite the method above without using array subscription. 5 mark (Hint: Assume a is an object of tyne Arrut-r¥ Define the following in relation to Java S marks 9 i Aclass i, An Object v.—_interitarve 3 1, Eneapsutation v. Instance variables 1 ). Consider the vollowing Java declarations: int 2 x = 105, y= 3; ‘What isthe value of the following expressions? S marks i xy “12 RM Y=ROPKHyLy i, zexthcy (%%5—=O|ly> x) && (7? ~*0") 1) A bieyele can be chars: terized by the following attributes: Its type, weight, mileage, and duration of sravel, Write down a class defrition that can be used to create objects of such bieyeles. 5 marks b)_ We now have another kind of bicycle that has al of the attributes in (a) above but on addition, contains the seat height and we want tobe abe to compute its average speed. Without re-writing any piece of cade a in (a), write down & class definition for this kind of bicycle. 5 marks 5. Considet the following recursive function: public static int mystery (int a, int b) ( nie O° es SE (b == 0) return OF 6/29) else if (b ¢ 2 == 0) return mystery(a + a, B / 2); 190 46 else return mystery(a +a, b / 2) + ar eras y a) What is the value of mystery (5, 25) and mystery (6, 36)? 6 marks a 6) ) Given positive integers a and b, describe what mystery (a,b) computes. 3 4 marks ae Bere ane i J ie ane 5 ont . — SCHOOLS CECCOMPUTING & INFORMATION SCIENCES recov? OF COMPUTING anD NFO ‘TEST [rt st, ce Ne BSE] (SC 1214 Object onented rogranming ae: mami 2 Instructions: os Ady 8) Attempt At questions (a-d) in thls pope: ') Answers to every question shouldbe strted ona frsh nage Questions 3) With examples( Java code), dtferentiate between: i) Class variables and instance Variables. “i 4 (4 Marks) wv 48) Agetter mothod and a seter method (marie) 1g 4) local and Global variable (Morte) Management ) Assume that you:have been tasked to develop a Students’ Atiéndance System ‘or yous class using an Object-Oriented Programming language like Java. One of the classes included 1s called Student... It is designed to have 3 private instance . variables: that is; name, email address and gender ‘and one class variable called incremented by 1 every time an objectis created irons this class ie. We will keep track of the number of students who register. The class should also have setter and getter uethods for the emaii_eddress: Further more, the Student class should have a const uetor to initialize the variables given. approach to, develop such a system over structured or procedural prograusing \¥ 4 ¢ (2Marks) ‘i. Write the Scucient class with all the necessary variables and methods ~ (@ Marks). ii. Writea test class called Student Test which has the main method and in which you 1) Instantiate an object Student! whase name, email address and gender are Lunkuse Phionah, lunluise@cismakacng and Female respectively (4 Marks). 2) Change the email address to junisise@yalioo.com using the setter method (4 Marks) ), With java code examples, differentiate between i. Method overloading and Method overriding (3 Marks) | ii, A final class and an abstract class (2 Marks) (3 Marks)_) iii, An Interface and abstract class )) In order to improve security in the Universities, the Ugandan Government has requested ‘every university to develop a security system. The government specified the protocol for the Inter-University Security system as follows. = Each University security system must provide a method called Register () which fakes in the name, gender and Date of birth toreyister usersinto the system, | Itreturas nothing Each University System should provide a method called Facial Recoginition() that takes no parameters but can recognizes individuals by their faces. X Write a java interface that could be used to enforce the above scenario. (4Marks) | ~END~ IAKERERE U re ERERE UNIVERSITY ee GE OF ce COMPUTING & INFORMATION SCIENCES SCH OOL OF COMPUTING & INFORMATICS TECHNOLOGY END OF SEMESTER | EXAMINATION 2013/2014 PROGRAMME: Software Engineering YEAR OF STUDY: II COURSE NAME: Programming Methodology Il COURSE CODE: CSc 2115 DATE: . Tuesday 31% December 2013. TIME: 12:00 15:00 EXAMINATION INSTRUCTIONS ATTEMPT ALL QUESTIONS IN SECTION A (40 MARKS) ATTEMPT THREE (03) QUESTIONS IN SECTION B (60 MARKS) DO NOT OPEN THIS EXAM UNTIL YOU ARE TOLD TO DO SO ATTEMPT EACH QUESTION IN SECTION B ON A NEW PAGE ALL ROUGH WORK SHOULD BE IN YOUR ANSWER BOOKLET reene gE ay Ant») yareyo) return mystery (a + a, bs 207 eryla tay bs 2) ta isthe value aide Neofmyatery(s, 25) andmystery (6, 36)? following gen Mhieh arable names are allowed in Java Briefly state which ofthe "NB are valid and which ones are not. a m8 .as) fi, 2y99 im Goliad s i 4x “0 porn m (0:6) aa meres 8) (aos 1) 1He in (HOSE I4EO Yat, we me,se) m (21 18) rs wa ae satan OV % ECTION B -oane geod mine) au Vouestion 1: mene 3) ae Suppose you have an integer variable sina Java program Write down ava language expression whose valuc is frue if i is greater than 5 or 1 is an odd number. (4 Marks ) b. Define the term Inheritance in relation to object oriented programming. 2 Marks) c. A bicycle can be characterized by the following attributes: its type, weight, mileage, and duration of travel. Write down a class definition that can be used to create objects of such bicycles. (8 Marks ) d. We now have another kind of bicycle that has all of the attributes in (b) above but on addition, contains the seat height and we want to be able to compute its average speed. Without re-viiting any piece of code as in (@, write down a class definition for this kind of bicycle. (6 marks ) Page 3 of 5 ii, Aclass (2 Marks ) iv. An object (2 Marks ) ¢. Given an array of integers write a Java method to determine if there are any. duplicates. Your method should have the following signature: boolean containsDuplicates (int () array) (8 Marks ) 4 a. Briefly explain the ty, catch, throw, throws, and finally keywords in Java (10 Marks ) b. Consider the following piece of Java code: 4. class myClass { 2, public static void main(String args(}) { 3 System.out.print ("Factors less chan 10: “); p 4 for (int = 0, d= 4000; 4 < 10; 444) 5. if (@ 0 i == 0) 6 System.out.print(: + ™ %); a) 8.) ©. Identify any error(s) in this Java program stating clearly the line mumber(s) (5 Marks ) 4. Suggest possible ways of curbing the error(s) identified in (a) above (if any) BGP 7 (5 Marks) Question : a. Briefly explain each of the following terms (2-Marks Bach, Total of 10) statie variable, encapsulation, polymorphism, overriaing, overloading, e Page 4 of H company Fequires a videos and CDs. The propa Program to manage rentals of items like DVDs, ven sm should a intain 9 complete inventory of al items ‘evord each customer, including details such as name, address, phone re cord each rental a customer has, inching the start and end dates. Provide the operations needed to make a rental and handle a rental return, including checking ifthe retun is late and determining alte return fine Identify a collection of classes that could be used to implement the program. For ‘each class give the instance variables and methods required. Java code is not required (lo Marks Y a. Write down the syntax of the Java language else-if statement and explain how it works. (5 Marks ) 1), Write down the syntax of the Java longuage for statement and explain how it work. (5 Marks} cc. Consider the following piece of Java code: ign o while (i#+ < 10 11 3 < 20) ¢ geaezrges rata ae ) Re-write the code above removing the while statement, replacing it with a single {for statement, The resulting code may not contain more than 2 expression statements, and must be semantically equivalent to the above, (6 Marks ) 4. Write dow a piece of Java code to print out all the perfect cubes less than 4000 Ge. 1,8,27, (4 Marks ) Page $ of S

You might also like