cmsc132 exam #1 review

0.0(0)
studied byStudied by 8 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/118

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

119 Terms

1
New cards

application programming interface (api)

description of what’s available in a class to the user

2
New cards

api examples

String, public features

3
New cards

data encapsulation

hides the representation of data from the user; the implementation details of a class are hidden from the public

4
New cards

procedural encapsulation

hides the algorithmic procedure; allows user to use system without worrying about specifics behind how the system works

5
New cards

autoboxing

Integer x = 7;

6
New cards

autounboxing

System.out.println(x+2);

7
New cards

comparable

interface that can be implemented to sort objects in “natural order”

8
New cards

comparable syntax

interface Comparable<T> {

public int compareTo(T other);

}

9
New cards

what are some java libraries that use comparable?

Collections.sort, Collections.max, Collections.min, Collections.binarySearch, Arrays.sort, Arrays.binarySearch

10
New cards

what similarities/differences to enumerations have to classes?

similarities: both have instance + static members

differences: constructors for enums are always private!

11
New cards

enumeration syntax

public enum Day {

SUN, MON, TUES, WED, THURS, FRI, SAT;

}

12
New cards

iterator

object that can sequentially access the elements of a collection

13
New cards

iterable interface

collections that implement this interface provide an iterator that can access its own elements

14
New cards

t/f: arraylists are iterable

true

15
New cards

write an iterator for the following arraylist: ArrayList<Cat> list = new ArrayList<>();

Iterator<Cat> iterator = list.iterator();

16
New cards

iterable interface syntax (include the three methods typically implemented in this interface)

interface Iterator<E> {

boolean hasNext();

E next();

void remove();

}

17
New cards

t/f: for-each loops are iterators

true

18
New cards

why do we need iterators/for-each loops?

1) the only way to cycle through elements of any java collection

2) efficient way to remove elements

19
New cards
term image
20
New cards

immutable class/object

instance variables set during construction but cannot be modified after that

21
New cards

mutable class/object

instance variables set during construction and might be modified later

22
New cards

what happens when a mutable data member has been encapsulated?

  • modification of its state within methods of the class is possible

  • direct mutation by external code is not possible

23
New cards

what is a privacy leak? come up with an example that generates a privacy leak, and then practice correcting it

when private mutable data is accidentally exposed

24
New cards

what is inheritance?

creating new classes based on existing ones

keyword: extends

25
New cards

what is always at the root of the inheritance tree?

Object

26
New cards

what is polymorphism?

allows user to define one interface and implement it in various ways

27
New cards

suppose A extends B and B extends C. consider the method below:
void foo(B x) {...}
a. what classes of objects can be passed as arguments to this method?
b. what methods (from which classes) can be called on x inside this method?

28
New cards

what is method overloading?

methods in class w/ same name but different parameter types

29
New cards

what is method overriding?

subclass “replaces” inherited method (same parameter types)

30
New cards

what is “this”?

keyword used to refer to current object in a method/constructor

31
New cards

how is “this” used in constructors?

used to initiate a constructor in a current class

32
New cards

how is “this” used outside of constructors?

33
New cards

what is “super”?

used to invoke constructors/methods/instance variables of immediate parent class

34
New cards

how is “super” used in constructors?

35
New cards

how is “super” used outside of constructors?

36
New cards

early (static) binding

compiler makes decision (what is the type of variable?)

37
New cards

early binding example

Person p = new Student();

p.walk();

with early binding, walk() will be called via the Person class

38
New cards

late (dynamic) binding

decision made at runtime (what is class?)

39
New cards

late binding example

Person p = new Student();

p.walk();

with late binding, walk() will be called via the Student class

40
New cards

which type of binding does java use?

late (dynamic) binding

41
New cards

upcasting

casting “up the tree”; done implicitly

42
New cards

downcasting

  • casting “down the tree”

  • never done implicity

43
New cards

what is the format for an equals() method?

@Override

public boolean equals(Object other) {

if (!(other instanceOf Person) {

return false;

}

Person p = (Person)other;

return name.equals(p.name) && ID == p.ID;

}

44
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

Person y = s;

yes, implicit upcast

45
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

Student y = p;

no, attempted implicit downcast

46
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

Student y = tricky;

no, attempted implicit downcast

47
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

(Student)p

complies, but throws exception

48
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

(Student)tricky

yes

49
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

(Faculty)s

no, student + faculty on same level of tree

50
New cards

Person p = new Person();

Student s = new Student();

Person tricky = new Student();

will the following code compile?

(Faculty)tricky

compiles, but throws exception

51
New cards

which type of casting is done implicitly?

upcasting

52
New cards

what’s a safe form of downcasting (using the teacher + student classes)?

Student s = (Student)p;

if (p instanceOf Student) {

Student s = (Student)p;

}

53
New cards

what’s the purpose of safe downcasting?

prevents exceptions (in the case that p (Person) refers to object that does not satisfy type Student)

54
New cards

what is the visibilty of public int x?

x is visible anywhere in project

55
New cards

what is the visibilty of private int x?

x is visible only in current class

56
New cards

what is the visibilty of int x (package visibility)?

x is visible in any class within current package

57
New cards

what is the visibilty of protected int x?

x is visible in any class within the current package + any subclass of the current class

58
New cards

will the underlined method compile?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public int m (int x) {…}

}

no

59
New cards

will the underlined line of code compile?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public void m (int x) {…}

}

override

60
New cards

will the underlined line of code compile?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public void m (double x) {…}

}

overload

61
New cards

which of the numbered method is called?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public int m (int x) { … } (1)

public void m (int x) { … } (2)

public void m (double x) { … } (3)

}

Base x = new Base();

Base y = new Derived();

Derived z = new Derived();

x.m(5);

1 (superclass)

62
New cards

which of the numbered method is called?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public int m (int x) { … } (1)

public void m (int x) { … } (2)

public void m (double x) { … } (3)

}

Base x = new Base();

Base y = new Derived();

Derived z = new Derived();

y.m(6);

2 (override in subclass)

63
New cards

which of the numbered method is called?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public int m (int x) { … } (1)

public void m (int x) { … } (2)

public void m (double x) { … } (3)

}

Base x = new Base();

Base y = new Derived();

Derived z = new Derived();

y.m(7.0);

won’t compile

64
New cards

which of the numbered method is called?

public class Base {

public void m (int x) { … }

}

public class Derived extends Base {

public int m (int x) { … } (1)

public void m (int x) { … } (2)

public void m (double x) { … } (3)

}

Base x = new Base();

Base y = new Derived();

Derived z = new Derived();

z.m(8.0);

3 (overload in subclass)

65
New cards

x == y

true when x and y are the SAME object (aliases)

66
New cards

x.equals(y)

true when x and y are objects that “look” the same (same state)

67
New cards

equals() is called extensively throughout __________ framework

collection

68
New cards

is multiple inheritance allowed in java?

no!

69
New cards

can you override a variable that’s inherited

yes, it’s called (shadowing) (not recommended)

70
New cards

what are the benefits of abstract classes (in comparison of interfaces)

  • instance variables (state)

  • constructors

71
New cards

what is the benefit of interfaces (in comparison of abstract classes)

a class can implement more than one

72
New cards

what’s the purpose of the keyword final?

  • final int x = 7; → x cannot be modified

  • final Cat c = new Cat(); → c cannot refer to a different Cat

  • final void foo {…} → cannot override/create subclass

73
New cards

abstract class

  • cannot be instantiated directly

    • can’t use “new”

  • purpose: to be extended in different ways

74
New cards

unchecked exceptions

  • things that should never happen

  • typically programming errors

  • usually don’t require try/catch handlers

75
New cards

checked exceptions

  • represent common events that can occur even w/ correct implementation

  • normally require a catch block (catch/declare):

    • handle w/ catch block

    • declare method that throws it

76
New cards

unchecked exception class syntax

public class IllegalFishPositionException extends RuntimeException {…}

77
New cards

checked exception class syntax

public class NetworkTooSlowException extends Exception {…}

78
New cards

static nested classes syntax

public class A {

public static class B {

}

}

79
New cards

inner nested class syntax

public class A {

public class B {

}

}

80
New cards

what’s the difference between static nested classes and inner nested classes?

  • static classes: essentially like having two ordinary classes

  • inner classes: instance of inner class is always “attached to” an instance of the outer class

    • “shares” its members

    • no privacy between A + B

81
New cards

instantiating inner class objects syntax

Outer outer = new Outer();

Outer.Inner inner = outer.new Inner();

82
New cards

comparator interface

separate class (implementing Comparator) to define “additional orderings”

83
New cards

comparator interface syntax

interface Comparator<T> {

int compare(T obj1, T obj2);

}

84
New cards

anonymous inner classes

  • class w/ no name

  • declared + instantiated in the same spot

  • must implement an interface or extend a class

  • cannot have constructors

  • if declared in static method, object in not “attached to” any instance of outer class

85
New cards

functional interface

interface w/ just one abstract method

86
New cards

functional interface examples

BinaryFunction, Comparator, Comparable, Runnable, ActionListener

87
New cards

lambda expressions

  • objects that contain code for a single method

  • no state, exactly "one behavior”

  • can assign to variable whose type is a functional interface

88
New cards

lambda expressions syntax

BinaryFunction multiplication = (a, b) → {

return a*b;

};

89
New cards

what are some advantages lambda expressions have over anonymous inner classes?

  • uses (slightly) less memory

  • runs (slightly) faster

  • sometimes fewer objects at runtime (reduces need for garbage collection)

90
New cards

what are two ways to copy an object

1) copy constructor

2) clone() method (if class implements Cloneable)

91
New cards

clone()

  • inherited from Object class

    • returns a very fast shallow copy of entire object

    • class of new object is same as the class of the original

  • implements Cloneable interface

    • no methods to implement

92
New cards

clone() syntax

public Triangle clone() throws CloneNotSupportedException {

return (Triangle)super.clone();

}

93
New cards

when does a static initializer block run?

runs once, when class is first loaded

94
New cards

static initializer syntax

public class Foo {

static {

}

}

95
New cards

instance initializer syntax

public class Foo {

{

}

}

96
New cards

when does a instance initializer block run?

  • runs every time an object is instantiated

  • runs before the constructor

  • can eliminate redundancy in constructors

97
New cards

reference copy

two variables point to exact same object in memory

98
New cards

reference copy example

Person p = new Person();

Person refCopy = p;

99
New cards

shallow copy

make a new object, but make reference copies for the different instance variables

100
New cards

shallow copy example

int[] a = {1, 2, 3};

int[] shallowCopy = a.clone();