3: Quizzes Flashcards

1
Q

If A “Is-A” B, what else can we say about the relationship between class A and class B?

  1. Class A extends Class B.
  2. Class A inherits from Class B.
  3. Class A derives from Class B.
A

All 3.

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

What tool is used to show inheritance or composition relationships among classes.

A

A class diagram.

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

Which of the following is a specialized version of a class?

  1. An ancestor.
  2. A subclass.
  3. A superclass.
  4. class Object.
  5. class Class
A

2

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

Which of the following statements is true?

  1. Objects never change the class to which they belong.
  2. In some situations, a variable may be bound to different types of objects.
  3. The behavior of any object is determined by the object’s class and not by the data type of the variable that references it.
A

All 3 are true.

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

Given this start to a class definition:

public class Beta extends Alpha {
...
}

What is true about instance variables from Alpha?

  1. Class Beta must redeclare all of Alpha’s instance variables.
  2. They should not be redeclared in Beta.
  3. The Beta class should redeclare only those instance variables from Alpha that it wants.
  4. Class Beta should redeclare only the private instance variables from Alpha.
A

2.

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

If class Fruit is the superclass of class Apple, which of the following is also true?

  1. Fruit is the base class of Apple
  2. Fruit is the parent class of Apple
  3. Apple “Is-A” Fruit
A

All 3 are true.

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

If a class definition does not include the keyword extends, which of the following is true?

  1. The class has no sub class
  2. The class has no super class
  3. The class implicitly derives from Class.
  4. The class implicitly derives from Object.
A

4.

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

Which of the following things would prevent a method declaration in a subclass from properly overriding an inherited method?

  1. not matching the method name
  2. not matching the return type
  3. not matching the parameter “signature” (number, order and data types of parameters)
  4. trying to make a public method private
A

All of them would prevent a method declaration from properly overriding an inherited method.

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

What is always the very first thing that occurs in the constructor of a derived class?

A

A call to the superclass constructor is made.

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

Which of the following statements creates a composition (or aggregation) relationship?

  1. public class Fun extends Hun { … }
  2. return new Fun();
  3. if ( myThing instanceof Point ) { … }
  4. private Widget myThing;
A

4.

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

Which Java keyword can be used to access the super class version of an overridden method from within the sub class?

A

super

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

Given the following code (assume that the statements compile with NO syntax errors):

Red hot = new Red();
Green top = hot;
Orange ocean = new Blue();
hot = ocean;

What is the static type of ‘top’?

A

Green

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

Given the following code (assume that the statements compile with NO syntax errors):

Red hot = new Red(); 
Green top = hot;
Orange ocean = new Blue();
hot = ocean;

What is the dynamic type of ‘ocean’ after all the statements have been executed?

A

Blue

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

Given the following code (assume that the statements compile with NO syntax errors):

Red hot = new Red(); 
Green top = hot;
Orange ocean = new Blue(); 
hot = ocean;

What is the dynamic type of ‘hot’ after all the statements have been executed?

A

Blue

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

What determines which version of an overridden method is executed?

A

The dynamic type of the variable that acts as the ‘qualifier’ in the method call.

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

What determines which version of an overloaded method is executed?

A

The data types of the arguments of the method call.

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

A programmer writing code in class Point attempts to override the following inherited method:

public boolean equals( Object o ) { … }

but does not match the method signature exactly. Instead, the programmer uses the following method signature:

public boolean equals( Point p ) { … }

What is the result?

A

The code compiles, but the inherited method has not been overridden – it has been overloaded instead.

18
Q
Given the following class hierarchy: 
Siamese "Is-A" Feline "Is-A" Mammal "Is-A" LivingThing 

Which of the following is true?

  1. ( new Mammal() ) instanceof LivingThing
  2. ( new Feline() ) instanceof LivingThing
  3. ( new Siamese() ) instanceof LivingThing
A

All 3 are true.

19
Q

A type cast using reference type is a promise to the compiler that the dynamic type of the expression will conform to the type specified in the cast. What happens if this promise is broken?

A

A ClassCastException will be thrown.

20
Q

How many different classes can a sub class derive from, using the keyword extends?

A

Exactly one.

21
Q

If type A conforms to a different type B, then wherever code expects an object of type B, an object of type A may be used.

A

True.

22
Q

If a class D derives from class S, then objects of type D conform to type S.

A

True.

23
Q

What is the term for the data type used to declare a reference variable?

A

Static type.

24
Q

What is the term for the data type of an object bound to a reference variable?

A

Dynamic type.

25
Q

This term describes when a subclass implements a method with the exact same signature as a method in the super class.

A

override

26
Q

Which of the following statements tests for CONTENT equality?

  1. if ( x == “Hello” ) { … }
  2. if ( x.equals( “Hello” ) ) { … }
  3. if ( x != “Hello” ) { … }
  4. if ( x.contains( “Hello” ) ) { … }
A
  1. if ( x.equals( “Hello” ) ) { … }
27
Q

Which comparison below is acceptable in Java syntax if the following declarations are made?

CSC142Point p1 = new CSC142Point( 3, 4 );
CSC142Point p2 = new CSC142Point( 5, 6 );
  1. p1.getX() > p2.getX()
  2. p1.setX(5) == p2.setX(5)
  3. p1 < p2
A
  1. p1.getX() > p2.getX()
28
Q

Which of the following statements creates a composition (or aggregation) relationship?

  1. public doSomething(Boo b){ …}
  2. return new Boo();
  3. if ( myThing == b ) { … }
  4. private Boo myThing;
  5. Boo Hoo
A
  1. private Boo myThing;
29
Q

Which of the following things may an abstract class contain in addition to abstract methods?

  1. method definitions with implementation
  2. instance variable declarations
  3. constructor definitions with implementations
  4. class constants (final variables)
A

All 4.

30
Q

What is the difference between an abstract class and a concrete class?

  1. An abstract class may NOT be extended.
  2. An abstract class may NOT be instantiated.
  3. All methods in an abstract class must be abstract.
  4. A reference variable cannot have an abstract class as its static type.
A
  1. An abstract class may NOT be instantiated.
31
Q

Which of the following is true about a concrete class?

  1. A concrete class may be extended.
  2. A concrete class may be instantiated.
  3. A concrete may NOT contain abstract methods.
  4. All of the above.
A
  1. All of the above.
32
Q

Which of the following is true?

  1. A class extends exactly one other class and may implement any number of interfaces.
  2. A class may extend at most one concrete class, may extend any number of abstract classes, and may implement any number of interfaces.
  3. A class may extend one concrete class and also one abstract class, and may implement any number of interfaces.
  4. A class may extend exactly one other class, OR may implement any number of interfaces, but not both.
A
  1. A class extends exactly one other class and may implement any number of interfaces.
33
Q

Given the following declarations,

public class Parent { ... } 
public class Child extends Parent implements Wizard { ... } 
public class Grandchild extends Child { ... }

Which of the following is NOT true?

  1. Child “Is-A” Wizard
  2. Parent “Is-A” Wizard
  3. Grandchild “Is-A” Child
  4. Grandchild “Is-A” Parent
  5. Grandchild “Is-A” Wizard
A
  1. Parent “Is-A” Wizard
34
Q

In Java a class can inherit members from more than 1 super class. True/false

A

False

35
Q

Given the following definition:

public class Alpha implements Freezable {…}

And the following client code:

Freezable f = new Alpha();

Which is true?

  1. The variable f has static type Freezable and dynamic type Freezable
  2. The variable f has static type Freezable and dynamic type Alpha
  3. The variable f has static type Alpha and dynamic type Alpha
  4. The variable f has static type Alpha and dynamic type Freezable
A
  1. The variable f has static type Freezable and dynamic type Alpha
36
Q

Which of the following creates a new data type?

  1. an abstract class definition
  2. a concrete class definition
  3. an interface definition
  4. All of the above.
A
  1. All of the above
37
Q

Which of the following is NOT one of the three categories of collections we have studied?

  1. Set
  2. Iterator
  3. Map
  4. List
A
  1. Iterator
38
Q

Which of the following is NOT a wrapper class?

  1. String
  2. Double
  3. Character
  4. Integer
A
  1. String
39
Q

Which of the following kinds of things may be stored directly in an ArrayList?

  1. object references
  2. None of the above
  3. Either of these two kinds of data
  4. primitive values
A
  1. object references
40
Q

In this declaration:

ArrayList< Point > polygon;

what do we call ‘Point’?

A

Type argument

41
Q

In this declaration:

ArrayList< Point > polygon;

what is the data type of the variable ‘polygon’?

A

ArrayList< Point >

42
Q

If this method call:

myList.size()

returns the value 7, what is the range of legal values allowed for the variable x in the following method call?

myList.get( x )

A

0-6