Java Advanced Flashcards

1
Q

What is an interface?

A

A special type of construct in Java which is used as a contract to guarantee certain behaviors in a subclass

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

What type of entity is an interface?

A

An abstract entity

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

How to you implement an interface in your class?

A

You use the implements keyword in the class declaration - after extending a class, if you do.

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

What are some differences between abstract classes & interfaces?

A
  1. A class can extend only one class (abstract or not) but can implement multiple interfaces
  2. Interface variables are implicitly public static final. Abstract classes can declare any modifiers on instance variables.
  3. Interfaces can (as of Java 8) provide default methods, but all others are public abstract. Abstract classes can declare any modifiers on their methods.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are exceptions?

A

Problems occurring in compilation or execution of your code

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

What is the use of exceptions?

A

They allow Java to deal with runtime problems in a flexible manner. You can handle each type of exception in a different way, and allow your application to continue without crashing.

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

What is a checked, and unchecked exception?

A

Checked means that the compiler will look for & catch the error, & you will not be able to run your application until it’s fixed.
An unchecked exception will slip through & crop up in the middle of execution, potentially killing your application if not handled.

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

What are data structures?

A

Standardized patterns of code designed to organize one or more variables

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

What is an array?

A
  1. Fixed-size data structures that use an index to reference their contents.
  2. A linear collection of variables of uniform size, and in typed languages, uniform type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is you don’t care about memory adjacency & you want a collection that can grow or even shrink as needed? (an array wouldn’t work for this)

A

You would use a list, sometimes called a vector

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

How does memory addressing work?

A

Memory space is allocated in nits of bytes, and each variable occupies a certain number of bytes in memory.

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

What happens if you remove an element from an array?

A

The array will just be empty at that index until it is filled later

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

What is one shortcoming of arrays?

A

They must always be of a fixed length, & that length can only be set once.

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

What is a list or vector?

A

They are essentially wrappers around arrays, & expose the ability to add or remove contents through functions. Because they are wrappers for arrays they can still access their contents by an index.

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

If a vector is a wrapper around an array, how can it grow in size?

A

When an array in a vector or list gets full, it automatically creates a new array that is larger - often double the size of the previous array - & copies the contents of the old array into the new one.

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

What happens when an element is removed from a vector or a list?

A

The elements after the now-empty position are usually shifted down to close the gap. This helps keep these structures a little leaner, but these features are a trade off of performance for flexibility

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

What is a stack?

A

A stack is like a list in that it’s a flexible collection of elements, but stacks enforce a procedure for accessing their elements, which is Last-In-First-Out, or LIFO.

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

When you want to add a new element to a stack where will it be placed?
When you want to remove an element from a stack where will it be removed from?

A

The element will be added to the top of the stack,
The element will be removed from the top of the stack
(think pancakes)

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

What does LIFO stand for, and what does it relate to?

A

Last in first out, this relates to how elements are added and removed from an array in using a stack

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

What is a queue?

A

Think First-In-First-Out
A queue is like a stack, but instead of adding new items to the top of the collection, you add them to the bottom. This is just like a person getting into a line, the person at the front of the line gets removed first, & the newest person at the end of the line gets removed last.

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

What does FIFO stand for?

A

First in first out,
This is just like a person getting into a line - the person at the front of the line gets removed first, and the newest person at the end of the line gets removed last.

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

When are stacks generally used?

A

When you want to enforce an ordering of events.

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

When are queues generally used?

A

They are most often used for scheduling. When you want to try to ensure that every element has an equal wait time before being removed from the collection, you would use a queue.

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

What is a linked list?

A

They’re like a list in that they are a linear collection of elements, but they have no index, and they aren’t a wrapper for an array.

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

What is a linked list made up of?

A

Several structures called nodes. Each node is a wrapper for the actual variable or object you want to put in the linked list, but each node also additionally knows the memory address of its neighbor.

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

What is the benefit of a linked list?

A

The contents of a linked list do not have to be adjacent in memory, & the size is only exactly what we need it to be.

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

What is the downside of a linked list?

A

Navigating to any particular element requires searching through every element in the list prior to it, which is more time consuming than just accessing an element by an index.

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

What is a map?

A

A collection of node structures like an array list, but each node is not aware of any others. Instead, each node both wraps the actual variable or object being stored in the map, & also contains a key value that can be used to refer to the node in question directly.

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

What is a variable?

A

Computer programs use variables in the same way a math equation does, they’re a store for a value of some kind, but the particular value can be changed.

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

How is a computer’s memory broken up?

A

It is broken up into bytes, 8 bits, or binary digits. 8 bits makes a byte, 1024 bytes makes a kilobyte, 1024 kilobytes makes a megabyte, etc.

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

What is “typing”

A

When a programming language separates values into groups, like integers, alphanumeric characters, or decimal numbers.

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

In Java, what is the fixed amount of memory space that an integer is allowed to occupy?
What are the two effects that this has?

A

In Java an integer variable is allowed 4 bytes (32 bits) of memory.

  1. The range of integer values that a variable could store is limited to what can be expressed with 32 binary digits.
  2. The amount of memory reserved is always going to be 4 bytes, even if the actual value stored doesn’t require that much space.
33
Q

If a variable occupies more than one byte, how till the computer typically know to access that variable?

A

Typically through the address of the first byte reserved.

34
Q

What is an array in the context of computer memory?

A

A series of reserved memory spaces for a fixed number (and type) of variables.

35
Q

How are array indices wrapped?

A

They are always wrapped by brackets: myArr[2]

36
Q

What are strings?

A

A string is a series of alphanumeric characters, like a word or a sentence.

37
Q

What is a palindrome?

A

A series of characters that are the same when read forward or backward, like “racecar”

38
Q

In three words what is a list?

A

An Expandable array

39
Q

In truth, what is a list?

A

Just a structure built on top of an array, with functions to add list functionality on top of array functions.

40
Q

In Java what happens to a list when it outgrows it’s array’s size?

A

It will double in size. In other words the size is increased by more then is needed to keep resizing to a minimum.

41
Q

In a linked list what is the first node called?

A

The head

42
Q

In java what is a runner?

A

A second variable that runs through the rest of the list repeatedly, starting at whatever the current node is, and stopping at the last node.
(used to eliminate duplicates in a linked list in example)

43
Q

What are the benefits of using trees & graph’s as data structures?

A

While they are more complicated to search than other algorithms, trees & graphs can be designed to perform those functions with lower time complexity than many other data structures.

44
Q

What is a graph?

A

A series of nodes that may or may not have child nodes. These nodes may direct to each other in a circular fashion, & they may be bi-directional, i.e. nodes A & B both have each other as child nodes.

45
Q

In a graph what are child subnodes?

A

Collections of nodes that are part of the same graph, but no connection exists between them.

46
Q

Why would you most commonly use a graph?

A

Typically you would use them to emulate real life data structures. For example you could use them for mapping out the web of friend relationships on social media, or to represent intersections from a street map.

47
Q

What are the two common approaches used when searching a graph?

A

Breadth-first search & depth -first search.

48
Q

How does a depth-first search work?

A

You progress through each branch completely before moving on to the next branch.

49
Q

What is a depth-first search best used for?

A

When you are searching for a specific node, or when you want to guarantee that you check every one.

50
Q

How does a breadth-first search work?

A

You explore each neighbor before moving on to any children?

51
Q

When would a breadth-first search be useful?

A

When you’re trying to find a path from A to B.

52
Q

List two differences between a depth-first search and a breadth-first search?

A
  1. While a depth-first search covers searches through every available child node before moving to a neighbor, a breadth-first search visits every neighbor of a node before visiting any children
  2. A depth-first search can be written recursively or iteratively with a stack, but a breadth-first search uses a queue to track which nodes it still needs to visit.
53
Q

What are trees?

A

They are a sub-type of graph, but they do not contain any cycles, where node connections loop back into each other.

54
Q

With regard to trees what is a node with no children called?

A

A Leaf node.

55
Q

What is a binary tree?

A

It is a tree that only allows two children. This is the most common type of tree.

56
Q

Why are binary trees often used?

A

Because if they are sorted, they can be easy to search through.

57
Q

What is a binary search tree?

A

A sorted binary tree

58
Q

What does a binary search tree require?

A

That for every node, all left-hand children must be less than or equal to the node, & that node must be less than all right-hand children. All right-hand children of a node must be greater then that node.

59
Q

When is a binary search tree said to be balanced?

A

When the size of each sub-tree for a node is roughly the same size as all other sub-trees of neighbor nodes in the same level.

60
Q

What is a full binary tree?

A

One where every level has exactly 0 or 2 children.

61
Q

What is a perfect binary tree?

A

Both full and complete

62
Q

When traversing a tree & not performing a search what are the three ways of doing so?

A
  1. in-order traversal
  2. pre-order
  3. post-order
63
Q

How does an in-order traversal work?

A

It visits the left child before the current node, then the right child. This is true for every node. (recursive algorithm). In a binary search tree, the in-order traversal will visit every element in ascending order.

64
Q

How does a pre-order traversal work?

A

It visits the current node before exploring the left & right child

65
Q

How does a post-order traversal work?

A

It visits the node after the left & right child.

66
Q

What is a “collection” in Java?

A

An object that acts as a storage system for multiple elements, similar to an Array. However, collections in Java deal strictly with objects, and operate in wildly different ways.

67
Q

How does a priority queue maintain order?

A

In a manner supplied by the developer.

68
Q

What is a Map?

A

A type of container that stores information in key/value pairs. In some languages they are called dictionaries.

69
Q

Does the map interface extend the collection interface?

A

No

70
Q

Under the map interface, which well-known implementing classes will you find?

A

Hashtable, HashMap, and TreeMap

71
Q

How do Hashtable and HashMap store objects?

A

Based on a special identifier called a hash code, which is generated for an object using a special hashing function.

72
Q

What are Hashtable and Hashmap used for?

A

Both are used to sort and retrieve objects extremely efficiently.

73
Q

What are two important differences between Hashtable and HashMap?

A
  1. Hashtable is synchronized, meaning that in applications that use multiple concurrent processes or threads, a Hashtable can be accessed safely by two different processes at the same time.
    HashMap is not synchronized, but it is faster and preferred for applications where only one process needs to access the map at a time.
  2. A Hashtable does not allow any null keys or values whereas a HashMap will allow up to one null key and any value can be null.
74
Q

What are the three main map methods?

A
  1. Put
  2. get
  3. keySet
75
Q

Explain the map method “put”

A

Used to add a key/value pair to a map. A single map accepts a single data type for the key, & a single datatype for the value. These data types can be different.

76
Q

Explain the map method “get”

A

Used to retrieve a value by specifying its associated key. If the given key does not exist in the map, the returned result will be null.

77
Q

Explain the map method “remove”

A

Can be used to remove a key/value pair from a map by specifying the key.

78
Q

Explain the map method “keyset”

A

A method created for your convenience to return a list of all the keys in the map. It is typically used to iterate over a map. You could also use the values method to get a collection of the values contained in the map, but you won’t have the keys by which to identify them, if that’s important.