Comp301 f24 m1 Practice Solution
Comp301 f24 m1 Practice Solution
1.1. (1 point) double is a reference type. 1.11. (1 point) A call to super() must be the
⃝ True False first statement in a constructor unless it is
chaining to another constructor.
1.2. (1 point) The value of a non-null reference True ⃝ False
type variable is the location of an object
instance in memory. 1.12. (1 point) A class can be marked abstract
True ⃝ False even if it does not have abstract methods.
True ⃝ False
1.3. (1 point) The this keyword in a construc-
tor references a newly allocated object in 1.13. (1 point) An interface may extend more
memory than one parent interface.
True ⃝ False True ⃝ False
1.4. (1 point) In instance methods and con- 1.14. (1 point) Overloaded methods are an ex-
structors, you must use the this keyword ample of polymorphism.
to access object field values
True ⃝ False
⃝ True False
1.15. (1 point) An overridden method requires
1.5. (1 point) If an object is immutable, it can
the @Override directive on the line above
not have setter methods.
the method.
True ⃝ False
⃝ True False
1.6. (1 point) The principle of encapsula-
1.16. (1 point) Overridden methods are always
tion motivates declaring object fields as
virtual.
public.
True ⃝ False
⃝ True False
1.7. (1 point) Class fields are declared with the 1.17. (1 point) Dependency injection is a form
static keyword. of inversion of control.
True ⃝ False True ⃝ False
1.8. (1 point) Java instance methods are either 1.18. (1 point) The class Object has an “is-a”
virtual or non-virtual depending on how relationship with every other class.
they are declared. ⃝ True False
⃝ True False
1.19. (1 point) One characteristic of an aggrega-
1.9. (1 point) Methods marked as abstract tion is that the inner objects often mean-
may contain a method body. ingfully exist independently.
⃝ True False True ⃝ False
1.10. (1 point) A class has an "is-a" relation- 1.20. (1 point) If you do not specify the access
ship with all of the interfaces that it im- modifier for a field, it is assumed to be
plements. public.
True ⃝ False ⃝ True False
Page 1
Question 2: Multiple Choice Completely fill in the bubble next to your answer using a pencil. Each
question should have exactly one filled-in bubble. All questions are assumed to be asking about Java.
2.1. (2 points) Which of the following is true 2.5. (2 points) Which concept is not related to
of inheritance in Java the idea of inheritance?
⃝ A subclass can extend one or ⃝ Method overriding
more parent classes. Method overloading
⃝ A parent class has an "is-a" rela-
⃝ Extending classes
tionship with its subclasses.
⃝ A subclass must be in the same ⃝ Extending interfaces
package as its parent class.
A subclass of a parent class that 2.6. (2 points) Which access modifier allows a
implements an interface, by def- member to be accessible from within the
inition, also implements that in- class and any subclasses?
terface. ⃝ default
protected
2.2. (2 points) Which keyword is associated
⃝ private
with calling a constructor?
⃝ implements ⃝ public
⃝ static
⃝ class 2.7. (2 points) Which of the following state-
new ments is correct about overloading?
⃝ Overloaded methods must have
2.3. (2 points) Which of the following is not the same return type.
traditionally considered an example of ⃝ Overloaded methods may have
polymorphism? different access modifiers.
Multiple instances of the same Overloaded methods can take in
class different types of parameters.
⃝ Multiple methods with the same
⃝ Overloaded methods may have
name
different names.
⃝ One class with multiple con-
structors
2.8. (2 points) Which of the following state-
⃝ Using generic types so a field or
ments is false regarding the final key-
variable can take on different
word?
data types
⃝ Instance fields when declared as
2.4. (2 points) Which of the following would final cannot be changed after
be considered related to the principle of instantiation.
encapsulation? Classes declared as final may
⃝ Multiple methods with the same have subclasses.
name. ⃝ Methods declared as final can-
⃝ Marking instance fields as public not be overridden.
Using getters and setters to ac- ⃝ Local variables declared as
cess field values final must be initialized when
⃝ Removing derived fields from all declared.
classes
Page 2
Question 3: Method Execution Consider the following Java class definitions.
1 class A {
2 public int process ( int x , int y ) {
3 if ( process ( x ) + process ( y ) > 12) {
4 return x + y ;
5 } else {
6 return x * y ;
7 }
8 }
9
10 public int process ( int x ) {
11 if ( x %2 == 0} { // x is even
12 return x /2;
13 } else { // x is odd
14 return process (2*( x -1) ) ;
15 }
16 }
17 }
18
19 class B extends A {
20 @Override
21 public int process ( int x ) {
22 return x +2;
23 }
24 }
3.1. (2 points) Upon execution of the following code snippet, what is the value of result?
1 A processor = new A () ;
2 int result = processor . process (5 , 6) ;
Solution: 30
3.2. (2 points) Upon execution of the following code snippet, what is the value of result?
1 B processor = new B () ;
2 int result = processor . process (5 , 6) ;
Solution: 11
3.3. (2 points) Upon execution of the following code snippet, what is the value of result?
1 A processor = new B () ;
2 int result = processor . process (5 ,6) ;
Solution: 11
Page 3
Question 4: Class Members in Java Consider the following Java class definition.
1 public class UNCStudent {
2 private String name ;
3 private int age ;
4 private final int pid ;
5 private final static boolean tarHeelBred = true ;
6
7 public UNCStudent ( String name , int age , int pid ) {
8 this . name = name ;
9 this . age = age ;
10 this . pid = pid ;
11 }
12
13 public String getName () {
14 return name ;
15 }
16
17 public void setAge ( int age ) {
18 this . age = age ;
19 }
20
21 public static void saySomething () {
22 System . out . println ( " I ’m a UNC student ! " ) ;
23 }
24
25 public static void speakAgain () {
26 System . out . println ( " This exam is easy ;) " ) ;
27 }
28 }
4.1. (1 point) List the names of all instance fields defined by Example.
4.2. (1 point) List the names of all class fields defined by Example.
Solution: tarHeelBred
4.3. (1 point) List the names of all instance methods defined by Example.
4.4. (1 point) List the names of all class methods defined by Example.
4.5. (1 point) In total, how many members are defined by class Example?
Solution: 8
Page 4
Question 5: Subtype Polymorphism Consider the following Java class and interface definitions
which exhibit inheritance. Note that the bodies are intentionally left empty.
1 interface I0 {}
2 interface I1 extends I0 {}
3 interface I2 {}
4 class C implements I1 { }
5 class D extends C { }
6 class A extends D implements I2 { }
7 class B extends A { }
8 class E extends C implements I2 { }
9 class F extends A { }
5.1. (4 points) List all classes from which B 5.4. (1 point) Is the following code valid?
inherits, either directly or indirectly. 1 C obj = new B () ;
Valid ⃝ Invalid
1 A obj = new C () ;
5.6. (1 point) Is the following code valid?
1 I2 obj = new F () ;
⃝ Valid Invalid 2 I0 obj2 = ( I0 ) obj ;
Valid ⃝ Invalid
5.3. (1 point) Is the following code valid?
5.7. (1 point) Is the following code valid?
1 B obj = new F () ; 1 I2 obj = new A () ;
2 E obj2 = ( E ) obj ;
⃝ Valid Invalid
⃝ Valid Invalid
Page 5
Question 6: Inheritance Suppose the following code for classes Charmander and Squirtle is to
be refactored by first creating a superclass named GenOne that implements the interface Pokemon.
And then, Charmander and Squirtle are to be rewritten as subclasses of GenOne. In doing so, as-
sume GenOne is written to "extract" or "factor out" as much shared code between Charmander and
Squirtle as is appropriate in accordance to the principles of encapsulation and inheritance. After
this transformation takes place, all existing code using the original classes as written below should
still work and instances of these classes should expose only the same functionality as before.
Review the code below with this in mind and answer the questions that follow.
1 public interface Pokemon {
2 boolean canEvolve () ;
3 double getLevel () ;
4 }
Page 6
1 public class Squirtle implements Pokemon {
2 private double level ;
3 private String type ;
4 private String name ;
5
6 public Squirtle ( double level , String type , String name ) {
7 this . level = level ;
8 this . type = type ;
9 this . name = name ;
10 }
11
12 public double getLevel () {
13 return level ;
14 }
15
16 public boolean canEvolve () {
17 boolean result ;
18
19 // Assume there is quite a bit of complex
20 // Squirtle - specific code here that eventually
21 // determines the value of result .
22
23 return result ;
24 }
25
26 public void waterGun () {
27 System . out . println ( name + " used water gun " ) ;
28 }
29
30 public String getType () {
31 return type ;
32 }
33 }
Page 7
6.1. (2 points) Which of these is the most appropriate way to declare GenOne?
⃝ public class GenOne
⃝ public class GenOne implements Pokemon
⃝ public class GenOne extends Pokemon
⃝ public abstract class GenOne
public abstract class GenOne implements Pokemon
⃝ public abstract class GenOne extends Pokemon
6.2. (4 points) Which of these members would you expect to be included in GenOne?
NOTE: potential members are listed below only with type information and without other ac-
cess modifiers or keywords that may be needed or expected. Select all to be included.
String name
String type
double level
double getLevel()
boolean canEvolve()
⃝ void flamethrower()
⃝ void waterGun()
String getType()
⃝ String getName()
6.3. (1 point) In the box below, list all of the members you selected in part 6.2 that should be
marked as public if any, or write "None".
6.4. (1 point) In the box below, list all of the members you selected in part 6.2 that should be
marked as protected if any, or write "None".
6.5. (1 point) In the box below, list all of the members you selected in part 6.2 that should be
marked as private if any, or write "None".
Solution: name if getName() selected in 6.2 otherwise not listed here. type, level
Page 8
6.6. (1 point) In the box below, list all of the methods you selected in part 6.2 that you would ex-
pect to be overridden in a subclass if any, or write "None".
Solution: canEvolve
6.7. (1 point) In the box below, list all of the methods you selected in part 6.2 that you would ex-
pect to be marked abstract if any or write "None".
Solution: canEvolve
6.8. (3 points) In the box below, provide the code that you would expect for the GenOne constructor.
Solution:
public GenOne ( String name , String type , double level ) {
this . name = name ;
this . type = type ;
this . level = level ;
}
Page 9
6.9. (4 points) In the box below, provide the complete code you would expect for Squirtle.
Solution:
Page 10
This page is blank on purpose. You may use it as scratch / extra space.
Solution:
Page 11