Quiz 5 (incomplete) Flashcards
You can declare two variables with the same name in ________.
different methods in a class
Object-oriented programming allows you to derive new classes from existing classes. This is called ________.
inheritance
Which of the following statements are true? (Choose two.)
A subclass is usually extended to contain more functions and more detailed information than its superclass.
“class A extends B” means A is a subclass of B.
Suppose you create a class Cylinder to be a subclass of Circle. Analyze the following code:
class Cylinder extends Circle { double length;
Cylinder(double radius) { Circle(radius); } }
The program has a compile error because you attempted to invoke the Circle class’s constructor illegally.
What is the output of running class C?
class A { public A() { System.out.println("The default constructor of A is invoked"); } }
class B extends A { public B() { System.out.println("The default constructor of B is invoked"); } }
public class C { public static void main(String[ ] args) { B b = new B(); } }
“The default constructor of A is invoked”“The default constructor of B is invoked”
Analyze the following code:
public class Test { public static void main(String[ ] args) { B b = new B(); b.m(5); System.out.println("i is " + b.i); } }
class A { int i;
public void m(int i) { this.i = i; } }
class B extends A { public void m(String s) { } }
The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.
Given the following code, find the compile error. (Choose two.)
public class Test { public static void main(String[ ] args) { m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); }
public static void m(Student x) { System.out.println(x.toString()); } }
class GraduateStudent extends Student { }
class Student extends Person { public String toString() { return "Student"; } }
class Person extends Object { public String toString() { return "Person"; } }
m(new Person()) causes an error
m(new Object()) causes an error
Analyze the following code: (Choose two.)
public class Test { public static void main(String[ ] args) { Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); } }
class A { int x; public String toString() { return "A's x is " + x; } }
When executing System.out.println(a2), the toString() method in the Object class is invoked.
When executing System.out.println(a1), the toString() method in the A class is invoked.