Quiz 5 (incomplete) Flashcards Preview

Computer Science 111 > Quiz 5 (incomplete) > Flashcards

Flashcards in Quiz 5 (incomplete) Deck (8)
Loading flashcards...
1

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

different methods in a class

2

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

inheritance

3

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.

4

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.

5

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"

6

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.

7

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

8

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.