Shared Flashcard Set

Details

Object-Oriented Programming
Terms from my OOP class in Java
144
Computer Science
Graduate
09/12/2024

Additional Computer Science Flashcards

 


 

Cards

Term
object-oriented programming
Definition
Designing a program by discovering objects, their properties, and their relationships
Term
class
Definition
a programmer-defined data type
Term
public interface
Definition
the features (methods, variables, and nested types) of a class that are accessible to all clients
Term
behavior of an object
Definition
the actions taken by an object when its methods are invoked
Term
state
Definition
the current value of an object, which is determined by the cumulative action of all the methods that were invoked on it
Term
instance variable
Definition
a variable defined in a class for which every object of the class has its own value
Term
modifier
Definition
a reserved word that indicates the accessibility of a feature, such as "private" or "public"
Term
type
Definition
a named set of values and the operations that can be carried out with them
Term
instance method
Definition
a method with an implicit parameter; that is, a method that is invoked on an instance of a class
Term
mutator method
Definition
a method that changes the state of an object
Term
accessor method
Definition
A method that accesses an object but does not change it
Term
implicit parameter
Definition
The object on which a method is invoked
Term
explicit parameter
Definition
A parameter of a method other than the object on which the method is invoked
Term
constructor
Definition
A sequence of statements for initializing a newly instantiated object
Term
object reference
Definition
A value that denotes the location of an object in memory. In Java, a variable whose type is a class contains a reference to an object of that class.
Term
static variable
Definition
A variable defined in a class that has only one value for the whole class, and which can be accessed and changed by any method of that class
Term
static method
Definition
A method with no implicit parameter
Term
array list
Definition
A Java class that implements a dynamically resizable array of objects
Term
wrapper class
Definition
A class that contains a primitive type value, such as Integer
Term
auto-boxing
Definition
Automatically converting a primitive type value into a wrapper type object
Term
unboxing
Definition
Automatically converting a wrapper type object into a primitive type value 
Term
difference between size and capacity of an ArrayList
Definition
Size is the number of elements, capacity is the amount of space allocated
Term
Default capacity of an ArrayList
Definition
10
Term
when enhanced for loop cannot be used
Definition
Algorithms that require indexing
Term
initialize an Iterator
Definition
Iterator iterator = list.iterator() where list is an ArrayList
Term
encapsulate
Definition
1. Package data members and method implementations into a capsule

2. Hide data members and method implementations in the capsule
Term
information hiding
Definition
Providing a public interace and keeping data members, method implementations, or even entire methods (especially helper methods) hidden
Term
reason for encapsulation
Definition
Make things simpler and less error-prone for the client
Term
If you provide public setters and getters, why not make data members public?
Definition
1. You can choose which data members to provide setters and getters for

2. You can perform input validation inside setter methods
Term
overriding
Definition
Changing the default toString(), equals(), and hashCode methods of Object
Term
Parameter and return types for overriden toString()
Definition
No parameters, return a String
Term
parameter and return types for overriden equals()
Definition
Object parameter, boolean return type
Term
parameter and return type of overriden hashCode
Definition
no parameter, int return type
Term
What does hashCode() do?
Definition
Hashes the data members of the class
Term
How many copies of a String literal found multiple times in a program will the String pool keep?
Definition
-- One if aliasing
-- Two if there is a new Scanner input
Term
Do you print the toString() method? What is the syntax?
Definition
No. When you call System.out.print, that method automatically calls toString().

System.out.print(foo)
Term
What does toString() do? Syntax?
Definition
Prints out all data members.

return "x = " + this.x + ", f = " + this.f;
Term
Why is it safer to use an ArrayList instead of a built-in array?
Definition
ArrayList can resize itself and built-in array can't, so you can accidentally assign elements out of bounds in a built-in array
Term
Syntax of a hash map when it is printed out
Definition
{hola = hello, adios = goodbye}
Term
initialize a HashMap
Definition
HashMap map = new HashMap <> ();
Term
how to add to a HashMap
Definition
put(key, value)
Term
how to get a value from a HashMap
Definition
get(key)
Term
What does getOrDefault() do, and what is its syntax?
Definition
getOrDefault(key, defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
Term
how to check if a key is in a HashMap
Definition
containsKey(key)
Term
functional decomposition
Definition
Breaking your code into parts, then breaking those parts down into parts, and so on
Term
components of a class
Definition
Data members, constructor (optional), setter and getter methods (optional), toString(), equals(), hashCode()
Term
What does equals() do?
Definition
Compares data members of "this" and "other"
Term
Why use ArrayList(intial Capacity) constructor?
Definition
ArrayList runs slower when it resizes, so if you know what your capacity is going to be, you should specify it
Term
Can setter methods change a private data member?
Definition
Yes
Term
Tell me about equals()
Definition
-- Primitives use == to compare values
-- In String and other library functions, equals() compares values
-- But in programmer-defined objects, equals() acts like ==
-- We must override this default with code that makes equals() compare values
Term
Differences between Java classes and C structs
Definition
1) Structs don't have functions, you have to send the struct in explicitly as a parameter to the functions. A Java class has methods that use an object as a parameter implicitly.

2) C has a variable that is a struct, and also a variable that points to a struct. Java object variables are pointers to objects automatically.

3) A Java object can access a data member directly (object.value). A C pointer accesses the object it points to, which then accesses the value (pointer->value).

4) Java's main() is static, and C's is not because every function in C is static.
Term
Three ways to set the value of public static int y, after initializing Foo foo = new Foo()
Definition
1) Foo.y = 99;
2) y = 99;
3) foo.y = 99;
Term
Why is main always static?
Definition
1) There is no Main class, so you cannot create a Main object.

2) By being static, main() can work in the same way for every object

3) main() is the starting point for execution, so we haven't created any objects yet
Term
What do all classes inherit from class Object?
Definition
1) toString()
2) equals()
3) hashCode()
Term
Why is Integer.valueOf(String s) static?
Definition
Integer is a wrapper class with a primitive int as a data member. This int is not used in valueOf; instead, a new int is created, based solely on the information relayed by the String argument and not by the int data member.
Term
String string1 = "cat";
String string2 = "cat";

What happens on the heap?
Definition
There is only one String in the heap that both variables point to
Term
String aString = "abc";
aString += "xyz";

What happens on the heap?
Definition
The heap has a string "abc".
The heap then creates a new string, "abcxyz".
Term
How to tell if a method is a setter or a getter
Definition
When you call the method twice in a row, a getter will return the same value
Term
What happens in the heap when you use toUppercase()?
Definition
A copy of the string is made and set to uppercase. The original string does not change or leave the heap.
Term
linear search
Definition
Searching an array or list for an object by inspecting each element in turn
Term
binary search
Definition
A search algorithm on a sorted array that narrows the search down to half of the array in every step.
Term
two-dimensional array
Definition
A tabular arrangement of elements in which an element is specified by a row and a column index
Term
What does it mean for a Java String to be immutable? Can its value ever be changed?
Definition
1) It's characters cannot be directly changed.

2) But the value can be changed by concatenation.

3) And the value can be changed by new variable assignment.
Term
What gets passed to a method implicitly as a parameter?
Definition
An object. NOT an instance variable.
Term
Argument vs. parameter
Definition
An argument is a parameter value given in the function call-- it is the “actual” data sent (and is sometimes called an “actual parameter”).

A parameter is defined in the function signature. It is a placeholder that receives the actual parameter. It is also called a “formal parameter”
Term
When an object-oriented function call is made, how can the execution of the function change the information in the caller? In Java, what kinds of arguments are passed “by value” and which are passed “by reference”?
Definition
Primitives are passed by value. No matter what you do to those parameters in the function, the corresponding actual parameter won’t change. Objects and arrays are sent by reference, meaning the address of the object/array is sent as the value which is placed in the formal parameter’s memory cell. If the function changes the object/array, it will affect the caller’s data. So if in the call obj and foo are objects, arr is an array, and x is an int, only obj, foo, and arr may be affected by the function. The function can also affect the world by returning a value (or printing something).
Term
When is toString called? What is the job of toString? If you don’t provide a toString in your class, what is the default behavior?
Definition
When you print an object, e.g., System.out.print(obj) print will call toString on obj. toString can also be called explicitly, e.g., s=obj.toString().

the job of “toString” is to serialize an object. It returns a string, typically including the name/value of all data members.

If you don’t provide a toString, the default toString, defined in class Object, returns the object’s class name and a hashcode.
Term
What is the job of an equals method? If you don’t provide an equals in your class, what is the default behavior?
Definition
equals is meant to compare the content of two instances of a class for equality. This often means comparing all of an object’s data members for equality, but it can be defined in other ways as well.

If you don’t provide an equals, the default behavior is to compare the addresses of the objects for equality (note: really the hashcodes are compared, and the hashcodes are based on address).
Term
When do you need to use the keyword “this”?
Definition
In an object-oriented method, “this” can be implicit. So you can refer to a data member directly, e.g., x = 5.

If there is a parameter or local variable defined with the same name as the data member, then you must refer to the data member using “this”, e.g., this.x

Otherwise, the parameter or local variable will be prioritized over the data member.


There is also another use of “this”. You can call one constructor from another using this.
Term
What happens on a new call, e.g., foo = new Foo(x,y)
Definition
1) object is allocated on the heap

2) constructor is called to initialize data members
Term
How do you create an array of 4 Foo objects?
Definition
Foo[] fooArray = new Foo[4];

fooArray[0]= new Foo()
fooArray[1]= new Foo()
fooArray[2]= new Foo()
fooArray[3]= new Foo()
Term
How is using an ArrayList simpler and less error-prone
than using a built-in array?
Definition
ArrayList will grow “automatically” when needed, array has a fixed size

You can append without considering size

You CAN still get an "index out of range" error with get()
Term
What happens in ArrayList add -- how does ArrayList grow?
Definition
add (int x)
if numElements == capacity
int [] temp = new int[capacity * 2]
copy elements from data member arr into temp
point arr to temp
put new element in
Term
What is iterative descent? What should each parseX function do?
Definition
parseX should look at the next token to see if it is an x
Term
The function call of the form: foo.func(x) is treated internally as func(foo, x). Explain.
Definition
Object-oriented syntax is such that the object itself, "foo", is sent to the function as if it is a parameter and mapped to "this" in the function, even though "this" doesn't appear in the function signature.
Term
When you construct a well-formed class in Java, what should be in it?
Definition
data members
constructors
toString
equals
hashCode
Term
When is a constructor called? What is the job of the constructor?
Definition
A constructor is called when you "new" an object. The job of the constructor is to initialize data members.
Term
What is stored on the stack?
Definition
Activation records, the data for each active method starting with main. That includes "this", the function's parameters, and the function's local variables.
Term
What is stored on the heap?
Definition
Dynamically allocated data. This includes objects created with "new", String literals, and arrays.
Term
data member
Definition
a variable inside a class
Term
function
Definition
a sequence of operations
Term
method
Definition
a function that is inside a class. Can modify the class's data members directly.
Term
dynamically allocated data
Definition
1) objects that get allocated with a "new" call

2) Strings
Term
pass-by-value and pass-by-reference
Definition
1) Pass-by-value passes the content of a variable. The variable itself does not change in the function, the function just makes a copy. Usually the value of the copy that is returned will be stored in a new variable in main.

2) Pass-by-reference passes the address, directly changing the variable.

3) If your parameter is an object, then you are sending a pointer to an object.
Term
function signature
Definition
modifier return_type variable_name (parameter_list)
Term
How big are addressable slots in memory?
Definition
1 byte
Term
What are primitive types and how much space is allocated for each?
Definition
integer - 32 bits
float - 32 bits
char - 16 bits
short - 16 bits
long - 64 bits
double - 64 bits
boolean - 8 bits (only 1 bit is necessary, but the minimum size of an address slot is 1 byte)
Term
How is an int represented in 0's and 1's?
Definition
An int is represented in a base-two, or binary, number system, with the leftmost bit denoting the sign (0 is positive, 1 is negative)
Term
How is a float represented in memory?
Definition
A float is represented as a binary number with a sign bit, a whole number, and a fractional part.

In the IEEE system, the whole number part has 8 bits and the fractional part has 23 bits.
Term
How is a string represented in memory?
Definition
A string is represented in the string pool, a section of the heap. Each character in the string gets two bytes, and at the end of every string is \0, an invisible character that means null.
Term
What does it mean for a value to overflow?
Definition
There is not enough space in memory to hold it. This can happen if an integer is added to another integer and there isn't enough space to hold its sum (or its difference in the case of two negatives)
Term
What is the heap?
Definition
the space in memory for dynamically allocated data. This includes objects created with "new", String literals, and arrays.
Term
What is the stack?
Definition
The stack stores the activation record, the data for each active method, including main. This includes "this", function arguments, and a function's local variables.
Term
What is an activation record?
Definition
The record of the data for each active method, including main
Term
What is the string pool?
Definition
the part of the heap where string literals are stored
Term
What does it mean for a language to have static typing versus dynamic typing?
Definition
Static -- the type of a variable or data member must be declared, and it usually does not change

Dynamic -- the type is not declared and can be changed at any time

In Java, a new varible can be created through casting of the original variable.

Types can also be changed by polymorphism. If suzy is an Animal, it could be a Bear, then be changed to a Cat because both Bear and Cat types are of type Animal.
Term
Difference between an object and an object reference
Definition
An object reference is the address of an object. Variables of a class type point to the address of an object.
Term
What do javac and java do?
Definition
Command-line: $ javac and $ java

javac is the compiler. It checks for errors and generates byte code.

Then the byte code goes to the Java Virtual Machine, which runs the program by changing the byte code to machine code.
Term
How can a method affect the world?
Definition
By returning a new value for a variable in main() or by modifying data members.
Term
"This"
Definition
"This" is an implicit parameter of a method. It represents the object that the method will operate on.

It also distinguishes data members from method parameters and local variables with the same name.

You can also use it to call one constructor from another.
Term
Static methods vs. instance methods -- what is the difference and how to access static data and methods (4)
Definition
Static methods are class-based. They are the same for any object.

Static methods don't use data members and instance methods do. Static methods use local variables from main.

They can be accessed by Class.method.

Instance methods operate on one object of the class.

They can be accessed by object.method.

Static methods don't use data members and instance methods do. Static methods use local variables from main.

There are static data members (constants).
Term
Understand the internals of ArrayList (3)
Definition
1) Add: add() grows the capacity of the ArrayList if needed, then adds the value passed in to the end of the ArrayList's elements. The capacity is grown by creating a new array with a larger capacity, copying the elements of the original array to the new array, and assigning the original array variable to the new array.

2) Adding at an index does this, but before adding in the new value, the values at and after the index shift to the right (a[i] = a[i - 1])

3) Remove: the values after the index shift to the left (a[i] = a[i + 1])
Term
The difference between String and StringBuilder and how to use each (3)
Definition
1) String is immutable and StringBuilder is mutable

2) String's characters cannot be changed directly, StringBuilder can use setCharAt()

3) String's value can be extended by concatenation, StringBuilder can extend with the append() method
Term
Declare an ArrayList
Definition
ArrayList myArray = new ArrayList <> ();
Term
Declare an array
Definition
int [] myArray = new int [10];
Term
When to declare data members and when to declare local variables
Definition
Use data members to represent a property of the class, to directly modify objects, must be accessible across multiple methods within the class

Use local variables when the data is needed temporarily within a specific method or block of code (like a loop) or when it is only needed in main
Term
What does a constructor do?
Definition
Initializes an object's data members
Term
What does toString do?
Definition
Prints a string of the class's data members and their values
Term
What does equals() do?
Definition
compares the content of one object to another
Term
Give an example of a visibility error that can occur
Definition
Trying to access one class's private data member from another class by using:
variable = otherClass.dataMember
Term
Why is it common practice to make all data members private? (3)
Definition
We don't want private data members to be accessed and changed improperly. This could especially conflict with use of an object's methods.

When the data members are private, you can choose which ones you want to make setter methods for and which ones you don't.

You can use input validation in a setter method.
Term
When to make a method private
Definition
When it's a helper method. Calling a helper method on its own could cause an error when the method it helps is called.
Term
How to use chars as numbers
Definition
getNumericValue(ASCII character)
Term
How to check if a char is a letter, a number, or either
Definition
isLetter()
isDigit()
isLetterOrDigit()
Term
How to check or change a character's case
Definition
toLowerCase()
toUpperCase()
isLowerCase()
isUpperCase()
Term
How to check for whitespace
Definition
Character.isWhitespace(char)
Term
How to turn a character into a string
Definition
toString()
Term
How to turn a string into an int
Definition
Integer.parseInt(String s)
Term
How to turn an int into a string
Definition
Integer.toString(int)
Term
How to tell if there is more input from a Scanner
Definition
hasNext()
hasNextBoolean()
hasNextDouble()
hasNextInt()
Term
How to access Scanner input
Definition
next()
nextLine()
nextDouble()
nextInt()
Term
How to get the character at an index of a string
Definition
charAt()
Term
How to tell if a substring exists in a string
Definition
contains(substring)
Term
How to get the index of a certain string character
Definition
indexOf()
Term
How to get a substring
Definition
substring(int i)
substring(int i, int j)
Term
How to convert a character to an integer
Definition
int newInt = (int) character;
Term
Turn an integer into a character
Definition
char aCharacter = (char) integer;
Term
ASCII arithmetic
Definition
'a' + 1
'a' + 'b'
Term
ASCII values
Definition
A = 65
Z = 90
a = 97
z = 122
Term
How to sort a dictionary by key
Definition
sorted_dict = dict(sorted(my_dict.items())
Term
How to sort a dictionary by descending value
Definition
sorted_dict = dict(sorted(my_dict.items(), key = lambda item: item[1], reverse = True)
Term
How to get the first key from a dictionary
Definition
list(my_dict.keys())[0]
Term
inheritance
Definition
The is-a relationship between a more general superclass and a more specialized subclass.
Term
superclass
Definition
A general class from which a more specialized class (a subclass) inherits
Term
subclass
Definition
A class that inherits variables and methods from a superclass but adds instance variables, adds methods, or redefines methods
Term
substitution principle
Definition
The principle that a subclass object can be used in place of any superclass object.
Term
overriding
Definition
redefining a method in a subclass
Term
overloading
Definition
Giving more than one meaning to a method name
Term
dynamic method lookup
Definition
Selecting a method to be invoked at run time. In Java, dynamic method lookup considers the class of the implicit parameter object to select the appropriate method.
Term
polymorphism
Definition
Selecting a method among several methods that have the same name on the basis of the actual types of the implicit parameters
Term
hash code
Definition
A value that is computed by a hash function
Term
interface
Definition
A type with no instance variables, only abstract methods and constants
Term
implementing an interface
Definition
Implementing a class that defines all methods specified in the interface
Term
inner class
Definition
A class that is defined inside another class.
Supporting users have an ad free experience!