Skillstorm Intro to Coding - Data Structures in Java Flashcards

(105 cards)

1
Q

In Java, every Object has two comparison methods. What are they?

A

equals and hashCode Methods

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

The equals and hashCode methods must be _________ to work properly.

Why?

A

Overridden

Without them, you would have to create very large “if” comparisons, comparing every field from an object, making code really confusing and hard to read.

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

Fill in the Blanks of the attached image

A

boolean equals Object

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

Fill in the blanks of the following code:

public _____ _____ () {

final int prime = 31;

int result = 1;

result = prime * result + size;

if (topping == null) {

result = prime * result + 0;

}else {

result = prime * result + topping.hashCode();

}

return result;

}

A

int hashCode

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

A List is an _________ collection that may contain _________ elements.

A

Ordered

duplicate

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

What are the two types of general-purpose list implementations?

A

ArrayList and LinkedList

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

Are Java LinkedList automatically Doubly LinkedList?

A

Yes

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

ArrayLists are resizable. But by what size do they increase when they are full?

A

50%

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

ArrayList and LinkedList are both ordered by ________ position.

A

index

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

Because you can add and remove from the heads and tails of LinkedList, this makes them perfect for __________ and __________.

A

Stacks (and) Queues

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

What is Last In First Out? Stacks or Queues

A

Stacks

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

What is First In First Out? Stacks or Queues

A

Queues

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

Manipulating LinkedList. See image…

A

add

add

remove

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

Manipulating ArrayList. See attached image…

A

< String >

add

get

size

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

What is a Queue?

A

A collection for holding elements prior to processing

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

A ________ is a double-ended queue.

A

Deque

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

A Deque supports ________, removal, and __________ of elements at both end-points!

A

insertion,

examination

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

Deque interface implements both ______ and ______ at the same time.

A

Stacks

Queues

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

Queue Manipulation. See attached images…

A

queue. offer
queue. peek
queue. poll

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

Stack Manipulation. See attached images…

A

push

push

pop

peek

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
  1. Is a Map an object that maps equal keys?
  2. What are mapped to unique keys?
  3. Do Maps include methods for basic operations?
  4. Do Maps include methods for non-bulk operations?
  5. Do Maps includes methods for collection views?
A
  1. False: They map unique keys
  2. Values
  3. True
  4. False: They include methods for bulk operations
  5. True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Java contains three general-purpose Map implementations. What are they?

A

HashMap, TreeMap, and LinkedHashMap

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

HashMap Manipulation. See attached image…

A

put

put

get

size

containsKey

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

Can a Set contain duplicate elements?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Sets add a stronger contract on the behavior of the _______ and \_\_\_\_\_\_\_. It allows Set instances to be compared meaningfully even if their implementation types differ.
equals (and) hashCode
26
What are the three general-purpose Set implementations?
HashSet, TreeSet, and LinkedHashSet
27
HashSet Manipulation. See attached images...
public int hashCode public boolean equals Object obj
28
LinkedList is a(n) ______ collection.
Ordered
29
LinkedList has an underlying array that changes size as needed? True or False
False
30
Two objects with the same hashCode must be equal? True or False
False
31
A Queue is a first-in last-out collection? True or False
False
32
Two objects that are equal must have the same hashCode? True or False
True
33
The equals method in java.lang.Object compares two objects and returns a(n)\_\_\_\_\_\_.
boolean
34
A LinkedList in Java is _______ by default.
Doubly-linked
35
The performance of accessing a value in a HashMap is \_\_\_\_\_\_\_\_\_\_\_\_\_\_.
Constant Time
36
An ArrayList will grow ___ in size when it reaches its maximum capacity.
50%
37
A Stack is a ______________ collection
last-in first-out
38
A HashSet will maintain duplicate values. True or False....
False
39
In Java with Object Equality, you can decide when two objects are equivalent, but with what method and how is it written in Java?
1. The method is ==\> java.lang.Object.equals method 2. In Java it is written as ==\> public boolean equals(Object other)
40
In Object Equality, does it check the memory location by default?
Yes
41
In Object equality what must you override to compare object state?
The method
42
True or False, in Object Equality, some data structures require unique objects...
True
43
True or False, the hashcode isn't used to give a consistent integer that represents the object...
False
44
How is the hashCode method written in code?
public int hashCode()
45
Is hashCode loved by HashMap, HashSet, and others?
Yes
46
True or False, HashCode puts objects into a smaller group to optimize searching...
True
47
# Fill in the blanks of this code: public _____ \_\_\_\_\_ ( _____ obj) { Pizza other = (Pizza) obj; if (size != other.size) return false; if (topping == null) if (other.topping != null) return false; } else if (!topping.equals(other.topping)) return false; return true; }
boolean equals ( Object obj)
48
True or False, ArrayList is a class in the Collections API in Java...
True
49
What does ArrayList do in Java?
1. Creates an array 2. Provides methods for working with ArrayList
50
What is the ArrayList default size when it is created?
16
51
True or False, ArrayList is not mutable...
False
52
What happens if an ArrayList fills up?
It grows 50% larger to accommodate more elements
53
True or False, ArrayList does not allow for duplicate elements...
False
54
True or False, in ArrayList order of insertion is preserved...
True
55
LinkedList is a Class in the Collections API in Java, True or False?
True
56
What is the LinkedList a series of?
Nodes
57
What are Nodes in LinkedList?
A Node wraps up a value (String, Pizza, etc.)
58
How are Nodes in LinkedList connected?
They are "Linked" by a reference to the next Node
59
Are LinkedList singly-linked or doubly-linked?
Doubly-linked
60
How are doubly-linked Nodes connected?
The Nodes have a previous and next reference.
61
LinkedLists allow duplicate elements, True or False?
True
62
Order of insertion is not preserved in LinkedList, True or False?
False
63
# Fill in the blanks of the following code: public class LinkedListExample { public static void main(String[] args) { List list = new LinkedList(); // write Dill pickle to the list list.\_\_\_\_\_\_ (new Pickle("Dill")); // write Sweet pickle to the list list.\_\_\_\_\_\_ (new Pickle("Sweet")); // delete the pickle at index 1 list.\_\_\_\_\_\_ (1); } } class Pickle{ String flavor; public Pickle(String flavor) { this.flavor = flavor; } }
1. add 2. add 3. remove
64
# Fill in the blanks in the following code: // create an ArrayList of Strings ArrayList _____ list = new ArrayList\<\>(); // add XYZ to the list list.\_\_\_\_\_\_ ("XYZ"); // print element at index 2 System.out.println(list.\_\_\_\_\_\_ (2)); // print how many Strings are in the list System.out.println(list.\_\_\_\_\_\_ ());
1. 2. add 3. get 4. size
65
Is the Queue an interface in the Collections API in Java?
Yes
66
True or False, the queue is First-in/First-out...
True
67
1. When you Push in a Queue, what happens? 2. What you Pop in a Queue, what happens?
1. Node goes to the tail 2. Node goes to the head
68
True or False, the Queue doesn't allow for duplicate elements...
False
69
True or False, in a Queue, the order of insertion is preserved...
True
70
True or False, the Stack is a Class in the Collections API in Java...
True
71
Stack is Last-in/First-out, True or False?
False and True, it can be First-in/Last-out and Last-In/First-Out!
72
1. In a Stack what happens when we Push? 2. In a Stack what happens when we Pop?
1. Data/Node goes to the tail 2. Data/Node comes from the tail
73
True or False, Stacks don't allow for duplicate elements...
False
74
In Stacks order of insertion is preserved, True or False?
True
75
# Fill in the following blanks in the code: public class QueueExample { public static void main(String[] args) { Queue queue = new LinkedList(); // add new Person to tail 'push' \_\_\_\_\_\_.\_\_\_\_\_ (new Person()); // check what's at the head of the queue \_\_\_\_\_\_.\_\_\_\_\_(); // 'pop' the head of the queue System.out.println( \_\_\_\_\_.\_\_\_\_\_ ()); } } class Person{}
1. queue.offer 2. queue.peek 3. queue.poll
76
# Fill in the blanks in the following code: public class StackExample { public static void main(String[] args) { Stack stack = new Stack(); // add two cards to the stack stack. \_\_\_\_\_ (new Card("A of Spades")); stack. \_\_\_\_\_ (new Card("K of Diamonds")); // remove the top of the stack System.out.println(stack.\_\_\_\_\_ ()); // look at (but not remove) top of the stack System.out.println(stack.\_\_\_\_\_ ()); } } class Card { String value; public Card(String value) { this.value = value; } }
1. push 2. push 3. pop 4. peek
77
A HashMap is not a class in a Collections API that stores key-value pairs, True or False?
False
78
In HashMaps, how do you identify the value?
By the Key For example, a Social Security Number maps to a U.S. Citizen.
79
True or False, Keys do not have to be unique whereas values must be unique...
False, keys must be unique while values have no restriction
80
What happens if a key already has a value and then you add a different value?
It updates the value for that key. For example, if the key is 79 and the value assigned to it is "Joe Rogan takes horse dewormer!" and then you change the value of 79 to "Joe Rogan actually takes Soolantra (generic brand for Ivermectin for humans for the last 16 years)," then the value for the key 79 updates to the last quote!
81
In a HashMap, stores "Entry" objects in a Queue, True or False?
False, the "Entry" objects are stored in an array.
82
What does the Object's HashCode do?
It helps calculate the array index to store the object in.
83
In HashMaps, if the calculation for 2 indices yields the same value, what is the outcome?
HashMap collisions occur and then Java appends the Entry in a a LinkedList.
84
In HashMaps, if the LinkedList gets too long, what problem occurs?
Read operations are much slower.
85
What is the time complexity of HashMap Reads?
O(1) - Constant Time
86
In HashMaps, when LinkedLists get too long, what happens?
When the LinkedList is 8+, it is converted to a red-black tree.
87
# Fill in the blanks of the following code for HashMap: public class HashMapExample { public static void main(String[] args) { HashMap citizens = new HashMap\<\>(); // add two entries to the map citizens. \_\_\_\_\_ ("5001", new Citizen("Brian")); citizens. \_\_\_\_\_ ("4524", new Citizen("Diane")); // retrieve the value for the key 5001 System.out.println(citizens.\_\_\_\_\_ ("5001")); // print how many entries are in the map System.out.println(citizens.\_\_\_\_\_ ()); // print if the key 5001 is in the map citizens.\_\_\_\_\_ ("5001"); } } class Citizen{ String name; Citizen(String name) { this.name = name; } }
put put get size containsKey
88
True or False, HashSet is a class in the Collections API...
True
89
In the HashSet, are duplicates are allowed?
No, HashSets only store unique objects.
90
True or False, in HashSets order of elements is guaranteed...
False, Order of elements is not guaranteed.
91
Do HashSets have indices available?
No, there is no index available unlike in Lists.
92
Where do HashSets store values?
HashSets store values inside of a HashMap. Use set.add(); to store values tied to a key in a HashMap.
93
# Fill in the blanks of the following HashSet Code: public class HashSetExample { public static void main(String[] args) { HashSetExample friends = new HashSet(); friends. add(new Friend("Dan", 24)); friends. add(new Friend("Dan", 24)); } } class Friend{ String name; Friend(String name, int age){ this. name = name; this. age = age; } // hashcode method \_\_\_\_\_ _____ \_\_\_\_\_ () { return this.age; } // equals method \_\_\_\_\_ ______ \_\_\_\_\_\_ ( _____ \_\_\_\_\_\_ ) { Friend other = (Friend) obj; if (age != other.age) return false if (name == null) { if (other.name != null) return false } else if (!name.equals(other.name)) return false return true } }
1. public int hashCode 2. public boolean equals ( Object obj )
94
LinkedList is a(n) ______ collection
Ordered
95
True or False, LinkedList has an underlying array that changes size as needed...
False
96
True or False, Two objects with the same hashcode must be equal...
False
97
True or False, A Queue is a first-in last-out collection...
False
98
True or False, Two objects that are equal must have the same hashcode...
True
99
The equals method in java.lang.Object compares two objects and returns a(n) _____ .
boolean
100
A LinkedList in Java is _____ by default.
Doubly-linked
101
The performance of accessing a value in a HashMap is _____ .
Constant time
102
An ArrayList will grow _____ in size when it reaches its maximum capacity.
50%
103
A Stack is a _____ collection.
last-in first-out
104
A HashSet will maintain duplicate values, True or False?
False
105
Why does the main method have the static modifier?
Because the static modifier allows us to call all members in main without having to create an Object!