Quiz 4 - Abstract Class, Interfaces
Which line or lines of code below will cause compile errors? Java - class Parent { public int x; } class Child extends Parent { public void doStuff() { System.out.println("I'm a child"); } } class Main { public static void main(String[] args) { Parent p1 = new Parent(); Child c1 = new Child(); Parent p2 = new Child(); p1.doStuff(); //Line 1 c1.doStuff(); //Line 2 p2.doStuff(); //Line 3 ((Child)p2).doStuff(); //Line 4 } } options: 1. Line 1 Line 3 2. Line 1 Line 3 Line 4 3. Line 1 Line 2 Line 3 Line 4 4. Line 1
1. Line 1 Line 3
Will the following code compile? Java - interface IA {public void do_stuff1();public void do_stuff2();} class C implements IA { @Override public int do_stuff1() { return 1; } @Override public void do_stuff2() { int b=1; } } class Main { public static void main(String[] args) { C myC = new C(); } } options: 1. No class C does not implement do_stuff1() appropriately. 2. No, interface IA cannot have 2 virtual methods. 3. No, class C cannot be instantiated because it implements an interface. 4. Yes
1. No class C does not implement do_stuff1() appropriately.
Will the following code compile? Java - abstract class A { public int do_stuff1() { return 3; } } class B extends A { @Override public int do_stuff1() { return 4; } public int do_stuff2() { return 7; } } class Main { public static void main(String[] args) { //Note the polymorphism: A myB = new B(); myB.do_stuff1(); myB.do_stuff2(); } } options: 1. No, class A is marked abstract, but has no abstract methods, and that is not allowed. 2. No, class A has no do_stuff2() therefore you can't call do_stuff2() on myB in main. 3. No, class A is abstract, so you can't call do_stuff1() even though there is an override in class B. 4. Yes
2. No, class A has no do_stuff2() therefore you can't call do_stuff2() on myB in main.
What is the output of the following code? Java - interface IA {public void do_stuff1();} abstract class B { public abstract void do_stuff2(); public int do_stuff3() { return 2; } } class C extends B implements IA { @Override public void do_stuff1() { int a=1; } @Override public void do_stuff2() { int b=1; } } class Main { public static void main(String[] args) { C myC = new C(); System.out.println(myC.do_stuff3()); } } options: 1. This code doesn't compile. Object myC doesn't have a method do_stuff3() 2. 1 3. 2 4. 3
3. 2
Will the following code compile? Java - abstract class A {public abstract int do_stuff1();public abstract int do_stuff2();} class B extends A {@Overridepublic int do_stuff1() {return 4;}} class Main { public static void main(String[] args) { B myB = new B(); } } options: 1. No, class A is marked abstract, and all it's methods are abstract which is not allowed 2. No, you cannot make an object of class B because it's not marked abstract. 3. No, class B has no concrete method do_stuff2(), but it should have one. 4. Yes
3. No, class B has no concrete method do_stuff2(), but it should have one.
Will the following code compile? Java - interface IA {public void do_stuff1();} abstract class B { public abstract void do_stuff2(); } class C extends B implements IA { @Override public void do_stuff1() { int a=1; } @Override public void do_stuff2() { int b=1; } } class Main { public static void main(String[] args) { C myC = new C(); } } options: 1. No, Class C can't extend one class (B) and implement an interface (1A), it can only do one or the other. 2. No, Class C doesn't correctly override do_stuff2() 3. No, class C cannot be instantiated because it implements an interface. 4. Yes
4. Yes
When a set of methods have the same name but different types/number of parameters in the same class, they are called: A. Overridden methods B. Overloaded methods C. Constructors D. toString() methods
B. Overloaded methods
The following is a valid (i.e. will compile) abstract class definition: abstract class A { public int do_stuff1() { return 3; } public int do_stuff2() { return 4; } } True or False
True