2 - Java OCA 8 - Object Orientation

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following are not true? (Choose all that apply) a) method overloading occurs at runtime b) method overloading occurs at compile time c) method overloading is defined as two or more methods with the same name but different number of parameters or different parameter types. d) method overloading is defined as two or more methods with the same name but different number of parameters only.

a) and d) are not true. ➤ a) method overloading occurs at runtime. The above is false. Method overloading occurs at compile time ➤ d) method overloading is defined as two or more methods with the same name but different number of parameters only. The above is also false. Method overloading is defined as two or more methods with the same name but different number of parameters or different parameter types. The Following Is True: b) method overloading occurs at compile time. The above is true.

Given: public class A { public A(){ System.out.println("A1"); } public A(int i){ System.out.println("A2"); this(); } public static void main(String[] args) { A a = new A(1); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print A2 and then A1 d) none of the above

a) is correct. The program will give a compiler error. public class A { public A(){ System.out.println("A1"); } public A(int i){ System.out.println("A2"); this(); } public static void main(String[] args) { A a = new A(1); }} 'this()' must be the first statement in the constructor to be called. 'this()' calls the current class A default constructor.

Which of the following are correct? (Choose all that apply) a) package declaration must be the first line of code otherwise compiler error b) import declaration must be first line of code otherwise compiler error c) package declaration must be the first line of code otherwise runtime error d) none of the above

a) is correct. a) package declaration must be the first line of code otherwise a compiler error will occur.

Which of the following are false? (Choose all that apply) a) A subclass can call the superclass constructor b) A subclass cannot call it superclass constructor c) A constructor can be private d) A constructor cannot be final, static or abstract e) A constructor can access static and nonstatic members of the class.

a) is not true. A subclass cannot call the superclass constructor. The following are true: b) A subclass cannot call it superclass constructor c) A constructor can be private d) A constructor cannot be final, static or abstract e) A constructor can access static and nonstatic members of the class.

Given: 1: strictfp class A { 2: strictfp A() { m(); } 3: strictfp double d = 1; 4: strictfp static void m(){ System.out.println("m() in A"); } public static void main(String[] args) { A a = new A(); a.m(); }} What can be removed from the above to enable it to compile and run (Choose all that apply) a) Remove strictfp from line 1 b) Remove strictfp from line 2 c) Remove strictfp from line 3 d) Remove strictfp from line 4

b) and c) are correct. strictfp can only be declared on a class or method and not on a constructor or instance variable as outlined below on the line 2 and 3: 1: strictfp class A { 2: strictfp A() { m(); } 3: strictfp double d = 1; 4: strictfp static void m(){ System.out.println("m() in A"); } public static void main(String[] args) { A a = new A(); a.m(); }} On the line 2 and three, a compiler error will occur. Solution: The following code increments strictfp correctly strictfp class A { double d = 1; strictfp static void m(){ System.out.println("m() in A"); } public static void main(String[] args) { A a = new A(); a.m(); } } The above will compile and print: "m() in A" strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. strictfp is supposed to prevent avoid round-off errors

Which of the following are correct? (Choose all that apply) a) specified packages must be placed in the same directory as the class name b) specified packages must be placed in the same directory as the package name c) specified packages must be placed in the same directory as the superclass name d) none of the above

b) is correct. Specified packages must be placed in the same directory as the package name.

Given: class A { public static void main(String[] args) { main(1); } static void main(int i) { System.out.println(i); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 1 d) none of the above

c) is correct. The program will compile and print 1. 1: class A { 2: public static void main(String[] args) { 3: main(1); 4: } 5: static void main(int i) { 6: System.out.println(i); 7: } } On the line 5, the method main(int i) is an overloaded method. Note: They must be both static methods otherwise if one is static and the other method is nonstatic a compiler error occurs.

Given: interface A { void mA(); } class B implements A{ public void mA(){ System.out.println("m() in B"); }} class C extends B{ public void mA() {System.out.println("mA() in C"); } public static void main(String[] args) { B b = new B(); C c = new C(); b = (B)(A)c; b.mA(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print mA() in C d) none of the above

c) is correct. The program will compile and print: mA() in C This question tests your knowledge on 'double casting'. As the name suggests, Double Casting, is where there are two casts on a reference. In order to help in answering this question, it would be advisable to write out a sketch of the relationship among the classes. From our code sample, we can see that: A ⇽ implements - B The above reads: "class B implements the interface A B ⟵ extends - C The above reads: "class C extends the class B Now let's step to the program: ===Code Step Through=== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} On the line 12, a new object of type B is created with a reference name 'b', also of type B. On the line 13, a new object of type C is created with a reference name 'c' of type C. ========2======== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} On the line 14, the double casting takes place. Firstly, reference c is upcast to interface A. Even though it's cast to an interface, the compiler still knows it is an object of type C. This is fine, as class B implements interface A. This means that B and A are "related". ========3======== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} Continuing on the line 14, the reference c is cast to type B. As class B is a subclass of interface A so the reference b = (B)(A)c is fine ========4======== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} On the line 15, reference 'b', which is pointing to the object of type C, calls the method mA() from the class C. ========5======== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} On the line 9, the mA() method body is entered. ========6======== 1: interface A { 2: void mA(); 3: } 4: class B implements A{ 5: public void mA(){ 6: System.out.println("m() in B"); 7: }} 8: class C extends B{ 9: public void mA() {System.out.println("mA() in C"); 10: } 11: public static void main(String[] args) { 12: B b = new B(); 13: C c = new C(); 14: b = (B)(A)c; 15: b.mA(); 16: }} On the line 9, the following gets printed to the screen: mA() in C

Given: class A { private int h = 1; public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); System.out.println(a1==a2); a2 = a1; System.out.println(a1.equals(a2)); System.out.println(a1==a2); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print false false true false d) none of the above

d) is correct. None of the answers are correct. The program will compile and print: false false true true ===Code Step Through=== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the lines 4 and 5, two new objects are created of type A. Each of these have references a1 and a2. Each of these objects are allocated addresses in memory by the Java virtual machine e.g. &221 and &432. A pseudocode depiction of the objects in memory as outlined below: a1 🠚 Object A(Heap Address &221) { h→ 1 } a2 🠚 Object A(Heap Address &432) { h → 1 } Note: The object A has a instance variable named h which is initialized with the primitive int value 1. ========2======== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the line 6, equals() method compares the contents of objects. But how it compares contents of the objects needs to be explicitly written by the developer? Even though the two objects contain the same content (i.e. h = 1) theres no way to compare the contents as the equals() is not overidden. Therefore the current output is: false ========3======== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the line 7, the addresses in heap memory of both objects referred to by a1 and a2 are compared using the '==' operator. Looking at the pseudocode depiction of the objects in memory, one can see that the addresses are not equal: a1 🠚 Object A(Heap Address &221) { h → 1 } a2 🠚 Object A(Heap Address &432) { h → 1 } Therefore, the current output is: false false ========4======== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the line 8, the reference a2 is reassigned to the object which is pointed to by the reference a1 i.e. object with address &221. This means that there are now two references to the same object with address &221. An outline of this depicted below in pseudocode. There is a now a null reference (represented by the character 'Ø') to the object with memory address &432. a2 🠚 a1 🠚 Object A(Heap Address &221) { h → 1 } Ø 🠚Object A(Heap Address &432) { h → 1 } Note: There is now no reference to the object with address &432. Therefore, this particular object is available for the garbage collection. This means that the object gets destroyed and the memory address is available for the storage of another object in the heap. This makes the code efficient. ========5======== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the line 9, the two objects which are referenced by a1 and a2 are compared using the equals() method. A pseudocode description of the objects in memory are outlined below: a2 🠚 a1 🠚 Object A(Heap Address &221) { h → 1 } Ø 🠚Object A(Heap Address &432) { h → 1 } The equals() method has not been overridden by the developer. The default behavior of the equals() method is that if the addresses of two objects (of the same type) are equal, then the contents are also equal. Therefore the output is: false false true Note: The equals() method can be overridden to compare the contents of objects (which are of the same type). ========6======== 1: class A { 2: private int h = 1; 3: public static void main(String[] args) { 4: A a1 = new A(); 5: A a2 = new A(); 6: System.out.println(a1.equals(a2)); 7: System.out.println(a1==a2); 8: a2 = a1; 9: System.out.println(a1.equals(a2)); 10: System.out.println(a1==a2); 11: }} On the line 10, as the references a1 and a2 are pointing to the same object, then the result is: false false true true

class Mammal { String name = "furry "; String makeNoise() { return "generic noise"; }} class Zebra extends Mammal { String name = "stripes "; String makeNoise() { return "bray"; }} public class ZooKeeper { public static void main(String[] args) { new ZooKeeper().go(); } void go() { Mammal m = new Zebra(); System.out.println(m.name + m.makeNoise()); }} What is the result? A. furry bray B. stripes bray C. furry generic noise D. stripes generic noise E. Compilation fails F. An exception is thrown at runtime

A is correct The program will compile in print: furry bray ===Code Step Through=== 1: class Mammal { 2: String name = "furry "; 3: String makeNoise() { 4: return "generic noise"; 5: }} 6: class Zebra extends Mammal { 7: String name = "stripes "; 8: String makeNoise() { 9: return "bray"; 10: }} 11: public class ZooKeeper { 12: public static void main(String[] args) { 13: new ZooKeeper().go(); 14: } 15: void go() { 16: Mammal m = new Zebra(); 17: System.out.println(m.name + m.makeNoise()); 18: } } On the line 13, a new object is created of type ZooKeeper. A go() method call is made on the same line. ========2======== 1: class Mammal { 2: String name = "furry "; 3: String makeNoise() { 4: return "generic noise"; 5: } } 6: class Zebra extends Mammal { 7: String name = "stripes "; 8: String makeNoise() { 9: return "bray"; 10: } } 11: public class ZooKeeper { 12: public static void main(String[] args) { 13: new ZooKeeper().go(); 14: } 15: void go() { 16: Mammal m = new Zebra(); 17: System.out.println(m.name + m.makeNoise()); 18: } } On the line 15, the go() enters. On the line 16, the new object of type Zebra is created with a reference 'm' of type Mammal. ========3======== 1: class Mammal { 2: String name = "furry "; 3: String makeNoise() { 4: return "generic noise"; 5: } } 6: class Zebra extends Mammal { 7: String name = "stripes "; 8: String makeNoise() { 9: return "bray"; 10: } } 11: public class ZooKeeper { 12: public static void main(String[] args) { 13: new ZooKeeper().go(); 14: } 15: void go() { 16: Mammal m = new Zebra(); 17: System.out.println(m.name + m.makeNoise()); 18: } } On the line 17, we have an expression containing an instance variable which is to be added to the return contents of a method. The question is where do we start the execution of this expression? Making sure you have memorized the Order of Precedence Rules, you should know then that an expression that has variable call and a method call, the method call gets executed first. ========4======== 1: class Mammal { 2: String name = "furry "; 3: String makeNoise() { 4: return "generic noise"; 5: } } 6: class Zebra extends Mammal { 7: String name = "stripes "; 8: String makeNoise() { 9: return "bray"; 10: } } 11: public class ZooKeeper { 12: public static void main(String[] args) { 13: new ZooKeeper().go(); 14: } 15: void go() { 16: Mammal m = new Zebra(); 17: System.out.println(m.name + m.makeNoise()); 18: } } Looking back at line 16 we can see that the object created is of type Zebra and the reference is of type Mammal. The Zebra class and the Mammal class contains a method named makeNoise(). Based on the principles of polymorphism, the compiler knows that the makeNoise() method located within the Zebra class is executed and not the superclass of Mammal. Therefore, the string "bray" it's returned to the main() method. ========5======== 1: class Mammal { 2: String name = "furry "; 3: String makeNoise() { 4: return "generic noise"; 5: } } 6: class Zebra extends Mammal { 7: String name = "stripes "; 8: String makeNoise() { 9: return "bray"; 10: } } 11: public class ZooKeeper { 12: public static void main(String[] args) { 13: new ZooKeeper().go(); 14: } 15: void go() { 16: Mammal m = new Zebra(); 17: System.out.println(m.name + m.makeNoise()); 18: } } On the line 17, a call is made to the instance variable 'name'. This prints the contents of the Mammal class instance variable 'name' to the screen. We now have the two Strings: "furry " + "bray" These strings are appended together resulting in the final output: "furry bray"

Given: 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood)tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree { } What is the result? (Choose all that apply.) A. An exception is thrown at runtime B. The code compiles and runs with no output C. Compilation fails with an error at line 8 D. Compilation fails with an error at line 9

A is correct. An exception is thrown at runtime. In order to undertake this question it would be important to sketch out the class relationships. The 'Redwood' class extends 'Tree' class. The above means that the 'Redwood' class is a subclass of 'Tree' class. Let's step through the program ===Code Step Through=== 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood) tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree { } On the line 5, new object of type Redwood is created and a method call go() is made to it. ========2======== 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood) tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree {} On the line 7, the go() method is entered. ========3======== 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood) tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree { } On the line 8, a method call is made to go2(). ========4======== 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood) tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree { } On the line 11, the go2() method is entered. ========5======== 3. public class Redwood extends Tree { 4. public static void main(String[] args) { 5. new Redwood().go(); 6. } 7. void go() { 8. go2(new Tree(), new Redwood()); 9. go2((Redwood) new Tree(), new Redwood()); 10. } 11. void go2(Tree tl, Redwood rl) { 12. Redwood r2 = (Redwood) tl; 13. Tree t2 = (Tree)rl; 14. } 15. } 16. class Tree { } On the line 12, the reference 'tl' which is referring to the object of type 'Tree ', is being casted to an object of type 'Redwood '. Referring to the relationship among the classes: 'Redwood ' class is a subclass of 'Tree' class. Alternatively, Tree is a superclass of Redwood . Therefore, on line 12, an attempt is made to downcast from the superclass object (Tree ) to subclass of the superclass object (Redwood ). This is illegal and it causes a ClassCastException to be thrown.

Given: class Clidder { private final void flipper() { System.out.println("Clidder"); }} public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); }} What is the result? A. Clidlet B. Clidder C. Clidder Clidlet D. Clidlet Clidder E. Compilation fails

A is correct. The program will compile in print: Clidlet class Clidder { private final void flipper() { System.out.println("Clidder"); } } public class Clidlet extends Clidder { public final void flipper() { System.out.println("Clidlet"); } public static void main(String [] args) { new Clidlet().flipper(); }} Although a final method cannot be overridden, in this case, the method flipper() is private and therefore hidden in class Clidder. If it was public, then it would give a compiler error The effect is that a new accessible method flipper is created in class Clidlet. Therefore no polymorphism occurs in this example the method invoked is simply that of the child class (Clidlet) and no error occurs. The method flipper() gets called from within the Clidlet class which prints: "Clidlet".

Given: public class Locomotive { Locomotive() { main("hi"); } public static void main(String[] args) { System.out.print("2 "); } public static void main(String args) { System.out.print("3 " + args); }} What is the result? (Choose all that apply.) A. 2 will be included in the output B. 3 will be included in the output C. hi will be included in the output D. Compilation fails E. An exception is thrown at runtime

A is correct. The number 2 will be included in the output. This question tests your knowledge of overloaded methods. public class Locomotive { Locomotive() { main("hi"); } public static void main(String[] args) { System.out.print("2"); } public static void main(String args) { System.out.print("3 " + args); } } It's legal to overload main() since no instances of Locomotive are created, the constructor does not run and the overloaded version of main () does not run.

Given: public abstract interface A { public void twiddle(String s); } Which is the correct class? (Choose all that apply.) A) public abstract class Frob implements A { public abstract void twiddle(String s) { } } B) public abstract class Frob implements A { } C) public class Frob extends A { public void twiddle(Integer i) { } } D) public class Frob implements A { public void twiddle(Integer i) { } } E) public class Frob implements A { public void twiddle(String i) { } public void twiddle(Integer s) { } }

B and E are correct. Considering the interface again: public abstract interface A{ public void twiddle(String s); } B) public abstract class Frob implements A{ } B is correct because an abstract class need not implement any or all of an interface's methods. It passes the implementation to the sublcass of the abstract class E) public class Frob implements A { public void twiddle(String i) { } public void twiddle(Integer s) { } } E is correct because the class implements the interface method and additionally overloads the twiddle() method. The Following Will Not Compile: A) public abstract class Frob implements A{ public abstract void twiddle(String s) { } } An abstract method cannot have the body compiler error C) public class Frob extends A { public void twiddle(Integer i) { } } A concrete class cannot extend an interface therefore compiler error Note: public abstract interface A{ public void twiddle(String s); } The above program compiles fine

As of Java 8, it is now possible to inherit concrete methods from which classes below? (Choose all that apply) a) Abstract classes b) Interfaces c) Anonymous classes d) none of the above

B is correct. As of Java 8, it is now possible to inherit concrete methods from interfaces. As an example, consider a class C that implements two interfaces. The two interfaces have a concrete implementation of a method of the same name i.e. m() The code to implement these interfaces is as follows: 1: interface A{ 2: default public void m(){ 3: System.out.println("Interface A: m()"); 4: } 5: } 6: 7: interface B{ 8: default public void m(){ 9: System.out.println("Interface B: m()"); 10: } 11: } 12: 13: class C implements A,B { 14: 15: public void m(){ 16: System.out.println("Concrete C: m()"); 17: } 18: 19: public static void main(String[] args) { 20: C aC = new C(); 21: aC .m(); 22: new A(){}.m(); 23: new B(){}.m(); 24: } 25: } Note: The keyword 'default' keyword is required for the concrete implementations of the method m() within the interface. The above would output: Concrete C: m() Interface A: m() Interface B: m() Let's step through the code: ===Code Step Through=== 1: interface A{ 2: default public void m(){ 3: System.out.println("Interface A: m()"); 4: } 5: } 6: 7: interface B{ 8: default public void m(){ 9: System.out.println("Interface B: m()"); 10: } 11: } 12: 13: class C implements A,B { 14: 15: public void m(){ 16: System.out.println("Concrete C: m()"); 17: } 18: 19: public static void main(String[] args) { 20: C aC = new C(); 21: aC.m(); 22: new A(){}.m(); 23: new B(){}.m(); 24: } 25: } On the line 20, a new C object is created with reference aC. On the line 21, a method call is made to the concrete implementation of the method m() and on the line 16, "Concrete C: m()" gets printed to the screen. This, in essence, is the concrete implementation for method m() of the TWO interfaces A and B method m(). Even though interface A and B have concrete implementations of the method m(), the class C must provide its own concrete implementation of all the methods of the interfaces A and B including m(). ========2======== 1: interface A{ 2: default public void m(){ 3: System.out.println("Interface A: m()"); 4: } 5: } 6: 7: interface B{ 8: default public void m(){ 9: System.out.println("Interface B: m()"); 10: } 11: } 12: 13: class C implements A,B { 14: 15: public void m(){ 16: System.out.println("Concrete C: m()"); 17: } 18: 19: public static void main(String[] args) { 20: C aC = new C(); 21: aC.m(); 22: new A(){}.m(); 23: new B(){}.m(); 24: } 25: } On line 22, the interface A is instantiated solely for the purposes of explicitly calling its concrete method m(). The program then jumps to the line 3 and outputs the following to the screen: Concrete C: m() Interface A: m() ========3======== 1: interface A{ 2: default public void m(){ 3: System.out.println("Interface A: m()"); 4: } 5: } 6: 7: interface B{ 8: default public void m(){ 9: System.out.println("Interface B: m()"); 10: } 11: } 12: 13: class C implements A,B { 14: 15: public void m(){ 16: System.out.println("Concrete C: m()"); 17: } 18: 19: public static void main(String[] args) { 20: C aC = new C(); 21: aC.m(); 22: new A(){}.m(); 23: new B(){}.m(); 24: } 25: } Likewise on the line 23, the interface B is instantiated and its concrete method m() is explicitly called. The program then jumps to the line 16 and this outputs the following to the screen: Concrete C: m() Interface A: m() Interface B: m()

Given: 3. public class B extends A { 4. public static String sing() { return "fa"; } 5. public static void main(String[] args) { 6. B t = new B(); 7. A s = new B(); 8. System.out.println(t.sing () + " " + s.sing()); 5. } 10. } 11. class A { 12. public static String sing() { return "la"; } } What is the result? A. fa fa B. fa la C. la la D. Compilation fails E. An exception is thrown at runtime

B is correct. The program will print: fa la The code is correct, but polymorphism doesn't apply to static methods. ===Code Step Through=== 3. public class B extends A { 4. public static String sing() { return "fa"; } 5. public static void main(String[] args) { 6. B t = new B(); 7. A s = new B(); 8. System.out.println(t.sing () + " " + s.sing()); 5. } 10. } 11. class A { 12. public static String sing() { return "la"; } } On the line 6, new object is created of type B and which has a reference 't'. On the line 7, a new object is created of type B and which has a reference 's' (of type A) ========2======== 3. public class B extends A { 4. public static String sing() { return "fa"; } 5. public static void main(String[] args) { 6. B t = new B(); 7. A s = new B(); 8. System.out.println(t.sing() + " " + s.sing()); 5. } 10. } 11. class A { 12. public static String sing() { return "la"; } } On the line 8, t.sing() is called which calls the static method of class B on line 4. On line 4, string "fa" is return to the main() method and gets printed to the screen. ========3======== 3. public class B extends A { 4. public static String sing() { return "fa"; } 5. public static void main(String[] args) { 6. B t = new B(); 7. A s = new B(); 8. System.out.println(t.sing() + " " + s.sing()); 5. } 10. } 11. class A { 12. public static String sing() { return "la"; } } On the line 8, s.sing() is called which calls the static method of class A. On line 4, string "la" is return to the main() method and gets printed to the screen. Final Ouptut: The program compiles and prints: "fa la" Polymorphism: Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. Consider the following code which has a superclass name Animal and which has two subclasses Pig and Chicken. In the main method, an array of Animals stores a number of animal objects names. When the code is run, the compiler knows ("polymorphically") that inside animalFarm[0], a Pig object is stored. The compiler calls the method getNoise() from the Pig class and prints "Oink" to the screen. The same is for animalFarm[1]. This is an example of Polymorphism. class Animal { public String getNoise() { return "Noise"; } } public class Pig extends Animal { public String getNoise(){ return "Oink"; } } } class Chicken extends Animal { public String getNoise() { return "Cluck Cluck"; } public static void main(String[] args) { Animal [] animalFarm = {new Pig(),new Chicken()}; System.out.println(animalFarm[0].getNoise()); System.out.println(animalFarm[1].getNoise()); }

Given: class A { A(){ System.out.print("b "); } A(String name) { this(); System.out.print("bn " + name); }} public class B extends A { B() { System.out.print("h "); } B(String name) { this(); System.out.print("hn " + name); } public static void main(String[] args) { new B("x "); } } What is the result? A. h hn x B. hn x h C. b h hn x D. b hn x h E. bn x h hn x F. b bn x h hn x G. bn x b h hn x H. Compilation fails

C is correct This program will print: "b h hn x" It would be highly advisable to know the class loading sequence order for the exam: Class Loading and Sequence #1 Load all classes related to C starting from superclass A and working down. #2 Load all static initiation blocks in sequence order starting from the superclass A #3 Call the main() method #4: Call chain of super() up the class hierarchy to the top class. #5: For the current class that is entered: #5.1: Load all instances of that particular class. #5.2: Call the contructor for each class ===Code Step Through=== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: }} 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } #1 Load all classes related to C starting from superclass A and working down. #2 Load all static initiation blocks in sequence order starting from the superclass A #3 Call the main() method On the line 16, a new object is created which is of type B and a parameter string is passed into the constructor of it. ========2======== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: } } 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } Step4: Call super() (default constructor) on the top class of the class hierarchy (i.e A()). This 'super()' is implicitly called after line 11 and before line 12. Note: Even though we are calling the non-default constructor (line 16), there is still an implicit super() call to the default super constructor. ========3======== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: } } 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } On the line 2, the super default constructor is executed and string "b " is printed to the screen. Current Print Scream Output: "b " ========4======== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: }} 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } On the line 12, a call to 'this()' is made which calls the default constructor of this particular class B. ========5======== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: }} 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } On the line 9 of the string "h " is printed to the screen. Current Print Scream Output: "b h " ========6======== 1: class A { 2: A(){ System.out.print("b "); } 3: A(String name) { 4: this(); 5: System.out.print("bn " + name); 6: }} 7: public class B extends A { 8: B() { 9: System.out.print("h "); 10: } 11: B(String name) { 12: this(); 13: System.out.print("hn " + name); 14: } 15: public static void main(String[] args) { 16: new B("x "); 17: } 18: } The program then jumps to line 13, which appends the contents of the string 'name' ("x ") to the end of the string "hn " and prints: "hn x " to the screen. Final Print Output Stream: "b h hn x "

Given the following: 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3 . 4. class Chrome { 5. public static void main(String [] args) { 6. X xl = new X(); 7 . X x2 = new Y(); 8. Y yl = new Y(); 9. // insert code here 10. } } Which of the following, inserted at line 9, will compile? (Choose all that apply.) A. x2.do2() ; B. (Y) x2.do2() ; C. ( (Y) x2).do2() ; D. None of the above statements will compile

C is correct. In order to undertake this question, it would be advisable to sketch out the relationship among the classes. Class Y extends class X The above means that class Y is a subclass of class X. ===Code Step Through=== 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X xl = new X(); 7 . X x2 = new Y(); 8. Y yl = new Y(); 9. ((Y) x2).do2(); 10. } } On the line 6, a new object is created of type X and with a reference 'xl' of the same type X. ========2======== 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X xl = new X(); 7 . X x2 = new Y(); 8. Y yl = new Y(); 9. ((Y) x2).do2(); 10. } } On the line 7, a new object is created of type Y and with a reference 'x2' of the type Y. As class X is a subclass of class Y, this is a perfectly fine assignment. On the line 8, a new object instance is created of type Y. ========3======== 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X xl = new X(); 7 . X x2 = new Y(); 8. Y yl = new Y(); 9. ((Y) x2).do2(); 10. } } On the line 9, the object referenced by 'x2' is cast as Y. As the object referenced by 'x2' is of type Y, this is a perfectly fine casting. ========4======== 1. class X { void do1() { } } 2. class Y extends X { void do2() { } } 3. 4. class Chrome { 5. public static void main(String [] args) { 6. X xl = new X(); 7 . X x2 = new Y(); 8. Y yl = new Y(); 9. ((Y) x2).do2(); 10. } } Continuing on the line 9, the method do2() gets called from within the class Y, namely, on the 2. Before you can invoke Y's do2() method, you have to cast x2 to be of type this Y on the line 9. Note: In the case of the answer "B. (Y) x2.do2();" The above looks like a proper cast, but without the second set of parentheses, the compiler thinks it's an incomplete statement.

Given: class A { static String s = " "; protected A() { s += "alpha "; } } class B extends A { private B() { s += "sub "; } } public class C extends A { private C() { s += "subsub "; } public static void main(String[] args) { new C(); System.out.println(s); } } What is the result? A. subsub B. sub subsub C. alpha subsub D. alpha sub subsub E. Compilation fails

C is correct. The program will print: alpha subsub. Because the code doesn't attempt to make a B object, the private constructor in B is okay. For this question, pay particular attention to the fact that C extends A. This question tests your knowledge of the class loading sequence. It would be imperative to know the class loading sequence order for the exam: Class Loading and Sequence #1 Load all classes related to C starting from superclass A and working down. #2 Load all static initiation blocks in sequence order starting from the superclass A #3 Call the main() method #4: Call chain of super() up the class hierarchy to the top class. #5: For the current class that is entered: #5.1: Load all instances of that particular class. #5.2: Call the contructor for each class ===Code Step Through=== 1: class A { 2: static String s = " "; 3: protected A() { s += "alpha "; } 4: } 5: class B extends A { 6: private B() { s += "sub "; } 7: } 8: public class C extends A { 9: private C() { s += "subsub "; } 10: public static void main(String[] args) { 11: new C(); 12: System.out.println(s); 13: } 14: } On the line 11, a new object of type C is created with no reference. This calls implicitly, 'super()' which calls the superclass default constructor on the line 3. ========2======== 1: class A { 2: static String s = " "; 3: protected A() { s += "alpha "; } 4: } 5: class B extends A { 6: private B() { s += "sub "; } 7: } 8: public class C extends A { 9: private C() { s += "subsub "; } 10: public static void main(String[] args) { 11: new C(); 12: System.out.println(s); 13: } 14: } On the line 3, string "alpha " is appended to the static string s. ========3======== 1: class A { 2: static String s = " "; 3: protected A() { s += "alpha "; } 4: } 5: class B extends A { 6: private B() { s += "sub "; } 7: } 8: public class C extends A { 9: private C() { s += "subsub "; } 10: public static void main(String[] args) { 11: new C(); 12: System.out.println(s); 13: } 14: } On the line 6, the B constructor is private so the compiler skips this as B is not used in the code. ========4======== 1: class A { 2: static String s = ""; 3: protected A() { s += "alpha "; } 4: } 5: class B extends A { 6: private B() { s += "sub "; } 7: } 8: public class C extends A { 9: private C() { s += "subsub "; } 10: public static void main(String[] args) { 11: new C(); 12: System.out.println(s); 13: } 14: } On the line 9, even though the constructor for class C private, since we are explicitly calling this object of type C in the main(), the C constructor gets executed. String "subsub " is appended onto the string s. On the line 12, the content of static string s is printed to the screen. Final output: "alpha subsub"

Given: interface I1 { default int doStuff() { return 1; } } interface I2 { default int doStuff() { return 2; } } public class MultiInt implements I1, 12 { public static void main(String[] args) { new MultiInt ().go(); } void go() { System.out.printIn(doStuff()); } int doStuff() { return 3; }} What is the result? A. 1 B. 2 C. 3 D. The output is unpredictable E. Compilation fails F. An exception is thrown at runtime

E is correct. The program will give a compiler error. This is kind of a trick question. On the line 14 below, the implementing method doStuff() in class MultiInt must be marked public. 1: interface I1 { 2: default int doStuff(){ return 1; } 3: } 4: interface I2 { 5: default int doStuff(){ return 2; } 6: } 7: public class MultiInt implements I1, I2 { 8: public static void main(String[] args) { 9: new MultiInt().go(); 10: } 11: void go() { 12: System.out.printIn(doStuff()); 13: } 14: int doStuff() { 15: return 3; 16: } } Solution: As Interface methods are implicitly public, if it was in the original program question, all the other code is legal, and the output would be 3. 1: interface I1 { 2: default int doStuff(){ return 1; } 3: } 4: interface I2 { 5: default int doStuff(){ return 2; } 6: } 7: public class MultiInt implements I1, I2 { 8: public static void main(String[] args) { 9: new MultiInt().go(); 10: } 11: void go() { 12: System.out.printIn(doStuff()); 13: } 14: public int doStuff() { 15: return 3; 16: }} If you understood all the multiple inheritance rules and just missed the access modifier, give yourself half credit

Given: class Top { public Top(String s) { System.out.print("B"); }} public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.printIn(" ") ; }} What is the result? A. BD B. DB C. BDC D. DBC E. Compilation fails

E is correct. The program will give a compiler error. Below, inside Bottom2 class, there is an implicit super() call to the super constructor. But there is no default contructor in Top class, therefore a compiler error occurs. class Top { public Top(String s) { System.out.print("B"); } } public class Bottom2 extends Top { Bottom2(String s){ super(); System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.printIn(" ") ; } } Solution: insert default constructor in superclass: class Top { public Top(String s) { System.out.print("B"); } public Top(){} } public class Bottom2 extends Top { Bottom2(String s){ super(); System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.printIn(" ") ; } }

Given: class Dog { public void bark() { System.out.print("woof "); }} class Hound extends Dog { public void sniff () { System.out.print("sniff "); } public void bark() { System.out.print("howl "); } } public class DogShow { public static void main(String[] args) { new DogShow().go(); } void go() { new Hound().bark(); //Line 14 ((Dog) new Hound()).bark(); //Line 15 ((Dog) new Hound()).sniff(); }} What is the result? (Choose all that apply.) A. howl howl sniff B. howl woof sniff C. howl howl followed by an exception D. howl woof followed by an exception E. Compilation fails with an error at line 14 F. Compilation fails with an error at line 15

F is correct. Compilation fails with an error at line 15. Class Dog doesn't have a sniff() method. class Dog { public void bark() { System.out.print("woof "); } } class Hound extends Dog { public void sniff(){ System.out.print("sniff "); } public void bark(){ System.out.print("howl "); } } public class DogShow { public static void main(String[] args) { new DogShow().go(); } void go() { new Hound().bark(); //Line 14 ((Dog) new Hound()).bark(); //Line 15 ((Dog) new Hound()).sniff(); } } On the line 15 above, a new object of type Hound is created. This new object is cast to type Dog. This is perfectly acceptable as class Hound is a subclass of class Dog. Next, on the line 15, a call is made to sniff() method. As the Hound object reference is casted to the superclass Dog, then the Dog class must have its own method 'sniff()'. As Dog class has not the method sniff() implemented, therefore this causes the compiler error.

If the subclass does not define all abstract methods that the superclass defines then: (Choose all that apply) a) The program will give a compiler error. b) The program will give a runtime error c) The subclass should also be defined as abstract in order for the program to compile. d) none of the above

a) and c) are correct a) The program will give a compiler error. c) The subclass should also be defined as abstract in order for the program to compile. If the subclass does not define all abstract methods that the superclass defines, the subclass should also defined as abstract in order for the program to compile.

Given: import java.util.*; class A { private int theInt = 0; private ArrayList<String> subjects; public A() { subjects = new ArrayList<>(); subjects.add("Math"); } int getId(){ return theInt; } ArrayList<String> getList(){ return subjects; }} Which are true? (Choose all that apply) a) int getId(){ return theInt; } returns a copy of 'theInt' b) int getId(){ return theInt; } returns a original of 'theInt' original c) ArrayList<String> getList(){ return subjects; } returns a copy of 'subjects' d) ArrayList<String> getList(){ return subjects; } returns a original of 'subjects'

a) and d) are correct. Let's step through the answers: a) import java.util.*; class A { private int theInt = 0; private ArrayList<String> subjects; public A() { subjects = new ArrayList<>(); subjects.add("Math"); } int getId(){ return theInt; } ArrayList<String> getList(){ return subjects; } } This method returns a copy of theInt as it is a primitive type. A copy of the contents of this primitive type named 'theInt' is returned. d) import java.util.*; class A { private int theInt = 0; private ArrayList<String> subjects; public A() { subjects = new ArrayList<>(); subjects.add("Math"); } int getId(){ return theInt; } ArrayList<String> getList(){ return subjects; } } This method returns a original of subjects object. subjects is a reference that is pointing to (refering to) an ArrayList object that stores String Note: In relation to the answer d), the method 'getList()' returns the original of subjects object address in memory. This is bad encapsulation. Solution: Have to copy of the object subjects.

Given: public class A { public int i = 10; private int j = 0; } public class B extends A { private int i = 20; protected int j = 30; } public class C extends B { public static void main(String[] args){ C c = new C(); //insert code// }} Which lines of code, inserted above will make the program to not compile? (Choose all that apply) a) System.out.println(c.i); b) System.out.println(((A)c).i); c) System.out.println(c.j); d) System.out.println(((A)c).j);

a) and d) will make the program not compile. Let's step through the program with each of these choices: a) public class A { public int i = 10; private int j = 0; } public class B extends A { private int i = 20; protected int j = 30; } public class C extends B { public static void main(String[] args){ C c = new C(); System.out.println(c.i ); } } Compiler error as variable i in class B is private and therefore cannot be accessed outside its class. d) public class A { public int i = 10; private int j = 0; } public class B extends A { private int i = 20; protected int j = 30; } public class C extends B { public static void main(String[] args){ C c = new C(); System.out.println(((A)c).j ); } } Compiler error as variable j in class A is private. In the main method of the C, class C object references casted as the superclass A, in which case, the variable j in the class A is called which causes the compiler error. The Following Will Compile: b) public class A { public int i = 10; private int j = 0; } public class B extends A { private int i = 20; protected int j = 30; } public class C extends B { public static void main(String[] args){ C c = new C(); System.out.println(((A)c).i ); }} Accessing variable i , which is public, is ok. c) public class A { public int i = 10; private int j = 0; } public class B extends A { private int i = 20; protected int j = 30; } public class C extends B { public static void main(String[] args){ C c = new C(); System.out.println(c.j ); } } Accessing variable j which is protected is ok. Note: Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same package and so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclass and world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is public would be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Given: public class A { public A(int x) { theX = x; } private int theX; } public class B extends A{ public B(int x) { A(x){ theX = x; }}} } What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

a) is correct A subclass cannot override a superclass constructor as constructors are not inherited. In the program below, the constructor of the class A is attempted to be overridden in subclass B on the line 9. This causes the compiler error: 1: public class A { 2: public A(int x) { 3: theX = x; 4: } 5: private int theX; 6: } 7: public class B extends A { 8: public B(int x) { 9: A(x){ 10: theX = x; 11: } } } A solution to the above problem would be to use the keyword 'super(x)' which explicitly calls the superclass constructor on the line 9: 1: public class A { 2: public A(int x) { 3: theX = x; 4: } 5: private int theX; 6: } 7: public class B extends A { 8: public B(int x) { 9: super(x); 10: } 11: }

Given: interface A { public void mA(); } class B { public void mB(){ System.out.println("B"); }} class C extends B{ public void mA() { System.out.println("C"); }} class D extends B implements A { public void mA(){ System.out.println("mA() in E"); } public void mE(){ System.out.println("mE() in E"); }} class E { private void runD(B aB){ //Line 1 ((E)aB).mE(); //Line 2 ((C)aB).mE(); //Line 3 ((C)aB).mA(); } public static void main(String[] args){ E e = new E(); e.runD(new C()); }} Which lines will not compile or give a runtime exception? (Choose all that apply) a) Line 1 and 2 b) Line 2 and 3 c) Line 1 only d) Line 2 only

a) is correct Line 1 and 2 will not compile. It would also be highly advisable do a quick sketch of the classes, interfaces and their relationships. In order to do this, let: ➤ The clear arrow head '⇽' symbolize that a class implements an interface. ➤ The straight arrow head '⟵' symbolize that a class extends another class. After this, sketch out 'associations' (relationships) among the classes/interfaces. Doing a class sketch for this example will yield the following: [ A ] ⇽ implements - [ D ] The above reads: "class D implements the interface A" [ B ] ⟵ extends - [ C ] The above reads: "class C is a subclass of class B" [ B ] ⟵ extends - [ D ] The above reads: "class D is a subclass of class B" Now let's step to the program. ===Code Step Through=== 1: interface A { 2: public void mA(); 3: } 4: class B { 5: public void mB(){System.out.println("B"); 6: }} 7: class C extends B{ 8: public void mA() {System.out.println("C"); 9: }} 10: class D extends B implements A { 11: public void mA(){System.out.println("mA() in E"); 12: } 13: public void mE(){System.out.println("mE() in E"); 14: }} 15: class E { 16: private void runD(B aB){ 17: //Line 1 18: ((E)aB).mE(); 19: //Line 2 20: (C)aB).mE(); 21: //Line 3 22: ((C)aB).mA(); 23: } 24: public static void main(String[] args){ 25: E e = new E(); 26: e.runD(new C()); 27: } } On the line 25, the object E gets created and assigned a reference 'e'. On the line 26, the object E's runD() method gets called and the new object C is passed into it. ========2======== 1: interface A { 2: public void mA(); 3: } 4: class B { 5: public void mB(){System.out.println("B"); 6: }} 7: class C extends B{ 8: public void mA() {System.out.println("C"); 9: }} 10: class D extends B implements A { 11: public void mA(){System.out.println("mA() in E"); 12: } 13: public void mE(){System.out.println("mE() in E"); 14: }} 15: class E { 16: private void runD(B aB){ 17: //Line 1 18: ((E)aB).mE(); 19: //Line 2 20: (C)aB).mE(); 21: //Line 3 22: ((C)aB).mA(); 23: } 24: public static void main(String[] args){ 25: E e = new E(); 26: e.runD(new C()); 27: }} On the line 16, the runD() is entered and its local parameter is named 'aB' of type B. This local paramter in the method signature 'aB' is pointing to the C object that was passed into the method on line 26. ========3======== 1: interface A { 2: public void mA(); 3: } 4: class B { 5: public void mB(){System.out.println("B"); 6: } } 7: class C extends B{ 8: public void mA() {System.out.println("C"); 9: }} 10: class D extends B implements A { 11: public void mA(){System.out.println("mA() in E"); 12: } 13: public void mE(){System.out.println("mE() in E"); 14: }} 15: class E { 16: private void runD(B aB){ 17: //Line 1 18: ((E) aB ).mE(); 19: //Line 2 20: (C) aB ).mE(); 21: //Line 3 22: ((C) aB ).mA(); 23: } 24: public static void main(String[] args){ 25: E e = new E(); 26: e.runD(new C()); 27: } } On the line 18, aB is pointing to the C object created on the line 26. A cast of type E is attempted to be applied to aB (which is the method parameter and is of type B). As class E is completely unrelated to class B then a compiler error occurs. If class B was a subtype of class E then this line of code would compile fine. If line 18 was commented out, the code would continue to line 20. This is outlined below. ========4======== 1: interface A { 2: public void mA(); 3: } 4: class B { 5: public void mB(){System.out.println("B"); 6: }} 7: class C extends B{ 8: public void mA() {System.out.println("C"); 9: } } 10: class D extends B implements A { 11: public void mA(){System.out.println("mA() in E"); 12: } 13: public void mE(){System.out.println("mE() in E"); 14: }} 15: class E { 16: private void runD(B aB){ 17: //Line 1 18: ((E) aB ).mE(); 19: //Line 2 20: (C) aB ).mE(); 21: //Line 3 22: ((C) aB ).mA(); 23: } 24: public static void main(String[] args){ 25: E e = new E(); 26: e.runD(new C()); 27: }} On line 20, although the object reference 'aB' is of type B (and class B is not a subtype of C). Nonetheless, we tell the compiler that we are guaranteeing that the reference aB is pointing to an object of type C (or is an object that is a subtype of class C). In this case, the reference 'aB' is pointing to an object of type C. At this point the code is fine. However, an attempt is made to call the method mE() but there is no such method in the C class therefore a compiler error occurs. ========5======== 1: interface A { 2: public void mA(); 3: } 4: class B { 5: public void mB(){System.out.println("B"); 6: }} 7: class C extends B{ 8: public void mA() {System.out.println("C"); 9: } } 10: class D extends B implements A { 11: public void mA(){System.out.println("mA() in E"); 12: } 13: public void mE(){System.out.println("mE() in E"); 14: }} 15: class E { 16: private void runD(B aB){ 17: //Line 1 18: ((E) aB ).mE(); 19: //Line 2 20: (C) aB ).mE(); 21: //Line 3 22: (C) aB ).mA(); 23: } 24: public static void main(String[] args){ 25: E e = new E(); 26: e.runD(new C()); 27: }} If lines 18 and 20 are commented out, the code would continue to line 22. On the line 22, the cast is undertaken fine and a call is made to method mA() which is located in class C as a consquence "C" is printed to the screen.

Given: class A { void mA(){ System.out.println("mA()"); ; }} class B extends A{ public void mB(){ System.out.println("mB() in B"); } void mA(){ System.out.println("mA()in B"); } public static void main(String[] args) { ((A)new B()).mA(); ((A)new B()).mB(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print mA()in B mB() in B d) none of the above

a) is correct. The program gives a compiler error. 1: class A { 2: void mA(){ System.out.println("mA()"); ; 3: }} 4: class B extends A{ 5: public void mB(){ 6: System.out.println("mB() in B"); 7: } 8: void mA(){ System.out.println("mA()in B"); 9: } 10: public static void main(String[] args) { 11: ((A)new B()).mA(); 12: ((A)new B()).mB(); 13: } } On the line 12, a new unreferenced object is created of type B. This object B is cast to a type A. As B is a subclass of class A, the casting is fine. Following on from this, a method call is made to mB(). As 'A' class cast reference type doesn't have method mB() therefore a compiler error occurs. Note: If the method call on line 12 was to mA(), instead of mB(), then it would compile and run fine and print 'mA()'

Given: class A { static void mA(){ System.out.println("mA() in A"); }} class B extends A { void mA(){ System.out.println("mA() in B"); } public static void main(String[] args) { A a = new B(); a.mA(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print "mA() in B" d) none of the above

a) is correct. The program will give a compiler error. 1: class A { 2: static void mA(){ 3: System.out.println("mA() in A"); 4: } } 5: class B extends A { 6: void mA(){ 7: System.out.println("mA() in B"); 8: } 9: public static void main(String[] args) { 10: A a = new B(); 11: a.mA(); 12: }} The static method, on the line 2, in the superclass A a static method cannot be overridden by a non-static method in the subclass B and vice a versa. As a general rule of thumb, a static method in superclass cannot be overridden by a nonstatic method in a subclass and vice a versa. Why? Static methods, are declared once in class memory and are shared between all object instances. The static method, once created, lasts for the duration of the program. e.g In the Java API (Application Programming Interface), there is a java.lang.Math Inside this Math class there is a static method named random() This static random() method cannot be overridden by an instance method because there can be, potentially, millions of object instances within an application. The compiler would not know which one of those millions of objects instances the static method is related to. Therefore static methods and instance methods cannot be related.

Given: class C extends B{ private int h = 44; int getH() {System.out.println("getH()"); return h; } public static void main(String[] args) { C c = new C(); System.out.println(c.h+" "+c.getH()); }} What is the result? (Choose all that apply) a) compile and print: getH() 44 44 b) compile and print: 44 getH() 44 c) compile and print: 44 44 getH() d) none of the above

a) is correct. The program will compile and print: getH() 44 44 This question tests your knowledge on the Operator Order of Precedence (Rules of Precedence). This is very important to learn for the exam. See note below. ===Code Step Through=== 1: class A { 2: int h = 44; 3: int getH() { 4: System.out.println("getH()"); 5: return h; 6: } 7: public static void main(String[] args) { 8: C c = new C(); 9: System.out.println(c.h+" "+c.getH()); 10: }} On the line 8, a new object of type C is created. ========2======== 1: class A { 2: int h = 44; 3: int getH() { 4: System.out.println("getH()"); 5: return h; 6: } 7: public static void main(String[] args) { 8: C c = new C(); 9: System.out.println(c.h+" "+c.getH()); 10: }} On the line 9, there is an expression which is comprised of: int + string object + method Where: int represents c.h string object represents " " method represents c.getH() The question is: where do we start the execution of this expression? Based on the rules of precedence, methods gets executed first. Therefore: c.getH() gets executed first ========3======== 1: class A { 2: int h = 44; 3: int getH() { 4: System.out.println("getH()"); 5: return h; 6: } 7: public static void main(String[] args) { 8: C c = new C(); 9: System.out.println(c.h+" "+c.getH()); 10: }} On the line 3, the method getH() is entered. On the line 4, "getH()" is printed to the screen. On the line 5, the contents of variable h are returned to the main method i.e. int 44 is returned. The following is a snapshot of the expression after line 5 has executed Expression Snapshot: "getH()" c.h+" "+44 ========4======== 1: class A { 2: int h = 44; 3: int getH() { 4: System.out.println("getH()"); 5: return h; 6: } 7: public static void main(String[] args) { 8: C c = new C(); 9: System.out.println(c.h+" "+c.getH()); 10: }} On the line 9, c.h gets appended to string: Expression Snapshot: getH() 44+" "+44 Then the string expression is evaulated left to right This results in: "44 44" Final output: getH() 44 44 Note: Operator Order of Precedence This question tests your knowledge on the Operator Order of Precedence (Rules of Precedence). This is very important to learn for the exam. Expressions, Operators and Operands: Firstly, it's important to explain expressions, operators and operands. Below is an example of an expression. 1 + 2 + "3" Expressions are comprised of operands(e.g. numbers and/or objects). Below is an outline of operands in an expression i.e. 1,3 and the String object "3": 1 + 2 + "3" Expressions are also comprised of operators (e.g. +, -, %, /, etc). Below is an outline of the operators in the question: 1 + 2 + "3" The Problem: Where does an expression (equation) start when executed? On the left-hand side, right hand side or somewhere else in the equation? Java has well-defined rules for solving the above problem. Solution The Operator Order of Precedence Rules solves this. Below outlines 16 rules in order of precedence. The rules are as follows where: Rule 1 has the highest priority operator in the expression (equation) execute first. Rule 2 has the second highest priority operator in an expression executed next and so forth. Below also outlines the 16 rules in relation to associativity. Associativity: Associativity is important when an expression has more than one operator of the same kind (e.g. 3 - 2 - 1 where '-' is the minus symbol). Do we start with subtracting 2 from 3 or do we start with subtracting 1 from 2 first? The order of precedence provides the answer to this also. It would be advisable to memorise the operator order of precedence with the following suggested 16 rule abbreviation (write this down in the exams as a reference): B-PUC-MAS-REBBLLTA Where: B = Brackets P = Postfix i++ etc. Operator Order of Precedence: 1 Brackets e.g. () [] method(); (Associativity: Starting left to right) 2 Postfix e.g. i++, i-- (Associativity: Starting left to right) 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 5 Multiplication, Division, mod % e.g. *, /, % (Associativity: Starting left to right) 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) 7 Shift e.g. >>, <<, >>> (Associativity: Starting left to right) 8 Relational e.g. <, <=, >, >= instanceof (Associativity: Starting left to right) 9 Equality e.g. ==, != (Associativity: Starting left to right) 10 Bitwise AND, & (Associativity: Starting left to right) 11 Bitwise XOR ^ (Associativity: Starting left to right) 12 Bitwise OR | (Associativity: Starting left to right) 13 Logical AND && (Associativity: Starting left to right) 14 Logical OR || (Associativity: Starting left to right) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) It would also be prudent to memorise for the exam the following associativity rules which executes right to left instead of the majority left-to-right as outlined above: U-CAST-TA 3 Unary e.g. ++i, --j (Associativity: Starting right to left) 4 Cast or object creation e.g. (int), newObject() (Associativity: Starting right to left) 15 Tiernary e.g. (boolean)?varA:varB; (Associativity: Starting right to left) 16 Assignment e.g. = =, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= (Associativity: Starting right to left) The Operator Order of Precedence can be explained as with the following example: System.out.println(1+2+"3"); 1 + 2 + "3" is an expression with two operators that are the same (i.e. +) Rule 6 applies to the above expression: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) with associativity starting from left to right 1+2+"3" 3+"3" After that, the expression is comprised of the digit 3 and the String object containing "3" outlined above. According to the rules of precedence again: 6 Addition subtraction e.g. +, - (Associativity: Starting left to right) Therefore the digit 3 gets appended to the string object "3". This results in the final output: "33"

Given: interface A { int k = 10; } public class B implements A { public B(){ k=20; System.out.println(k); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 20 d) none of the above

a) is correct. The program will give a compiler error. interface A { int k = 10; } public class B implements A { public B(){ k=20; System.out.println(k); } } The variable 'k' is a constant declared within the interface A. The class B is attempting to reassign a literal value of the constant variable k (declared in the interface). This causes a compiler error as constant values cannot be changed (reassigned). Note: As all variables declared in an interface are constants, they implicitly have public static final before their variable declaration.

Given: abstract class A { protected void m() { System.out.println("A"); }} public class B extends A{ void m() { System.out.println("B"); }} class C { public static void main(String[] args) { A b = new B(); b.m(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print B d) none of the above

a) is correct. The program will give a compiler error. 1: abstract class A { 2: protected void m() { 3: System.out.println("A"); 4: } } 5: public class B extends A{ 6: void m() { 7: System.out.println("B"); 8: } } 9: class C { 10: public static void main(String[] args) { 11: A b = new B(); 12: b.m(); 13: }} On the line 12, the reference b attempts to call method m() in class B but this overriding method, in class B, has decreased accessibility (default) in comparison to superclass A method m(). This therefore this gives a compiler error. Remember: ➤ Increasing Accessibility: private → default → protected → public ➤ Decreasing Accessibility: private ← default ← protected ← public Note: Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same package and so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclass and world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is public would be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Given: class A { static { System.out.println("Static Block"); System.out.println(b); } public static void m(){ System.out.println("Static method"); } private static int b = 1; public static void main(String[] args) { A a = new A(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print "Static Block" "Static method" d) none of the above

a) is correct. The program will give a compiler error. This question tests your knowledge of the Order of Initialization at class loading time. 1: class A { 2: static { 3: System.out.println("Static Block"); 4: System.out.println(b); 5: } 6: public static void m(){ 7: System.out.println("Static method"); 8: } 9: private static int b = 1; 10: public static void main(String[] args) { 11: A a = new A(); 12: }} On the line 2, a static block gets created before the static variable 'b' On the line 4, the static block is attempting to call the static variable 'b' but this gives a compiler error as the compiler cannot find the variable 'b'. There is a sequential class loading order for when the internals of a object are created and stored in memory: Class Loading Order: ➤ 1 static block stored in memory ➤ 2 static method stored in memory ➤ 3 static block executed ➤ 4 static variables stored in memory ➤ 5 super() or this() called. The above class loading order can be explained as follows: ➤ Firstly, the static blocks are stored in memory. ➤ Secondly, the static methods are stored in memory. ➤ Thirdly, static block gets executed and so forth... If the static variable was called inside the static method m() the code would compile and run fine.

Given: public class A { { System.out.println("Non static block A"); } static { System.out.println("static block A"); } } public class B extends A{ static { System.out.println("static block B"); } { System.out.println("Non static block B"); } public static void main(String[] args){ B b = new B(); }} What is the output of the above code? What is the result? (Choose all that apply) a) static block A static block B Non static block A Non static block B b) Non static block A Non static block B static block A static block B c) static block A Non static block A Non static block B static block B d) none of the above

a) is correct. The program will output: static block A static block B Non static block A Non static block B This question tests your knowledge of static and nonstatic initialization blocks. Static Initialization Blocks The Java language includes static initialization blocks. Think of it as a constructor for static variables. Static initialization blocks gives you a chance to initialize the variables before anyone uses them. static{} block is executed when the class is loaded. Within the static parenthesis {} 'block', there would be the static code (usually static variables) that would be the same for all instances. Note: Starting at the top of the class hierarchy, all static blocks get executed first in sequential order down the class hierarchy. This is followed by the same procedure for nonstatic blocks. Nonstatic Initialization Blocks Similar to static blocks, Nonstatic blocks are simply parenthesis '{}' which would also contain code: { } The non-static initialization block would contain code (usually instance variables) that would need to be initialized first when an object is instantiated. non-static blocks of code can access the instance variables of an object. This is not the same for static blocks of code. static blocks are stored in class memory. This means that the code within the static block can never be changed when the application executes. This saves memory and makes the code more efficient. Nonstatic blocks are stored in a heap memory Let's step through the code: ===Code Step Through=== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A{ 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: }} On the line 17, a new object of type B is instantiated. ========2======== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A{ 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: }} On the line 17, the B constructor will first automatically call the superclass A constructor using implicitly super(). Class A does not have an explicit constructor so the default constructor is provided implicitly. ========3======== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A{ 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: }} On the line 5, inside the superclass constructor A(){} calls static block first which outputs to the screen: static block A ========4======== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A { 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: } } The the next static block down the code is called. This outputs to the screen: static block A static block B ========5======== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A{ 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: }} After all static blocks have been executed, the nonstatic blocks get executed next from the top down in the class hierarchy. This outputs next: static block A static block B Non static block A ========6======== 1: public class A { 2: { 3: System.out.println("Non static block A"); 4: } 5: static { 6: System.out.println("static block A"); 7: } 8: } 9: public class B extends A { 10: static { 11: System.out.println("static block B"); 12: } 13: { 14: System.out.println("Non static block B"); 15: } 16: public static void main(String[] args){ 17: B b = new B(); 18: } } The next nonstatic block in the class hierarchy is executed which is located between the lines 13 and 15. After the execution of the above code, the following is a final snapshot of what is outputted to the screen: static block A static block B Non static block A Non static block B

Given: interface A { void mA(); } class B implements A{ public void mA(){ System.out.println("m() in B"); }} class C extends B{ public void mA() {System.out.println("mA() in C"); } public static void main(String[] args) { B b = new B(); C c = new C(); c = (B)(A) b; b.mA(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print mA() in C d) none of the above

a) is correct. interface A { void mA(); } class B implements A{ public void mA(){ System.out.println("m() in B"); }} class C extends B{ public void mA() {System.out.println("mA() in C"); } public static void main(String[] args) { B b = new B(); C c = new C(); c = (B)(A)c; b.mA(); }} Double Casting Taking a code snippet from above: c = (B)(A)c; Reference 'b' is referring to object B and is upcast to type A. This is fine as class B implements interface A and are therefore "related". Even though it's cast to an interface, the compiler still knows it is an object of type C. Reference 'b' is then casted to type B. This is fine as class B is of type B - obviously. We now have a reference of type C pointing to the object of type B. This gives a compiler error as class C is not a superclass of B. (B is a superclass of C) Note: double casting, as the name suggests, is where there are two casts on a reference

Given: public class A { private int i=10; } public class B extends A{ protected int i = 20; } class C { public static void main(String[] args) { A b = new B(); System.out.println(b.i); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 20 d) none of the above

a) is correct. The program will give a compiler error. Let's examine the program: public class A { private int i =10; } public class B extends A{ protected int i = 20; } class C { public static void main(String[] args) { A b = new B(); System.out.println(b.i); }} Within class C, 'b.i' intends to call the instance variable i in the reference class A. As this instance variable i is private it will give a compiler error. If the instance variable i was public or protected then it would print 10. As the reference to the object instance B is of type A, then the call to the instance variable i will be in the superclass and not the class B of the instance. Note: Inheritance Access Control: The inheritance access modifier table is essential in helping to understand access control. This is the fundamental of aspect of object-oriented programming. The need to know rule of the contents of classes i.e. instance variables/methods. e.g. A private method or instance variable can be accessed only within the class where it was created. A default method or instance variable declared in a class can be accessed within its own class or within other classes of the same package and so forth. The following table would be best memorized and quickly written out on paper during the exam: class | package | subclass | world private _Yes_ | _No_ | _No_ | _No_ default _Yes_ | _Yes_ | _No_ | _No_ protected Yes | _Yes_ | _Yes_ | _No_ public _Yes | _ Yes_ | _Yes_ | _ Yes_ No: Where the subclass is in a different package, otherwise Yes Interpreting the above table would be as follows: The column containing private, default, protected or public would represent the level of access for a method or instance variable of a class. The rows containing class, package, subclass and world would represent where a particular method or instance variable can be used/called (depending on its access modifier of being private, default, protected or public). e.g. A method/instance variable that is private can be used in a class but cannot be used in a package, subclass or the world (the world being any other class attempting to access that particular method/instance variable in the running program). On the other extreme, a method/instance variable that is public can be used in a class, package, subclass or the world (the world being any other class attempting to access that particular method/instance variable). A method/instance variable that is public would be contrary to the Principles of Object Orientated Programming as any class could change the said public method or instance variable thereby significantly increasing risks of code having bugs and making code almost impossible to maintain.

Which of the following is true? (Choose all that apply) a) 'this' reference is not available within static method otherwise it will cause compiler error b) 'this' reference is available within static method otherwise compiler error c) 'this' reference must be the first line in a constructor otherwise compiler d) none of the above

a) is true. a) 'this' reference is not available within static method otherwise it will cause compiler error. The following is false c) 'this' reference must be the first line in a constructor otherwise compiler. Note: Although not one of the questions, the following is true: this() method must be the first line in a constructor otherwise compiler

A constructor cannot be which of the following? (Choose all that apply) a) final b) static c) abstract d) none of the above

a), b) and c) are correct. A constructor cannot be final, static or abstract as its function is to create instances of the class.

Given: class A { public String mA() { return "mA()"; }} class B extends A{ public String mA() { return "mB()"; } public static void main(String[] args) { A a = new B(); System.out.println(a.mA()); }} What is the result? (Choose all that apply) a) This is an example of method overloading b) This is an example of method overriding c) compile and print mB() d) none of the above

b) and c) are correct. This is an example of method overriding (which occurs at runtime). The output of this program would be: 'mB()' An overriding method has the exact same: ➤ Method name ➤ Parameter list ➤ Return type class A { public String mA( ) { return "mA()"; } } class B extends A{ public String mA( ) { return "mB()"; } public static void main(String[] args) { A a = new B(); System.out.println(a.mA()); }}

Which of the folllowing are true statements? (Choose all that apply) a) "type of" indicates an "has a" relationship b) "have" indicates an "has a" relationship c) "type of" indicates an "is a" relationship d) none of the above

b) and c) are true. ➤ b) "have" indicates "has a" relationship i.e. instance variable e.g. Airplane class has a Landing_Gear class ➤ c) "type of" indicates "is a" relationship i.e. extends or implements e.g. Whale class "is a" type of Mammal Class

The following is a directory tree containing the program X.java: /root/com/foo/X.java X.java contains a static variable named LOGICID The following is a directory tree containing the program Y.java: /root/com/bar/Y.java Y.java wants to use LOGICID Which of the following, when placed in program Y, will compile and run fine? (Choose all that apply) a) import static X; b) import static com.foo.X.*; c) import static com.foo.*; d) import static com.foo.X.LOGICID;

b) and d) are correct. ➤ b) import static com.foo.X.*; The above will import all static members of class X ➤ d) import static com.foo.X.LOGICID; The above will import specific static member LOGICID of class X The Following Will Not Compile: c) import static com.foo.*; The above is attempting to import all static classes within a folder. This gives a syntax (compiler) error. a) import static X; In the above, it is attempting to import a class statically. This gives a compiler error as you cannot import a class statically.

Given: class A { {System.out.println("init block A"); } public A() { System.out.println("contructor A"); } static { System.out.println("static block A"); }} class B extends A{ static { System.out.println("static block B"); } public B() { System.out.println("Constructor B"); } {System.out.println("init block B"); }} class C extends B{ public static void main(String[] args) { System.out.println("Main"); new C(); System.out.println("End C"); }} List, in sequential order, all of the outputs that get printed when the above code gets run.

b) is correct. The following is printed in sequence: static block A static block B Main init block A contructor A init block B Constructor B End C It would be imperative to know the class loading sequence order for the exam: Class Loading and Sequence #1 Load all classes related to C starting from superclass A and working down. #2 Load all static initiation blocks in sequence order starting from the superclass A #3 Call the main() method #4: Call chain of super() up the class hierarchy to the top class. #5: For the current class that is entered: #5.1: Load all instances of that particular class. #5.2: Call the contructor for each class ===Code Step Through=== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: } } 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: } } 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} Based on the Class Loading Sequence: Load all static initiation blocks of the code get loaded in sequential order starting from the superclass A. On the line 8, "static block A" gets printed to the screen. On the line 12, "static block B" gets printed to the screen. The following is the output printed to the screen this far: static block A static block B ========2======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: }} 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: }} 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} On the line 21, "Main" is printed to the screen. Current Print Stream: static block A static block B Main ========3======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: }} 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: }} 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} The code flows to line 22 which creates a new object of type C. It has no references to it. According to rule 4 of the Class Loading Sequence #4: Call a chain of super() in the class hierarchy starting at the top class. The default constructor of class C is automatically called. Inside this a default constructor, super() is called implicitly. This then jumps to the highest class in the program, namely, class A. ========4======== 1: class A { 2: { System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: } } 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: }} 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} On the line 1, class A is entered. On the line 2, the initialization block of class A is executed resulting in "init block A" printed to the screen. Note: At this point, all instance variables, instance methods and non-static initialization blocks are initialized (allocated addresses in stack memory by the JVM) for this particular class A The following is the output printed to the screen thus far: Current Print Stream: static block A static block B Main init block A ========5======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: } } 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: }} 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} On the line 5, the constructor of the current class (class A) is executed resulting in "contructor A" printed to the screen. Current Print Stream: static block A static block B Main init block A contructor A ========6======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: }} 10: class B extends A { 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: { System.out.println("init block B"); 18: } } 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} On the line 10, class B is entered. On the line 17, the initialization block of class B is executed resulting in "init block B" getting printed to the screen. Note: At this point, all instance variables, instance methods and non-static initialization blocks are initialized (allocated addresses in stack memory by the JVM) for this particular class B. The following is the output printed to the screen thus far: Current Print Stream: static block A static block B Main init block A contructor A init block B ========7======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: }} 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: } } 19: class C extends B{ 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: }} On the line 15, the constructor of the current class (class B) is executed resulting in "Constructor B" printed to the screen. The following is the current output printed to the screen thus far. Current Print Stream: static block A static block B Main init block A contructor A init block B Constructor B ========8======== 1: class A { 2: {System.out.println("init block A"); 3: } 4: public A() { 5: System.out.println("contructor A"); 6: } 7: static { 8: System.out.println("static block A"); 9: }} 10: class B extends A{ 11: static { 12: System.out.println("static block B"); 13: } 14: public B() { 15: System.out.println("Constructor B"); 16: } 17: {System.out.println("init block B"); 18: }} 19: class C extends B { 20: public static void main(String[] args) { 21: System.out.println("Main"); 22: new C(); 23: System.out.println("End C"); 24: } } On the line 23, the output "End C" is printed to the screen. The following is the final output printed to the screen thus far. Current Print Stream: static block A static block B Main init block A contructor A init block B contructor B End C

Given: public class A { public A getInstance() { return new A(); } public A(){} } What is true about the above? (Choose all that apply) a) There are two constructors in the above code b) There is one constructor in the above code c) It will give the compiler error.

b) is correct. There is one constructor in the code: 1: public class A { 2: public A getInstance() { 3: return new A(); 4: } 5: public A(){} 6: } On the line 5, is the default constructor. On the line 2, is the getInstance() method to an instance of an object and not the constructor

Given: interface RemoteControl1 { int curChannel=1; public void turnOnTV(); } interface RemoteControl2 { int curChannel=2; public void turnOnTV(); } class RemoteControler implements RemoteControl1, RemoteControl2 { //insert code// } What code inserted above will cause it to compile and run fine? (Choose all that apply) a) public void turnOnTV(){} public void turnOnTV(){} b) public void turnOnTV(); c) public void turnOnTV<RemoteControl1>(){} public void turnOnTV()<RemoteControl2>{} d) none of the above

b) is correct. The program was compiled with the following method inserted: public void turnOnTV(); Let's look at the full program below: 1: interface RemoteControl1 2: { int curChannel=1; 3: public void turnOnTV(); 4: } 5: interface RemoteControl2 6: { int curChannel=2; 7: public void turnOnTV(); 8: } 9: class RemoteControler implements RemoteControl1, RemoteControl2 { 10: public void turnOnTV(){} 11: } On the line 10, the turnOnTV() method is required to be implemented for two interfaces which is perfectly fine. The Following Will Not Compile: a) public void turnOnTV(){} public void turnOnTV(){} The above will give a compiler error as you cannot implement two methods with the same name and parameter signature c) public void turnOnTV<RemoteControl1>(){} public void turnOnTV()<RemoteControl2>{} The above will give syntax errors

Which of the following are true? (Choose all that apply) a) 'unsigned' is a keyword in Java b) When referencing a class from a different package, you must specify the full name of the package. c) 'exit' is a keyword name and not the method d) A constructor cannot access static members

b) is true. b) When referencing a class from a different package, you must specify the full path of the package. e.g. packageA.packageB.MyClass The Following Are Not True: ➤ a) 'unsigned' is a keyword in Java. The above is incorrect as unsigned is a not keyword in Java. ➤ c) 'exit' is a keyword name and not the method. The above is incorrect as exit() is a method name and not a java keyword. ➤ d) A constructor cannot access static members. The above is incorrect as a constructor can access static members

Given: public class A { public void m() { System.out.println("A"); }} public class B extends A{ public void m() { System.out.println("B"); }} class C { public static void main(String[] args) { A b = new B(); b.m(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print B d) none of the above

c) is correct The program will compile and print B. public class A { public void m() { System.out.println("A"); } } public class B extends A { public void m() { System.out.println("B"); } } class C { public static void main(String[] args) { A b = new B(); b.m(); }} In the main method of class C, the call b.m() prints the overriden method in the subclass B. Even though the reference 'b' is of type A, it is the actual object, which is of type B, that has its method m() called. This is important to remember.

Given: interface Fruit { public void grow(); } interface Apple extends Fruit { public void growApple(); } public class TestInterface implements Apple{ //Insert Code// } What code, inserted in TestInterface will enable it to compile? (Choose all that apply) a) 'grow(){}' only. b) 'growApple(){}' only. c) 'grow(){}' and 'growApple(){}' d) none of the above

c) is correct. The following code inserted in Test Interface with the name and the program to compile: 'grow(){}' and 'growApple(){}' Below you will see the full code implementation for this question: interface Fruit { public void grow(); } nterface Apple extends Fruit { public void growApple(); } public class TestInterface implements Apple{ public void grow(){} public void growApple(){} } TestInterface must implement all methods in interface Apple and its superclass Fruit

Given: class A { int k =1; } class B extends A { int k =2; } class C extends B{ int k =3; public static void main(String[] args) { C c = new C(); System.out.println(((A)c).k); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 1 d) compile and print 2 e) compile and print 3 f) none of the above

c) is correct. The program will compile and print : 1 1: class A { 2: int k = 1; 3: } 4: class B extends A { 5: int k =2; 6: } 7: class C extends B{ 8: int k =3; 9: public static void main(String[] args) { 10: C c = new C(); 11: System.out.println(((A)c).k); 12: }} On the line 11, the c reference, which is casted as A, will retrieve the the variable k in class A on the line 2 and not the variable the sub class C.

Given: class A { private static int h = 1; public static void main(String[] args) { int h=2; System.out.println(h); }} What is the result? (Choose all that apply) a) compiler error b) compile and print 1 c) compile and print 2 d) none of the above

c) is correct. The program will compile and print: 2 This program gives an example of a variable shadow. Let's look at the code below to explain. 1: class A { 2: private static int h = 1; 3: public static void main(String[] args) { 4: int h=2; 5: System.out.println(h); 6: }} Variable Shadowing: As stated, the program will print 2, the local variable in the main(). The static variable h, on the line 2, has the same name as the local variable h inside main() on the line 4. This local variable h is shadowing static variable h. Note: The static variable and a local variable of the same name are two completely different variables which store completely different values.

Given: class A { public Object m() { return "A"; }} abstract class B extends A { public abstract String m(); } class C extends B{ public String m() { return "C"; } public static void main(String[] args) { A a = new C(); Object s = a.m(); System.out.println(s); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print C d) none of the above

c) is correct. The program will compile and print: C ===Code Step Through=== 1: class A { 2: public Object m() { 3: return "A"; 4: }} 5: abstract class B extends A { 6: public abstract String m(); 7: } 8: class C extends B{ 9: public String m() { 10: return "C"; 11: } 12: public static void main(String[] args) { 13: A a = new C(); 14: Object s = a.m(); 15: System.out.println(s); 16: }} On the line 13, a new object of type C is created. It has a reference 'a' which is of type A. As class C is a subclass of A then this is fine. ========2======== 1: class A { 2: public Object m() { 3: return "A"; 4: }} 5: abstract class B extends A { 6: public abstract String m(); 7: } 8: class C extends B{ 9: public String m() { 10: return "C"; 11: } 12: public static void main(String[] args) { 13: A a = new C(); 14: Object s = a.m(); 15: System.out.println(s); 16: }} On the line 14, the method m() is called. ========3======== 1: class A { 2: public Object m() { 3: return "A"; 4: }} 5: abstract class B extends A { 6: public abstract String m(); 7: } 8: class C extends B{ 9: public String m() { 10: return "C"; 11: } 12: public static void main(String[] args) { 13: A a = new C(); 14: Object s = a.m(); 15: System.out.println(s); 16: } } On the line 9, the m() method is entered. On line 10, string "C" gets returned to the main() method. Note: Even though the reference variable is of type A, its the method within the object that gets called (object of type C) and NOT the m() method within A i.e. method m() within C class gets called. ========4======== 1: class A { 2: public Object m() { 3: return "A"; 4: }} 5: abstract class B extends A { 6: public abstract String m(); 7: } 8: class C extends B{ 9: public String m() { 10: return "C"; 11: } 12: public static void main(String[] args) { 13: A a = new C(); 14: Object s = a.m(); 15: System.out.println(s); 16: }} On the line 14, a reference named 's', which is of type Object , stores the return value of the method m() i.e. the string C Note: Object is the superclass of all classes, including String objects. ========5======== 1: class A { 2: public Object m() { 3: return "A"; 4: }} 5: abstract class B extends A { 6: public abstract String m(); 7: } 8: class C extends B{ 9: public String m() { 10: return "C"; 11: } 12: public static void main(String[] args) { 13: A a = new C(); 14: Object s = a.m(); 15: System.out.println(s ); 16: }} On the line 15, the value stored within the variable 's' is printed to the screen i.e. "C" Note: If C doesnt implement m() then compiler error

Given: class A { int k =2; void p(){ System.out.println("A"); }} class B extends A { void p(){ System.out.println("B"); }} class C extends B{ void p(){ System.out.println("C"); } void test(){ C c = new C(); c.p(); super.p(); } public static void main(String[] args) { C c = new C(); c.test(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print C and B d) none of the above

c) is correct. The program will compile and print: C and B ===Code Step Through=== 1: class A { 2: int k =2; 3: void p(){ 4: System.out.println("A"); 5: }} 6: class B extends A { 7: void p(){ 8: System.out.println("B"); 9: }} 10: class C extends B{ 11: void p(){ 12: System.out.println("C"); 13: } 14: void test(){ 15: C c = new C(); 16: c.p(); 17: super.p(); 18: } 19: public static void main(String[] args) { 20: C c = new C(); 21: c.test(); 22: }} On the line 20, a new object is created of type C and with reference 'c'. Then, on line 21, the method test() is called. ========2======== 1: class A { 2: int k =2; 3: void p(){ 4: System.out.println("A"); 5: }} 6: class B extends A { 7: void p(){ 8: System.out.println("B"); 9: }} 10: class C extends B{ 11: void p(){ 12: System.out.println("C"); 13: } 14: void test(){ 15: C c = new C(); 16: c.p(); 17: super.p(); 18: } 19: public static void main(String[] args) { 20: C c = new C(); 21: c.test(); 22: }} On the line 14, the test() method enters. =======3========= 1: class A { 2: int k =2; 3: void p(){ 4: System.out.println("A"); 5: }} 6: class B extends A { 7: void p(){ 8: System.out.println("B"); 9: }} 10: class C extends B{ 11: void p(){ 12: System.out.println("C"); 13: } 14: void test(){ 15: C c = new C(); 16: c.p(); 17: super.p(); 18: } 19: public static void main(String[] args) { 20: C c = new C(); 21: c.test(); 22: }} On the line 15, a new object is created of type C and with a reference 'c' ========4======== 1: class A { 2: int k =2; 3: void p(){ 4: System.out.println("A"); 5: }} 6: class B extends A { 7: void p(){ 8: System.out.println("B"); 9: }} 10: class C extends B{ 11: void p(){ 12: System.out.println("C"); 13: } 14: void test(){ 15: C c = new C(); 16: c.p(); 17: super.p(); 18: } 19: public static void main(String[] args) { 20: C c = new C(); 21: c.test(); 22: }} On the line 16, the method p() is called. The code jumps to line 11, and the p() method is entered and on the line 12, "C" is outputted to the screen Current Output Stream: "C" ========5======== 1: class A { 2: int k =2; 3: void p(){ 4: System.out.println("A"); 5: }} 6: class B extends A { 7: void p(){ 8: System.out.println("B"); 9: } } 10: class C extends B{ 11: void p(){ 12: System.out.println("C"); 13: } 14: void test(){ 15: C c = new C(); 16: c.p(); 17: super.p(); 18: } 19: public static void main(String[] args) { 20: C c = new C(); 21: c.test(); 22: }} The code then jumps to the line 17. The p() method is called in the superclass of class C (the superclass of C is class B). The code jumps to line 7, and p() method is entered. On line at 8, "B" is printed to the screen. Current Output Stream: "C" "B" Note: If there is no p() in class C then it will attempt to call p() in class B.

Given: interface Fruit { public void grow(); } interface Apple extends Fruit { public void growApple(); } abstract class TestInterface extends Apple{ public void growApple(String anApple){ System.out.println("Growing apple "+ anApple);} } What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and run fine d) none of the above

c) is correct. The program will compile and run fine. interface Fruit { public void grow(); } interface Apple extends Fruit { public void growApple(); } abstract class TestInterface extends Apple{ public void growApple(String anApple){ System.out.println("Growing apple "+ anApple);} } Any subclass of this abstract TestInterface class must implement Apple and Fruit interface methods. Note: The abstract class TestInterface contains the method growApple(String anApple) which has a parameter. The interface Apple contains a method with the same name growApple() but without parameters. As a consequence of TestInterface extending Apple, the method within the TestInterface class with the name growApple(String anApple) is an overloaded method

Which of the following are true? (Choose all that apply) a) only one of this(..) or super(..) can be called in the first line of a constructor b) this(..) and super(..) can be called together in the first line of a constructor c) this(..) and super(..) cannot be called together d) none of the above

c) is correct. this(..) and super(..) cannot be called together in class this(..) or super(..) can only be called in first line of constructor. Where the '...' represents arbitrary parameters into the said constructor or is empty with a default constructor.

Given: class Apple { Apple(String aType) { this.type=aType; } private String aType; } Which of the following will compile: (Choose all that apply) a) class CoxApple extends Apple { } b) class GrannyApple extends Apple { GrannyApple(){ } } c) class GoldenApple extends Apple { GoldenApple(String aType){ System.out.println("Type "+aType); }} d) class Pear { Pear (){ } Pear (String aType){ System.out.println("Type "+aType); } } e) none of the above

d) is correct class Apple { Apple(String aType) { this.type=aType; } private String aType; } class Pear { Pear(){ } Pear (String aType){ System.out.println("Type "+aType); } } Pear doesn't extend Apple therefore compiles and runs fine The Following Will Not Compile: a) class Apple { Apple(String aType) { this.type=aType; } private String aType; } class CoxApple extends Apple { } Default constructor is implicitly inserted with super() i.e. class CoxApple extends Apple { CoxApple() { super(); } } In order to call the default constructor of a class, all the superclasses of the class must also have a default constructor. Apple doesn't have default constructor therefore compiler error. b) class Apple { Apple(String aType) { this.type=aType; } private String aType; } class GrannyApple extends Apple { GrannyApple(){ } } Likewise, super() is implicitly inserted on the first line of the constructor GrannyApple which attempts to call Apple default constructor. Apple doesn't have default constructor a therefore compiler error occurs class GrannyApple extends Apple { GrannyApple(){ super(); } } c) class Apple { Apple(String aType) { this.type=aType; } private String aType; } class GrannyApple extends Apple{ GrannyApple(String aType){ System.out.println("Type "+aType); } } super() implicitly inserted but there is no default constructor in Apple class therefore compiler error Solution Place default constructors in the Apple class e.g. class Apple { Apple(String aType) { this.type=aType; } Apple(){} private String aType; }

Given: class A { public A() { p(); } void p(){ System.out.println("A"); }} abstract class B extends A { void p(){ System.out.println("B"); } public static void main(String[] args) { A a = new A(); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print B d) none of the above

d) is correct. None of the answers are correct, it will print: A. A concrete class can be extended by an abstract class. ===Code Step Through=== 1: class A { 2: public A() { 3: p(); 4: } 5: void p(){ 6: System.out.println("A"); 7: }} 8: abstract class B extends A { 9: void p(){ 10: System.out.println("B"); 11: } 12: public static void main(String[] args) { 13: A a = new A(); 14: }} On the line 13, the new object of type A is created with a reference 'a'. =======2========= 1: class A { 2: public A() { 3: p(); 4: } 5: void p(){ 6: System.out.println("A"); 7: }} 8: abstract class B extends A { 9: void p(){ 10: System.out.println("B"); 11: } 12: public static void main(String[] args) { 13: A a = new A(); 14: }} The program then jumps line 2, the default constructor of class A. On the line 3, the method p() is called. ========3======== 1: class A { 2: public A() { 3: p(); 4: } 5: void p(){ 6: System.out.println("A"); 7: } } 8: abstract class B extends A { 9: void p(){ 10: System.out.println("B"); 11: } 12: public static void main(String[] args) { 13: A a = new A(); 14: }} The program then jumps to line 5 and enters the method p(). Inside the method p() on the line 6 it prints: A Note: class A overrides the method p() from the abstract class B.

Given: public class A { public A(){ System.out.println("A1"); } public A(int i){ this(); System.out.println("A2"); } public static void main(String[] args) { A a = new A(1); }} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print A1 only d) none of the above

d) is correct. None of the answers are correct. It will print A1 and A2 Let's step through the program: ===Code Step Through=== 1: public class A { 2: public A(){ 3: System.out.println("A1"); 4: } 5: public A(int i){ 6: this(); 7: System.out.println("A2"); 8: } 9: public static void main(String[] args) { 10: A a = new A(1); 11: } } On line 10, a new object of type A gets created with a reference 'a'. ========2======== 1: public class A { 2: public A(){ 3: System.out.println("A1"); 4: } 5: public A(int i){ 6: this(); 7: System.out.println("A2"); 8: } 9: public static void main(String[] args) { 10: A a = new A(1); 11: }} On the line 6, the this() is called which jumps to A() on line 2 ========3======== 1: public class A { 2: public A(){ 3: System.out.println("A1"); 4: } 5: public A(int i){ 6: this(); 7: System.out.println("A2"); 8: } 9: public static void main(String[] args) { 10: A a = new A(1); 11: }} On the line 2, the default constructor is called which results in the following getting printed to the output stream: A1 ========4======== 1: public class A { 2: public A(){ 3: System.out.println("A1"); 4: } 5: public A(int i){ 6: this(); 7: System.out.println("A2"); 8: } 9: public static void main(String[] args) { 10: A a = new A(1); 11: }} The code then jumps back into the non-default constructor and executes line 7. A2 is outputted to the screen. Final Output: A1 and A2

Given: class A { A() { m(); } static void m(){ System.out.println("m() in A"); }} class B extends A{ static void m(){ System.out.println("m() in B"); } public static void main(String[] args) { A a = new B(); a.m(); }} What is the result? a) compiler error b) runtime error c) compile and print: m() in A m() in B d) none of the above

d) is correct. None of the answers are correct. The program will compile and print: m() in A m() in A ===Code Step Through=== 1: class A { 2: A() { 3: m(); 4: } 5: static void m(){ 6: System.out.println("m() in A"); 7: }} 8: class B extends A{ 9: static void m(){ 10: System.out.println("m() in B"); 11: } 12: public static void main(String[] args) { 13: A a = new B(); 14: a.m(); 15: }} On the line 13, a new object B is created with a reference named 'a' which is of type A (a superclass of B). As the B object has no explicit constructor decleared in the class B, the compiler calls its default constructor, namely, B(). Inside the default constructor of class B there is an implicit super() called on the first line of the constructor. This super() automatically calls a chain of constructors starting at the top of the class hierarchy and moving down to class B. Note: As there are only two classes in this code example, the superclass is class A. However, if there was more than two classes in the class hierarchy, as stated previously, then the default constructor at the very top of the class hierarchy would be called first. ========2======== 1: class A { 2: A() { 3: m(); 4: } 5: static void m(){ 6: System.out.println("m() in A"); 7: } } 8: class B extends A{ 9: static void m(){ 10: System.out.println("m() in B"); 11: } 12: public static void main(String[] args) { 13: A a = new B(); 14: a.m(); 15: } } Following the call of the default constructor in class B (i.e. B()) on line 13, the superclass default constructor of class A get called, which is is entered on line 2. ========3======== 1: class A { 2: A() { 3: m(); 4: } 5: static void m(){ 6: System.out.println("m() in A"); 7: } } 8: class B extends A{ 9: static void m(){ 10: System.out.println("m() in B"); 11: } 12: public static void main(String[] args) { 13: A a = new B(); 14: a.m(); 15: }} On the line 3, a m() is called which is referring to the static m() in class A. On the line 6, 'm() in A' is printed to the screen. Note: As m() is the static method in both A and B, the m() in B is shadowing and NOT overriding the m() in A. If both methods m() were non-static, then m() in B would get executed as it would be the overriding method. If one of the methods m() were non-static and the other static, this would give a compiler error. This is because you cannot have an instance method related to a static method. ========4======== 1: class A { 2: A() { 3: m(); 4: } 5: static void m(){ 6: System.out.println("m() in A"); 7: } } 8: class B extends A{ 9: static void m(){ 10: System.out.println("m() in B"); 11: } 12: public static void main(String[] args) { 13: A a = new B(); 14: a.m(); 15: }} On the line 14, a call is made to the method m(). As the reference named 'a' is of type A, the static method m() located class A in gets executed. On the line 6, 'm() in A' is printed to the screen. Therefore, the total output to the print stream would be: m() in A m() in A Note: If the line 13, if reference 'a' had a type B, (i.e. B a = new B()) then the output would be: m() in A m() in B

Given: public class A { public static void main(String[] args){ int i =3; short s = 3; if(i==s) { System.out.println("1"); } Integer i2 = new Integer(3); if(i2==s) { System.out.println("2"); } if(i2.equals(s)) { System.out.println("3"); }}} What is the result? (Choose all that apply) a) compiler error b) runtime error c) compile and print 1 2 3 d) none of the above

d) is correct. None of the suggested answers are correct. The program will compile and print: 1 2 ===Code Step Through=== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 3, the variable ' i ' of type int is assigned the value 3. On the line 4, the variable ' s ' of type short is assigned the value 3 also. ========2======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i ==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 5, the contents of the variable 'i ' is compared the contents of the variable 's'. As both contents are equal, the expression resolves to true. Even though the variable 's' is of type short and variable 'i ' is of type int, but these primitive variable types are very similar and their contents can be compared. ========3======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 6, the string containing the digit 1 is printed to the screen. ========4======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 8, the number 3 is wrapped in an Integer object. ========5======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 9, the Integer object with reference 'i2' gets unwrapped (unboxed) to its primtive type '3' and this value is compared to the short primitive type variable named 's' which also contains 3. As both of these variables contain the value 3, the expression resolves to true. This results in the if block entering and line 10 executing. Any two primitive types can be compared with the equality operator '==' ========6======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 10, the value 2 is printed to the screen. ========7======== 1: public class A { 2: public static void main(String[] args){ 3: int i =3; 4: short s = 3; 5: if(i==s) { 6: System.out.println("1"); 7: } 8: Integer i2 = new Integer(3); 9: if(i2==s) { 10: System.out.println("2"); 11: } 12: if(i2.equals(s)) { 13: System.out.println("3"); 14: }}} On the line 12, a comparison is made to see if the contents of the objects compared are equal. As reference 'i2' is of type Integer and the reference s is of primitive type short, this expression will resolve to false. Therefore, the if block will not be entered, resulting in the next line in the code to be executed i.e. 14. Final output: 1 2 Notes: ➤ The equality operator '==' compares addresses of objects in memory. ➤ Any two primitive types can be compared with the equality operator '==' ➤ If two objects are of different type and unrelated they will give a compiler error if compared. Below is an example that will give a compiler error: Student s = new Student("4"); Employee e = new Short("4"); if(s.equals(e)) { System.out.println("equals"); }


संबंधित स्टडी सेट्स

Cyber Awareness Challenge 2024 Knowledge Test

View Set

CMPE150 Midterm Review Questions

View Set

HPU Macroeconomics Howard Final prep

View Set

Unit 6 Rocks and Minerals study guide

View Set

Part 4: Writing to Evaluate Mortimer's Style Quiz

View Set

ACTG 350 Key Terms (Midterm 2 - Final)

View Set

Adding Fractions with Different Denominators

View Set

emergency care: Cardiac emergencies, CPR, and AED

View Set