TEST 1 Flashcards

1
Q

What are the parts of software development?

A

Requirements, Design, Implementation, Testing and Debugging, Maintenance

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

What two things shape what design needs to meet

A

functional requirements and software quality metrics

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

What are design choices

A

ALGORITHM- what sequence operations is preformed DATA STRUCTURE- how information is represented COMPONENT DESIGN - how software is designed

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

Application Software

A

Software designed to solve specific tasks for users, typically written in a high level language

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

system software

A

software that interfaces between the application software and hardware (or lower layers of system software) ie runtime, operating, virtualization

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

hardware

A

physical resources such as memory (RAM), processing power (CPU), storage (disk), and I/O devices

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

What are the key components of programming languages

A

grammar, semantics, libraries

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

interpreter

A

a program that executes programs written in a language

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

complier

A

program that translates program language into another language (typically a binary)

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

what are the primary programming paradigms

A

Imperative, object- oriented, functional, declarative

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

imperative

A

series of statements that mutate programs state, these statements detail how to preform each operation – outputs can depend on combo of inputs and external states, side effects are allowed

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

functional

A

immutability, all functions are pure (no side effects, output soley depends on inputs)

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

cons of imperative

A

Behavior can be harder to understand
● Debugging can be more challenging
● Order is more crucial

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

Pros of imperative:

A

Efficient
● Familiar
● Parallels how computers actually work

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

Pros of functional

A

Proofs and analysis are easier
● Can be easier to test and debug
● Order of execution is less important
(easier to parallelize)

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

Cons of functional

A

Can sometimes be less efficient
Can’t use familiar constructs such as
loops

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

declarative

A

express logic of what without detailing its control flow how

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

Pros of more declarative:

A

Abstracts away the low-level details
● Equivalent code is typically shorter
● Self-documenting (goal is explicit)

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

Cons of more declarative:

A

Can sometimes be less efficient
● Gives the programmer less control

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

Is OOP imperative or functional, in terms of mutation

A

Can be either! Imperative is more common

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

Is OOP imperative or declarative, in terms of abstraction

A

Some argue declarative: once objects are defined, you can use
them abstractly without knowing how they work under the hood
I disagree; someone still has to define how they operate before
using them!

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

pros of OOP

A

High level of abstraction & encapsulation
● High level of modularity
● High level of extensibility & reusability

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

Static type system

A

type-checking happens at compile-time
○ Variables have types; type system checks that assigned values
match declared variable types

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

Dynamic type system

A

type-checking happens at runtime
○ Values have types; variable types are inferred from their values

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

Pros of static

A

Guarantees that type errors will not occur at runtime;
effectively free,
automatic testing by the compiler

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

Cons of static

A

Code is a bit bulkier
can feel slow and onerous to write code that will pass static type checks

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

Cons of dynamic

A

May encounter runtime type errors (potentially after code has been running for a long time)

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

Pros of dynamic

A

● Code is sleeker
● Writing code is faster

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

strong typed

A

Variables have a
shallow fixed type
Variables have a
deep fixed type

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

weak typed

A

No notion of types
(e.g. assembly code,
machine code)
Values have
a fixed type
stronger typing leads to greater type safety(the extent to which type errors are disallowed)

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

type safe

A

can be used to describe languages or programs
If a language is type safe, programs written in it are type safe

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

examples of type safe languages

A

Java is a type-safe language
while JavaScript and C++ is not a type-safe language

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

evolution of programming languages

A

machine-independent, higher-level abstractions (functions), support for OOP, programming languages created , built in libraries

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

Java Compilation & Execution

A

source code, complier, bytecode, class loader, execution engine, result

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

type system of java

A

strong and statically typed- means you must declare things explicitly ot implicitly

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

primitive types

A

numeric types, boolean, char

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

strings

A

object type but behavior like primitive- cant be mutated and can be created without constructor- surrounded by double quotes

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

literal

A

a symbolic representation of a value ie x = 5 assignment of literal 5 to variable x

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

variable

A

a name associated with memory location ie int x a declaration that variable x is of type int

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

Declaration

A

a statement associating a variable with a type

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

Assignment or definition

A

a statement that places a value in
the memory location associated with a variable

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

Rules of Assignment

A

The defined value of a variable must match the declared type
The defined value of a variable must match the declared type

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

Primitive Type numeric definitions

A

long literals can optionally
include a trailing “L” float literals must include
a trailing “F”; without it java assumes its a double
double literals can optionally
include a trailing “D”

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

Primitive Type boolean definitions

A

boolean literals must be lowercase

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

Primitive type char definitions

A

char literals must be designated with
single quotes; String literals must be
designated with double quotes

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

initial values

A

For numeric types: 0 (or 0.0, or 0.0F)
For booleans: false
For chars: the null character (‘\u0000’)
For Strings, arrays, and objects: null

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

final keyword

A

final keyword tells the compiler that a variable will never
be re-defined ie final int x = 5

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

operator

A

a symbol that is built into a programming
language and has a specific, well-defined behavior, takes arguments (amount of arguments it takes it fixed)

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

Arithmetic operators:

A

take numeric expressions yield 1 numeric values
addition (can preform a cantaonation between string and int) plus operaters automatically takes care of any conversions
subtraction
multiplication
divison (overloaded 3/2 = 1 but 3/2.0 = 1.5)
modulus

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

logical

A

yeild a boolean value

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

relational operators

A

used for comparison used for any types

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

ternary

A

<boolean> ? <expr> : <expr> --> if <boolean> is true, yield <expr>;
otherwise, yield <expr>
String a = (b > c) ? “b is greater” : “c is greater or eq”
</expr></expr></boolean></expr></expr></boolean>

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

augmented assignment operations

A

with exceptions of += a and b need to be numeric a = b
a += b a = a + b
a -= b a = a - b
a *= b a = a * b
a /= b a = a / b
a %= b a = a % b

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

Control flow

A

the sequence of statements executed in a program

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

“straight-line”

A

sequences of statements excuted one after the next

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

“branches” to the control flow

A

conditionals and loops, multiple paths, ○ Which path is taken depends on the current program state

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

Blocks

A

group together a series of statements to be executed
In Java, blocks are generally denoted using curly braces { }

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

Scope

A

the set of blocks in which a variable is valid/usable– , a variable’s scope is the block in which it was declared
+ any blocks nested inside that block

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

two rules of scope

A

Can’t reference a variable outside of its scope
Can’t re-declare a variable within its scope

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

Conditionals

A

Control flow depends on the value of a boolean expression
if (<boolean>) {
// do something
...
} else if (<boolean>) {
// do something else
...
} else {
// do something different entirely
}
Exactly one if block per conditional,
followed by zero or more else if blocks
only first if block whos condition is true gets executed even if others are true
At most one else block per conditional - only executed if everything else was false</boolean></boolean>

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

Switch Statements

A

Control flow depends on the value of a single variable

switch (<variable>) {
case <value>:
// do thing 1
break;
case <value>:
// do thing 2
break;
default:
// do thing 3
}</value></value></variable>

these values must be constant, cannot be arrtibuary involving variables

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

default case

A

executes everything from the match case to the default statement if there is no break statement or if it does not meet any of the cases

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

pure function in java

A

“procedure” or “subroutine” = a
sequence of statements packaged together into a callable unit

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

parameters

A

variables that are defined in the function
header and can be referenced within the function

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

function header

A

public void addTwoInts(int num1, int num2) structure is privacy, return type (void if nothing is returned), name of function in camel case, the parameter list a list of <type> <name> pairs</name></type>

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

arguments

A

the specific values that get “bound” (assigned)
to the parameters (variables) when the function is called

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

local variables

A

Their scope is the function in which they’re declared

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

If you want your compiled program to be a standalone executable, you
must define a function with this exact header to serve as the “entry point

A

public class Main {
public static void main (String[] args) {
System.out.println(“hello world!”);
}
}

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

What are array limitation?

A

An array is declared with a type; all elements must be of that type
An array is defined with a size and cannot exceed that size

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

Why cant array sizes change?

A

ordered sequence of fixed-size storage locations
○ Each slot typically fits one byte (8 bits)
○ At runtime, all of the data that your program creates is stored in memory
If we fix an array’s size, we can place it in a contiguous chunk of memory,
making it easy to find the ith element

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

how to declare an array in java?

A

int[] myArray; int myArray[];

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

primitive types

A

values associated with variables stored directly in the chunk of memory associated with variable name (no arrows or pointers)

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

what does new keyword do

A

demonstrates a constructor for arrays and objects because java doesn’t allocate memory till after construction

74
Q

null

A

java reverses a special value, one that isn’t a valid memory address for data to represent null X doesnt point to null it is null

75
Q

accessing arrays

A

don’t reference each individual object elements directly embed within an array

76
Q

how to populate arrays

A

Use curly braces { } to explicitly
define the elements; this must be done
at the same time as construction
int[] myArray = new int[]{7, 5, 8};

Use square brackets [ ] to set
individual values in the array;
int[] myArray = new int[3];
myArray[2] = 8;
myArray[1] = 5;
myArray[0] = 7;

77
Q

how to print contents of string

A

int[] myArray = new int[]{1, 2, 3};
System.out.println(Arrays.toString(myArray));

78
Q

Fields

A

characteristics of an object (“has” relationship) object has
class variables, scope is entire class

ie
Student object
fields: college(string), gpa (int), hobbies (array

79
Q

Methods

A

actions that an object can perform (“can” relationship)
can be invoked on objects of those class types

ie
Student object
methods: eat(), sleep(), test()

80
Q

A class

A

a template for a specific type of object

81
Q

how to define a class

A

public class <classname> {
// field declarations
...
// method declarations
}</classname>

82
Q

this

A

Method calls & field
accesses within the
class should be
prefaced by this

83
Q

public

A

accessible outside of the class in which it was defined

84
Q

protected

A

only accessible in the class + any sub-classes

85
Q

private

A

only accessible in the class in which it was defined

86
Q

package-protected

A

only accessible in the package in which the
class is defined

87
Q

Constructor

A

a special method used to create new objects
Defined as a method with the same name as the class
no return type;
returns an instance of
the Bunny class
constructors often set up
initial values for the fields
public Bunny(String color, float weight) {
this.color = color;
this.weight = weight;
}

88
Q

objects constructed

A

use new keyword <classname> <varname> = new <classname>(<args>);
ie Bunny fluffy = new Bunny(“black”, 7.1F);</args></classname></varname></classname>

instance of the Bunny class

89
Q

Define Method

A

public void eatCarrots(int numCarrots) {
this.numCarrotsEatten += numCarrots;
}

90
Q

primative types

A

A variable of primitive type directly stores its value

91
Q

reference type

A

Objects are reference types, which means that a variable of
“object type” actually contains a memory address

92
Q

within a class method how do you refer to object operated on

A

you refer to object being operated on as this
When you invoke a method on an object, within the context of that
method call this becomes another reference to that object

93
Q

why do we use static

A

every function must be defined inside of a class
static fields & methods can be accessed without an instance,
e.g. Math.E, Math.PI, Math.pow(double a, double b)

94
Q

when can static fields be accessed

A

can be accessed without an instance
static methods cannot access non-static fields & methods
static fields & methods can be accessed by both static &
non-static methods
static fields’ values are shared by all instances of the class
static methods are invoked
using <class>.<method> rather
than <object>.<method></method></object></method></class>

95
Q

“wrapper”

A

this allows primitives to be treated as objects
These typically have the exact same names, but capitalized
○ e.g. long → Long, float → Float,
○ Two exceptions: int → Integer, char → Character
allow creators of java to define key methods for primitive types which otherwise wouldn’t be possible since primitive types cant have methods

96
Q

Value of

A

while you can construct a new
Integer in the “normal” way (new
Integer(5)), the recommended
approach is to use the static
valueOf() method, like so:
Integer x = Integer.valueOf(5);
public class Integer {
private int value;
public static Integer valueOf(int value) {
// Create a new Integer object wrapped
// around the given int value
}

97
Q

implicit type conversions

A

happen between primitive types and their corresponding wrapper classes

98
Q

Autoboxing & unboxing

A

automatic conversion from primitive
types to corresponding wrapper classes and vice versa
int a = 5;
Integer b = a;
Although a is technically the wrong type (int
vs. Integer), the compiler autoboxes a’s value

99
Q

explicit type conversations

A

Casting = a “true” conversion for primitive types; a reassurance
to the compiler for object types
double a = 5.0;
int b = (int) a;

100
Q

type conversions

A

Java will
automatically
perform a
widening cast
You must
explicitly cast
to convert to a
“narrower” type

101
Q

casting between real numbers to integer types

A

java rounds down when casting real number type to integer type

102
Q

object type casting

A

wont perform conversion bc it doesnt know how

Object o = …;
Bunny b = (Bunny) o;
not safe only checked at run time

103
Q

Conversion Functions

A

functions that are designed to
convert between types
○ Generally the preferred approach; checked at compile-time
○ You can define conversion functions for your classes if need be
○ The wrapper classes provide some conversion functions
String to numeric: Integer.parseInt(“5”),
Double.parseDouble(“5.0”)

Numeric to numeric: Number.intValue(5.0),
Number.doubleValue(5), etc

104
Q

continue

A

skip the remainder of the current iteration of the
(innermost) loop and proceeds to the next iteration

105
Q

for loops

A

for (<init>; <condition>; <update>) {
// do something
}
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
Typically all three statements (<init>, <condition>, <update>) are
included, but they’re actually all optional
but if you skip a statement a semi colon must take its place for (; i < 10; i++)</update></condition></init></update></condition></init>

good to use when number of iterations is known or iterating with indices

106
Q

for each loop

A

for (<vartype> <varname> : <iterable>) {
// do something
}
declaration of a variable that will be used to
refer to each successive element in <iterable>
int[] numbers = new int[]{0, 1, 2}
for (int num : numbers) {
System.out.println(num);</iterable></iterable></varname></vartype>

good when iterating over an iterable and only the values of elements are desired

107
Q

while loop

A

while (<condition>
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}</condition>

good when number of iterations is unknown
want to iterate while/until a condition is true

108
Q

when is a while loop better

A

event driven programming

109
Q

Do-While Loops

A

do {
// something
} while (<condition>);
first iteration happens regardless of wheter or not that condition is true</condition>

Sometimes <condition> needs to check a variable that you want
to define in the body; a while loop might require duplicate code</condition>

String line = reader.readline();
while (line != null) {
line = reader.readline();
}

String line;
do {
line = reader.readline();
} while (line != null);

goof when want first iterations to occur regardless of condition

110
Q

time efficiency

A

performance = how quick software runs = algo high, data , str low

111
Q

space efficiency

A

memory uti/complexity = how much space = data strc

112
Q

scalability

A

how software responds to increased work (impacted by all)

113
Q

extensibility

A

how easy it is to add a functionality = components

114
Q

reusability

A

software used for diff purposes = component

115
Q

modularity

A

how distinct software components are separated increases both above

116
Q

algorithm design

A

what sequence of operation is performed

117
Q

data structure

A

how information is represented

118
Q

component design

A

how software is structured

119
Q

out sub-directories

A

files that the compiler generates (.class files)

120
Q

namespace

A

All classes in the same package live in the same namespace
○ Classes defined in the same namespace can refer to each other
without requiring imports

121
Q

imports

A

import <package>.<class> = import the single specified class
○ import <package>.* = import the entire package (all classes
defined within it)
○ import <package>.<class>.<staticMethod> = import the single
specified static method</staticMethod></class></package></package></class></package>

import main.rice.subpackage1.*;
import main.rice.subpackage1.ClassA;

122
Q

Testing

A

the process of executing your software
with sample inputs in order to discover bugs

123
Q

Debugging

A

the process of scrutinizing your software
(and its behavior) in order to fix bugs found during testing

124
Q

Testing serves two purposes

A
  1. Discover bugs: are there situations in which the software does
    not behave as expected?
  2. Clarify the specification: are there situations in which we’re not actually sure what we expect the software to do?
125
Q

test case

A

(-2, 8) → -16,
you should include inputs and expected outputs!
A good test suite should include tests that cover:
○ Typical cases
○ Edge cases or boundary cases = inputs that are on the edge of
where the program’s control flow or behavior changes
○ (sometimes) Invalid inputs
○ (sometimes) Checks for unexpected side effects

126
Q

White-box testing

A

implementation is known

127
Q

Black-box testing

A

implementation is unknown

128
Q

Pros of white-box

A

● Can achieve “coverage” of every code
path

129
Q

Cons of white-box

A

May miss classes of inputs that the
implementation does not account for

130
Q

Cons of black-box

A

May miss code paths if implementation
has unanticipated special cases

131
Q

Pros of black-box

A

Forces you to systematically
brainstorm “classes” of like inputs

132
Q

test junit

A

@Test
void testMiniumNegative () {
int [] input = new int [] {3,4,5,2,1}
int expcted = 1;
int actual = SampleClass.minimum(input);
assertEquals(expected,actual);
}
}

133
Q

Code coverage

A

lines of code exercised by test suite /
lines of code in the program

134
Q

Pros of OOP

A

High level of abstraction & encapsulation
High level of modularity
High level of extensibility & reusability

135
Q

Cons of OOP

A

Not always a good fit
Can add a slight time overhead

136
Q

Principles of OOP (Abstraction via Encapsulation + Privacy)

A

never make fields public
○ Methods should only be public when necessary

137
Q

Principles of OOP (Single Responsibility + Delegation)

A

each class should have a purposes that is clear, bounded,
and fully contained within that class
Break the problem you’re solving into logical sub-problems
○ Encapsulate the logic for each sub-problem into its own class
○ Have the main class delegate tasks to the other classes

138
Q

Principles of OOP (Loose coupling + Decoupling)

A

Goal: keep the dependencies between classes low
● In practice:
○ Within your program, aim for loose coupling: classes interact
through interfaces (collections of public methods) that make
minimal assumptions about the underlying implementation
○ When interacting with other programs, aim for decoupling:
use common protocols & formats such as HTTP, JSON, URL

139
Q

loose coupling

A

when classes interact with each other through interfaces within your own program

140
Q

decoupling

A

interacting with other programs using common protocols and formats such as HTTP, JSON, and URL

141
Q

Principles of OOP Avoid Duplication

A

● Goal: don’t duplicate logic or functionality
In practice:
○ Use composition to define an object of one class as being a
composite of other objects
○ Use inheritance to define one class as being a more specific
version of another class

142
Q

Inheritance

A

declaring that one class “inherits” features
(fields) and/or functionalities (methods) from another class

143
Q

class hierarchy

A

A class can only directly extend one other class
○ However, there can be arbitrarily many “levels” in the hierarchy
A subclass can only have one direct superclass
○ A superclass can have multiple direct subclasses; this is called
hierarchical inheritance

144
Q

What is inherited

A

protected and public fields and methods
○ Package-protected fields and methods, if the subclass is defined
in the same package as the superclass
What is not inherited?
○ private fields and methods
○ Constructors*

145
Q

shadow

A

Define a new field with the same name but a different type (does
not have to be a subclass of the original)
Access the new version with this.<name>
○ Access the original version with super.<name></name></name>

146
Q

override

A

Define a new implementation, optionally with a more specific
return type (must be a subclass of the original type)
Access the new version with this.<name>
○ Access the original version with super.<name></name></name>

147
Q

concrete class

A

Fields: can be declared and defined
○ Methods: must not only be declared, but implemented
○ Constructors: can define a constructor and be instantiated
Concrete classes can inherit from other concrete classes, but
will often instead inherit from an abstract class

148
Q

Abstract Classes

A

○ Fields: can be declared and defined
○ Methods: can be declared and implemented or just declared
○ Constructors: can define a constructor; cannot be instantiated

149
Q

declare abstract

A

an abstract method cannot be private
abstract keyword can be used on classes & methods, like so:
○ public abstract <className> { ... }
○ public abstract <returnType> <methodName>(<args>);
end with semi color bc no definition</args></methodName></returnType></className>

150
Q

When should the superclass be abstract?

A

Pretty much any time that you don’t want to instantiate it!
○ It’s bad form to implement a method in the superclass if that
implementation won’t be shared by 2+ (ideally most) subclasses

when the super class will never be used on its own ie a farm represented using Aanimal would care about the specific animals

If a method’s implementation will vary, you can leave it abstract
○ But if a method’s implementation will be shared by most/all
subclasses, you can still implement it in the abstract class!

151
Q

abstract class style guide

A

the COMP 215 style guide requires that you begin the
name of an abstract class with an A for improved clarity

152
Q

Polymorphism

A

the ability for one method to take on different forms

153
Q

covariance

A

the ability to use
a more specific type than declared

154
Q

difference between abstract and interfaces

A

Fields: non-static fields cannot be declared or defined
○ Constructors: cannot define a constructor or be instantiated

155
Q

methods in interfaces classes

A

● All methods must be public
● public and abstract designators are optional (assumed if not
present); methods can be declared like so:

<returnType> <methodName>(<parameters>);
no non-static fields or constructors
concrete classes that implement an interface must define all unimplemented methods
</parameters></methodName></returnType>

156
Q

class using an interface methods

A

public class Bunny implements ICanEat { (implements not extends)

157
Q

When should you use an abstract vs. an interface class?

A

You want to define fields (and/or implement methods that
need to access fields)
You want to override any methods of the Object class

158
Q

why cant you use interfaces if u want to override any methods of the Object class

A

For any method, the version that’s inherited from the superclass
hierarchy (including from Object itself) will always take precedence

159
Q

Interface Inheritance

A

An interface can also extend another interface
○ In this case, we use the keyword extends rather than implements
● An interface cannot extend a class!

160
Q

do two equivalent objects have the same hashcode?

A

they must have the same hash code

161
Q

do two inequivalent objects have same hashcode

A

likey have different ones but noy guaranteed

162
Q

object class

A

automatically top class hierachy– all methods defined in object class can be overriden or invoked on all objects

163
Q

checking equality

A

The == operator:
● For primitive types, compares by value
● For arrays and objects, compares by reference → will evaluate to
false on two distinct objects with identical values

The equals(Object o) method:
public boolean equals(Object o)
Compares by value → will evaluate to true on two distinct objects
with equivalent values (if defined properly!)

164
Q

how to check types

A

○ Usage: <object> instanceof <class>
Boolean isBunny = fluffy instanceof Bunny;</class></object>

165
Q

Pattern matching:

A

equivalent to a type check
+ a cast, but without an
explicit cast
used ti check the type of an object and assign it to a new variable of that type if its type matches

166
Q

Hashing

A

Hashing is a technique used to store data that relies on
computing an integer “hash code” for each piece of data

167
Q

hash collisions

A

two objects that arent equal have the same hash code

168
Q

toString()

A

combines the name of the class with the
hash code of the instance, e.g.: “main.rice.Bunny@a9cd3b1”

you’ll typically want to override toString()
More informative: include the value of one or more key fields
● You can delegate to a field’s toString()
● You can combine multiple strings using concatenation (+) or
Java’s built-in StringBuilder class

169
Q

string builder

A

the StringBuilder class defines two constructors:
● a zero arg constructor representing the empty string
● a constructor that takes the first segment of the
String being built

170
Q

Every user-defined class must either

A

Define one or more constructors, or
Inherit a zero-argument (“default”) constructor from its superclass
hierarchy
Java will only search the superclass hierarchy for
a default constructor as long as it has not yet
found a constructor with > 1 arguments

171
Q

Every constructor must call its superclass’ constructor either

A

Explicitly: super(<args>); in the first line of the constructor
Implicitly: if no call to super() is found, the compiler tries to
automatically insert it (with zero arguments)</args>

172
Q

When a class Sub does not explicitly extend another class

A

○ Sub’s direct superclass is Object
○ Object defines a zero-argument constructor
○ Therefore Sub doesn’t need to define any constructors
○ If Sub does define constructors, they do not need to call super()
explicitly

173
Q

When a class Sub does explicitly extend another class Supr:

A
  1. If Supr defines a zero-argument constructor:
    ○ Sub does not need to define any constructors
    ○ If it does, these constructor(s) do not need to call super()
    explicitly (compiler will insert appropriate call)
  2. If Supr does not define any constructors:
    ○ Assuming Supr compiles… same as case 1!
  3. If Supr defines one or more constructors, but they all take one or
    more arguments:
    ○ Sub must define at least one constructor
    ○ These constructor(s) can have any number of arguments
    ○ Must explicitly call super(<args>) with appropriate arguments
    in the first line of the constructor</args>
174
Q

constructor takeaways

A

even if you define a constructor in the subclass,
the superclass(es)’ default constructor(s) will get called

if the
superclass has a
default constructor,
the subclass doesn’t
need a constructor

if at any point in
the hierarchy a class X has
no constructor, subclasses
of X will find X’s parent class’
default constructor if it exists

: if a class X has a constructor with
parameters but no default constructor, its child
classes cannot call super() without args (even
if there’s a default constructor higher up)

you can explicitly call the superclass’ non-default constructor

175
Q

obj instance class Other

A

checks if obj is instance of Class, if it is creates a new object (other) that is of class Class
returns a boolean

176
Q

what is privacy of abstract methods

A

public

177
Q

What are the two rules of assignment in Java?

A

values cannot be defined before variable type is declared

value of variable must be same type its declared as

178
Q

What privacy level should be used for fields? What privacy level should be used for methods?

A

For fields they should be protected or private and for methods it should also be protected or private.

179
Q

What is JVM, what are its two key components

A

Write once run anywhere, JVM defines platform-independent behavior. It is basically a run time system that includes an interceptor.
JVM takes the burden of resource management off the programmer’s shoulders. Serves as an extra layer of system software running beneath java programs. The 2 key components are a class loader which is responsible for loading classes and making them available for execution. The second is the execution engine which is key for executing and communicating with the operations system in order to manage resources.

180
Q

What is JIT compilation? Give an example of how JIT compilation could be used to optimize program performance.

A

During execution the compiler executes hot code (code run frequently) and compiles it; the idea is that it incurs a one-time overhead but will speed up all subsequent executions of the compiled piece of code which makes the trade off worthwhile for code that is run frequently.

181
Q

If you want your compiled program to be a stand alone executable ,you must define a function with what signature?

A

Public static void main (String[] args);

182
Q

define the 2 primary patterns of reusability in OO design

A

a. Composition: where a defined object of one class is a composite of other objects
b. Inheritance : where one class is defined as a more specific version of a different class