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.
When would you use a for loop instead of a while loop?
A for loop is used when the number of iterations are known, while loops can be used if the number of iterations are not known but a certain condition can lead to the exit of the function
What are explicit flow control statements, name them and state when each would be used.
Return: to return a value from a function and exit the function
break: used to immediately exit for, for each, while, do while and switch statement
continue is used within loops to ensure that in the case of a particular iteration the loop will disregard the remaining statement and continue to the next iteration
What is the capacity of an array?
The length/ number of elements that it is able to accommodate for
What are the different ways to create an array?
Using the new operator and specifying the size or not specifying the size and initializing
What are the design principles of OOP?
Abstraction: distilling complicated parts into their most simple components. We can also use ADT that allow us to specify what a specific class that realizes that interface will do but not how it will do it Encapsulation: speaks of hiding away coupling details from components that do not need access to their implementation details Modularity: dividing components into organized separate functional units.
What are design patterns and how are they helpful?
Design patterns are a general template that can be applied to solve different types of problems.
They consist of a name (identifies the pattern), a template(specifies how the template is to be used) and a context(specifies in which scenarios the design pattern would be used)
A class can implement multiple interfaces but cannot extend multiple classes. T/F
True
overloading vs overriding
overloading is when methods of the same class have the same name but may have different return types and parameters. It a compile time polymorphism since the compiler can differentiate between the methods.
overriding is runtime polymorphism, it cannot happen without inheritance. The method signature and return types must be same across all the different classes
Dynamic dispatch
Dynamic dis: a mechanism where a call to an overridden method is resolved at run-time
Interface vs abstract classes
Interface classes have only abstract methods whereas abstract classes needn’t have only abstract method. Neither of them can be instantiated. Interface is declared using the interface keyword and not the abstract keyword. We can have multiple implementation of an interface but not multiple inheritance with abstrcact classes.
What does the finally clause do in an exception
It insures that the code within those braces run whether or not the exception happens
An interface class can only have one constructor
False, all the variables in an interface class are public static final by default so there is no need for a constructor, hence it does not have one
Checked vs unchecked exceptions and give an example of each
checked exceptions are outside the control of the programmer and the are verified during compile time e.g filenotfoundexception
unchecked exceptions: java does not verify these at compile time, it specifies an error in the code of the programmer .i.e. dividing by 0. Java does not force you to handle these types of exceptions
What is type erasure?
mechanism in java that removes all the parameters and replaces them with the actual datatypes specified by the user.
Why do parameters in geneerics allow for primitive datatypes?
During compile time, type erasure will take plsce and suppose instead of T, int is placed there but this is a problem becasue int does not extend Object
What are the critical members of a class
Instance variable(fields): represent the data associated with the class, it must have types which can be base types(int, dbl, bool) or refence types also known as class types Methods: blocks of code called to perform a specific action
What does the new operator do?
Returns a reference to a newly created instance/object and it is then stored in a variable for further use
What 3 things occur with the creation of a new instance of a class/an object
- memory is allocated for the object/instance that has just been created
- the new operator returns the memory address for where the object is stored and it is often assigned to an object variable for further use
- The constructor is called with the specified parameters to perform any additional computations that must be done
what is the dot operator used for?
It is used for accessing methods and properties of an object
What does the String datatype store and what methods does it offer
- Stores a sequence of characters
- Allows for indexing of characters with a
- concatenate which uses the + to join 2 strings together
Why does the stringbuilder run faster than + operator
- the + operator when used, a new string is created and then copies all the characters into the new string instance
- Stringbuilder will not do this, it is a mutable version of a string
immutable vs mutable
Immutable cannot be changed after creating but objects mutable can