Download presentation
Presentation is loading. Please wait.
Published byMerilyn Gregory Modified over 9 years ago
1
Advanced Programming Rabie A. Ramadan Rabie@rabieramadan.org http://www.rabieramadan.org/classes/2011/Ad vpro/ Lecture 4
2
Chapter 6 Interfaces and Inner Classes 2
3
Q1 3 In java, A class can implement more than one interface (True/False)?
4
Q2 4 All methods in “interface” are automatically “protected” (True/False)?
5
Q3 5 Can I define constants in an interface?
6
Q4 6 Wwhat is wrong with the following code: public interface Comparable { int compareTo(Object other); } int compareTo(Object otherObject) { Employee other = (Employee) otherObject; if (salary < other.salary) return -1; if (salary > other.salary) return 1; return 0; }
7
Q5 7 if “Comparable” is an interface, what is the meaning of the following line of code : if (anObject instanceof Comparable) {... }
8
Q6 8 What is wrong with the following code: public interface Moveable { void move(double x, double y); } A. public interface Powered extends Moveable { double milesPerGallon(); } B. public interface Powered extends Moveable { double milesPerGallon(); double SPEED_LIMIT = 95; // a public static final constant }
9
Q7 9 In interfaces, fields are always public and final only (true/false)?
10
Q8 10 Why do we need interfaces while we have abstracts?
11
Q9 11 Describe what actually happen in: Employee original = new Employee("John Public", 50000); Employee copy = original; copy.raiseSalary(10);
12
Q10 12 How can we solve the previous problem?
13
Q11 13 clone () method initially is “protected” method and returns an object, can I implement it as follows? If No, how can I implement it? class Employee implements Cloneable { public Employee clone() throws CloneNotSupportedException { return (Employee) super.clone(); }... }
14
Q12 14 clone () method is used to copy the whole class methods and fields. Why do I need to clone the hireDay field in the following code separately ? class Employee implements Cloneable {... public Employee clone() throws CloneNotSupportedException { // call Object.clone() Employee cloned = (Employee) super.clone(); cloned.hireDay = (Date) hireDay.clone() return cloned; }
15
Q13 15 Which of the following implementation to the clone () method is better than the other : 1. public Employee clone() throws CloneNotSupportedException 2. public Employee clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { return null; } }
16
Q14 16 what is the main idea behind “inner class”? why it is important ?
17
Q15 17 Is there anything wrong with this code : if nothing wrong, describe how the inner class accesses the fields of the outer class during the run time. public class TalkingClock { public TalkingClock(int interval, boolean beep) {... } public void start() {... } private int interval; private boolean beep; private class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { Date now = new Date(); System.out.println("At the tone, the time is " + now); if (beep) Toolkit.getDefaultToolkit().beep(); }}}
18
Q16 18 Which of the following statements will be passed by the compiler without any error message considering the previous implementation: 1. if (outer.beep) Toolkit.getDefaultToolkit().beep(); 2. 3. if (beep) Toolkit.getDefaultToolkit().beep(); 4.
19
Q17 19 Inner classes are handled by the virtual machine ? (True/False) Explain?
20
Q18 20 What is the output of the following commands : java ReflectionTest TalkingClock\$TimePrinter or javap -private TalkingClock\$TimePrinter
21
21
22
Q19 22 If the following is the output of the previous instruction, what is the meaning of final TalkingClock this$0 ;
23
Q20 23 What does it mean to create “Local Inner Class”?
24
Q21 24 what is missing in the following code to start a timer that is listening on TimePrinter: public void start() { class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { Date now = new Date(); System.out.println("At the tone, the time is " + now); if (beep) Toolkit.getDefaultToolkit().beep(); }
25
Q22 25 Local inner classes are always declared as “private” (True/False) ?
26
Q23 26 Why the following code does not compile: public void start(int interval, boolean beep) { class TimePrinter implements ActionListener { public void actionPerformed(ActionEvent event) { Date now = new Date(); System.out.println("At the tone, the time is " + now); if (beep) Toolkit.getDefaultToolkit().beep(); } ActionListener listener = new TimePrinter(); Timer t = new Timer(interval, listener); t.start(); }
27
Q24 27 The following is the output of javap -private TalkingClock\$TimePrinter: What was the original class structure ?
28
28
29
Q25 29 Reconstruct the original code from the following output of the following javap command
30
30
31
Q26 31 What is a “blank final variable”?
32
Q27 32 What is the difference between the following two statements: public static final double SPEED_LIMIT = 55; public final double SPEED_LIMIT = 55;
33
Q28 33 What does it mean to create Anonymous Inner Classes?
34
Q29 34 It is obvious that the constructors of Anonymous inner classes must be declared at the beginning of the class ? (True/False)
35
Q30 35 Define the concept of Proxies in java ?
36
Q31 36 what is wrong with this code :
37
Q32 37 Do you see a problem with the following assignments: 1- a[i] = a[j]; 2- *px = *py;
38
Q33 38 What might go wrong with the following code:
39
Q34 39 What are the possible solutions to the previous problem ?
40
Q35 40 what is the summary of item 12 recommendations?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.