You are on page 1of 8

THE BCS PROFESSIONAL EXAMINATION BCS Level 5 Diploma in IT April 2009 EXAMINERS' REPORT Object Oriented Programming Section A

Question A1 a) b) c) d) e) Distinguish between method overloading and method overriding. (5 marks) Distinguish between accessor and mutator methods. (5 marks) Distinguish between abstract and concrete classes. (5 marks) Distinguish between constructor and destructor methods. (5 marks) Distinguish between class and instance variables. (5 marks) Answer Pointers (a) Method overloading is the technique of having multiple occurrences of a method with a common name, but with different arguments. A specific version of the method is selected such that it matches the arguments used at invocation. Method overriding is the technique of creating a new implementation of a super-class method in a sub-class. The sub-class method must share the same argument list as the super-class method for overriding to occur, otherwise the sub-class method will simply be added to the set of methods available. Both are examples of polymorphism. (b) Accessor methods are used to retrieve data member values from within an object, commonly known as getters, and mutator methods are used to change the value of data member, and are commonly known as setters. Mutator methods therefore accept arguments (reflecting the data member that they are destined to update), whereas accessor methods usually possess a return type reflecting the data member that they are to convey back to the caller. Both are typically designated as public to facilitate their invocation through an object: accessors to report information pertaining to the objects state, and mutators to modify the objects state. (c) Abstract classes are not used to create objects but to specify data and behaviours that must be present in sub-classes created in an inheritance hierarchy. This is sometimes referred to as inheritance for specification. Concrete classes, unlike abstract classes, are instantiable i.e., can be used to create objects. They are ostensibly complete, unlike abstract classes which typically contain method stubs (known as abstract methods, or pure virtual functions) whose implementation is, as yet, undefined. (d) Constructor methods are called when an object is created, and destructor method are called when an object goes out of scope. Destructors may not exist in some languages with garbage collection (e.g. Java). Constructors undertake initialisation duties such as setting data to default values, preparing files, sockets, or GUI components. Destructors are used to end the existence of an object gracefully, closing open files, sockets, or GUI components, and, in some languages, are used to deallocate dynamically allocated memory to prevent memory leakage.

(e) Class variables may be considered to be shared by all instances of a class, or, rather, to belong to the class rather than to any particular instance of that class. For this reason, irrespective of how many objects of a class are created, each class variable exists only once. Class variables are implemented by means of the keyword static in Java. Instance variables belong to specific instances of a class, and thus each instance possesses its own value for each of the instance variables it contains that are not related to the values possessed by any other instances of that class. Examiners Comments This question was attempted by the vast majority of candidates (94%), and was answered relatively successfully with 72% achieving 40% of the available marks. This question tested candidates knowledge of basic theory by contrasting common terms in object oriented programming. A relatively high number of candidates confused the terms listed (e.g., overloading and overriding), but did not lose all available marks if their descriptions were otherwise accurate. Of the questions set, the least convincingly answered was the question requiring class and instance variables to be distinguished. Many students lost marks by providing answers that were excessively convoluted or contradictory, too brief, used examples to answer the question without sufficient accompanying text, or presented examples that were seriously syntactically flawed, such that it was apparent that the candidate did not truly understand how the concepts are used in practice. Answers with minor syntactical errors were not unduly penalised. Question A2 a) Briefly outline the impact of using access specifiers to designate the members of a class as private, protected or public. (10 marks) Distinguish between single and multiple inheritance using suitable diagrammatic examples to illustrate your answer. (5 marks) What is meant by the term signature in describing a method? (5 marks) d) What is the purpose of defining a method as abstract? (5 marks) Answer Pointers (a) The designation of class members using the access specifiers private, protected or public has an impact both for objects created of a class, and also in inheritance. Within a single class, variables designated as private or protected can only be accessed via methods within that class. Public variables, on the other hand, can also be accessed directly through an object created from that class. Also within a single class, methods designated as private or protected can only be accessed by other methods within that class. Methods designated as public can be accessed through an object created from that class, and constitute the class interface (5). In standard (public) inheritance, private variables and methods are not inherited into a sub-class. Variables and methods designated as protected are inherited into a sub-class, and continue to behave as protected members within that class (as outlined above). Variables and methods designated as public continue to possess a public designation in the sub-class, and can therefore continue to be accessed through an object of that subclass, as could be done through the super-class, in addition to being available to methods within the class (5). The class access specifiers enable the internal workings of a class to be separated from its interface, thereby facilitating information hiding (i.e., keeping unnecessary complication hidden). (b) Single inheritance allows a generic class to be extended to create a more specialized sub-class. An inheritance hierarchy generated through multiple single level inheritance steps can be possible: Publication Book Textbook Chemistry Textbook

b)

c)

Multiple inheritance, which is unsupported in some object oriented languages since it is potentially ambiguous, enables inheritance from more than one super-class at the same time. An example is shown below: Clock Radio

ClockRadio

(c) The signature of a method usually comprises its return type, name, and the number and type of its arguments (however, in Java, the return type is not considered to be part of the method signature). It is essentially a prototype of a method, enabling the programmer to easily identify the purpose of a method (via its name), the data it requires to operate (via its argument list), and any end result that is produced (its return type), such as the outcome of a calculation or simply a status value that specifies whether the method was successful or unsuccessful. (d) Defining a method as abstract, which is possible only within an abstract class, enables us to defer implementation to a later stage in the inheritance hierarchy. This is used when we know that a particular method must be made provided, but it is too early to prescribe exactly how it will be accomplished. For example, an abstract class animal may contain an abstract method reproduce, since this is a basic requirement of all organisms classed as animal, before we know whether the specific animal sub-class we create will require a sexual or asexual implementation. Examiners Comments Like question 1, this question was attempted by most candidates (94%), and achieved only a slightly lower success rate (70% scoring 40%). Question (a) was answered relatively well, but some candidates concentrated only on one aspect of visibility for example, its impact on the accessibility of members through an object, or the inheritability of members. For full marks it was necessary to address both issues. In question (b), several candidates incorrectly confused multiple inheritance with performing a series of single inheritances rather than a concurrent inheritance from >1 base class. Most candidates were able to specify the meaning of the term signature in question (c) but several did not provide a complete list of all parts that form a function/method signature. A sizeable number of candidates were unable to correctly convey the purpose of an abstract method in question (d). Question A3 a) Specify the symbols used in UML to represent the following inter-class relationships: i. aggregation, ii. association, iii. composition, iv. dependency, v. specialization. (5 marks) Specify the meaning of the symbols #, +, and -? How do we distinguish between attributes and operations? (5 marks) Provide a class diagram for an online bookshop comprising at least 5 classes, including a range of instance variables. (15 marks)

b)

c)

Answer Pointers (a) i. ii. iii. iv. v. solid arrow-headed line emanating from an empty diamond solid arrow-headed line solid arrow-headed line emanating from a filled diamond dotted arrow-headed line line emanating from an empty triangle

(b) The symbol specifies that a data member or method is private, + that it is public, and # that it is protected. We can distinguish between attributes and operations by the existence of round brackets () and, possibly, after a methods name, arguments and/or a return type. These items do not exist for data members. (c)

Examiners Comments Relative to questions 1 and 2, question 3 was both attempted by fewer candidates (62%) and successfully answered by a smaller proportion of those that attempted it (65%). In part (a), most candidates were able to correctly provide the notation used to represent one or more of the inter-class relationships listed, but a considerable number expended a great deal of time providing code illustrations and lengthy written descriptions. Candidates should be mindful to answer only the question asked, and pay attention to the marking scheme i.e., only 5 marks were available, a clear indication that detailed explanations were not required. In part (b), candidates were relatively successful at designating #, + and -, but fewer were able to describe how we distinguish between variables and methods. In part (c), a number of mistakes reduced the marks awarded. Some candidates failed to draw a class diagram, instead providing use-cases or other UML diagram type for which no marks were awarded. Several other candidates did answer the question fully, providing fewer classes than requested, or a larger number of classes but without instance variables. Several candidates provided very few instance variables, but provided a range of methods. Since the question did not request methods, no marks were awarded for these. Some candidates provided diagrams that did not illustrate how the classes were related to one another, or used arbitrary relationships.

Section B
Question B4 a) You are working in a company which uses object-oriented programming techniques and UML as a design tool. The company is about to undertake a project which is safety critical. Your manager had heard that the use of the Object Constraint Language (OCL) could be beneficial in this particular project. Write a short report which explains what OCL is and how it can contribute to the construction of high quality software. (10 marks) In the context of OCL explain the following terms: i) pre-condition; ii) post-condition; iii) invariant. (9 marks)

b)

c)

Given the following UML class diagram:

BusJourney -journeyNr: Integer 0..* 0..* +journeys

+journeys 0..*

+bus 1

Bus +numberOfSeats: Integer

+passengers

Person -name: String

Explain the meaning of the following OCL fragment: context BusJourney inv: passengers->size() <= bus.numberOfSeats (6 marks) Answer Pointers a) UML class diagrams capture a limited number of the real world constraints in the domain to be modelled. For example, they record the fact that an attribute has a given type (which is a constraint on the values the attribute may hold) and they are able to express limited constraints on the cardinality of associations OCL offers the designer a mechanism whereby many other additional real world constraints can be specified. Moreover these constraints can be expressed in an unambiguous mathematical manner. Since mathematical expressions are used within OCL it is possible to reason about these constraints and prove properties of the design. b) A precondition to an operation is a restriction that must be true at the moment that the operation is going to be executed. The obligations are specified by postconditions. A postcondition to an operation is a restriction that must be true at the moment that the operation has just ended its execution. An invariant is a constraint that states a condition that must always be met by all instances of the class, type, or interface. An invariant is described using an expression that evaluates to true if the invariant is met. Invariants must be true all the time. c) This UML diagram indicates that a bus may undertake zero or many bus journeys. A bus journey always involves one bus. A passenger may participate in zero or many bus journeys. A bus journey may involve zero or many passengers. The OCL indicates that the number of passengers on a bus journey cannot exceed the number of seats on the bus.

Examiners Comments Object Constraint language has been listed in the syllabus since its last major revision. Despite this, it is apparent that many candidates have not studied this topic. Only a few candidates chose to answer this question and very few of these offered satisfactory answers. As this area of the syllabus has not been explored by previous examinations, the question was not very demanding. The few candidates who had covered OCL in their preparation were able to gain high marks. Those candidates who had not studied OCL but nevertheless still attempted the question did not score highly.

Question B5 a) Explain what is meant by the techniques black-box testing and white-box testing and how they may be used when testing object-oriented software. (10 marks) Explain the purpose of integration testing and how it is used to assure the quality of object oriented systems. Why is the integration testing of an object-oriented system likely to be more complex than that for a system developed using a top-down decomposition approach? (10 marks) Discuss whether, when testing a subclass, it is safe to assume that all the methods inherited from a previously tested superclass will function correctly. (5 marks)

b)

c)

Answer Pointers a) Black-box testing is testing generated from a specification. The specification is used to identify ranges of inputs which will generate certain values. These are called equivalence partitions. The tester identifies boundaries between equivalence partitions and generates test values either side of the boundaries. White-box testing is undertaken with respect to a knowledge of the internals of the code. It can be shown that black-box testing often fails to exercise all the code in a routine. In a white-box test branches within the code are examined so as to generate test data which will force all paths in the code to be traversed. In O-O black box and white box testing can be applied to classes to test that the class meets its specification and that all branches within methods are exercised. b) Assuming pieces of code have been tested in isolation from each other (using stubs and skeleton code), integration testing checks that when a system is assembled from tested fragments that it works correctly. In hierarchically constructed systems control normally flows up and down the hierarchy whereas in an object-oriented system control flow is more complex. This makes O-O integration testing more difficult. c) When inheritance is exploited the there will be a certain amount of code reuse. There may therefore be no need to re-test this previously tested code, however, it is important to understand that the code will only be correct if it is used in the same context as it was originally designed for. Therefore a careful tester will apply the same tests to a subclass as to a superclass to ensure that the context the code is used in has not altered.

Examiners Comments Most candidates who attempted this question had memorised the definitions of white and black box testing (though some were confused as to which term applied to which definition). Even so the answer were cursory and did not demonstrate an understanding of how these techniques are used in practice. Some candidates had also memorised the definition of integration testing, however a few candidates were unfamiliar with this term. Only a minority of candidates were able to present arguments supporting the assertion that integration testing is potentially more difficult in object oriented systems. In marking part c), credit was given for arguments which supported the assertion that inherited methods did not require testing and for counter arguments in favour of testing. For full credit candidates needed to express both sides of the discussion. Most candidates chose to support only view and hence did not attract full marks.

Question B6 a) Define the following terms: i) ii) iii) iv) b) object oriented languages and typed languages; structured programming and procedural programming; abstract data types and encapsulation; coupling and cohesion. (16 marks) Discuss the way in which features of object-oriented languages support the enforcement of good programming practice. (9 marks)

a) An object oriented language allows the programmer to manipulate object oriented concepts such as objects and classes. A typed language checks that the type of values corresponds with the types of the variables that hold them. Structured programming is a style of programming in which the programmer divides his program's source code into logically structured chunks of code. In procedural programming the program is divided into modules like functions or subroutines or procedures or subprograms. An abstract data type is a specification of a set of data and the set of operations that can be performed on the data. Encapsulation is the process of combining elements to create a new entity. For example, a procedure is a type of encapsulation because it combines a series of computer instructions. Likewise, a complex data type, such as a record or class, relies on encapsulation. Coupling is the degree to which each program module relies on each one of the other modules. Cohesion is a measure of how strongly-related and focused the various responsibilities of a software module are. b) Object-oriented programming effectively encourages those aspects of programming which have been identified as good practice. For example, by grouping data and operations it accrues the advantages of abstract data types and encourages high cohesion. It discourages high coupling by providing mechanisms by which the internal aspects of one part of a program can be hidden from other parts of the program. The mechanisms to support low coupling and high cohesion also encourage software reuse.

Examiners Comments In part a) many candidates gave definitions which were similar to those found on Wikipedia. Whilst Wikipedia is often a good place to begin reading about a topic, its definitions may not be as precise as those expected as answers in a professional examination. Preparation for this examination should involve reading some of the leading textbooks in the field. From April to July 2009 the Wikipedia entry for encapsulation was labelled as possibly unreliable by a moderator yet this was the definition quoted by many candidates. This aside, most answers to this part of the question attracted high marks. Part b) is a question which has been asked in a number of guises in previous papers. Although it was a question that candidates could have prepared for, it was clear than few did. The question seeks to ascertain that candidates not only know about the features of object-oriented languages but also know why those features have been created. The majority of answers to this part were quite poor.

You might also like