POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE COURSE NAME: OBJECT ORIENTED PROGRAMMING COURSE CODE: OOP521S NQF LEVEL: 6 DATE: NOVEMBER 2015 DURATION: 2 HOURS MARKS: 100 PAPER: THEORY 1 ST OPPORTUNITY EXAMINATION QUESTION PAPER EXAMINER: MODERATOR: MR. MARSHAL GOREJENA DR. SHAWULU H. NGGADA INSTRUCTIONS 1. Write all your answers in the answer booklet provided. 2. This paper consists of three sections; Section A, B and C. 3. Answer ALL questions in all sections. 4. Begin each section on a new page. 5. Marks/scores per question are given in [ ]. 6. Do not use or bring into the examination venue books, programmable calculators, mobile devices and other material that may provide you with unfair advantage. Should you be in possession of one right now, draw the attention of the examination officer or invigilator. 7. Polytechnic of Namibia s examination rules and regulations apply. This paper consists of 13 pages including this cover
SECTION A - Multiple Choice, each question carries 1 mark [50 marks] 1. Which one of the following is true about primitive data types? A. They are under developed B. They lack standard C. They are inbuilt into the programming language D. All of the above 2. What is the acronym for OOP? A. Object Oriented Programming B. Object Orientation programming C. Object Oriented Program D. Object Oriented Procedure 3. is the feature of OOP that implies that the non-essential details of an object are hidden from the user and an access is provided to its essential details. A. Inheritance B. Abstraction C. Polymorphism D. Encapsulation 4. What is the type of inheritance in which a subclass is derived from more than one super class known as? A. Single Inheritance B. Multiple Inheritance C. Multilevel Inheritance D. Hybrid Inheritance 5. Predict the output of the expression, 16 % 3? A. 5 B. 0 C. 1 D. 4 6. Consider the statements: Statement A: The name of a variable can begin with a digit. Statement B: The name of a variable can contain white spaces. Identify the correct option. A. Statement A is true and statement B is false. B. Statement A is false and statement B is true. C. Both, statements, A and B, are true. D. Both, statements, A and B, are false. 2
7. variables are the local variables that are accessed by the function in which the variables are declared. A. Static B. Automatic C. Instance D. Class. 8. literals are enclosed in single quotes. A. String B. Character C. Boolean D. Integer 9. Which access specifier is used with the data member of a class that is accessible at the class level only in which it is defined? A. public B. private C. protected D. default 10. The keyword is used to define class variables and methods that belong to a class and not to any particular instance of the class. A. static B. native C. synchronized D. abstract 11. Consider the statements: Statement A: A final method cannot be overridden. Statement B: A final class cannot be inherited. Identify the correct option. A. Statement A is true and statement B is false. B. Statement A is false and statement B is true. C. Both, statements, A and B, are true. D. Both, statements, A and B, are false. 12. When do you get an error message as : Exception in thread "main" Java.lang.NoSuchMethodError:main A. When the class has been incorrectly declared in a program. B. When you try to execute a program that does not have a main() method. C. When you have given incorrect arguments to the main() method. D. When you save the file with a different name as the name of the class in which the code is written 3
13. Consider the statements: Statement A: An abstract class can be instantiated. Statement B: An abstract class cannot be inherited. Identify the correct option. A. Statement A true and statement B false. B. Statement A false and statement B true. C. Both, statements, A and B, true. D. Both, statements, A and B, false. 14. Which keyword is used to define the constants in an interface? A. super B. public C. private D. final 15. Which concept of object-oriented programming is used in method overriding? A. Polymorphism B. Abstraction C. Encapsulation D. Inheritance 16. What is the access specifier of the methods declared in an interface? A. default B. private C. protected D. public 17. The Throwable class is the sub class of class. A. Exception B. Error C. Object D. RunTimeException 18. Which exception occurs when you try to create an object of an abstract class or interface? A. ClassNotFoundException B. IllegalAccessException C. InstantiationException D. NoSuchMethodException 4
19. What is the output of the following code segment? t = 10; if(t > 7) { System.out.print("AAA"); System.out.print("BBB"); } A. AAA B. BBB C. AAABBB D. nothing 20. Assuming a variable f has been initialized to 5, which of the following statements sets g to 0? A. if(f > 6 f == 5) g = 0; B. if(f < 3 f > 4) g = 0; C. if(f >= 0 f < 2) g = 0; D. All of the above statements set g to 0. 21. The switch argument within a switch structure requires a(n). A. integer value B. character value C. double value D. integer or character value 22. Assuming a variable w has been assigned the value 15, what does the following statement do? w == 15? x = 2 : x = 0; A. assigns 15 to w B. assigns 2 to x C. assigns 0 to x D. nothing 23. Assuming a variable y has been assigned the value 6, the value of!(y < 7) is. A. 6 B. 7 C. true D. false 24. A structure that allows repeated execution of a block of statements is a. A. cycle B. loop C. ring D. band 25. What is the output of the following code? b = 1; while (b < 4) 5
System.out.print(b + " "); A. 1 B. 1 2 3 C. 1 2 3 4 D. 1 1 1 1 1 1... 26. If total = 100 and amt = 200, then after the statement total += amt,. A. total is equal to 200 B. total is equal to 300 C. amt is equal to 100 D. amt is equal to 300 27. If m = 9, then after n = m++, the value of n is. A. 8 B. 9 C. 10 D. 11 28. If j = 5 and k = 6, then the value of j++ == k is. A. 5 B. 6 C. true D. false 29. What does the following statement print? for(b = 1; b > 3; ++b) System.out.print(b + " "); A. 1 1 1 B. 1 2 3 C. 1 2 3 4 D. nothing 30. The loop that performs its conditional check at the bottom of the loop is a loop. A. while B. do...while C. for D. for...while 6
31. What does the following program segment print? for(m = 0; m < 4; ++m); for(n = 0; n < 2; ++n); System.out.print(m + " " + n + " "); A. 0 0 0 1 1 0 1 1 2 0 2 1 3 0 3 1 B. 0 1 0 2 1 1 1 2 2 1 2 2 C. 4 2 D. 3 1 32. If you declare two String objects as: String word1 = new String("happy"); String word2 = new String("happy"); the value of word1.equals(word2) is. A. true B. false C. illegal D. unknown 33. The method that determines whether two String objects are equivalent, regardless of case, is. A. equalsnocase() B. touppercase() C. equalsignorecase() D. equals() 34. The method parseint() converts a(n). A. integer to a String B. integer to a Double C. Double to a String D. String to an integer 35. When you declare an array,. A. you always reserve memory for it in the same statement B. you might reserve memory for it in the same statement C. you cannot reserve memory for it in the same statement D. the ability to reserve memory for it in the same statement depends on the type of the array. 36. You reserve memory locations for an array when you. A. declare the array name B. use the keyword new C. use the keyword mem D. explicitly store values within the array elements 7
37. If you declare an integer array as follows, what is contained in the element num[2]? int[] num = {101, 202, 303, 404, 505, 606}; A. 101 B. 202 C. 303 D. impossible to tell 38. Assume an array is declared as follows. Which of the following statements correctly assigns the value 100 to each of the four array elements? int[] num = new int[4]; A. for(x = 0; x < 3; ++x) num[x] = 100; B. for(x = 0; x < 4; ++x) num[x] = 100; C. for(x = 1; x < 4; ++x) num[x] = 100; D. for(x = 1; x < 5; ++x) num[x] = 100; 39. If a class named Student contains a method setid() that takes an int argument and you write an application in which you create an array of 20 Student objects named scholar, which of the following statements correctly assigns an ID number to the first Student scholar? A. Student[0].setID(1234); B. scholar[0].setid(1234); C. Student.setID[0](1234); D. scholar.setid[0](1234); 40. When you pass an array element to a method, the method receives. A. a copy of the array B. the address of the array C. a copy of the value in the element D. the address of the element 41. In Java, methods must include all of the following except. A. a declaration B. a call to another method C. curly braces D. a body 42. A public static method named computesum() is located in classa. To call the method from within classb, use the statement. A. computesum(classb); B. classb(computesum()); C. classa.computesum(); D. You cannot call computesum() from within classb. 8
43. Which of the following method declarations is correct for a static method named displayfacts() if the method receives an int argument? A. public static int displayfacts() B. public void displayfacts(int data) C. public static void displayfacts(int data) D. Two of these are correct. 44. Which of the following is a correct call to a method declared as public static double amethod(char code)? A. double amethod(); B. double amethod( V ); C. amethod(char M ); D. amethod( Q ); 45. The nonstatic data components of a class often are referred to as the of that class. A. access types B. instance variables C. methods D. objects 46. You send messages or information to an object through its. A. fields B. methods C. classes D. type 47. Most class data fields are. A. private B. public C. static D. final 48. If a class is named Student, the class constructor name is. A. any legal Java identifier B. any legal Java identifier that begins with S C. StudentConstructor D. Student 49. If you declare a variable as an instance variable within a class, and you declare and use the same variable name within a method of the class, then within the method,. A. the variable used inside the method takes precedence B. the class instance variable takes precedence C. the two variables refer to a single memory address D. an error will occur 50. Variables that are shared by every instantiation of a class are. 9
A. class variables B. private variables C. public variables D. illegal 10
SECTION B (True or False) [10 marks] 1. Method signature includes the return type. { True or False } (1 mark) 2. You can have more than one finally block for an exception-handler. {True or False} (1 mark) 3. Collections are an interface. {True or False} (1 mark) 4. An unchecked exception cannot be handled. {True or False} (1 mark) 5. A method that is declared final in a superclass cannot be overridden in a subclass. {True or False} (1 mark) 6. A constructor does not necessarily need to have the same name as the class. {True or False} (1 mark) 7. An individual primitive-type array element that is passed to a method and modified in that method will contain the modified value when the called method completes execution. {True or False} (1 mark) 8. Abstraction is a form of software reusability in which classes acquire the members of existing classes and embellish those classes with new capabilities. {True or False} (1 mark) 9. Abstract class can be instantiated under special conditions. {True or False} (1 mark) 10. Each class declared as public does not need to be stored in a file that has exactly the same name as the class. {True or False}(1 mark) 11
SECTION C (Structured Questions) [40 marks] 1. Objects have state, behaviour and identity. Using examples explain each of the three characteristics of an object. [3 marks] 2. Discuss the main differences between abstraction and encapsulation in Object Oriented Programming [7 marks] 3. Using appropriate examples compare and contrast method overloading and method overriding. [10 marks] 4. i) How does the java language provide error handling capabilities for its programs [4 marks] ii) Explain the three advantages to the way in which Java deals with exceptions [6 marks] 12
5. Given the following java source code, identify the OOP concepts manifested in the source code and briefly explain them highlighting their importance. What is the output of the code? [10 marks] /* * To change this template, choose Tools Templates * and open the template in the editor. */ package test1; /** * * @author Mugabe */ public class Test1 { /** * @param args the command line arguments */ public static void main(string[] args) { B b=new B(); b.callme(); b.callmetoo(); // TODO code application logic here } } abstract class A { private int a; int b; abstract void callme(); void callmetoo(){ System.out.println("This is a concrete method."); } } } public class B extends A { int c; void callme(){ System.out.println("Bs implimentation of callme.");} -------------------------------------End--------------------------------------------------- 13