Java Advanced Flashcards
What is an interface?
A special type of construct in Java which is used as a contract to guarantee certain behaviors in a subclass
What type of entity is an interface?
An abstract entity
How to you implement an interface in your class?
You use the implements keyword in the class declaration - after extending a class, if you do.
What are some differences between abstract classes & interfaces?
- A class can extend only one class (abstract or not) but can implement multiple interfaces
- Interface variables are implicitly public static final. Abstract classes can declare any modifiers on instance variables.
- Interfaces can (as of Java 8) provide default methods, but all others are public abstract. Abstract classes can declare any modifiers on their methods.
What are exceptions?
Problems occurring in compilation or execution of your code
What is the use of exceptions?
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.
What is a checked, and unchecked exception?
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.
What are data structures?
Standardized patterns of code designed to organize one or more variables
What is an array?
- Fixed-size data structures that use an index to reference their contents.
- A linear collection of variables of uniform size, and in typed languages, uniform type.
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)
You would use a list, sometimes called a vector
How does memory addressing work?
Memory space is allocated in nits of bytes, and each variable occupies a certain number of bytes in memory.
What happens if you remove an element from an array?
The array will just be empty at that index until it is filled later
What is one shortcoming of arrays?
They must always be of a fixed length, & that length can only be set once.
What is a list or vector?
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.
If a vector is a wrapper around an array, how can it grow in size?
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.
What happens when an element is removed from a vector or a list?
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
What is a stack?
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.
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?
The element will be added to the top of the stack,
The element will be removed from the top of the stack
(think pancakes)
What does LIFO stand for, and what does it relate to?
Last in first out, this relates to how elements are added and removed from an array in using a stack
What is a queue?
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.
What does FIFO stand for?
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.
When are stacks generally used?
When you want to enforce an ordering of events.
When are queues generally used?
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.
What is a linked list?
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.
What is a linked list made up of?
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.
What is the benefit of a linked list?
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.
What is the downside of a linked list?
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.
What is a map?
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.
What is a variable?
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 is a computer’s memory broken up?
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.
What is “typing”
When a programming language separates values into groups, like integers, alphanumeric characters, or decimal numbers.