CPSC 250 Advanced Java Flashcards

1
Q

// Does this compile? If not, why?

import java.util.ArrayList;

public class Exam2 {
 public static void main(String[] args) {
 ArrayList list = new ArrayList();
 list.add(new Parent());
 list.add(new Child());
 }
 }
class Parent {
 }
class Child extends Parent {
 }
A

It compiles. list.add(Parent x) expects a Parent. You can substitute a Child for a Parent any time.

“Every circle is a shape, but not every shape is a circle.”

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

// What is the difference between method1 and method2?

interface MyInterface {
     void method1();
     public abstract void method2();
 }
A

Nothing. All methods in interfaces are public and abstract.

It is implied by default, so you can leave out the keywords.

Writing interface methods as method1 is written is the best practice.

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

// Does this compile?

class Parent {
     Parent(int number) {
     }
 }
class Child extends Parent {
 }

// If not, why?

A

This one is complex.

It does not compile.

Parent supplies a constructor which take parameters (int), therefore the default constructor () is not supplied.

Since Child does not supply a constructor, the implicit default constructor is used.

The implicit default constructor calls the default constructor of the parent.

This fails because Parent did not define the default constructor().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
// What gets printed?
 public class Exam2 {
 public static void main(String[] args) {
 Child c = new Child();
 c.method();
 }
 }
class Parent {
 public void method() {
 System.out.println("PARENT!");
 }
 }
class Child extends Parent {
 public void method() {
 System.out.println("CHILD!");
 }
 }
A

CHILD!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
// Does this compile?
 abstract class MyClass {
        abstract void method1() {}
        void method2();
 }
 // If not, why?
A

No, it does not compile. The {} and ; are mixed up.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
// Does this compile?
 interface Interface {
     abstract void method();
 }
 // If not, why?
A

Yes, it compiles, though it isn’t standard. All methods in an interface are abstract, therefore the abstract keyword is optional and is usually left out.

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

// Does this compile?

abstract class MyClass {
     void method1() {
     }
abstract void method2();  } // If not, why?
A

Yes, it compiles. You can have both abstract and normal methods in an abstract class.

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

// Does this compile?

abstract class Parent {
     abstract void method();
 }
class Child extends Parent {
     void method() {
     }
 } // If not, why?
A

Yes, it compiles. Child implements all abstract methods of Parent.

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

// Does this compile?

abstract class MyClass {
     abstract void method();
 } // If not, why not?
A

Yes! It compiles. Abstract method in an abstract class.

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

// Does this compile?

abstract class Parent {
     abstract void method();
 }
class Child implements Parent {
     void method() {
     }
 } // If not, why?
A

No, it does not compile. Classes *extend* other classes (abstract or not) and *implement* interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
// What gets printed?
 public class Exam2 {
 public static void main(String[] args) {
 Parent p = new Child();
 p.method();
 }
 }
 class Parent {
 public void method() {
 System.out.println("PARENT!");
 }
 }
 class Child extends Parent {
 public void method() {
 System.out.println("CHILD!");
 }
 }
A

CHILD! Even though p is a Parent, the object is a Child (new Child()). Java always calls overridden methods from the child class, not the parent. This is called dynamic resolution, or run-time binding.

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

// Does this compile?

class Parent {
     Parent(int number) {
     }
 }
class Child extends Parent {
     Child(int number) {
     }
 } // If not, why?
A

Child (int number) does not call Parent’s constructor as its first line, therefore super() is called by the compiler (implicit parent constructor call).

Since Parent() does not exist, it is an error.

Perhaps we could make this change:

Child (int number) {

super(number);

}

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

// Does this compile?

interface Parent {
     void method();
 }
class Child implements Parent {
     private void method() {
     }
 } // If not, why?
A

Advanced Question.

No, it fails. You are not allowed to reduce the visibility of an inherited method. If it is public in the parent, it must be public in the child.

“method” is public in Parent because all methods in interfaces are public. Like “abstract”, it is implied.

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

// Does this compile?

class MyClass {
     MyClass() {
         super();
     }
 } // If not, why?
A

It compiles.

With the absence of the “extend” keyword, you might suspect that super() is a mistake. But MyClass extends Object. Object is the parent of all classes by default! So super() call Object(), which is just fine. Even if you delete super(), it is called implicitly.

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

// Does MyClass have a constructor?

class MyClass { }

A

Tricky question.

You don’t *see* a constructor, but MyClass indeed *has* a constructor. The implicit default constructor is MyClass().

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

// Does this compile?

abstract class MyClass {
     abstract void method() {}
 } // If not, why?
A

No, it does not compile. Abstract methods must not have a {body}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
// Does this compile?
 abstract class Mom {
     abstract void method1();
 }
 class Dad {
     void method2() {}
 }
 class Child extends Mom, Dad {
     void method1() { }
 } // If not, why?
A

No, it does not compile.

You cannot extend more than one class. Java only supports single inheritance between classes.

Multiple inheritance is partially supported through interfaces.

18
Q

// Does this compile?

interface Interface {
     void method1();
     void method2() {
         System.out.println("Default Implementation");
     }
 } // If not, why?
A

No, it does not compile. All methods in an interface must be abstract. No implementations { } allowed.

19
Q

// Does this compile?

abstract class Parent {
     abstract void method();
 }
class Child extends Parent {
 } // If not, why?
A

No, it does not compile. Child needs to implement all abstract methods in Parent.

20
Q
// Does this compile?
 class Parent {
     Parent(double d) {
     }
 }
 class Child extends Parent {
     Child() {
         System.out.println("Child calls Parent!");
         super(99.99);
     }
 } // If not, why?
A

It does not compile. If super() is called, it must be the first line.

21
Q

// Is this method overriding or overloading?

class Parent {
     public void method(double d) {
     }
 }
class Child extends Parent {
     public void method(char c) {
     }
 }
A

Overloading.

Overloading means that we are using the same name, “method”, to refer to two otherwise unrelated methods.

Java determines the overloaded method from the parameters.

Child c = new Child();

c. method(99.99); // Parent.method
c. method(‘a’); // Child.method

22
Q

// Does this compile?

class Parent {
     Parent(int number) {
     }
 }
class Child extends Parent {
     Child(int number) {
         super(number);
     }
 } // If not, why?
A

Yes, it compiles.

Neither Parent nor Child has a default() constructor, and that’s perfectly allowable.

23
Q

// Which constructor is the default constructor? What is special about it?

class MyClass {
     MyClass() {
     }
MyClass(int number) {
 }

MyClass(int number, char letter) {
 }  }
A

MyClass() {} is the default constructor.

Among other reasons, it is special because it is called automatically by the constructors of children of MyClass by default.

24
Q

// What gets printed if we call: new Child(“”) ?

class Parent {
     Parent() {
         System.out.println("two!");
     }
 }
class Child extends Parent {
     Child(String ignoreme) {
         System.out.println("one!");
     }
 }
A

two!

one!

Remember there is an implicit call to super() at the top of every constructor. This means Parent() gets called before println in Child().

25
Q

// Does this compile?

class Parent {
     Parent(int number) {
     }
 }
class Child extends Parent {
     Child(int number) {
         Parent(number);
     }
 } // If not, why?
A

It does not compile.

Right idea, wrong syntax. To call a parent’s constructor, use super().

26
Q

// What gets printed?

public class Exam2 {
 public static void main(String[] args) {
 Child c = new Child();
 c.method();
 }
 }
class Parent {
 public void method() {
 System.out.println("PARENT!");
 }
 }
class Child extends Parent {
 public void method() {
 super.method();
 System.out.println("CHILD!");
 }
 }
A

PARENT!

CHILD!

27
Q

// Does this compile?

abstract class Mom {
 abstract void method1();
 }
interface Dad {
 void method2();
 }
class Child extends Mom implements Dad {
 void method1() {
 }

public void method2() {
}
} // If not, why?

A

Yes, it does. You are allowed to extend one class and additionally to implement any number of interfaces.

28
Q

// Does this compile?

abstract class MyClass {
     void method();
 } // If not, why?
A

No, it does not compile.

“method” ends in a “;”, which means it has no method body. Methods must have a body {} or be abstract.

29
Q

// What gets printed?

public class Exam2 {
 public static void main(String[] args) {
 Child c = new Child();
 c.method();
 }
 }
class Parent {
 public void method() {
 System.out.println("PARENT!");
 }
 }
class Child extends Parent {
 public void method() {
 System.out.println("CHILD!");
 super.method();
 }
 }
A

CHILD!

PARENT!

30
Q

// Is this method overriding or overloading?

class Parent {
     public void method(char d) {
     }
 }
class Child extends Parent {
     public void method(char c) {
     }
 }
A

Overriding.

The child is replacing the method from parent. This only happens when the method name and parameters are exactly the same.

31
Q

// Does this compile?

class MyClass {
     void method() {
     }
 } // If not, why?
A

Yes, it compiles. And empty body {} is still a body.

32
Q

// Does this compile?

interface Interface {
     void method();
 } // If not, why?
A

Yes, it compiles. “method” is abstract, even without the keyword. All methods in an interface are abstract.

33
Q

// Does this compile?

class MyClass {
     abstract void method();
 } // If not, why?
A

No, it does not compile. Abstract methods must be in an abstract class.

34
Q

// What is the visibility of method: public, package, protected, or private?

class AClass {
     void method() {
     }
 }
A

package is the answer.

“package” visibility means that the class can be seen by all classes in this package. It is close to public. “package” does not have a keyword, it is the default visibility that applies when you do not specify public/private/protected.

35
Q

// What gets printed if we call: new Child(“”) ?

class Parent {
 Parent() {
 System.out.println("P()");
 }

Parent(String s) {
System.out.println(“P(s)”);
}
}

class Child extends Parent {
 Child(String ignoreme) {
 System.out.println("C(s)");
 }
 }
A

P()

C(s)

Even though P(s) might seem like a more natural choice to be called by C(s), the default super() is always called unless you calls super(?) explicitly.

36
Q

// What gets printed?

class Exam2 {
     public static void main(String[] args) {
         B b = new B();
         b.foo();
     }
 }
 class A {
     public void foo() {
         System.out.println("This is A");
     }
 }
 class B extends A {
     public void foo() {
         System.out.println("This is B");
     }
 }
A

This is B

37
Q

// What gets printed?

class Exam2 {
     public static void main(String[] args) {
         A a = new B();
         a.foo();
     }
 }
 class A {
     public void foo() {
         System.out.println("This is A");
     }
 }
 class B extends A {
     public void foo() {
         System.out.println("This is B");
     }
 }
A

This is B

38
Q

// Is the assignment legal, illegal, or legal with a cast?

class A {}
 class B extends A {}
 class C extends B {}
    A a = new B();
A

Legal

39
Q

// Is the assignment legal, illegal, or legal with a cast?

class A {}
 class B extends A {}
 class C extends B {}
        A a = new C();
         B b = a;
A

Legal with a cast

Since C is a subtype of B, it is OK to hold a C in b. (I’m asking you for a B, you’re giving me a C, which is a specific type of B).

But the compiler does not know that a C is sitting in a, so you have to cast it.

40
Q

// Is the assignment legal, illegal, or legal with a cast?

class A {}
 class B extends A {}
 class C extends B {}
        A a = new A();
         C c = a;
A

Illegal

You cannot treat an A as a C because C extends A and may have methods that A does not.