CMSC 132

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/571

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

572 Terms

1
New cards
What do interfaces do?
Facilitate polymorphism
2
New cards
True or False: you can declare static final variables in an interface?
True
3
New cards
Give an example of how to write an interface?
public interface CanEat{

public void locateFood(); //abstract method

public default void eat(){ //default method

syso(nom nom);

}

}
4
New cards
True or False: there cannot be constructors in an interface?
True
5
New cards
True or False: you can put a static method in an interface along with its implementation?
True
6
New cards
When should you use @Override?
When rewriting any method that has already been inherited. The names of the method and the type and order of the parameters match.
7
New cards
When should you use @Overload?
When there are multiple methods have the same name in the same class but a different order and types of parameters
8
New cards
True or False: you can implement multiple interfaces?
True
9
New cards
Give an example of how to implement multiple interfaces?
public class Owl implements CanEat, CanFly{…}
10
New cards
What is dynamic dispatch?
Call to the method is custom to the type during runtime
11
New cards
What are polymorphic collections?
Collections that can consist of different types
12
New cards
Give an example of a Polymorphic collection?
public interface FlyingThing{…}

public class Airplane implements FlyingThing{…}

public class Helicopter implements FlyingThing{…}

public class Squirrel implements FlyingThing{…}

FlyingThing\[\] thingsThatFly = new FlyingThing\[7\];

//thingsThatFly can consist of anything that implements the flyingThing class
13
New cards
What does API stand for?
Application Programming Interface
14
New cards
What is an API?
Features that are available to you as a programmer when you use that class, usually all the public stuff
15
New cards
What is encapsulation?
Anything that might change later is wrapped in a shell so no one is relying on it
16
New cards
What is a wrapper class?
Classes that “wrap” primitives so that they are used as objectives in the Java Collections
17
New cards
What is caching?
Not making multiple of the same class and value, reduces garbage, returns reference to the Object that already exists
18
New cards
What is auto-boxing and auto-unboxing?
Java automatically wraps and unrwraps it based on what is needed at the time
19
New cards
What is generic notation?
It allows the compiler to check what we have put into the the collection is the right type of object
20
New cards
What is the syntax for a for each loop?
for (Cat c: collection){

//process

}
21
New cards
What are enumerated types (enums)?
An enum is like a class with a small pre-defined number of instances that are identified with symbolic constants
22
New cards
Give an example of how to declare an enum?
public enum Day {

SUN, MON, TUE, WED, THU, FRI, SAT;

}
23
New cards
How do you access an enum?
nameOfEnum.SPECIFICELEMENT
24
New cards
Can you use == to compare enums? .equals()?
yes you can use both
25
New cards
What are some of the methods in the java collections class?
Collections.shuffle(list);

//next 3 must implement comparable

Collections.max(list);

Collections.min(list);

Collections.sort(list);

//must already be sorted

Collections.binarySearch(list, value);

Collections.reverse(list)
26
New cards
How do you go through all the different values in an enum?
enumType.values()
27
New cards
How do you access a specific value enum going through with index?
enumType.values()\[index\]
28
New cards
How do you write a for each loop iterating through enum values?
for (Day currentDay : Day.values())
29
New cards
How do you compare two enums in the same enum?
Day.WED.compareTo(Day.SUN);
30
New cards
How do you get the index of an enum?
Day.TUE.ordinal();
31
New cards
How do return something given the index in the enum?
Day.values()\[index\];
32
New cards
True or False: Enums can have instance variables and instance methods?
True
33
New cards
True or False: Enums cannot have static methods?
False
34
New cards
True or False: Constructors for enums are always private?
True
35
New cards
Give an example of how to create an enum with instances that have different values?
public enum Day{

//declaring using constructor

SUN(“sunday”, 0), MON(“monday”, 8), TUE(“tuesday”,6);

\
//instance variables

private String name;

private int workHours;

\
//constructor

private Day(String name, int workHours){

this.name=name;

this.workHours = workHours;

}

}
36
New cards
What is an Iterator?
An iterator is an object that can sequentially access the elements of a collection
37
New cards
What does the Iterable interface ensure?
That the class knows how to give you an iterator that goes through itself
38
New cards
True or False: most collections in java are iterable?
True
39
New cards
Give an example of how to get the iterator?
ArrayList list = new ArrayList<>();



Iterator iterator = list.iterator();
40
New cards
A collection implements what interface: Iterator or iterable?
Iterable
41
New cards
A iterator implements what interface: iterable or iterator?
Iterator
42
New cards
True or false: for each loops are actually iterators?
True
43
New cards
Why do we use iterators?
1\. Its the only way to cycle through elements of ANY java collection

2\. Removing elements without an iterator is tricky, whereas with it, it is implied that it can keep itself in check
44
New cards
True or False: In a for-each loop, you cannot add or remove elements?
True
45
New cards
Give an example of how to use an iterator in a loop?
Iterator iterator = list.iterator();

while(iterator.hasNext()){

Integer value = iterator.next();

if (value
46
New cards
True or False: when you call the remove method on the iterator, it can only remove what was last returned?
True
47
New cards
What is the difference between immutable and mutable?
Immutable means that after the instance variables are first instantiated, mutable means that they can change
48
New cards
Where do static variables go on a memory diagram?
In a little box in the corner of the heap
49
New cards
What is the Comparable interface?
public int compareTo(Other x);
50
New cards
How do we determine/define the natural order of things?
Using the comparable interface
51
New cards
Give an example of implementing the Comparable interface?
public class Car implements Comparable {

public int compareTo(Car other){

return weight-other.weight;

}}
52
New cards
When implementing an interface, what must you make sure to do?
Implement all non-default instance methods
53
New cards
What is instanceOf?
an operator that determines whether or not the instance is the instanceOf the class in question
54
New cards
How do you write an equals method that overrides the equals method in the Object class?
@Override

public boolean equals(Object other){

if (this == other){

return true;

}

if (! (other instanceOf Dog)){

return false;

}

Dog dog = (Dog) other;

return (dog.name.equals(name) && dog.size==size);

}
55
New cards
What is this a reference to?
The current object as the class that it is
56
New cards
What is super a reference to
a reference to the current object as its superclass
57
New cards
What is the first thing you do in a subclass to construct it?
call a constructor in the super class
58
New cards
Give an example of how to write a constructor for a subclass?
public Student (String name, int ID, double GPA, int admitYear){

super(name,ID);

this.GPA = GPA;

this.admitYear = admitYear;

}
59
New cards
If you do not call the super constructor, what will java do?
It will implicitly call the super constructor with no arguments. If there is no super constructor with no arguments, the code will not compile.
60
New cards
Give an example of how to write a copy constructor in a subclass?
public Student(Student other){

super(other);

GPA = other.GPA;

admitYear = other.admitYear;

}
61
New cards
Give an example of how to write a toString method in a subclass?
@Override

public String toString(){

return super.toString()+ “ GPA: “ + gpa + “ Admit Year: “ + admitYear;

}
62
New cards
Give an example of how to write an equals method in a subclass?
@Override

public boolean equals (Object other){

if (!(other instanceOf Student)){

return false;

}

Student s = (Student) other;

return super.equals(s) && GPA == s.GPA && admitYear == s.admitYear;

}
63
New cards
True or False: If your class doesn’t directly extend something, it will implicitly extend Object?
True
64
New cards
What is always at the root of an inheritance tree?
Object
65
New cards
How do you diagram an interface in a UML diagram?
A dashed line with italics
66
New cards
Explain these 3 statements:

1) The type of variable

2) The class

3) The type of object
type of variable is exactly what it is (1)

the class of a variable is exactly what it is (1)/it is an instance of exactly 1 class

the type of object is where “is a” comes into play (an object may satisfy many types)
67
New cards
What is overload?
Two different methods have the same name with different parameters and are in no way related
68
New cards
What is override?
A subclass “replaces” inherited methods. They have the same parameter types. The return types do not have to be the same but it must satisfy the type (is a)
69
New cards
True or False: you can change the privacy type in an override as long as it is more public and not less public?
True
70
New cards
What is early(static) binding?
It will run whatever type it is declared as. The compiler makes the decision immediately.

ex. Person p = new Student s;

will run person version
71
New cards
What is late (dynamic) binding?
It will run the version it literally is, decision is made at runtime.

ex. Person p = new Student s;

will run student version
72
New cards
True or False: You can override more than once?
False
73
New cards
When will a cast run successfully?
When you cast either up or down the tree (must be reached by moving exclusively up or down)
74
New cards
Which type of casting will always run? Why?
An upcast will always run because the lower levels “is a” higher level
75
New cards
What cast do you have to be careful with? Why?
Downcasting because it is not necessarily that type
76
New cards
When can you implicitly cast? When can you not?
You can always implicitly upcast, you can never implicitly downcast
77
New cards
What type of error would you get if you downcast incorrectly?
ClassCastException
78
New cards
When will a cast compile?
As long as it is above or below the type on the tree
79
New cards
What is the safe practice in downcasting so that it doesn’t throw exceptions?
if (p instanceof Student){

Student s = (Student) p;\`

}
80
New cards
Is multiple inheritance allowed in Java?
No
81
New cards
What is inheritance, what is composition?
Because you can only have 1 superclass, it is important to make sure that you really want make sure that is it is an absolutely necessary thing while in composition you can place an object of what you need inside the class and therefore you can still access all the functionalities
82
New cards
True or false: composition allows for polymorphism?
False
83
New cards
If given the choice, what do you usually favor, composition or inheritance?
Composition
84
New cards
What does it mean to have a final primitive variable?
It cannot change, the value must remain the same
85
New cards
What does it mean to have a final object?
The object itself can change (if it is mutable) it just cannot be assigned to a new object
86
New cards
What does having a final class mean?
No one can extend the class
87
New cards
What doe having a final method mean?
You cannot override the method
88
New cards
What is protected visibility restricted to?
The class, package, and its subclasses
89
New cards
What is shadowing?
Overloading a variable that is inherited
90
New cards
What is the purpose of an abstract class?
To be extended in various ways (like an interface)
91
New cards
True or false: Abstract classes can be instantiated?
False
92
New cards
True or false: Abstract classes can have state, instance variables, and constructors?
True
93
New cards
What is better about Abstract classes?
You can include instance variables and constructors
94
New cards
What is better about interfaces?
A class can implement more than 1 interface
95
New cards
When given the option, do you favor abstract classes or interfaces?
interfaces
96
New cards
How do you write an abstract class?
public abstract class Shape{

private int xCoord, yCoord;

private Color color;

private int width;

public Shape(int x, int y, Color color, int width){

xCoord=x;

yCoord=y;

this.color=color;

this.width=width;

}

}
97
New cards
How do you write an abstract method in an abstract class?
public abstract void drawShape();
98
New cards
How do you write a try catch block?
try {

//code

} catch (NullPointerException e) {

//handler

} catch (RuntimeException (e) {

//handler

} finally {



}
99
New cards
What is the difference between checked and unchecked exceptions?
Unchecked exceptions means that there is something wrong with your code, fix it. Checked exceptions means that at runtime something goes wrong, it is beyond your control.
100
New cards
For what type of exceptions do you use a try catch?
Checked exceptions