Java Basics and OO Flashcards
Name the different class modifiers and what they do
- Access modifies: private, public and protected
abstract methods may or may not have abstract methods but they do not allow for instantiation, they do however allow for the class to be extended
final classes cannot be instantiated, inherited neither can their values ever be changed or updated
What are the different modifiers for methods
Private, public, static, final, abstract, protected
Upcasting vs Downcatsing
Downcasting is when an object is narrowed down into it’s more specific object. It is specializing or narrowing
Upcasting is when an object is taken up further up the hierarchy, so a child is represented as it’s parent. It is widening/generalization
Implicit vs Explicit casting
Implicit casting is done internally by a java compiler
Explicit casting data loss occurs and it involves using the casting operator (int)blah. Can result in data loss if you are performing a narrowing cast
What is an ADT?
An abstract data type is type or class that gives no information on how specific functions will be performed, only what those functions are. Gives an implementation independent view.
Why would someone not want to use arrays, when do they stop becoming relevant?
Arrays are very good at storing data and accessing data stored in the cells but removal and insertion into the array is very complicated and costly
Different collections spoken of and their pros vs cons?
Arrays, DLL, SLL and CLL.
- Arrays: - Arrays are fixed size, they have a limit that has to be adjusted
- Array cell removal and insertion expensive
- good for data that will be accessed sequentially
- SLL: - Great for elements in which you won’t have to access in reverse.
- Less memory
- Deletion at the end is O(n)
- DLL: - Uses more memory for the additional storage of a previous node element
- Great for forward and backward traversal
- removal from the tail is easy since it is possible traverse backwards
- CLL: -is made up of SLN but there is no distinct head or tail
- can also be made of DLL
What are the goals of OO?
Robust: can cope well with unexpected input (error handling and exception)
Adaptability: can run efficiently on different types of hardware and software
Reusability: The ability to reuse a peice of code as a component to different systems
What is position ADT?
It is a model that helps us conceptualize the position in a data structure where a single element is stored
Give the algorithm for removing at the head, then removing at the tail in SLL and DLL
SLL:
-removal at the head:
*set the head to point to the element after it was previously pointing to
set that element’s next to be null and the garbage collecter will handle it
- removal at tail:
- if next node == node to be removed
- make current node point to tail
- get next node and make it point to null
DLL: * head.setNext(nodeInSearch.getNext()) *nodeInSearch.getPrev().setPrev(head) *NIS.setNext(null) NIS.setPrev(null)
Stack with a SLL, what is the space used and what is the O of operations
- we store n nodes, hence the space used is O(n)
* time taken is O(1) {because we pop, push, isEMpty and size }
Define inheritance
When a class acquires properties of another class
Define inheritance
When a class acquires properties of another class
What is a literal? Name the different kinds of literals for the different datatypes
a literal is a constant value used to initialize a variable or other expressions. String literal “fieri erhg edj” intl, double, float, long, true or false.
When would you use a do while instead of a while loop?
A do while loop will always allow the body to run a t least once since the condition is checked after. Whereas a while loop will check the condition first before it executes the body.