CNIT255 EXAM3

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/76

encourage image

There's no tags or description

Looks like no tags are added yet.

77 Terms

1
New cards

what do programs assume

everything goes right (open file, read #, use object)

2
New cards

explain david chen case study

financial system try-catch: nothing crashed so everything looked fine

  • transaction service couldn’t retry/tell the user

  • crash is bad, running on w/ wrong data worse


3
New cards

crashing vs silently running with bad data

  • stack trace, know where and when

  • no message, wrong records written, found weeks later


4
New cards

you should catch an exception..

only where you know what the program does next

5
New cards

helper vs service in catching exceptions

  • loads an account (can’t know whether to retry or roll back)

  • runs transaction (knows exactly what those words mean)


6
New cards

exception swallowing

program catches exception but continues execution without logging, processing, or reporting error

  • often empty/unhelpful catch block


7
New cards

throwable

every problem becomes an object

  • Java doesn’t print message and hope for best, it builds object

  • only Throwable can be thrown/caught


8
New cards

throwable family tree

throwable

  • error (UNCHECKED, DON’T CATCH)

    • StackOverflowError

    • OutofMemoryError

    • NoClassDefFoundError

  • exception

    • IOException (CHECKED)

      • FileNotFoundException

    • SQLException (CHECKED)

    • RuntimeException (UNCHECKED)

      • NullPointerException

      • ArrayIndexOutOfBoundsException


9
New cards

error: means, example, catch it? and why

  • JVM in trouble

  • OutOfMemoryError

  • no

  • stopping is safer


10
New cards

exception: means, example, catch it? and why

  • program hit a condition

  • FileNotFoundException

  • yes, where you can act

  • program can answer it


11
New cards

why not to catch an error

even though compiler lets you, catch block needs memory

  • catching it turns clean crash into unpredictable one

  • catch(Exception e) is already wide, catch(Throwable t) is too wide


12
New cards

what does the stack frame hold

main(), which calls run(), which calls load() (top → bottom)

  • one frame holds params, local variables, and return point

  • frames pushed on way in, popped on way out


13
New cards

StackOverflowError

recursion w/o base case (nothing to stop it)

  • frames: recursiveStackOverflowCauser() and 1000s more


14
New cards

NoClassDefFoundError

present at compile time, missing at run time

  • Main.class still names Helper, but JVM looks and finds nothing


15
New cards

NoClassDefFoundError: family, who asked?, cause, what to do

  • Error (unchecked)
    JVM for compiled code

  • class missing at run time

  • fix build or class path


16
New cards

ClassNotFoundException: family, who asked?, cause, what to do

  • Exception (checked)

  • your code

  • by-name lookup failed

  • must catch or declare it


17
New cards

when JVM lookup fails, ClassNotFoundException appears as…

the “Caused by” under NoClassDefFoundError

18
New cards

four things a program can do instead of crashing

  1. tell user

  2. roll operation back

  3. recover and carry on

  4. cleanly stop


19
New cards

RuntimeException: checked or unchecked

itself & everything below it is UNCHECKED

  • IOException checked, NullPointerException not


20
New cards

what decides checked/unchecked, and difference between both

compiler

  • checked = makes you say in advance what you will do

  • nothing thrown at compile time, compiler just refuses to build


21
New cards

what things are checked/unchecked (basic generalization)

  • checked: Exception and subclasses except RuntimeException

  • RuntimeException and below, including errors


22
New cards

what does the compiler demand of a checked exception

  • handle it

    • try {…}

    • catch (IOException e) {..}

  • or declare it

    • void load()

    • throws IOException

do neither and doesn’t compile.


23
New cards

try/catch

execution leaves try block at the throw

  • try block abandoned at point of throw, not resumed afterwards


24
New cards

how to catch matches (IS-A test)

} catch (IOException e) { runs if thrown object IS-A IOException

  • IOException subclass of FileNotFoundException


25
New cards

“As a result, FileNotFoundException will be thrown'.” what is actually thrown?

  • java.io.FileNotFoundException: missing.txt (No such file/directory)

  • caught by catch (IOException e) bc its IS-A IOException


26
New cards

using the exception object: getMessage() & more

  • e.getMessage() : detail text, can be null

  • e.getStackTrace() : full call chain

  • e.getCause() : earlier exception this one wrapped, if any


27
New cards

reading stack trace

top-down

  • Line 1: what was thrown and why

  • each “at”: one frame of stack (who called who)


28
New cards

reading cascading catch blocks

top-down

  • most specific first

  • first block whose type matches is one that runs, and is only one that runs


29
New cards

is wrong catch error a compile or runtime error

compile error

30
New cards

multi-catch

one block, several types (Java 7+)

  • two types must not be related


31
New cards

throws

declaring instead of handling

  • no try nor catch, and obligation moves to whoever calls it


32
New cards

throw: where, does, looks like, and how many

  • inside the body / a statement

  • throws one object, now

  • throw new X (“…”);

  • one object


33
New cards

throws: where, does, looks like, and how many

  • in header after brackets

  • declares what may come out

  • … load() throws IOException

  • as many types as you want


34
New cards

finally

for cleanup (closing files, releasing locks, returning connections)

  • always runs whatever happened (try succeeded, catch handled it, try or catch returned before value leaves)


35
New cards

how to close a resource correctly by hand

} finally {
	if (reader != null) {
		try { reader.close(); }
		catch (IOException e) { }
	}
}


36
New cards

what does if (reader != null) {...} check do

close() only runs if file was actually opened

37
New cards

try-with-resources syntax

closed automatically, however block ends

  • no finally / null check /nested try


<p>closed automatically, however block ends</p><ul><li><p>no finally / null check /nested try</p></li></ul><p></p>
38
New cards

what does try-with-resources do

keep original exception

  • close() throws - attached as suppressed exception

  • by hand just replaces old expression with new


39
New cards

NullPointerException

reference exists but no object on the other end of it

40
New cards

null

value that means “no object”

41
New cards

runtime exceptions: when to catch it vs fix it?

  • catch: only at boundary, request handler, or a simple record

  • fix: everywhere else, should be go-to


42
New cards

defining custom exception class

extends Exception gives you everything Exception already does

<p>extends Exception <strong>gives you everything Exception already does</strong></p>
43
New cards

throwing and catching custom exception

knowt flashcard image
44
New cards

extends Exception vs extends Runtime Exception

  • checked, callers must catch or declare it, use when caller can recover

  • unchecked, callers don’t have to do anything, use when caller made mistake


45
New cards

interfaces vs abstract classes

  • define capabilities; asks what can this thing DO?

  • share implementation; asks what IS this thing already?


46
New cards

abstract vs concrete methods

  • method with a semicolon (what, not how)

  • method with a body


47
New cards

what can an abstract class contain

  • abstract methods (signatures w/ no body, for subclasses to complete)

  • concrete methods (w/ bodies, inherited & usable)

  • instance fields

  • constructors (called by subclass w/ super(…)


48
New cards

can an abstract class be instantiated directly

no

49
New cards

how to declare an abstract class

access modifier abstract class class name

  • ex. public abstract class Cookie


50
New cards

abstract class setup

  1. state

  2. constructor

  3. subclasses need to add own definition

  4. subclasses MAY do x, y, z


51
New cards

access modifiers on abstract members

package-private abstract method: private String, public abstract void

  • can only be implemented by subclass in SAME package


52
New cards

can abstract methods be private, why or why not

no; nothing could implement it

53
New cards

can abstract classes be instantiated, why or why not

no

  • abstract = generic object = doesn’t exist


54
New cards

do abstract methods need an abstract class, and vice versa

yes, but abstract classes don’t need to have abstract methods

55
New cards

why do abstract classes have constructors

  1. object’s constructor runs first and sets shared field

  2. class constructor finishes own setup (parent part always built first)


56
New cards

constructor order

parent runs first

57
New cards

who must implement abstract method

inheriting subclass

  • concrete subclasses forced to implement, abstract can pass it on


58
New cards

abstract classes as types

cannot create a type, but you can declare one

  • each object runs own method at run time (polymorphism)


59
New cards

how does java fix the diamond problem

Java only allows one parent class

60
New cards

is inheritance of type, behavior, or state allowed and how

  • yes; one superclass with any number of interfaces

  • yes (Java 8 and on), default methods carry bodies into implementers

  • no; only one superclass can contribute instance fields


61
New cards

superclass vs interface

  • fields, constructor, intialization order

  • none of the above


62
New cards

interface

contract of what a class must be able to do, w/o explaining how

63
New cards

5 things an interface can contain, since when, and how

  • constants: Java 1.0; implicitly public static final

  • abstract methods; Java 1.0; implicitly public abstract

  • default methods: Java 8; a body inherited by implementers

  • static methods: Java 8; called on interface itself

  • private methods; Java 9; helpers for default methods


64
New cards

can interface contain instance variables

no

65
New cards

implicit modifiers in interfaces

you do not type and cannot remove

66
New cards

every interface method is _ whether it is _, _, or _. when is it not?

public; abstract, default, static

  • only Java 9 private helpers aren’t


67
New cards

implementing methods must be _ and why

public

  • override can widen access but never narrow it


68
New cards

how to implement interface and is override redundant

use keyword implements

  • @Override catches misspelled method name at compile time, so not redundant


69
New cards

default methods

every implementer gets default method for free and can override it if specified

70
New cards

why did Java 8 add default methods

  • adding abstract method to published interface: every implementer breaks

  • adding default method to published interface: nothing happens


71
New cards

how do default methods conflict and what type of error is it

if the interface bodies disagree; compiler error

72
New cards

how to resolve default method conflict

@Override and decide what to return

73
New cards

extending how many class(es) allows you to implement how many interfaces

one; any number

74
New cards

lambda components (ex. coffee -> coffee.iced )

  • coffee: parameter, no type needed

  • → : given that

  • coffee.iced : …return this (no return keyword, braces, or class name)


75
New cards

abstract class vs interface: answers what, fields, constructors, how many, instantiable

  • what IS this thing, real instance state, yes, one per class, no

  • what can it DO, constants only, never, infinite, no


76
New cards

when to use an abstract class vs interface

  • shared state/code or a real IS-A

  • capability, several roles, or loose coupling


77
New cards

can interfaces connect related classes too

yes via implements