EECS 132 Final Exam

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is an abstract class?

- A class that cannot be instantiated - Used to enforce common behavior of all its child classes - Has abstract methods: a method w/ no body (AKA method stubs) - Must be overridden and given a body in a regular class

What is a linked list?

- A data structure that stores values of the same type in "nodes". - Each node contains an element and a pointer to the next node of the list.

What is the Java API?

- A set of pre-written classes that defines services for a programmer - a library of pre-written classes and how to use them for the programmer

What is a wrapper class in Java? Why does Java have wrapper classes?

- For each primitive type, classes called wrapper classes exist that allow you to store the primitive inside an object. - This allows us to use primitives where the code is expecting Objects => for example, when using generics, generics in Java only work w/ objects, so wrapper classes allow us to use primitive types with generics - also results in cleaner code that is easier to read

What is JavaDoc?

- JavaDoc produces documentation from Java source files by reading Javadoc comments and parsing it into standard documentation format, which can then be output into a variety of document formats

Between binary search and linear search, which is the preferred search technique depending on what assumptions we have to place on our array/list for the different searches to work?

- LS is preferred if you do not know much information about the array/list. - BS is preferred if you know that the array is sorted in increasing order

What is the difference between binary search and linear search?

- Linear Search: search every element from start to end - Binary Search: 1. Examine the middle element. 2. If the middle element is smaller than x, repeat on the upper half of the array. 3. If the middle element is larger than x, repeat on the bottom half of the array.

How is arithmetic on a computer different from "real" arithmetic?

- Mathematics in Java ≠ real mathematics b/c errors will crop up due to the fixed size of the numeric data types that would not appear if we were using real mathematics - in Java, numbers are represented in binary, therefore some fraction numbers (like 3/10) cannot be represented exactly

What is the true type of an object?

- The type (AKA class) that an instance is created as - You know based on the class that is to the right of the new operator - Ex.: cellPhone c = new iPhone => the true type is iPhone - Every object knows its true type, and the true type does not change

What is a typecast? How is it different for when typecasting objects?What things are similar?

- Typecasting a primitive type converts a value of one type into a value of a different type - Typecasting a non-primitive type changes how you can use that variable, but it does not change the location in memory - Typecasting a non-primitive type means that the methods and fields you can access change depending on what you have typecasted to - The way in which you typecast is the same - For compound types, automatic typecasting occurs when you go from a lower to a higher place in the hierarchy; for primitive types, it is automatic when you go from a narrower type to a wider type

What is unwrapping (AKA unboxing), and how does it work?

- Unwrapping AKA unboxing is when there is a conversion from a wrapper class to its corresponding primitive type - works by if you assign a wrapper class object to an argument accepting a primitive type

What is polymorphism?

- When an instance is many types (classes) at the same time - Ex.: when you create an instance of a class [new iPhone()], it is an instance of the type iPhone, but it is also the type SmartPhone, CellPhone, and Phone

How do you create a string? How do you concatenate strings?

- You place the desired string inside of double quotes - With the + operator

What is an interface?

- a non-primitive type like a class, but it cannot contain instance methods, fields, or constructors - purpose is to contain public abstract methods - can contain static final fields, static public nested types, static methods, abstract public instance methods (AKA method stubs) - can extend 0 or more interface

What is the scope of a variable (AKA when a variable exists and can be accessed)?

- a variable declared at the beginning of a class (AKA a field) exists as long as the class exists and can be accessed depending on its access modifier, but minimum anytime/anywhere in the class - a variable declared inside of a method (AKA local variable) exists inside of a method and ceases to exist after the method ends. It can be accessed only inside that method - a variable declared inside of a for loop only exists inside of that for loop and can only be accessed inside of the for loop. Once the for loop is exited, the variables ceases to exist and cannot be accessed

When are foreach loops used?

- allow you to directly loop through each item in a list of items (like an array or ArrayList), and perform some action with each item - these loops are also a Java shortcut for Iterable classes and for arrays

What are static variables? How long do they exist?

- also known as class variables; similar to instance variables but are declared using the "static" keyword and there is only one copy per class irrespective of how many objects you create - are created at the start of a program execution and destroyed automatically when the execution ends - it can be accessed without creating an object of that class

Generally, when converting to a wider type, Java converts the value to as ____ as possible to the original value.When converting to a narrower type, Java generally ____ the value.

- close - truncates

What is "finally"?

- code that is always executed upon exit of the try and catch blocks place after try- catch statement: finally{}

How do you create an exception?

- create a class that extends an Exception class - Ex.: public class ClassName extends ExceptionName

How does the true or current type of an object determine what method or variable is accessed?

- current type of an object . . . - you can only access fields available to the current type - you can only call a method with a name and parameter signature that exists in the current type - true type of an object . . . - determines the version of an instance method that is called

What does it mean to extend a class? How do you extend something?

- extending a class indicates that the extended class is the super of this class (the current class) - A class inherits all the public and protected instance methods of its super class - A class has access to all the public and protected class methods, instance and class fields, and nested classes of the super class - Ex.: public class GeometricFrame extends JFrame{ }

How do you extend a class that contains a generic?

- like normally just add the generic public class Java extends LLNode<T>{}

What are situations in which a linked list, array, and ArrayList are better than the other?

- linked list: 1. Benefits: can easily increase/decrease the size as needed 1a. can easily insert or delete at any location 2. Disadvantages: fast access to only a few elements - array: 1. Benefits: Very fast access to any arbitrary element 2. Disadvantage: Cannot change size after created 2a. can't insert or delete without needing to shift values. In order to change size, you must create a new array and copy all the data over - ArrayList: 1. Benefits: can change size 1a. fast access to elements 2. Disadvantages: slow at inserting and removing elements

What is an instance variable? How long do they exist?

- non-static variables that are declared in a class outside of any method, constructor, or block - are created when the object of a class is created, and destroyed when the object is destroyed

What is the stack? What does it contain?

- organized memory used to store information used by method calls - contains: 1. method parameters 2. any local variables declared inside the method 3. bookkeeping information needed to return something from a function

What are the differences between primitive and compound types?

- primitive types: 1. the value of the type is the binary representation of the value 2. There are specifically 8 of them and they are pre-defined in Java - compound: 1. the value of the type is the location of the instance in memory 2. There are an endless amount and can be either pre-defined or programmer-defined 3. A basic compound type is called a class, which consists of variables of any type, methods that take 0 or more inputs + possibly perform some action + return 0 or 1 value, and other reference/compound types

How do you create a class that contains a generic? How do you instantiate objects of this class?

- public class ClassName<generic> = new Classname<DataType> {} - Ex.: public class LLNode<T>{}

What is the format for an interface? How do you access an interface from a regular class?

- public interface MyInterface{ void methodStub(int x, int y); int methodStub2(); } - public class MyClass implements MyInterface{ }

How do you create a recursive method? Write an example of one.

- recursion is when a method calls itself continuously in the method - Ex.: public int add(int x, int y){ if(x ≠ 0){ return add(x-1, y) + 1; } else{ return y; } } *if you input add(3, 8), the return value would be 11

What is the difference between super. and super()?

- super.: a special variable used to access methods and fields (and nested types) of the parent class - super(): calls a constructor of the super class

What is the difference between this. and this ()?

- this.: a special variable inside an instance method that stores the address of the object the method is acting on - this(): calls a constructor of the same class

What is a primitive type? How many primitive types are there in Java?

- types already predefined in Java - 8 1. int 2. double 3. float 4. long 5. short 6. boolean 7. char 8. byte

What is the heap? What does it contain?

- unorganized memory used for data that needs to be more permanent 1. For every class your program uses, a class "object" is stored in the heap. The class "object" stores: - the super class of the class - all constructors of the class - all methods (static and non-static) defined in the class - all static fields declared in the class - any classes that are inside the class 2. For every object (instance of a class) created by your program (AKA the new operator), space for the instance is allocated in the heap. It stores: - the true type of the object - all non-static fields of all the polymorphic types of the object

What is a local variable? How long does it exist?

- variable defined within a block, method, or constructor - only exists within the block/method of code in which the variable was declared; you can access it only within that block

When and how do you use wildcards, super, and extends?

- when you want to add restrictions 1. T extends DataType => When you declare generic type T, you restrict T to be the data type or narrower 2. ? extends DataType => When you use a wildcard, you say the generic type must be the data type or narrower 3. ? super DataType => When you use a wildcard and super, you say that the generic type must be the data type or wider

What is the current type of an object?

- which of the polymorphic types it is currently typecast to or is based on Java's type rules - Every place that value is used in the code will have a current type associated with it

How do you catch an exception?

- with a try/catch statement try { - code that could throw an exception } catch (ExceptionType e) { - code that is executed if an exception of typeExceptionType occurs inside the try block - e is a variable that stores the exception object address, and it exists inside this block }

What is wrapping (AKA autoboxing), and how does it work?

- wrapping AKA autoboxing is the automatic conversion of the primitive types into their corresponding object wrapper classes - works by if you assign a primitive type to an argument accepting a wrapper class object

How do you typecast?

- you place the desired type in parentheses and immediately to the left of the value(desired-type)value

What is the value of the first element in an array?

0

How do you copy an array?

1) create a new array of the same length 2) copy each element to the new array int[] array2 = new int[array1.length]; for(int index = 0; index < array2.length; index = index + 1){ array2[index] = array1[index]; }

What is the difference between "=", "==", and equals?

1. "=": assignment operator - returns the value stored in that variable - assigning a location in memory 2. "==": equality operator - result is boolean - asking whether or not these objects are the same location in memory 3. .equals: determines whether two things are equal to each other - determines whether a string is storing the same letters as another string - it determines whether the contents AKA data is the same, not the location in memory AKA the address

What is the order that things are executed inside a constructor + what does new do?

1. Allocates space for the object AKA amount of memory is set aside 2. initializes that instance (done by calling a constructor) 3. Returns the address (location in memory) for the object

When forming an object-oriented hierarchy, what is the role of the "is-a" and "has-a" relations?

1. If A is-a B, then make A and B classes where A extends B 2. If A has-a B, then make B a method/methods in A

What is the difference between class and instance fields, local variables, and method parameters?

1. Instance Fields - a variable that belongs to the instance of a class - therefore each separate instance has its own version of the variable 2. Class Fields - a variable that belongs to the class as a whole - there is a single copy of the variable that all instances of the class share 3. Local Variables - variable defined within a block, method, or constructor - can only be accessed within that block, method, or constructor 4. Method Parameters - The variable(s) that store an input to the method. It exists only in the body of the method

What is the difference between a static method and a non-static method? When should you make it either one or the other?

1. SM: acts on the class as a whole - therefore when you call it, you do class-name.method-name(0 or more inputs) 2. NSM: acts on an instance of a class - therefore when you call it, you do - Instance-location.method-name(0 or more inputs separated by commas)

How do constructors work?

1. The first line of a constructor must be a call to another constructor (only place in the code where we can have a constructor call) - There are 2 possibilities 1a. super(): possibly with input in the parentheses, this calls the constructor of the parent class 1b. this(): possibly with input in the parentheses, this calls the constructor of the same class - If you do not explicitly have a constructor call as the first line of your constructor body, Java automatically places super() with no input there 2. The constructors do the following when they are run: 2a. The first line of the constructor that calls another constructor is executed 2b. All fields of the instance are initialized 2c. The rest of the constructor body is executed

How does a for loop behave?

1. The initial statement is executed 2. The condition is evaluated 3. If the condition is true; 3a. the loop body statement is executed 3b. the increment statements are executed 3c. step 2 is repeated 4. If the condition is false, go to the next statement of the program

1. What is a variable? 2. What do all variables have associated with them? 3. What is a variable used for? What is its condition?

1. The name given to a location in memory 2. a type 3. to store data: you can only store values with the appropriate type into the variable

How does an abstract class differ from an interface?

1. abstract: - can contain everything that a normal class can (including constructors) - can contain abstract methods - can contain normal methods 2. interface: - a class can implement more than one interface at a time - cannot contain instance methods, fields, or constructors

List the primitive types in Java from widest to narrowest

1. double 2. float 3. long 4. int 5. short/char 6. byte

How does stack work?

1. every time you call a method, Java places a "stack frame" on top of the stack 2. the frame stores everything listed above 3. Once a method finishes, the stack frame is deleted

What is form for a foreach loop? 1. Easy way 2. Connamacher way (with Iterable)

1. for (String inventoryItem : inventoryItems) { System.out.println(inventoryItem); } - reads as "for each inventoryItem in inventoryItems, print inventoryItem. 2. LinkedList<Integer> list = new LinkedList<Integer>(); list.addToFront(1); list.addToFront(2); list.addToFront(3); for (Integer x : list) { System.out.print(x + " "); } - reads as "foreach Integer in list"

How exactly are int's, double's, char's, and boolean's represented? Why does a programmer need to know this?

1. int: is an integer or whole number 2. double: is a number with a decimal point or in scientific notation 3. char: represents a character of text - are represented as single characters with single quotes surrounding them - Ex.: 'a', '1', etc. 4. boolean: represents either true or false **A programmer needs to know this because Java is a "strongly typed language". Therefore, if they use a type incorrectly, not only is it hard to understand your code because the type you used to represent your data does not make sense, but the smallest error in assigning types or using types will mean your code may not work

What are the 3 types of variables in Java?

1. local variables 2. instance variables 3. static variables

What are the rules for forming a good hierarchy?

1. move each attribute as high up the hierarchy as you can 2. minimize the number of interfaces - If something should not be instantiated itself, make it an abstract class (can't be like i want shape, like what kind of shape) - If something can be instantiated itself, then it can be a normal class - interface is when it does not fit in a specific place of the hierarchy but applies to certain classes in the hierarchy

How do you throw an exception?

1. place "throws ExceptionType" in the header of the method. (must be done if throwing a checked exception) 2. Explicitly throw the exception with the throw statement. throw new ExceptionType();

Why do we make some things public, private, and protected?

1. public: the code can be used/accessed anywhere in the program 2. private: the code can only be used in the body of this type 3. protected: the code can be used in this type and in any type that extends this type (or in any type in the same folder as this type)

How does a while loop behave?

1. the condition is evaluated 2. If the condition is true: 2a. the loop body statement is executed 2b. step 1 is repeated 3. If the condition is false, go to the next statement of the program

What is an array?

A collection of variables of the same type stored in continuous memory

What are generic types?

A placeholder (usually a single capital letter) that indicates that the type will be specified later

What is a non-primitive/compound type in Java?

A type that is both pre-defined and programmer defined; It can be made of primitive types and other compound types

How do you declare and initialize multi-dimensional arrays?

DataType[][] arrayName = new DataType[size][size]

What is an example of how wrapping/autoboxing works? Explain it.

Integer p = 4; p = p + 1; 1. the first line creates a new object of type Integer and then stores the value of 4 in it (AKA Integer p = new Integer(4);) 2. the expression "p + 1" requires an int, so the Integer p is replaced by the int value of 4 2a. the expression "p =" requires that the int value 5 be placed in an Integer, and so a new Integer object is created containing 5, and p stores the address of that object *Conclusion* - the line really reads "p = new Integer(p.intValue() + 1);" - p now has the address of a different Integer object

What are exceptions?

Objects that are used to send an error indication

T/F: A constructor is NOT inherited by classes that extend this class. Each class must define its own constructors

T

T/F: You cannot typecast between primitive and compound types

T

What is the difference between checked and unchecked exceptions?

Unchecked: - the programmer does not have to explicitly state what to do if an exception occurs (is thrown). (The default is to stop the method and throw the exception on the calling method) Checked: - programmer must specify what to do if the exception is thrown. - It is a compiler error to use code that can generate a checked exception but not explicitly state how to deal with it.

When are typecasts done automatically?

Values are automatically converted from narrower to wider types

What is a data type?

What kind of data that a value represents - It is either specified by the programmer or by the rules of the programming language - every piece of data has an unambiguous type - there are two kinds of types in Java: primitive and non-primitive

When must a typecast be explicit?

When you are converting from a "wider" type to a "narrower" type

What is method overloading?

When you have multiple methods of the same name as long as they have different parameters (as in different numbers and types of parameters)

What is method overriding?

When you replace an inherited instance method by writing a method with the same name, return type (or narrower in the case of non-primitive types), and parameters

How do you access characters from a String?

With the charAt() method - Ex.: String s = "Hello"; s.charAt(1) = e

When should you use the + operator and when should you use StringBuilder?

You can use + when creating a short string, but use StringBuilder when use up less memory, and you don't have to re-type Strings and create unnecessary Strings

What is the end of line character?

\n

What does the stack do when a compound statement (blocks of code) containing a variable declaration is encountered?

a "mini-frame" is placed on the stack that contains any variable declared inside of the compound statement - this mini-frame is removed once the compound statement is done executing

What is a class?

a basic compound (non-primitive) type that consists of ... 1. variables of any type (AKA fields) 2. functions (AKA methods) that take 0 or more inputs, possibly perform some action, and return 0 or 1 value 3. Other compound types

When writing a while loop, what must you have in order to remember how many times something it has gone through the while loop?

a variable (perhaps set to 0) before the while statement

What is a multi-dimensional array?

an array that stores other arrays

How do you access an element from an array?

array-name[element index]

What kind of expression is the condition statement?

boolean

You can typecast (convert) between all primitive types except ____.

boolean

What is the format for a for loop?

for (initial statement; condition; increment statements){ loop-body-statement; }

What is the equivalent for loop for the foreach loop: for(String s : a)?

for(int index = 0; index < a.length; index ++){ String s = a[index]; }

2. How do you insert into an array?

int[] array1 = new int[20]; array1[5]= 6

What is an example of creating an array that stores 30 ints?

int[] array1 = new int[30];

1. How do you insert into an array (at the beginning)?

int[] array1 = new int[30]; int array2 = new int[array1.length + 1] array2[0] = 25; for(int i = 0 ; i < array2.length - 1; i++){ array2[i + 1] = array1[i]; }

What is the form of a constructor?

it has the same name as the class and no return type: private final int dieSize; public Die(int dieSize){ this.dieSize = dieSize; }

What is the format for an abstract class?

public abstract class Shape{ public abstract double area(); public abstract double perimeter(); }

public static int sumOfEven(List<Integer> integerList) { int sum = 0; for (Integer i: integerList) { if (i % 2 == 0) sum += i; } return sum; } - How is this an example of unwrapping/unboxing?

public static int sumOfEven(List<Integer> integerList) { int sum = 0; for (Integer i: integerList) { if (i % 2 == 0) sum += i; **//Integer to int } return sum; } ** At this point, the remainder (%) and unary plus (+=) operators do not apply on Integer objects. Therefore, the compiler automatically converts an Integer to an int at runtime by invoking the intValue() method.

What is a constructor?

special methods that are used by the new operator to initialize an object. The method has no return type and the name must be the same as the class name

What is a package?

specifies a group of related classes

All local variables live in the (heap/stack).

stack

How do you declare and initialize an array variable (AKA create an array)?

type[ ] array-name = new type[how many variables you want]

When would you use a wildcard?

when you do not care about the generic and you will not do anything where you require a generic to be anything other than Object

What is the format for a while loop?

while (condition){ loop-body-statement; }


Set pelajaran terkait

Paramedic Nancy Caroline: Ch. 32/38

View Set

Scientific Revolution and Enlightenment (Chapter 16)

View Set

Nutrition Final Exam BIOL 1322 Blinn:Brown

View Set

RC206 Obstructive Lung Disease,COPD,Asthma, and related Dieases Chp 23

View Set

EMT Chapter 18 GI and Urology emergencies

View Set