Skillstorm Intro to Coding - Data Structures in Java Flashcards

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
Q

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.

A

equals (and) hashCode

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

What are the three general-purpose Set implementations?

A

HashSet, TreeSet, and LinkedHashSet

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

HashSet Manipulation. See attached images…

A

public int hashCode

public boolean equals Object obj

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

LinkedList is a(n) ______ collection.

A

Ordered

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

LinkedList has an underlying array that changes size as needed? True or False

A

False

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

Two objects with the same hashCode must be equal? True or False

A

False

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

A Queue is a first-in last-out collection? True or False

A

False

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

Two objects that are equal must have the same hashCode? True or False

A

True

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

The equals method in java.lang.Object compares two objects and returns a(n)______.

A

boolean

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

A LinkedList in Java is _______ by default.

A

Doubly-linked

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

The performance of accessing a value in a HashMap is ______________.

A

Constant Time

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

An ArrayList will grow ___ in size when it reaches its maximum capacity.

A

50%

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

A Stack is a ______________ collection

A

last-in first-out

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

A HashSet will maintain duplicate values. True or False….

A

False

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

In Java with Object Equality, you can decide when two objects are equivalent, but with what method and how is it written in Java?

A
  1. The method is ==> java.lang.Object.equals method
  2. In Java it is written as ==> public boolean equals(Object other)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

In Object Equality, does it check the memory location by default?

A

Yes

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

In Object equality what must you override to compare object state?

A

The method

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

True or False, in Object Equality, some data structures require unique objects…

A

True

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

True or False, the hashcode isn’t used to give a consistent integer that represents the object…

A

False

44
Q

How is the hashCode method written in code?

A

public int hashCode()

45
Q

Is hashCode loved by HashMap, HashSet, and others?

A

Yes

46
Q

True or False, HashCode puts objects into a smaller group to optimize searching…

A

True

47
Q

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;

}

A

boolean equals ( Object obj)

48
Q

True or False, ArrayList is a class in the Collections API in Java…

A

True

49
Q

What does ArrayList do in Java?

A
  1. Creates an array
  2. Provides methods for working with ArrayList
50
Q

What is the ArrayList default size when it is created?

A

16

51
Q

True or False, ArrayList is not mutable…

A

False

52
Q

What happens if an ArrayList fills up?

A

It grows 50% larger to accommodate more elements

53
Q

True or False, ArrayList does not allow for duplicate elements…

A

False

54
Q

True or False, in ArrayList order of insertion is preserved…

A

True

55
Q

LinkedList is a Class in the Collections API in Java, True or False?

A

True

56
Q

What is the LinkedList a series of?

A

Nodes

57
Q

What are Nodes in LinkedList?

A

A Node wraps up a value (String, Pizza, etc.)

58
Q

How are Nodes in LinkedList connected?

A

They are “Linked” by a reference to the next Node

59
Q

Are LinkedList singly-linked or doubly-linked?

A

Doubly-linked

60
Q

How are doubly-linked Nodes connected?

A

The Nodes have a previous and next reference.

61
Q

LinkedLists allow duplicate elements, True or False?

A

True

62
Q

Order of insertion is not preserved in LinkedList, True or False?

A

False

63
Q

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;

}

}

A
  1. add
  2. add
  3. remove
64
Q

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.______ ());

A
    1. add
  1. get
  2. size
65
Q

Is the Queue an interface in the Collections API in Java?

A

Yes

66
Q

True or False, the queue is First-in/First-out…

A

True

67
Q
  1. When you Push in a Queue, what happens?
  2. What you Pop in a Queue, what happens?
A
  1. Node goes to the tail
  2. Node goes to the head
68
Q

True or False, the Queue doesn’t allow for duplicate elements…

A

False

69
Q

True or False, in a Queue, the order of insertion is preserved…

A

True

70
Q

True or False, the Stack is a Class in the Collections API in Java…

A

True

71
Q

Stack is Last-in/First-out, True or False?

A

False and True, it can be First-in/Last-out and Last-In/First-Out!

72
Q
  1. In a Stack what happens when we Push?
  2. In a Stack what happens when we Pop?
A
  1. Data/Node goes to the tail
  2. Data/Node comes from the tail
73
Q

True or False, Stacks don’t allow for duplicate elements…

A

False

74
Q

In Stacks order of insertion is preserved, True or False?

A

True

75
Q

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{}

A
  1. queue.offer
  2. queue.peek
  3. queue.poll
76
Q

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;

}

}

A
  1. push
  2. push
  3. pop
  4. peek
77
Q

A HashMap is not a class in a Collections API that stores key-value pairs, True or False?

A

False

78
Q

In HashMaps, how do you identify the value?

A

By the Key

For example, a Social Security Number maps to a U.S. Citizen.

79
Q

True or False, Keys do not have to be unique whereas values must be unique…

A

False, keys must be unique while values have no restriction

80
Q

What happens if a key already has a value and then you add a different value?

A

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
Q

In a HashMap, stores “Entry” objects in a Queue, True or False?

A

False, the “Entry” objects are stored in an array.

82
Q

What does the Object’s HashCode do?

A

It helps calculate the array index to store the object in.

83
Q

In HashMaps, if the calculation for 2 indices yields the same value, what is the outcome?

A

HashMap collisions occur and then Java appends the Entry in a a LinkedList.

84
Q

In HashMaps, if the LinkedList gets too long, what problem occurs?

A

Read operations are much slower.

85
Q

What is the time complexity of HashMap Reads?

A

O(1) - Constant Time

86
Q

In HashMaps, when LinkedLists get too long, what happens?

A

When the LinkedList is 8+, it is converted to a red-black tree.

87
Q

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;

}

}

A

put

put

get

size

containsKey

88
Q

True or False, HashSet is a class in the Collections API…

A

True

89
Q

In the HashSet, are duplicates are allowed?

A

No, HashSets only store unique objects.

90
Q

True or False, in HashSets order of elements is guaranteed…

A

False, Order of elements is not guaranteed.

91
Q

Do HashSets have indices available?

A

No, there is no index available unlike in Lists.

92
Q

Where do HashSets store values?

A

HashSets store values inside of a HashMap.

Use set.add();

to store values tied to a key in a HashMap.

93
Q

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

}

}

A
  1. public int hashCode
  2. public boolean equals ( Object obj )
94
Q

LinkedList is a(n) ______ collection

A

Ordered

95
Q

True or False, LinkedList has an underlying array that changes size as needed…

A

False

96
Q

True or False, Two objects with the same hashcode must be equal…

A

False

97
Q

True or False, A Queue is a first-in last-out collection…

A

False

98
Q

True or False, Two objects that are equal must have the same hashcode…

A

True

99
Q

The equals method in java.lang.Object compares two objects and returns a(n) _____ .

A

boolean

100
Q

A LinkedList in Java is _____ by default.

A

Doubly-linked

101
Q

The performance of accessing a value in a HashMap is _____ .

A

Constant time

102
Q

An ArrayList will grow _____ in size when it reaches its maximum capacity.

A

50%

103
Q

A Stack is a _____ collection.

A

last-in first-out

104
Q

A HashSet will maintain duplicate values, True or False?

A

False

105
Q

Why does the main method have the static modifier?

A

Because the static modifier allows us to call all members in main without having to create an Object!