logo

APSU Notes


Quiz 8

Question 1

A class called Printer has a method called printDocument. This calls a void method in the Printer class called printPage that takes an integer representing the page number as an argument. How should printDocument called printPage with an argument of 4?

printPage(4);

Question 2

What does class interface consist of?

The public named constants and headings of public methods of the class.

Question 3

Which of the following is not a good guideline for a well encapsulated class?

Declare all the instance variables as public.

Question 4

How are variables of a primitive type different from variables of a class type?

The memory location of a variable of a primitive type contains the data value itself. The memory location of a variable of a class type contains a reference to an object.

Question 5

When does the == operator return true when comparing two class variables?

If the two class variables refer to the same object.

Question 6

Consider the following code:

1
2
3
4
5
Employee empl = new Employee();
emp1.setName("Anna Karenina");
Employee emp2 - new Employee();
emp2.setName("David Copperfield");
emp2 = emp1;

Which of the following best describes the contents of the two variables emp1 and emp2 after this code has executed?

emp1 refers to the object with the name Anna Karenina. emp2 refers to the same object.

Question 7

What is unit testing?

Testing the correctness of individual units of code.

Question 8

When does the equals method return true when comparing two class variables?

All of the above.

Quesiton 9

Given the following class definition:

1
2
3
4
5
6
7
public class IntTester
{
    void addOneTo(int value)
    {
        value = value + 1;
    }
}

What is the output of the following statements?

1
2
3
4
IntTester it = new IntTester();
int value = 42;
it.addOneTo(value);
System.out.println(value);

43

Question 10

Given the following class definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class IntWrapper
{
    private int value;
    
    public void setValue(int newValue)
    {
        value = newValue;
    }
    
    public int getValue()
    {
        return value;
    }
}

public class IntWrapperTester
{
    public void increment(IntWrapper iw)
    {
        int value = iw.getValue();
        iw.setValue(value + 1);
    }
}

What would the output be for the following statements?

1
2
3
4
5
IntWrapper iw = new IntWrapper();
INtWrapperTester iwTester = new IntWrapperTester();
iw.setValue(42);
iwTester.increment(iw);
System.out.println(iw.getValue());

43