Term XXXXXXXXXXExamQuestion PaperData Structures and Algorithms – COIT20256TOTAL 50 MARKS Attempt all 10 questions in the Microsoft Word template provided.All answers must be...

1 answer below »
Please provide answers in word document against each Questions and answers.


Term 2 2020 Exam Question Paper Data Structures and Algorithms – COIT20256 TOTAL 50 MARKS Attempt all 10 questions in the Microsoft Word template provided. All answers must be written in the answer template provided, not in this question paper. Question 15 Marks This question relates to the following class public class QuestionX { private static QuestionX quest = new QuestionX(); private QuestionX(){ } public static QuestionX getQuestion() { return quest; } public void doSomething(){ System.out.println("In doSomething"); } //other methods for this class to do things } a) How is this class used by a client? Provide code showing how you would use this class in the main method of a program.(2 marks) b) What constraints are placed on class usage by the above implementation? Explain how this has been achieved. (3 marks) Question 2 4 Marks With the use of examples: a) Discuss the two major benefits of using an interface. (2 marks) b) When would you use an abstract class in preference to an interface? (2 marks) Question 3 2 Marks Often when a method encounters an error it returns a value that indicates that it has been unsuccessful. This allows the invoking code to test that return value and deal with the problem in some way before progressing. However, that is not an option when you use a constructor. Suppose you want to notify clients that create an instance of a class about any errors that occur in the construction process. Discuss two ways in which this can be achieved. Question 4 6 Marks a) What is the best case performance for a hashtable lookup? You must explain your answer very clearly. In your answer explain how hashtables work and how that relates to performance. (3 marks) b) What is the worst case performance for a hashtable lookup. You must explain your answer and again relate it to how hashtables work. (3 marks) Question 55 Marks The following Stack class uses the generic List class developed in chapter 21 of the text book and in the week 12 slides (Fig 21.3 ). public class Stack { private final List< e=""> stackList;//see Fig 21.3 for List public Stack(){ stackList = new List<>("stack"); } public void push(E object) { stackList.insertAtFront(object); } public E pop(){ return stackList.removeFromFront(); } public boolean isEmpty(){ return stackList.isEmpty(); } } public class GenericStack { public static void main(String[] args) { Stack stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(15); while (!stack.isEmpty()){ int item = stack.pop(); System.out.printf("%d%n", item); } } } Question 5 continued over page Question 5 continued a) What is output by this code? Explain your answer. (1 mark) b) The data pushed onto the stack is of type int, but the stack is of type Integer. What conversion happens when the int data is pushed onto the stack? (1 mark) c) Where else in the code do you see such mixing of types and what happens in that case? (1 mark) d) Suppose you want a stack of Result records (see the class diagram for Result below). Rewrite the main method to test your stack with 3 Result records. Highlight the changes you have made (in bold and yellow highlight).(2 marks) Question 6 3 Marks a) What are the advantages of a using a linked list rather than a conventional array in Java? (2 marks) b) When would it be more efficient to use an array rather than a linked list? Explain your answer.(1 mark) Question 7 6 Marks a) Given the following code, use lambdas and streams to calculate and display the number of marks greater than or equal to 50 (i.e. the total number that have passed).(1 mark) public static void main(String[] args) { int[] marks = {10, 53, 65, 49, 46, 95, 81, 45, 72, 85}; //use lambdas and streams to work out how many // marks are >= 50 and display the result. } Output should be similar to the following: The number of marks greater than or equal to 50 = 6 b) The following is the class diagram for the Result class used in this question. Given the starting point for the code below, use lambdas and streams to complete the main method as follows: i. Write a Predicate called supplementary that returns true if the mark is in the supplementary range (>=45 and <50) and="" false="" if="" the="" mark="" is="" not="" in="" this="" range.="" (1="" mark)="" ii.="" make="" use="" of="" the="" predicate="" from="" part="" (i)="" to="" display="" all="" the="" result="" records="" (student="" id="" and="" mark)="" with="" marks="" in="" the="" supplementary="" range="" (2="" marks)="" iii.="" use="" lambdas="" and="" streams="" to="" calculate="" and="" display="" the="" average="" mark.="" (2="" marks)="" question="" 7="" continued="" over="" page="" question="" 7="" continued="" in="" the="" answer="" template="" complete="" the="" code="" for="" parts="" (i)="" to="" (iii)="" where="" you="" see=""> in the code below: public class StreamQuestion { public static void main(String[] args) { Result[] results = {new Result("S123345", 10), new Result("S678901", 53), new Result("S778901", 65), new Result("S878901", 49), new Result("S078901", 81), new Result("S688901", 45), new Result("S698901", 72), new Result("S679901", 46), new Result("S678911", 72), new Result("S678912", 85)}; List resultList = Arrays.asList(results); //(i) Write the code for the supplementary predicate // (ii) using the predicate (and lambdas and streams), // display the student id and mark for all results in // the supplementary range // (iii) using lambdas and streams calculate and display // the average mark } } The output generated should be similar to the following: Supplementary List: Average Mark: 57.80 Question 8 7 Marks < question="" 8="" was="" about="" jdbc="" –="" this="" has="" been="" removed="" from="" the="" unit="" and="" replaced="" with="" graphs.="" see="" the="" week="" 12="" tutorial="" questions="" for="" the="" types="" of="" questions="" you="" could="" be="" asked="" about="" graphs="">> Question 910 Marks The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) { if (index == (a.length - 1)) System.out.printf("%d%n", a[index]); else { reverse(a, index); a) What does it do? Explain why it behaves in this way. (2 marks) b) There is more than one error in the code. Correct the code so that it will recursively print out the elements of the array in reverse order. Highlight your changes clearly (in bold with yellow highlight). (2 marks) c) Complete the following main method to show how you would invoke your corrected reverse method to test that it now works correctly. (1
Answered 9 days AfterOct 08, 2022

Answer To: Term XXXXXXXXXXExamQuestion PaperData Structures and Algorithms – COIT20256TOTAL 50...

Sairama answered on Oct 17 2022
48 Votes
1.
a.
     public static void main(String[] args) {
QuestionX q = QuestionX.getQuestion(); q.doSomething(); }
    b. It is a Singleton pattern. The constructor has been made private. Hence the object instantiation can onl
y be accessed from only inside the class and not from outside the class. Also the constructor has been made a static and private one.This way only a single instance of the class exists in the program. Also there exists a private object instance of the class. This single instance can be accessed by the static public method of the class. In our example, it's the getQuestion function, through which we can access the instance.
2.
a.
i. It is used to achieve total abstraction.    
ii. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances
interface Polygon{
void getArea(int length, int breadth);
}
// implement the Polygon interface
class Rectangle implements Polygon {
// implementation of abstract method
public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}
Multiple Inheritance Example
interface A {
// members of A
}
interface B {
// members of B
}
class C implements A, B {
// abstract members of A
// abstract members of B
}
B. An abstract class is a good choice if we are using the inheritance concept since it provides a common base class implementation to derived classes.
· An abstract class is also good if we want to declare non-public members. In an interface, all methods must be public.
· If we want to add new methods in the future, then an abstract class is a better choice. Because if we add new methods to an interface, then all of the classes that already implemented that interface will have to be changed to implement the new methods.
· If we want to create multiple versions of our component, create an abstract class. Abstract classes provide a simple and easy way to version our components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, we must create a whole new...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here