Tutorial 8 Date: 15/04/2014 1. What is wrong with the following interface? public interface SomethingIsWrong void amethod(int avalue) System.out.println("Hi Mom"); 2. Fix the interface in Question 2. 3. Is the following interface valid? public interface Marker 4. Assess the following Java program codes to discuss how principles of object oriented programming are applied in these codes. a) Class test int x,y; public void Add() System.out.println( x+y +(x+y)); public void Add(int a, int b) System.out.println((x+a)+, +(y+b)); Public void Add(double a, double b) System.out.println((x+a)+, +(y+b));
b) class person String name, address; public void display () public void enterdata() Class student extends person void enterdata() void DisplayStd() c) What will be the output of the program? try int x = 0; int y = 5 / x; catch (Exception e) System.out.println("Exception"); catch (ArithmeticException ae) System.out.println(" Arithmetic Exception"); System.out.println("finished"); A. finished B. Exception C. Compilation fails. D. Arithmetic Exception d 6. What will be the output of the program?
public class X public static void main(string [] args) try badmethod(); System.out.print("A"); catch (Exception ex) System.out.print("B"); finally System.out.print("C"); System.out.print("D"); public static void badmethod() throw new Error(); /* Line 22 */ A. ABCD B. Compilation fails. C. C is printed before exiting with an error message. D. BC is printed before exiting with an error message. 7. what will be the output of the program? public class X
public static void main(string [] args) try badmethod(); System.out.print("A"); catch (RuntimeException ex) /* Line 10 */ System.out.print("B"); catch (Exception ex1) System.out.print("C"); finally System.out.print("D"); System.out.print("E"); public static void badmethod() throw new RuntimeException(); A. BD B. BCD C. BDE D. BCDE Answer 1: It has a method implementation in it. Only default and static methods have implementations.
Answer 2: public interface SomethingIsWrong void amethod(int avalue); Alternatively, you can define amethod as a default method: public interface SomethingIsWrong default void amethod(int avalue) System.out.println("Hi Mom"); Answer 3: Yes. Methods are not required. Empty interfaces can be used as types and to mark classes without requiring any particular method implementations. For an example of a useful empty interface. Answer 4: a) Encapsulation: it uses class that encapsulates data and methods together. Polymorphism: it uses method overloading is implemented in this code. Where Class test contains three methods with the same name Add with different parameters. b) Encapsulation: it uses class that encapsulates data and methods together. The inheritance principle: Class person is superclass of class student. Subclass student can access variables of its superclass. Polymorphism: it uses method overriding where Class student have enterdata() method with the same signature in super class person.
Answer 5: Option C Explanation: Compilation fails because ArithmeticException has already been caught.arithmeticexception is a subclass of java.lang.exception, by time thearithmeticexception has been specified it has already been caught by theexception class. If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses). Answer 6: Option C Explanation: Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception and Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.error). Answer 7: Option C Explanation: A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.