0% found this document useful (0 votes)
8 views3 pages

COMP 250 Midterm 1 Crib Sheet

comp250 cheat sheet midterm 1

Uploaded by

christinacui841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

COMP 250 Midterm 1 Crib Sheet

comp250 cheat sheet midterm 1

Uploaded by

christinacui841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 250 Midterm 1 Crib sheet

Methods/classes: Most of the code will be inside a method and all code are part of a class. All methods are inside
of a class. The main method: public static void main (String[] args)) is a special one (runnable part of the code).
The execution of the program starts from the first statement of main and ends when the last statement of the
method has been executed.

Dead code/unreachable code (u.c): Dead code will never be executed but won’t raise an err, whereas u.c will
raise a CTE

Floating point: 1 ≠ 1.0 in Java! int will be a double if .0 after it (e.g. int x = 1.0 causes a cte). double x = 1/2 = 0
(incorrect value), but double x = 1.0/2 = 0.5 (correct val). double x = 1 is correct but not good style

Scope of variables (var/vars): Var only exists inside the block {} in which it was declared, it doesn’t exist outside
of that block. Starts to exist when declared and ends to exist when the block ends.

Arrays: int [3][] no errors. int [-nb] -> NegativeArrayIndexException (runtime error or RTE). type[] name = new
type[size] or {elements}. 0 <= index < arr.length, otherwise (o.w) ArrayIndexOutOfBoundsError (RTE)

Type casting for int/double: int -> double explicit cast not necessary, double -> int CTE (compile-time error) if no
explicit cast

Reference types (RT): Everything is an object except for primitive types/PT (byte, short, int, long, float, double,
char, boolean, starts w. lowercase letter). Objects (starts w. uppercase letter) store a reference (ref) to the location
in memory containing the value (weird output when printed!) rather than the value. E.g. arrays, strings, user-
defined objects, both store address where elements begin.

Mutable/Immutable: Can be changed after creation -> mutable (e.g. array), otherwise immutable (e.g. string).
User-defined obj immutable if only way to assign values to its fields = constructor. Mutable obj more flexible and
more efficient bc no need to create new instance to modify, but more error prone. Immutable obj easier to keep
track of changes and debug, and behave like primitive types.

Default values: byte/short/int/long = 0, float/double = 0.0, boolean = false, char = 0 and reference types = null
(no address). Uninitialized variables = CTE!

Packages/classes/methods: package: group (gp) of classes, classes = gp of methods and methods (sequence of
statements)

Access/Non-access modifiers: public (accessible from everywhere), private (within the class in which var was
defn), protected (subclass + package), default/no modifier (package). Static fields/methods/nested classes (belongs
to the entire class rather than an instance of the class), final vars/methods/classes (after initialization, field/var
values can’t be updated, methods can’t be overridden and class can’t be extended), abstract (class can’t be
instantiated, o.w. CTE; methods don’t have a body and must be inside an abstract class, they must be implemented
inside a subclass). Final fields/vars must be initialized (init.), o.w CTE!

Encapsulation: wrapping data and code acting on it in one unit using getters (accessor) / setters (mutator). To do
so, make all fields private and provide get/set when needed.

Shallow vs deep copy: A shallow copy (SC) is created by copying ref of old obj into new obj. Changes made to the
SC copy affect the original obj (bc same ref!). A deep copy (DC) is created by copying recursively the old obj
(creating new objects) into the new one. Changes made to the DC doesn’t affect the original bc they are indep.!

UML diagrams: + = public, - = private, ~ = no keyword, # = protected.

Inheritance: Parent class (PC) = superclass, child class (CC) = subclass can be derived (extended) from the parent.

java.lang.Object: Object is the only obj that doesn’t have a superclass, it’s the implicit superclass of every object
in absence of one.

Overloading/Overriding (OL/OR): 2 or more methods w. same name and diff. param. (signature = name + param)
= OL, 2 non-static methods w. same signature and return type (1 in PC, 1 in CC) and diff. implementation = OR; if
method static = hiding the method!
This/super: This is used to resolve naming conflicts (e.g. this.name = name). Super is used to: access members of
superclass (required when method has been OR/hidden and inside the constructor of subclass to invoke the
superclass constructor.

Type casting w. custom obj: Parent p = new Child(); = implicit (imp.) upcasting (UC) (always will work), Child c =
(Child) p; = explicit downcasting (exp. DC) required (to avoid errors use instanceof, err. name = ClassCastException
is RTE if failure). Imp. DC = CTE!

Polymorphism: Means “many forms”. CC can have own behavior yet share some functionality w. PC. E.g. A PC and
CC both have a method w. diff. implementation. Code: Parent p = new Child(); p.method();. The JVM invokes the
child’s implementation of method as p is an obj of type child.

Arrays time access/list defn: Constant = O(1) time -> indep. Of array size. List = ordered sets of elements,
where N = size of list

ArrayList: Data structure (DS) that uses an array to store elements and has a counter that keeps track of nb of
elem inside the array. Default size of Java’s official implementation = 10. Resizing (when not enough space):
Multiplies the current size by 1.5 and add 1 to the result. Time complexity: addFirst = O(n), removeFirst = O(n),
addLast = O(1) if array not full, removeLast = O(1). Space complexity = O(n).

Wrapper classes: Wraps a PT inside an object, turns it into an immutable RT. The conversion between wrapper &
their PTs is done automatically. It’s called autoboxing. Use this to initialize Java’s built-in data structures in order to
avoid errors.

Singly linked List (SLL): DS composed of a sequence of nodes, with a reference to the head (first elem.) and tail
(last elem, optional). Each node has a pointer (next) pointing to the next element of the LL, except the tail (tail.next
= null). Time complexity: addFirst = addLast = removeFirst = O(1), removeLast = O(n). Space complexity: O(2n) =
O(n) Traversal is only possible in one direction (H to T).

Doubly linked list (DLL): Similarly to a SLL, but with a pointer to the previous element as well, except the head
and tail (head.prev = null, tail.next = null). Hence traversal possible in both directions (H to T and T to H). Time
complexity: addFirst = addLast = removeFirst = removeLast = O(1). Space complexity: O(3n) = O(n). Use dummy
nodes to avoid edge cases (for node i, i=0 or i=size-1).

Bubble, selection, insertion sort: Goal = order list of int in ascending order. Bubble = simplest sorting algo,
repeatedly iterate through the list and swap adjacent elements if they are in the wrong order. Selection = consider
list as divided into 2 parts, 1 unsorted and 1 sorted (empty initially). 3 steps (select smallest elem in unsorted part,
swap it w elem. In init pos. of unsorted, change where arr is divided from sorted to unsorted). Insertion sort =
similar idea as selection, diff. procedure. 3 steps (select 1 st elem of unsorted part, insert it in the correct pos in
sorted part, change where arr is divided from sorted to unsorted). All 3 algo have O(n 2) time complexity.
Pseudocode below: Bubble sort (left), selection sort (middle), insertion sort (right)
Asymptotic notations: Big omega = lower bound, big theta = tight bound, big O = upper bound.

Ω ( g ( n ) ) ≤ θ ( g ( n ) ) ≤O(g ( n )). Functions ordered: 1 ≤ log 2 n ≤ n ≤ n× log 2 n ≤ n2 ≤ 2n ≤ n ! ≤ ∞ . Defns:


O ( g ( n ) )= {f ( n ) :∃ c , n0 f ( n ) ≤ cg ( n ) ∀ n ≥ n0 }, Ω(g ( n )): similarly as for O but with f(n) ≥ cg(n) instead of ≤,
θ ( g ( n ) ) ={ f ( n ) : ∃ c 1 , c 2 ,n 0 c1 g ( n ) ≤ f ( n ) ≤ c 2 g ( n ) ∀ n ≥ n0 }.
Loops in Linked lists: Use Floyd’s tortoise and hare algorithm to detect loops in LL. 3 steps: Move a pointer (fast
or slow) back to the head once collision hit. Move both pointers one step / iteration. Once they collide, get pointer to
first node in loop). The name comes from the story of the same name, the idea is for the hare (fast pointer) to
overtake the slow (tortoise) without meeting it.

You might also like