CPSC 250 Special Topics Flashcards

1
Q

What is a method override?

A

An overridden method is one defined in a child class with the same signature as a method in a parent class. The method in the child class is then preferred over the parent’s method.

The parent’s method gets removed from the child and replaced with the child’s version, unless super.method() is used explicitly somewhere in the child code.

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

Which assignment is broken, foofield or foolocal?

public class Foo {
 private int foofield;
public void outerclassmethod() {
 int foolocal;
 new Object() {
 public void innerclassmethod() {
 foofield = 1;
 foolocal = 2;
 }
 };
 }
 }
A

foolocal is broken. Specifically, it is a compiler error.

Inner classes may access outer class fields but may not access local variables from the outer class.

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

Explain the advantages and disadvantages of anonymous inner classes.

A

AD:

+ The really big advantage is that they put the procedural code directly next to the thing which triggers it. Example: Create a new button, followed by the code that runs when the button is pressed. Example: Create a new timer, follow by the code that runs when the timer ticks.

+ Cuts down on the number of classes and files in your project. This reduces the complexity of your project overall.

DIS:

  • You can only declare one instance of an anonymous inner class, so if you need two of the exact same one, you need to write the code twice (maybe a member inner class would be better for this?)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

// Note that ArrayList with no just holds Objects.

public static void changeList(ArrayList mylist) {
 mylist.add(new Object());
 mylist = null;
 }
public static void main (String[] args) {
 ArrayList mylist = new ArrayList();
 changeList(mylist);
 // Is mylist null? What size is it?
 }
A

mylist is NOT NULL at the comment, and it contains one element.

First, I hope you aren’t fooled by the name “mylist” being used twice. There are two mylists, one is local to main(), the other is local to changeList().

When class types are used as parameters, a new reference is created for them (we say passed by reference). Another way to say this is that the reference to the object gets copied.

Since mylist is a reference to the same object (same list) in both places, changes to that object will persist. The list gets extended when we add an element.

Setting mylist to null at the end of changeList has no effect.

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

Say you have two tasks, Task A and Task B. What does it mean for these tasks to run in parallel?

A

Parallel is actually a very architecturally specific term. You cannot talk about parallel processes without talking about available resources (hardware). If you’re only concerned about software design and architecture, “concurrent” is probably the right word.

If A and B run in parallel, then they run at exactly the same time using separate resources, such as multicore CPUs, multiple CPUs, GPGPUs, etc. Running parallel gets two jobs done at once, which is good for performance.

If tasks A & B run in parallel, then they can also be said to run concurrently. The converse is not true: If two tasks run concurrently, they do not necessarily run in parallel.

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

public static void changeInt(int myint) {
myint = 7;
}

 public static void main (String[] args) {
 int myint = 11;
 changeInt(myint);
 // what is myint here?
 }
A

myint is 11 at the comment.

First, I hope you aren’t fooled by the name “myint” being used twice. There are two myints, one is local to main(), the other is local to changeint().

Fundamental types are copied when they get used as parameters (we say passed by value). The copy is discarded at the end of the method call.

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

Say you have two tasks, Task A and Task B. What does it mean for these tasks to run concurrently?

A

A and B run concurrently if they both start before either ends. Examples:

A starts, B starts, A ends, B ends

or

A starts, B starts, B ends, A ends

or

B starts, A starts, B ends, A ends

etc.

Note that it is not implied that A and B are at any point running at exactly the same time. Either task can pause while the other gets a chance to run.

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

What is a method overload? How does the compiler treat overloaded methods?

A

Method overloads are idential method names for methods with different parameters. Overloading may be done with or without inheritance. For example:

foo(int i) {…

foo (double d) {…

The compiler treats these two methods as distinct and unrelated to each other, except for the fact that you must give different parameters for each or the compiler flags you for duplicate methods.

Overloading is nice for the user, because it gives them many ways to accomplish the same task. Think about something like:

new Sound(File soundFile)

new Sound(URL soundURL)

That’s constructor overloading. Same idea.

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

What is @Override? Why use it?

A

@Override is an annotation. Annotations always start with @ and are used by the compiler and tools to help analyze and extend the functionality of code. They do not affect the way your code runs in any way and in that manner are similar to comments.

@Override is used by the compiler to flag an error if you make a mistake in the method signature of an inherited + overridden method.

For example:

@Override

public boolean equals (Foo f) {…

Will flag you with an error. Foo should be Object, even if this is the equals for Foo.

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

What is the syntax for an anonymous inner class that extends a class?

What is the syntax for an anonymous inner class that implements an interface?

A

The syntax is the same for both cases.

new ClassYouExtend() { }

new InterfaceYouImplement() { }

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

Name four Java classes that will start new threads.

A

Thread

Runnable

Timer

JFrame

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

Say you have two tasks, Task A and Task B. What does it mean for these tasks to run serially?

A

Serial means that A must complete before B can start (or the other way around, if B runs first). Their execution cannot overlap at all.

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

public static void changeFoo(Foo myfoo) {
myfoo = null;
}

 public static void main (String[] args) {
 Foo myfoo = new Foo();
 changeFoo(myfoo);
 // is myfoo null here?
 }
A

myfoo is NOT NULL at the comment.

First, I hope you aren’t fooled by the name “myfoo” being used twice. There are two myfoos, one is local to main(), the other is local to changefoo().

When class types are used as parameters, a new reference is created for them (we say passed by reference). Another way to say this is that the reference to the object gets copied.

Assigning object types ala “myfoo = null” or “myfoo = new Foo()” really just makes that variable a reference to a different object (or no object at all). It doesn’t change the object in any way.

The new reference is discarded at the end of the method call. The old reference does not change.

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

What is the proper syntax to access an outer class field from an inner class when there is a conflict in naming?

public class Foo {
 private int foofield;
public void outerclassmethod() {
 new Object() {

private int foofield;

public void innerclassmethod() {
 // I want the outer foofield here
 }
 };
 }
 }
A

In this case, it would be

Foo.foofield

To get the inner class foofield, it would just be

foofield

It works for “this” as well.

Foo.this // for outer this

this // for inner this

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