CMPS 2800 EXAM 2 PEARSON STUDY QUESTIONS

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/27

encourage image

There's no tags or description

Looks like no tags are added yet.

28 Terms

1
New cards

___________ is attached to the class of the aggregating class to denote the aggregation relationship with the aggregated object.

An empty diamond

2
New cards

An aggregation relationship is usually represented as __________ in ___________.

a data field/the aggregating class

3
New cards

Composition means ______________.

that a class contains a data field that references another object

4
New cards

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

inheritance

5
New cards

Which of the following statements are true? Please select all that apply.

"class A extends B" means A is a subclass of B., A subclass is usually extended to contain more methods and more detailed information than its superclass.

6
New cards

Inheritance means ______________.

that a class can extend another class

7
New cards

Which of the statements regarding the super keyword is incorrect?

You can use super.super.p to invoke a method in superclass's parent class.

8
New cards

Which of the following statements are true? Please select all that apply.

Overloading a method is to provide more than one method with the same name but with different signatures to distinguish them.

A private method cannot be overridden. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.

To override a method, the method must be defined in the subclass using the same signature and compatible return type as in its superclass.

A static method cannot be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

It is a compile error if two methods differ only in return type in the same class.

9
New cards

Analyze the following code. What happens when it is executed?

public class Test {
  public static void main(String[] args) {
    new B();
  }
}

class A {
  int i = 7;
  
  public A() {
    System.out.println("i from A is " + i);
  }
  
  public void setI(int i) {
    this.i = 2 * i;
  }
}

class B extends A {
  public B() {
    setI(20);
  }
  
  @Override
  public void setI(int i) {
    this.i = 3 * i;
  }
}


The constructor of class A is called and it displays "i from A is 7".

10
New cards

Analyze the following code. What happens when it is executed?

public class Test {
  public static void main(String[] args) {
    new B();
  }
}

class A {
  int i = 7;
  
  public A() {
    setI(20);
    System.out.println("i from A is " + i);
  }
  
  public void setI(int i) {
    this.i = 2 * i;
  }
}

class B extends A {
  public B() {
  }
  
  @Override
  public void setI(int i) {
    this.i = 3 * i;
  }
}


The constructor of class A is called and it displays "i from A is 60".

11
New cards

Given the following code, find the compile error. Please select all that apply.

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 {
  @Override
  public String toString() {
    return "Student";
  }
}

class Person extends Object {
  @Override
  public String toString() {
    return "Person";
  }
}


m(new Object()) causes an error,

m(new Person()) causes an error

12
New cards

Polymorphism means ______________.

that a variable of supertype can refer to a subtype object

13
New cards

Which of the following are Java keywords?


instanceof

14
New cards

Analyze the following code:

public class Test {
  public static void main(String[] args) {
    String s = new String("Welcome to Java");
    Object o = s;
    String d = (String)o;
  }  
}


s, o, and d reference the same String object.

15
New cards

Given the following classes and their objects:

class C1 {};
class C2 extends C1 {};
class C3 extends C1 {};

C2 c2 = new C2();
C3 c3 = new C3();

Analyze the following statement:

c2 = (C2)((C1)c3);


You will get a runtime error because you cannot cast objects from sibling classes.

16
New cards

Which of the following class definitions defines a legal abstract class?

abstract class A { abstract void unfinished(); }

17
New cards

Which of the following declares an abstract method in an abstract Java class?

public abstract void method();

18
New cards

Which of the following statements regarding abstract methods is false?

An abstract class can have instances created using the constructor of the abstract class.

19
New cards

Which of the following statements regarding abstract methods is false?

A data field can be declared abstract.

20
New cards

Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?

A a = new B(); , B b = new B();

21
New cards

What is the output of running class Test?

public class Test {
  public static void main(String[] args) {
    new Circle();
  }
}

public abstract class GeometricObject {
  protected GeometricObject() {
    System.out.print("A");
  }

  protected GeometricObject(String color, boolean filled) {
    System.out.print("B");
  }
}

public class Circle extends GeometricObject {
  /** No-arg constructor */
  public Circle() {
    this(1.0);
    System.out.print("C");
  }

  /** Construct circle with a specified radius */
  public Circle(double radius) {
    this(radius, "white", false);
    System.out.print("D");
  }

  /** Construct a circle with specified radius, filled, and color */
  public Circle(double radius, String color, boolean filled) {
    super(color, filled);
    System.out.print("E");
  }
}




BEDC

22
New cards

Which of the following is a correct interface?

interface A { void print();}

23
New cards

_______ is not a reference type.

A primitive type

24
New cards

Show the output of running the class Test in the following code lines:

interface A {
}

class C {  
}

class D extends C {  
}

class B extends D implements A {
}

public class Test {
  public static void main(String[] args) {
    B b = new B();
    if (b instanceof A)
      System.out.println("b is an instance of A");
    if (b instanceof C)
      System.out.println("b is an instance of C");
  }
}


b is an instance of A followed by b is an instance of C.

25
New cards

Which of the following statements are true? Please select all that apply.

The Date class implements Comparable.

The BigInteger class implements Comparable.

The String class implements Comparable.

The Double class implements Comparable.

26
New cards

Analyze the following code.

1. public class Test  {
2.   public static void main(String[] args) {
3.     Fruit[] fruits = {new Fruit(2), new Fruit(3), new Fruit(1)};
4.     java.util.Arrays.sort(fruits);
5.   }
6. }

class Fruit {
  private double weight;
  
  public Fruit(double weight) {
    this.weight = weight;
  }
}


The program has a runtime error on Line 4 because the Fruit class does not implement the java.lang.Comparable interface and the Fruit objects are not comparable.

27
New cards

Which of the following statements is false?

If you compile a class with errors, a .class file is created for the class.

28
New cards

What is the best suitable relationship between Employee and Faculty?

Inheritance