Chapter 11 Flashcards

1
Q

A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?

A

The variable should be marked protected.

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

Invoking _________ returns the first element in an ArrayList x.

A

x.get(0)

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

Invoking _________ returns the number of the elements in an ArrayList x.

A

x.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Assume Cylinder is a subtype of Circle. Analyze the following code:
Cylinder cy = new Cylinder(1, 1);
Circle c = cy;
A

The code is fine.

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

What modifier should you use on the members of a class so that they are not accessible to another class in a different package, but are accessible to any subclasses in any package?

A

protected

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

Which of the following are Java keywords?

A

instanceof

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What is the output of running class C?
class A {
     public A() {
          System.out.println(
               "The default constructor of A is invoked");
     }
}
class B extends A {
     public B() {
          System.out.println(
               "The default constructor of B is invoked");
     }
}
public class C {
     public static void main(String[] args) {
          B b = new B();
     }
}
A

“The default constructor of A is invoked” followded by “The default constructor of B is invoked”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Given the following code:
class C1 {}
class C2 extends C1 { }
class C3 extends C2 { }
class C4 extends C1 {}
C1 c1 = new C1();
C2 c2 = new C2();
C3 c3 = new C3();
C4 c4 = new C4();
Which of the following expressions evaluates to false?
A

c4 instanceof C2

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

Inheritance means ______________.

A

that a class can extend another class

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

Polymorphism means ______________.

A

that a variable of supertype can refer to a subtype object

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

Invoking _________ removes all elements in an ArrayList x.

A

x.clear()

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

What modifier should you use on a class so that a class in the same package can access it but a class (including a subclass) in a different package cannot access it?

A

Use the default modifier.

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

Suppose an ArrayList list contains {“red”, “green”, “red”, “green”}. What is the list after the following code?
list.remove(“red”);

A

{“green”, “red”, “green”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What is the output of the following code:
public class Test {
     public static void main(String[] args) {
          Object o1 = new Object();
          Object o2 = new Object();
          System.out.print((o1 == o2) + " " + (o1.equals(o2)));
     }
}
A

false false

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

Given two reference variables t1 and t2, if t1 == t2 is true, t1.equals(t2) must be ___________.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What is the output of the following code:
public class Test {
     public static void main(String[] args) {
          String s1 = new String("Java");
          String s2 = new String("Java");
          System.out.print((s1 == s2) + " " + (s1.equals(s2)));
     }
}
A

false true

17
Q

Which of the following is incorrect?

A

A constructor may be static.

18
Q

Encapsulation means ______________.

A

that data fields should be declared private

19
Q

You can create an ArrayList using _________.

A

new ArrayList<>()