You are on page 1of 12
conser nase —_| OF DODOMA THE UNIVERSITY VICE CHANCELLOR IEPUTY SEARCH AND ‘CONSULTANCY OFFICE OF THE ACADEMIC, RES! FORMATICS AND VIRTUAL COLLEGE OF IN! EDUCATION ter Science and Enginecring Department of Compu! ity Examination for the 2022/2023 End of Semester One Univers Academic Year Object Oriented Programming in Java Course Name: PsperCodeNumber: CP 215 Date of Examination: 27" February, 2023 Time: 15:30-18:30 Duration: 3 Hours Venue(s): CIVE-AUDITORIUM, LRB 105, 106, FL1, 003D, 004C, 004D, 005B Sitting Programme(s): BSc. f BSc. BIS2, MTA, HIS2, DCBE2, ID CSDFE2, TE2, CE2, CNISE2, C52. 1528559 , IDIT2, INSTRUCTIONS TO CANDIDATES i. This examination paper consists of TWO (2) sections with SEVEN (7) questions in FIFTEEN (15) printed pages. Answer all questions in Sections A and THREE (3) questions in Section B. The total score for this examination is 100 points. Marks are allocated at the end of each question. ALL responses should be written in the answer book provided, Insert the examination Paper into the answer book after the examination. Students should not take any examination paper(s) Out of the examination room. SECTION A: (40 MARKS) Question One Read each question carefully and choose the most correct response. (1 Marks Each) i, The static variable declared in a class is called ;- D, Object variables A. Local Variables B. Class variables E. Global variables C. Instance variables Which of the following characteristics of an object-oriented programming language restricts behaviour so that an object can only perform actions that are defined for its class? D.Encapsulation A. Dynamic Binding B. Polymorphism E. Abstraction C. Inheritance iii, Which command lines will compile and run java class called echo respectively? A.javac echo and java echo,java B.javac echo.java and java echo C. java echo.class and javac echo D. java echo and javac echo.java E. javac echo.java and java echo,class iv, What type of relationship exists between myMethod in class Animal and myMethod in class Dog? class Animal { Public void myMethod ( ) { fe. Systen.out.printin("from class Animal") ; ! } ¢lass Dog extends Animal { Public void myMethod( String x) { L, System, out.printIn("from class Dog: +x) } ) A. Method overloading D. Method hiding B. Method binding E. Method Overriding C. Method copying ¥. Which of the following statement describes dynamic resizing as is applies to the ArrayList class? A. The size of a new ArrayList object must be specified at instantiation. B. The size of an ArrayList object can be iterated throughout a Program, C. The size of an ArrayList object is fixed at instantiation. D.The object changes size dynamically as new elements are Jass Teacher has T as its object class has P as its abject reference, cand § being the object reference for Student respectively. reference Which of these assignment statements are legal? LseT u.t-P u.P=T IV.P=S A.lland IV only Da, H, 1 and IV B. Mand Ill only E. Il, Ill and IV only C.1, Hand IV only vii, Which of the following statements is true? A.X extends Y is correct if and only if X is a class and Y is an interface. B. X extends Y is correct if and only if X is an interface and Y is aclass. C.X extends Y is correct for all combinations of X and Y being classes and/or interfaces. D.X extends Y is correct if X and Y are either both classes and both interfaces. E. Both C and D viii. The following are examples of checked exceptions except:- A. SQLException B. IOException C. RunTimeException D. ClassNotFoundException E. InputMismatchException ix. What is known by the declaration ArrayList list = new ArrayList(); 9 Aulist is an ArrayList object. B. Elements of the list array are objects. C. The type of information stored by list is unknown. D, The number of array elements is not specified. E. All of the above x.Which statement about interfaces is truc? 1. An interface contains only public abstract methods and public static final fields. Il, Ifa class implements an interface and then fails to implement any methods in that interface, then the class must be declared abstract. Ul. While a class may implement just one interface, it may extend more than one class A. Tonly B.Iand Il only C.land Itt only D. Hand Uf only E. 1, and It nding item in Column B. Question Two in Column A with its correspot (1 Mark Each) Match the item ss ‘Column B. ‘objects | As he eons: Float identify discard that are no longer needed by @ B. abs! 2 aula ii The aod ato te E D. final class feet that of the | E. Inheritance ee of abstract class Integer, Character is iii. Examples of wre classes |], ‘Object iv. It is always when | 5 Garbage collection ee K. Composition ; L. Overriding = anyone __from|M. Enhanced for loop fi ating els directly. e ArrayList class i cane O. Exror i, The mechanism allows prin() |P. Specialization pipe) ae Q. Instance of class invoke a method R. RuntimeException S. Late bi ix, Sheet ae of m7 nding x esi memb U. Collestion setter methods. getter and] y oy of abstract methods Question Three eas 2 Marks) is declared static? (a) Why main a add{index, values) affect the performance of () Briefly Sie (2 Marks) Java collection? following program and use it to answer o Sane fallow: Sees indicate where missing : re Raetea nia. (2 Marks Each) MyMain { ents void sein leering { Jargs){ ‘2)print the greeting isieasccuct @ MyClass object called amyObject 4/14) updatet) } } class MyCiass( “4 (1) definition of MyClass, constructor Public static void greetings (j{ “7 definition of greets public void update (int num, String title) { “7 definition of update } Public void print) / // definitien ef pring } Private int nuzds. j Private String r, L Write a sta ‘i 5 i myOhjectat ine wil construct MyClass object called i Write a statement that Items; €portTicle; a @ Marks) clase myclaryl tia myMethod ( ) throws RunTimeExcept ion { system.out.printin ("CP 215); ’ class myclass2 extends myClass ( void myMethod ( ) throws Exception { System.out.printin(“In Child class”); | public class ClassTest{ public static void main (String args [ }){ myClass S = new myClass2 ( ); S.myMethod( )¢ vy SECTION B: (60 MARKS) Attempt THREE (3) out of FOUR (4) questions provided Question Five Fi ‘igure ! shows a schematic representation of Payable interface, Invoice, Employee and SalariedEmployee classes. Use the following information to write an application to determine payments for employees and invoices. You are required to create interface Payable, which contains method getPaymentAmount that renums a double amount that must be paid for an object of any class that implements the interface. Method getPaymentAmount is a general- purpose method to be implemented by classes that implements interface Payable, After declaring Payable interface, define class Invoice and Employee, such that they implement interface Payable. Classes Invoice and Employee both represent things for which the company must be able to calculate a payment amount. Both classes implement the interface Payable, so that a program can invoke method getPaymentAmount on Invoice objects and Employee objects. Class Invoice represents a simple invoice that contains billing information for only one kind of part. The class declares private instance variables partNumber, partDescription, quantity and pricePerltem. Class Invoice also contains a constructor, get methods and a toString method that retums a String representation of an Invoice object. The class Employee contains employee's firstName, lastName, and employeelD. It also contains a constructor for initialization of instance variable and a toString method for returning, its Sing representation, Salaried employee is paid a fixed weekly salary Tegardless of the number of hours worked. Therefore, SalariedEmployee Glass should contain a unique variable called salary, a constructor, a JString method for returning te siting Fepretentation nehite nia, method to test your application. (20 Marts ‘Question Six (a) Java collection framework provides wrapper elasees that ens programmer to store primitive data type values ay objec 1. appropriate wrapper class, write a program that reads 4 tit of prot. from the keyboard in the range 0 to 100. The program shout prongs ; the user with “Please enter a grade between 0 ta (00 oF +1 tw quit rs each time before reading the next grade. Store each grade in a c oi corresponding Arrayl.ist (A, B, C, D or F|) ax follows: 9010 100 80 to 89 = B, 70 0 79 = C, 60 to 69 = D, and Oto 59~ F. four program should output the total number of grades entered on ay list. For example, if the inputs is. 38 86 92 $3.83 4240.1 separate line then the output will be; Total number of grades Number of A = 2 Number of B = 2 Number of C ~ 0 Number of Number of F = 3. Note that the grade may include devunal and include appropriate packages. =) you have two lists:-, List (2, 4, 8, 9, 11, 15.17.28 41. [4, 7, 11, 17, 19, 20, 23, 28, 37, 59, S11 Wite sorted array list two pat contains netuse * method int yerers and returns # new list th i in poth lists- G Marks) ts 1wO fist that CO GB Marks) Iterator 4 that acce| sorted array jist of that are foun ntains all aly ¢! it. anche # method union that accel integer parameters and returns 9 new found in poth lists. ay. els main ication. Use eee foe G Marks) Question Seven « swdent and Address. Every student an student and address is @ res, therfore the rela it in the reverse, the ew. But if you consider contain @ student. 5 E implement this relationshij write three java Pe aseckt> ie ps Note that Address may : iba city and country while student class contain street name and id. The third class may have stud esti ee cif ahaa mein meth = My se you have the following relati ie fySQL datab anata —_ Emp(eid int, named CompanyData. schema on your localhost We ‘tt, ename: ‘ lorks(eid: int, did: varchar(45) DOB: , vet iis haere time: i @ program 5 varchar(3 Salary: float, ) and add a” Sex column lish IDB float, man to the Em C conn ecti agerid: in| ip! ion to the 2 t) loyee table 13 and ensure that the values

You might also like