Presentation is loading. Please wait.

Presentation is loading. Please wait.

Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.

Similar presentations


Presentation on theme: "Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean."— Presentation transcript:

1 Previous Exam 1.0

2 Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean expressions A and B. False. If either expression has a side effect, you cannot swap them.

3 Question 1 - b Write a program that reads in a series of floating-point numbers and prints out the minimum, maximum and average values. Assume use of the ConsoleReader class.

4 Question 1 – b public class MinMaxAve { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); double low = 0; double high = 0; double sum = 0; int count = 0; boolean done = false;

5 Question 1 – b String input = console.readLine(); if (input == null) done = true; else { low = Double.parseDouble(input); high = low; sum = sum + low; count++; }

6 Question 1 - b while (!done) { input = console.readLine(); if (input == null) done = true; else { double next = Double.parseDouble(input); sum = sum + next; count++; if (next > high) high = next; else if(next < low) low = next; }

7 Question 1 - b System.out.println("The maximum value is: " + high); System.out.println("The minimum value is: " + low); if (count > 0) System.out.println("The average value is: " + sum / count); }

8 Question 1 - c Write a method that computes the alternating sum of all elements in an integer array. For example, if the method is invoked with the array containing : 3 1 4 2 5 9 2 6 3 then it returns 3 + 1 – 4 + 2 – 5 + 9 – 2 + 6 – 3 = 7.

9 Question 1 - c public int altSum(int[] a) { int r = 0; for (int i = 0; i < a.length ; i++ ) { if (i % 2 == 0) r = r + a[i]; else r = r - a[i]; } return r; }

10 Question 2 - a Write a simple Bank Account class that rejects negative amounts in the deposit and withdraw methods and rejects withdrawals that would result in an overdraft.

11 Question 2 - a public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { if (initialBalance >= 0) balance = initialBalance; }

12 Question 2 - a public void deposit(double amount) { if (amount >= 0) balance = balance + amount; } public void withdraw(double amount) { if (amount <= balance) balance = balance - amount; } public double getBalance() { return balance; }

13 Question 2 - b What is wrong with the following loop? Explain 2 ways of fixing the error. int[] v = new int [10]; for (int i = 1; i <= 10; i++) v[i] = i*i;

14 Question 2 - b There is a bounds error. The index of the for loop begins at 1 (thereby skipping the first element of the array) and ends with 10 (which is one past the end of the array and a fatal error). One remedy is to adjust the bounds for i: for (int i = 0; i < 10; i++) v[i] = (i + 1) * (i + 1); Another remedy is to adjust the array index: for (int i = 1; i <= 10; i++) v[i - 1] = i * i;

15 Question 2 - c For each of the following, say if it is true or false. –Array subscripts must be integers –Arrays cannot contain Strings as elements –Arrays cannot use Strings as subscripts –Parallel arrays must have equal length –Two-dimensional arrays always have the same number of rows and columns –Two parallel arrays can be replaced by one two- dimensional array –Elements of different columns in a two-dimensional array can have different types

16 Question 2 - c true (or an expression which evaluates to an integer) false, for example main(String[] args) true true (in order to be parallel) false false (the array elements might be of different types) false

17 Question 3 - a Explain the difference between class and instance methods/members. How can this be expressed in your code?

18 Question 3 - a instance method need object reference –reference.method(parameters) class methods belong to class, no need for object – ClassName.method(parameters) class methods via static

19 Question 3 - b Can class methods/members be used in instance methods? Explain why (not). Yes, since each object has access to the class from which it is instantiated.

20 Question 3 - c Can class instance methods/members be used in class methods? Explain why (not). No, a class does not have access to any of its objects.

21 Question 3 - d In software engineering, solutions to common design problems are often called design patterns. One of them is called Singleton and it describes how one can make sure that a certain class can have only one instance during a program's life-cycle. How would you implement such a Singleton class in Java?

22 Question 3 - d public class Singleton { private static Singleton instance; protected Singleton() { … } public static Singleton giveInstance() { if(instance == null) instance = new Singleton(); return instance; }

23 Question 3 - e How would you check that you can indeed have only one instance? reference1 == reference2)

24 Question 4 - a public static final double PI = 3.14; accessible from everywhere belonging to class level constant type double

25 Question 4 - a protected final void printString(char a) accessible from subclasses method cannot be overridden no return value takes char as an argument

26 Question 4 - a private BankAccount() constructor for BackAccount class default constructor only to be used inside this class

27 Question 4 - b Describe the mechanism of error/exception handling in java. exceptions – errors try – catch – finally throw

28 Question 4 - c Why would one use error handling in a constructor? What will happen with the (partial) object? to state that something went wrong during object creation object will be garbage collected

29 Question 4 - d Explain the differences between call-by- reference and call-by-value parameter passing? call-by-reference: pointer to memory is passed call-by-value: actual contents of memory is given - value

30 Question 4 - d Which systems are used in Java? objects by reference primitive types by value

31 Question 5 1. import java.util.*; 2. 3. public class Run 4. { 5. 6. Vector elements; 7. 8. public Run(int in) 9. { 10. int t = a; 11. elements = new Vector(); 12. for(int t = 0; t <in; t++) 13. { 14. if(t%2==0) 15. elements.addElement(new ElementA("a")); 16. else 17. elements.addElement(new ElementB(t)); 18. 19. } 20. }

32 21. 22. public String toString() 23. { 24.String res = "; 25.int s = elements.size(); 26.for(int i=0; i<s; i++) 27. { 28.res += elements.elementAt(i); 29.res += "\n"; 30. } 31.return res; 32. } 33. 34. public Vector giveElements() 35. { 36.return elements; 37. } 38.

33 39. public static void main(String[] argv) 40. { 41.Element a = new Element(); 42.Run r = new Run(4); 43.System.out.println(r); 44.ElementA b = new Element(5); 45.Vector el = r.giveElements(); 46.System.out.println((ElementA) el.elementAt(1)); 47. 48. } 49. 50.}

34 1.// abstract class Element 2. 3.public abstract class Element 4.{ 5. 6. 7. public Element() 8. { 9. System.out.println("Element created"); 10. } 11. 12. protected abstract String printContents(); 13. 14. 15. protected String printType() 16. { 17. return "Element " 18. } 19. 20. public String toString() 21. { 22. printType(); 23. } 24. 25.}

35 1.// public class ElementA 2. 3.public class ElementA extend Element 4.{ 5. String el; 6. 7. public ElementA() 8. { 9. System.out.println("ElementA created"); 10.el = "nothing"; 11. } 12. 13. public ElementA(String input) 14. { 15.System.out.println("ElementA created with input"); 16.el = input; 17. } 18.

36 19. /* protected constructor */ 20. protected ElementA(a) 21. { 22.System.out.println("ElementA created with int"); 23.el = Integer.toString(a); 24. } 25. 26. protected String printContents() 27. { 28.return el; 29. } 30. 31. protected String printType() 32. { 33.return "ElementA inherits from " super.printType(); 34. } 35. 36. public String toString() 37. { 38.return printType() + 39. "and contains " + printContents(); 40. 41. 42.}

37 1./ public class ElementB 2. 3. 4. 5.public class ElementB extends ElementA 6.{ 7. 8. Vector in; 9. 10. public ElementB() 11. { 12.System.out.println("ElementB created"); 13.in = new Vector(); 14. } 15. 16. 17. public ElementB(String el) 18. { 19 System.out.println("ElementB created with input"); 20. super(el); 21. 22. }

38 23. 24. public ElementB(int a) 25. { 26.super(a); 27.System.out.print("ElementB created with int"); 28.in = new Vector(); 29.final int el = 2; 30.System.out.println("and el equals to " + this.el); 31.el++; 32.for(int i=0;i<a; i++) 33. { 34.in.addElement(new Integer(i + el)); 35. } 36. } 37.

39 38. protected String printContents() 39. { 40.return el + " " + in.toString(); 41. } 42. 43. protected String printType() 44. { 45.return "ElementB inherits from " + super.printType(); 46. } 47. 48. public String toString() 49. { 50.return printType() + 51. "and contains " + printContents(); 52. } 53.

40 Question 5 - b Predict the output of this program. Element created ElementA created with input Element created ElementA created with int ElementB created with intand el equals to 1 Element created ElementA created with input Element created ElementA created with int ElementB created with intand el equals to 3 ElementA inherits from Element and contains a ElementB inherits from ElementA inherits from Element and contains 1 [2] ElementA inherits from Element and contains a ElementB inherits from ElementA inherits from Element and contains 3 [2, 3, 4]


Download ppt "Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean."

Similar presentations


Ads by Google