CSDS 132 FINAL
What is the difference between "=", "==" and .equals? What is the value returned by each operator?
"=" is an assignment operator that returns the type of the variable "==" checks if both objects point to the same memory location and returns a boolean .equals compares values in an object and returns a boolean
what is the end of line character?
"\n" - moves cursor to the next line when printing
what is the role of "is-a" and "has-a" when a forming object oriented hierarchy?
"is-a": if A is a B then make A and B classes and make A extend B "has-a" if A has-a B, then make B a method in A
what are the types from widest to narrowest?
1. Double 2. Float 3. Long 4. Int 5. Short/Char 6. Byte
what rules should you use to test loops?
1. test 0, test 1, test many 2. test first, test middle, test last
how do you create a class that contains a generic?
<T> must be added after the class name (in methods put <T> before the return type) (ex. public class Student<T> { })
how do you instantiate objects of that class?
<class name> must be placed when creating it
what are generic types?
A generic type is a place holder that indicates that the type will be specified later.
what is a jar file?
A jar file is just a collection of Java .class files, and when creating the jar file, we specify which class contains the main method that should be called when launching the program.
what is "super()"?
A reference variable that is used to refer parent class constructors (ex. salaried employee extends employee so ... public SalariedEmployee (String firstName, String lastName, String number, double salary) { super (firstName, lastName, number); this.salary = salary; })
what is "super"?
A reference variable that is used to refer parent class objects (ex. public class Vehicle { int maxSpeed = 120; } public class Car extends Vehicle { int maxSpeed = 180; public void display() { System.out.println("Maximum Speed: " + super.maxSpeed); } })
how does an abstract class differ from an interface?
An abstract class is a class that cannot be instantiated. It is used to enforce common behavior of all its child classes. Can contain methods with empty bodies, final, non-final, static and non static variables. interface: a class that can only contain method stubs (method header, but no method body), static final fields, and in Java 8 they can have static methods and method stubs can have default method bodies. you can implement multiple interfaces but only extend one abstract class
In what situations is an ArrayList better than a linked list and vise versa?
An arrayList is better for storing and accessing data while a linked list is better for manipulating data
why do we make variables final when using them inside an anonymous class?
Anonymous classes are stored on the heap while local variables and method parameters are stored on the stack. This memory difference leads to errors so Java limits anonymous classes to only using local variables if they are final.
what is the API?
Application Programming Interface: collection of prewritten packages and interfaces and their respective methods, fields, and constructors
What is an ArrayList?
ArrayList is a resizable array, however manipulating it is slow as it internally uses an array.
what is a method reference?
For when the body of the anonymous class calls a method with the same parameter signature as the interface method
what is "this()"?
It is used to call one constructor from the other of the same class
what is the optional class and what do we use it for?
It wraps a value that may be null. lets programmers define operations on the value and the option takes care of the null case
what is JavaDoc?
JavaDoc is a documentation generator for generating API documentation in HTML format from Java source code
What are ways that arithmetic on a computer differs from "real" arithmetic?
On a computer, there is a minimum value and a maximum value that can be reached based on the storage of the data type. When rounding from doubles to int, java rounds down regardless of decimal amount. You cannot set more than two values to be compared as greater than, less than, or equal to.
what are differences between primitive types and objects?
Primitives are independent data types, there is no hierarchy for them. Whereas every Object is descendent of class "Object" For primitive types, the value of the type is the binary representation of the value. For objects, the value of the type is the location of the instance in memory.
what is "this"?
Refers to the current object in a method or constructor
what is Java lambda expression?
Shortcut for creating anonymous classes that implement an interface with a single non default (or static) instance method
what is the heap? what is stored in it and how does it work?
The heap is unorganized memory used for data that needs to be more permanent. It contains all classes and objects For every object (instance of a class) created by your program, space for the instance is allocated in the heap.
What is a data type?
The type of a piece of data is what the piece of data represents. It is specified by the programmer and by rules of the programming language.
what does volatile do?
This informs Java that the value of the variables could be changed by another thread
what is method overriding?
When a subclass has the same method of its parent class, but with different parameters.
what are packages?
a collection of related classes
What is an array?
a collection of variables of the same type stored in a chunk of memory
what is a typecast? when is it done automatically and when must it be explicit?
a conversion from one type to another. typecast are automatically done when converting from a narrower type to a wider type. When going from wider to narrower, it must be explicit.
what is a linked list?
a data structure that stores values of the same type
how do you create a recursive method?
a method in which one of the statements contains a method call to the method itself
what is a constructor, what form does it have, and what is the order things are executed inside a constructor?
a method that is called by the new operator to initialize a class must have the same name as the class and have no return type a constructor is not inherited by classes that extend this class. each class must define its own constructors. if constructor is not made, java automatically provides a default one which takes no input (ex. public StudentAccount (String AccountNumber, String Name) { this.AccountNumber = AccountNumber; this.Name = Name; }
what is a nested class? how do you write one?
a non static nested class has full access to the members of the class within which it is nested, while a static nested class does not (ex. public class List1<T> implements Iterable<T> { private static class Node<T> { } })
what are threads and what can we use them to do?
a piece of the program that can run independently and concurrently with the rest of your program. useful if you want two things to happen at the same time without waiting
what are javaFX properties?
a private field along with a public (or protected) getter/setter method pair
how do you define a class
a template used to create objects and to define object data types and methods
what are variables? what are the rules when adding a variable to your program?
a variable is a name given to a location in memory. it is used to store data. (ex. int Number = 12;) you need to declare a variable before you can use it. the first use of a variable must be to assign it a value.
what similarities and differences are there in typecasting objects and typecasting primitive values?
all objects must be explicitly typecasted, and an object can only be typecasted to a type higher than it in the inheritance hierarchy.
what is anonymous class? how do you write one?
an anonymous class is a class created inside the body of a method. it does not have a name, instead you just state the interface you are implementing. can ONLY use local variables if they are final public static Comparator<Employee> compareByNumber() { return new Comparator<Employee>() { public int compare(Employee e1, Employee e2) { return e1.getNumber() - e2.getNumber(); } }; }
Iterator method
boolean hasNext() - returns true if there is a next element in list T next () - returns the next element in the list and iterates through list public LinkedListIterator(LLNode<T> firstNode) { nodeptr = firstNode; } public boolean hasNext() { return nodeptr != null; } public T next() { T element = nodeptr.getElement(); nodeptr = nodeptr.getNext(); return element; }
how do you copy an array?
by creating a new array of the same length and iterating through each element
how do you concatenate strings?
by using the + operator
what is the difference between class and instance fields, local variables, and method parameters?
class fields are stored inside of a class and exist as long as program is being run. an instance field is a separate variable stored in each instance of the class. Every instance has its own version. local variables are variables created inside a method and they exists for as long as the method is being ran a method parameter is a variable that stores an input of the method. in only exists in the body of the method.
what are rules when forming a good hierarchy?
each attribute must be moved as high up in the hierarchy as possible
what are exceptions?
exceptions are just objects and they behave exactly like all other objects in Java.
what is "finally" and how do it work?
finally { } ^ a block of code that is executed after exiting the try/catch block regardless of whether or not an exception was thrown
how do you write a for loop?
for (initial statement; condition; increment statement){ body }
how do you write a for each loop?
for (type var : array) { body }
what are generic types?
generics enable types to be used as parameters to classes, interfaces, and methods. Used by the compiler and only affects the current type The types that you specify must match (LLNode <String> node = new LLNode <String> ("Hi", null))
write code to catch and handle an exception
import java.lang.IllegalArgumentException; public class DNA { public static DNA string2DNA (String s) throws IllegalArgumentException { if (s.isEmpty()) throw new IllegalArgumentException ("String is empty"); else { } } or public static DNA string2DNA (String s) throws IllegalArgumentException { try { // code to try } catch (IllegalArgumentException e) { // code to handle error } }
what is the difference between insertion sort and merge sort? which is preferred?
insertion sort is when list is sorted by iterating through each element and putting it in its correct order. merge sort splits the list into two equal sized halves, sorts the halves, then merges the sorted halves together. merge sort is preferred as it is more time efficient when sorting a large amount of values.
Comparable method
int compareTo (T e) - compares this to e
Wrapper classes
int → Integer double → Double char → Character boolean → Boolean
what does new do?
it allocated memory for the object and initializes it
Why does Java have wrapper classes?
it allows you to store the primitive type inside an object
what is Java reflection and what can we use it for?
it is a way to look inside the structure of an object or class even if we don't have access to the source code
what is the purpose of the main method? how does it work? how do you write it?
it is what the java virtual machine calls to start your program. Each class may have only one main method. public static void main(String[] args) { //code to run }
what does synchronized do?
it locks code, for example, when put it on a method, that method is locked. any thread that wants to access a synchronized method of the class can not do so if another thread is using said method.
how do you remove an element in a linked list?
iterate through list until given point, remove element, have element before removed element point to the element after removed element
how do you insert an element into a linked list?
iterate through list, insert element at given point, have element before point to inserted element, and have inserted element point to next element
what is a multidimensional array?
java does not have multi-dimensional arrays. instead, java allows arrays to store other arrays. (ex. double[][] array = new double[5][8]; this is array of 5 elements an each element stores an array of 8 doubles)
what is "final"?
key word used in different contexts to define an entity which may only be assigned once. value of said entity cannot change after being set.
StringBuilder methods
length () - returns the number of characters in a given string (starts at 1 and includes spaces) charAt(int pos) - returns that character at position (starts at 0) setcharAt (int index, char ch) - sets the given char at the given index (starts at 0) append (String str OR char ch) - appends given string or char to the end of the stringBuilder string
String methods
length() - returns the number of characters in a given string (starts at 1 and includes spaces) charAt(int pos) - returns that character at position (starts at 0)
what is the difference between binary search and linear search? which is the preferred search technique?
linear search: traverses through the whole array and checks if the element searched for exists binary search: splits the array and checks whether the element is greater or less than the middle recursively until the element is found binary search is more efficient however it requires that the list is sorted first while linear search does not.
what are the pros and cons of an array?
pros - fast access to every element cons - cannot change the size after created and cannot insert or remove values without having to create a new array
what are the pros and cons of a linked list?
pros: can easily adjust the size by removing or adding elements cons: do not have fast access to every elements, only to the first and last
Iterable method
public Iterator<T> iterator() { return new LinkedListIterator<T>(getFirstNode()); }
Object methods
public String toString () { return "" + this.getNumber() + ""; } public boolean equals (Object o) { if (o instanceof Employee) { Employee e = (Employee) o; // code to compare this employee to e employee } }
a basic class with fields, constructors, setter and getter methods, and generic types
public class DoubleLinkedList<T> { public DLNode<T> front; public DLNode<T> back; public T element; public DoubleLinkedList() { front = back = null; } public DLNode<T> getFront() { return front; } public void setFront (DLNode<T> node) { front = node; } }
why do we make things public, private, or protected?
public is used so that code can be accessed anywhere in the program private is used so that the code can only be used in the body of this type. often used to hide the way in which something works, such as a LLNode. protected is used so that the code can be used in this type and in any type that extends or is in the same folder as this type
what is the difference between static and non static methods?
static methods are class methods. objects don't need to be created to invoke them non-static methods are invoked only on objects
What is polymorphism?
the ability of an object to take on many forms
how do you extend a class?
the default is that a class extends object, otherwise it must be specified by using "extends" (ex. public class MyClass extends HisClass () { })
how do you extend a class that contains a generic?
the same generic must be added after the new class name (ex. public class Student<T> extends Teacher<T> { })
what is the stack? what is stored in it and how does it work?
the stack is organized memory used for method calls. it contains local variables, method parameters, and bookkeeping needed to be returned by function every time a method is called, a "stack frame" is placed on top of the stack and is taken off when the method ends.
what is unit testing?
the testing of individual components in the source code, such as classes and their provided methods
how does the true type and current type determine what method or variable is accessed?
the true type determines which version of the method will be executed
how do you insert into an array
this is not possible because the length of an array cannot be changed after it is created. in order to insert a value into an array, a new array of a greater length must be created
what new issues do we have to deal with when coding with threads?
threads can cause problems if the two executions try to use the same variables.
how do you throw an exception?
throw new Exception()
What is the true type and current type of A a = new B ();
true type: B Current type: A
What is the difference between true type and current type?
true type: the type that the object is initially created as current type: which type the object is currently acting as
how do you catch an exception?
try/catch method: try{ // code that could throw an exception } catch (ExceptionType e) { // code that is executed if an exception of type ExceptionType occurs }
how do you declare an array variable?
type[] name (ex. int[] array1)
how do you declare a multidimensional array?
type[][] name = new type [rows][columns]
how do you initialize an array?
type[]name = new type[length] or type[]name = new type {values} (ex. int[] array1 = new int[10] or int[] array1 = new int {1,2,3,4,5})
what is the difference between a checked and unchecked exception?
unchecked - where programmer does not explicitly state what to do if an exception occurs. instead, the method is stopped and the error is thrown checked - when the programmer specifies what to do if an exception is thrown
When should you use + operator or StringBuilder?
use StringBuilder when making a large number of concatenations or modifications to a string
how and when do you use wildcards?
use wildcard <?> when there is never a case where the generic is required to be anything other than an object use wildcard <? extends A> when the generic is only required to be A or any of its subclasses use wildcard <? super A> when the generic is only required to be A or any of its superclasses
What is method overloading?
when a class has multiple methods by same name but with different parameters
when can we omit the type inside the <> brackets?
when initializing an object (ex. new LLNode<> ("String", null) will automatically assume that generic type is string)
how do you write a while loop?
while (condition) { body }
How do you access an element from an array?
with an integer index using the notation a[ I ] (count starts at 0)
What is wrapping and unwrapping, and how does it work?
wrapping: when a primitive is made into an object unwrapping: when an object is made into its primitive