Quiz 5 (incomplete) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

You can declare two variables with the same name in ________.

A

different methods in a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Object-oriented programming allows you to derive new classes from existing classes. This is called ________.

A

inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following statements are true? (Choose two.)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);
} }
A

The program has a compile error because you attempted to invoke the Circle class’s constructor illegally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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();
    }
}
A

“The default constructor of A is invoked”“The default constructor of B is invoked”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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) {
    }
}
A

The method m is not overridden in B. B inherits the method m from A and defines an overloaded method m in B.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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";
    }
}
A

m(new Person()) causes an error

m(new Object()) causes an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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;
    }
}
A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly