Lecture 4 Flashcards

1
Q

What is aliasing?

A

Having a variable refer to an existing object

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

How does Object.clone() work?

A

All primitive fields are copied

All Object fields are aliased, i.e. refer to the old ones

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

What interface must objects implement to allow cloning?

A

Cloneable

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

Why do you have to be careful with cloning an object that contains mutable fields?

whats the solution?

A

Changes to mutable fields will change in every object clone

Solution: clone the mutable field during cloning of the object

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

How are objects serialised using Object streams?

A

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(“bank.dat”));

out.writeObject(b);

ObjectInputStream in = new ObjectInputStream(new FileInputStream(“bank.dat”));

BankAccount b = (BankAccount)(in.readObject());

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

What can readObject() throw?

A

ClassNotFoundException and IOException

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

Why do you have to be careful serialising objects?

A

All object fields will also be serialised

So many many objets might be serialised from one call - potential massive files

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

How can a class be marked as Serialisable?

A

Implement interface Serializable

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

how to you prevent field being serialised?

A

Mark it as transient

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

If the same object is serialised twice in one ObjectOutputStream, what happens?

A

Assigned a serial number and second serialisation just prints that out.

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

What is the version problem?

How is it generally solved?

A

If the structure of a class changes between serialisation and deserialisation

Give null or empty values to new fields

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

Why is a BufferedReader better than calling Paths.readAllLines(path)

A

readAllLines reads entire file into memory

BufferedReader goes line by line

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

what is a potential problem with buffered streams?

A

May have to explicitly flush them

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

How do you format a number?

A

System.out.format(“sqrt(%d) = %.8f%n”, i, r);

// sqrt(2) = 1.41421356

.8 means 8 digits after decimal point

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

what is GSON?

A

Open source library to convert Java objects to and from JSON

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

How do you serialise and deserialise objects using GSON?

A

Gson gson = new Gson();

String oneJson = gson.toJson(1);

Integer one = gson.fromJson(oneJson, Integer.class);

17
Q

What value types are supported in JSON?

A

Primitives: strings, numbers, booleans, null

Arrays

Objects

18
Q

What is an object in json?

A

Unordered collection of name/value pairs, e.g:

{“field1”: value1, “field2”: value2..}

19
Q

What is an array in json?

A

Ordered collection of values:

[value1, value2…]

20
Q

What is needed for deserialisation of generic types in GSON?

A

TypeToken:

Type listType = new TypeToken<list>&gt;(){}.getType();</list>

List<book> result = GSON.fromJson(reader, listType);</book>

21
Q

What is JAX-RS?

A

Java framework for RESTful web services

22
Q

What is Jersey?

A

JAX-RS reference implementation

23
Q

How are Resource methods annotated in JAX-RS?

A

request method designator:

@GET, @PUT, @POST, or @DELETE

These are invoked in response to HTTP requests

24
Q

What does an @Path annotation determine in JAX-RS?

Give an example

A

mapping from URLs to class/methods used for response

@Path(“/book”)

public class BookQuery{

@GET

@Path(“/{isbn}”)

public string getBook(@PathParam(“isbn”) String isbn) {

// return stuff

}

}

25
Q

@ annotations can be processed by what?

A

IDEs, JUnit, javac

26
Q

What are the advantages and disadvantages of assertions?

A

advantage: highlights issues to developers
disadvantage: workload of writing assertions

27
Q

how does JUnit work?

A

Annotate test method with @Test

use methods like assertTrue or assertEquals, assertNull, assertNotEqual

28
Q

How can you run code before/after each test in JUnit?

A

@Before and @After