The Islamic University Gaza Department of Electrical & Computer Engineering Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324 Instructor: Dipl.-Ing. Abdelnasser Abdelhadi Date: 31.03.2013 / Duration: 1.5 hours Name SID Max Mark Mark 30 Question 1 Determine whether the following statements are true or false? 1. An abstract class can be extended (F) 2. A subclass of a non-abstract superclass can be abstract (T) 3. If a class contains a single abstract method, it must be abstract. (T) 4. Polymorphism means that a variable of superclass can refer to a subtype object. (T) 5. A class can implement many interfaces. (T) 6. a subclass can override a private method declared in superclass (F) 7. Variables declared in interfaces are implicitly public, static, and final. (T) 8. All methods in an Interface are always private and abstract by default. (F) 9. a method can overridden in the same class (F) 10. An interface can extend many interfaces. (T) page 1/5
Question 2 Given is the following code. public class Division { public static void main(string[] args) { Block1: for (int i = 2; i >= -3; i--) { try { double z = division(5, i); System.out.println("Result: " + z); catch(arithmeticexception ar){ System.out.println(ar.getMessage()); break; Block2: catch (Exception ex) { System.out.println(ex.getMessage()); finally{ System.out.println("i: "+i); System.out.println("Finish"); public static double division(double x, double y) throws Exception{ if(y==0) { throw new ArithmeticException("Cann`t divid number by zero!"); else if(y==1) { throw new Exception("division by 1 is forbidden."); else{ return x/y; a. What is the output? Result: 2.5 i: 2 division by 1 is forbidden. i: 1 Cann`t divid number by zero! i: 0 Finish b. How would the exchanging of Block1 and Block2 affect the program? (explain your answer) It case an compilation error, because we can t handle the specific exception (ArithmeticException) after the general exception (Exception) Since all ArithmeticException is an Exception so it will handled by block 1 and block2 become unreachable code. page 2/5
Question 3 Given is the following program: public interface Animal { int numberoflegs(); boolean ispredator(); public abstract class Mammal implements Animal{ public int id; public Mammal(int id) { this.id = id; public int numberoflegs() { return 4; return "Is preditor: " + ispredator() ; public class Sheep extends Mammal{ public Sheep(int id) { super(id); public boolean ispredator() { return false; return "This Sheep " + super.tostring(); public class Shark implements Animal{ public boolean ispredator() { return true; public int numberoflegs() { return 0; return "Shark predator: "+ispredator(); page 3/5
public class Tester { public static void main(string[] args) {; 1. display(new Sheep(1)); 2. display(new Shark()); static void display(animal m){ 3. System.out.println(m.toString()); 1. Complete the missing methods or constructor in the upper classes. Shark is predator and has 0 legs. Sheep is not predator and has 4 legs. The Shark object tostring() return Shark predator: true,legs: 0. 2. Complete the parameter list of the method display so that the invoking in line 1 and 2 compile correctly. 3. Is a Sheep object instance of Mammal? Yes 4. Is a Sheep object instance of Animal? Yes 5. What do we call the repeating of the method tostring () in Mammal and Sheep? Overriding 6. What do we call the using of super class and subclass objects in line 1 and 2 to invoke the method display? Polymorphism 7. What is the output of the program after completion your code? Shark predator: true,legs: 0 This Sheep Is preditor: false,legs: 4 page 4/5
Question 4 Complete the program as following: 1. Your code produce the showing Jfram 2. Set the frame title to Midterm Frame. 3. Set the frame size to 220 x 300. 4. The spaces between buttons 1-6 is 2. 5. The spaces between buttons 1-6 and East Button is 5. 6. South button has: Gray background. Times New Roman bold font with size 12. Helping keywords: BorderLayout, FlowLayout, GridLayout BorderLayout.CENTER, BorderLayout. EAST, BorderLayout. WEST, BorderLayout. SOUTH Font.BOLD, Color.gray public class ExamGUI extends JFrame{ public ExamGUI(String title) { super(title); JPanel p = new JPanel(new GridLayout(3, 2,2,2)); for(int i=1;i<=6;i++){ p.add(new JButton(""+i)); setlayout(new BorderLayout(5, 5)); add(p,borderlayout.center); JButton jb = new JButton("South"); jb.setbackground(color.gray); jb.setfont(new Font("Times New Roman", Font.BOLD, 12)); add(jb,borderlayout.south); add(new JButton("East"),BorderLayout.EAST); public static void main(string[] args) { ExamGUI gui = new ExamGUI("Mid Term"); gui.setdefaultcloseoperation(exit_on_close); gui.setlocationrelativeto(null); gui.setsize(220, 300); gui.setvisible(true); page 5/5