Chapter 10 Flashcards

1
Q

What is polymorphism?

A

Polymorphism indicates that a something can have many forms.
A polymorphic variable means it can have different types of objects at different points in time. (i.e. the type can change during the execution of the program).

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

What is late binding?

A

If a method is called on a polymorphic variable, Java will check which method to invoke depending on the type the variable is currently referencing. If the type changes during the execution, Java will change the method that is invoked.

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

If a declared object is of type X, what other types could this object reference (run-time speaking)?

A

An object declared with a specific type could potentially reference any of type that is child of the original type declaration.
If X is a parent and Y is a child.
A X object could become a Y object during the program.

Example:
Holiday day;
day = new Christmas();

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

Suppose this situation:
Holiday day = new Christmas();
day.celebrate();

Of what type is the invoked method? Are there limitations?

A

The method called depends on the type of the object being referenced, not the reference type.

This means, that although day is declared as a day type, the method invoked is of type Christmas, as this is the type that is being referenced by the object.

CONDITION: The type of the object (not the one being referenced) must also implement the called method. Otherwise a compiler error is shown.

In other words, the celebrate method called in this case will be of type Christmas, but the celebrate method of type Holiday must also exist.

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

What does the following do?

(day is of Holiday type)
((Christmas)day).getTree();

In which cases is it used?

A

This statement calls on object day the getTree method of type Christmas. This will NOT raise an exception ONLY IF object day references a Christmas object.

This statement is used when we’re sure that the object will be of the type that implements such method.

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

If MusicPlayer is the parent of CDPlayer, which of the following assignments are valid?

MusicPlayer mplayer = new CDPlayer();

CDPlayer cdplayer = new MusicPlayer();

A

The first one is a legal statement, because CDPlayer is-a MusicPlayer. In the sense that mplayer can reference the child class even if it’s declared as a parent (i.e. generic) type.

The second statement is wrong. That’s because a CDPlayer cannot be a MusicPlayer. CDPlayer is a more specific type of MusicPlayer. When referencing a MusicPlayer, some instance data of CDPlayer may not be initiated.

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

How is overriding related to polymorphism?

A

When a child class overrides the definition of a parent’s method, TWO definitions of that method exist (one of the parent class and the other of the child class). The version that will be invoked depends on the type of the object that is referenced by the polymorphic reference variable.

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

If an object is declared with the type of an interface, what limitations does it have?

A

If an object is declared with the same type of an interface, the object can execute the methods of the type it’s referencing, only on the condition that such method is defined in the interface.

For example:
Speaker special = new Philosopher(); //speaker is interface.
special.pontificate(); //if pontificate() is not defined in the interface Speaker, a compiler error will occur.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is selection sort about?

A

A way would be to find the smallest value in the list and then to swap it with the value in the first position. Then find the next smallest value and so on…

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

Does swapping of values require more than 2 variables?

A

To swap 2 values, 3 variables are required. One of which has to keep the value temporarily.

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

Look at this code:
public boolean equals (Object other)
return (lastName.equals(((Contact)other).getLastName()) …
Why do we need to cast the class Contact?

A
The class contact must be casted, because the method equals is an override of the method of an interface.
To override the method we will have to use the same signature
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is insertion sort?

A

Its the strateg of putting any item directly into its proper place by analysing the array and eventually shifting the elements to make room for the new one.

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

What are the effieciencies of the Selection and Insertion Sort algorithms?

A

They have similar efficiencies, for an array of size n they do approximately n^2 number of comparisons.

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

What’s the difference between a linear search and a binary search?

A

A linear search scans every element of an array in turn.

A binary search is used in sorted arrays, it compares an element to understand what part of the two he should check in next.
Example:
If an array has values between 0 and 99. If the method looks for 56 and it finds the value 38, it will check only the numbers on the right of the array. As the value 56 cannot be less than 38 (and be on the left then).

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

What is a property in JavaFX?

A

A property is an object that holds a value. By binding properties together a value of one will automatically change the value of the other.

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