CSCI 211 Test 1 Flashcards

1
Q

variables declared as “primitive types” ? represent information and are stored on the ?

A

directly; runtime stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

runtime stack

A

belongs to the program; keeps track of return addresses for method calls and variables that are local to a method (including method parameters)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

variables declared as class types are ? to objects

A

“references”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

objects are stored on the

A

runtime heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

the reference to an object is on the

A

runtime stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

pass by value

A

when calling a method, copies of the argument values are given to the method parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pass by references

A

a copy is given to the method parameters, but the copy of the reference points to the same object as the original (gives you the ability to make changes to the object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

object

A

a programming construct that consists of a state and set of behaviors; an instance of a class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

an object’s state is described by

A

data members (e.g. int count, boolean full)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

class

A

describes a set of objects of the same type (data members, methods); a blueprint that says how to build an object of that type; contains actual code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

interface

A

a set of methods that are provided by a unit of code; java lets us declare an interface as a set of methods (doesn’t require an implementation)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Java allows us to group a collection of classes into a ?

A

package; just put “package packagename;” at the top of all files in the package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

when application code wants to use a package

A

import packagename.*; (matches all classes in the package)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

API (Application Programming Interface)

A

the set of public methods supported by the classes in the package (their interfaces) is an example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

public

A

visible to the application

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

private

A

only accessible by class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

(no access modifier)

A

default = package access only

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

protected

A

visible to child classes and the package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

collection

A

a container that holds of group of objects, often called elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

the Java Collection Framework supports several types of collection:

A

List, Set, Maps

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

List

A

an ordered collection; order can be determined by insertion order or sometimes by the compareTo() method of the Comparable Interface or a Comparator object that’s passed to a sort() method of the collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Set

A

usually no guarantee that order will be preserved; null elements are allowed; NO duplicates are allowed (they will be ignored if entered)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Maps

A

a collection of key-value pairs (each key is associated with one value object - typically use key to look up a value)

24
Q

List Interface provides methods such as

A
void add(E, e);
void add(int index, E e);
25
Q

ArrayList vs LinkedList

A

ArrayList uses an array, which is quick for “random access,” but slow for insertion and deletion
LinkedList uses a “chain” of “Nodes,” each containing an element; slow for finding elements (“random access”) because it has to traverse the list from top to bottom, but quick for insertion and deletion

26
Q

Stack

A

a specialist list; elements are added and removed from the top of the stack (behaves like a stack of dinner places); an example of a LIFO data structure

27
Q

Stack methods

A
push(E element) - add element to top of stack
E pop() - remove the topmost element from the stack, then return element
E peek() - returns a reference to the topmost element
28
Q

how to create an instance of the Stack class

A

Stack< Integer > s = new Stack<>();

29
Q

Queue

A

elements are added to the tail (end) of the queue and removed from the head (beginning), like a bank teller line; FIFO data structure

30
Q

Queue methods

A
add(E element) - add element to end of queue (aka enqueue); throws exception on failure
boolean offer(E element) - add to end of queue; returns false on failure
E remove() - remove element from head of queue (aka dequeue)
31
Q

how to create an instance of the Queue class

A

Queue< Integer > q = new LinkedQueue<>();

32
Q

Deque

A

can add or remove elements from either end

33
Q

Deque methods

A

addFirst(E element)
addLast(E element)
E removeFirst()
E removeLast()

34
Q

PriorityQueue

A

order of elements is data dependent; elements are removed in order of priority, as determined by a compareTo() method

35
Q

We can make our own stack using Linked Lists:

A

a Node class will proved a reference to the next Node in the linked list, and also a reference to the data being stored (“payload”); the Node class could be an “inner class” - defined inside of another class; also, we should use generics to allow the same stack class to handle a variety of element types

36
Q

Test Driven Development

A

write Unit tests for all the cases you can think of at the same time as the code, then write just enough code to pass the test; tests should expose bugs, then you can use the debugger with the tests to diagnose the cause of the bugs; each test should be small and self-contained so they are each run separately

37
Q

how to write a test that should throw an exception

A
@Test(expected = NoSuchElementException.class)
void testName() {
     //test stuff
}
38
Q

how to write a test

A
@Test
void testName {
     //stuff
}
39
Q

how to write a Stack class

A
class Stack {
     class Node {
          T data;
          Node next;
     }
     Node top;
     public push(T element) {
          Node n = new Node();
          n.data = element;
          n.next = top;
          top = n;
     }
     public T pop() {
          if(top==null) {
               throw new NoSuchElementException();
          }
          T retval = top.data;
          top = top.next;
          return retval;
     }
}
40
Q

Iterators

A
for Collection classes, we can use a special loop syntax:
MyCollection collection = getCollection();
for(Element e : collection) {
     //do something with e
     S.o.pl(e);
}

the loop body executes once for each element where the loop variable e takes on each value in the collection in succession
with Java collections, an exception is thrown if the collection is modified (however, the Iterator interface has a remove() method that may be implemented if needed)

41
Q

to implement an Iterator

A
in the Collection: class Foo implements Iterable
must implement the Iterable interface
public Interator iterator();

in the class that implements Iterator: public class FooIterator implements Iterator
public boolean hasNext()
public T next()

42
Q

growth functions

A

we can estimate the behavior (execution time/memory consumption) of a program (or algorithm) by counting the number of operations (should be important to the performance) performed for a given problem size (the number of elements in a collection, denoted by n); we can relate n (the problem size) to the number of operations using a growth function (e.g. comparisons = 5n+2)

43
Q

Big O Notation

A

a way of characterizing the asymptotic behavior of the growth function for an algorithm; throw away all constants, except for exponents; keep only the term that grows most quickly with increasing n

44
Q

Big O Notation examples:

a) 5n+2
b) log(n)+n
c) n^2 + 400
d) 17
e) n * log(n)

A

a) O(n) “linear time”
b) O(n) “linear time”
c) O(n^2) “quadratic time”
d) O(1) “constant time”
e) O(n^2) “quadratic time”
f) O(nlogn) (best sorting algorithms are nlogn)

45
Q
Getting a growth function from code examples:
a) for(i=0; i < n; i++) {
// do something
}
b) for(i=0;  i < 12; i++) {
//do something
}
c) for(i=0; i < n; i++) {
for(j=0; j < n; j++) {
//do something
}
}
A

a) O(n) because the loop body executes n times
b) O(1) because the loop body executes 12 times no matter what
c) O(n^2) because the outer loop runs n times and the inner loop runs n times

46
Q

Logarithms

A

log base 2 of x tells how many times we can (recursively) divide x into two halves (e.g. binary search)

47
Q

public void push ( T element )

A
Node n = new Node();
n.data = element;
n.next = top;
top = n;
48
Q

public T pop ( )

A
if ( top == null ) {
throw new NoSuchElementException();
}
T retval = top.data;
top = top.next;
return retval;
49
Q

class Node

A

T data;

Node next;

50
Q

public void add ( E e )

A
Node n = new Node;
n.element = e;
if ( front == null ) {
front = back = n;
}
else {
back.next = n;
back = n;
}
51
Q

public E remove ( )

A
if ( front == null ) {
throw new NoSuchElementException();
}
E retval = front.element;
front = front.next;
return retval;
52
Q

Use an iterator to traverse a list and remove any occurrence of the number 5

A
Iterator < Integer > iter = list.iterator();
while ( iter.hasNext()) {
Integer i = iter.next();
if(i == 5) {
iter.remove;
}
}
53
Q

Write code that prints all the elements in a Bag containing Items

A

Bag < Item > theBag = foo.getRelevantItems();
for ( Item i : theBag) {
S.o.pl(i);
}

54
Q

public boolean hasNext ( )

A
if ( next == null) {
return false;
}
else {
return true;
}
55
Q

public E next ( )

A
if ( hasNext() == false ) {
return null;
}
E retval = next.element;
next = next.next;
return retval;