You are on page 1of 26

Take Test: Final Exam

Test Information
Description Final Examination

Instructions Examination can be taken anytime between Wednesday December 13 at


6 PM and Thursday December 14 at 6 PM. Examination is time limited
to 90 minutes from the start of the test and will auto-submit after 90
minutes.
Timed Test This test has a time limit of 1 hour and 30 minutes.This test will save and submit automatically
when the time expires.
Warnings appear when half the time, 5 minutes, 1 minute, and 30 seconds remain.
Multiple Not allowed. This test can only be taken once.
Attempts
Force This test can be saved and resumed at any point until time has expired. The timer will continue to
Completion run if you leave the test.

Remaining Time:

22 minutes, 54 seconds.

Question Completion Status:

QUESTION 1
1. Consider the following operations on a queue data structure that stores int values:

Queue q = new Queue();


q.enqueue(3);
q.enqueue(5);
q.enqueue(9);
System.out.println(q.dequeue());
q.enqueue(2);
q.enqueue(4);
System.out.println(q.dequeue());
System.out.println(q.dequeue()); // d3
q.enqueue(1);
q.enqueue(8);

What value is returned by the last dequeue operation, denoted with a d3 as a comment.

a. 5
b. 4

c. 2

d. 9

e. 3

3 points
QUESTION 2
1. Interface classes cannot be extended but classes that implement interfaces can be
extended.
True

False

1 points
QUESTION 3
1. A reference variable can refer to any object created from any class related to it by inheritance
True

False

1 points
QUESTION 4
1. An interface reference can refer to any object of any class that implements the interface
True

False

1 points
QUESTION 5
1. Can a program exhibit polymorphism if it only implements early binding?
a. Yes, because one form of polymorphism is overloading
b. No, because without late binding, polymorphism cannot be supported.
c. Yes, because early binding has nothing to do with polymorphism
d. Yes,
because as long as the program uses inheritance and/or interfaces, it supports
polymorphism
1 points
QUESTION 6
1. Which of the following messages passed to the String str could throw
a StringIndexOutOfBoundsException?

a. str length();
b. str charAt(2);
c. str replace('a', 'A');
d. str equals(str);
e. Any of these could throw a StringIndexOutOfBoundsException

3 points
QUESTION 7
1. In a linked list in Java
a. the link is a class
b. the link is a reference
c. the link is a node
d. the link is an int
e. the link is an object

1 points
QUESTION 8
1. Assume that Student, Employee and Retired are all extended classes of Person, and all
four classes have different implementations of the method getMoney. Consider the following
code where ... indicates the required parameters for the constructors:

Person p = new Person(...);


int m1 = p.getMoney(); // assignment 1
p = new Student(...);
int m2 = p.getMoney(); // assignment 2
if (m2 < 100000)
p = new Employee(...);
else if (m1 > 50000)
p = new Retired(...);
int m3 = p.getMoney(); // assignment 3

The reference to getMoney() in assignment 2 is to the class

a. Student
b. This cannot be determined by examining the code
c. Employee
d. Person
e. Retired

3 points
QUESTION 9
1. You can create or view the contents of binary or object files with many text editors
True

False

1 points
QUESTION 10
1. Consider a class called ChessPiece. This class has two instance data, String type and int
player. The variable type will store "King", "Queen", "Bishop", etc. and the int
player will store 0 or 1 depending on whose piece it is. We wish to
implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is
compared to a ChessPiece passed as a parameter. Pieces are ordered as follows:

 "Pawn" is a lesser piece to a "Knight"


 a "Bishop" and "Knight" are equivalent for this example
 both "Bishop" and "Knight" are lesser pieces to a "Rook"
 a "Rook" is a lesser piece to a "Queen"
 a "Queen" is a lesser piece to a "King".

Which of the following method headers would properly define the method needed to make this
class Comparable? (cp designates ChessPiece)

a. public int compareTo(Object cp)


b. public int compareTo()
c. public int comparable(Object cp)
d. public boolean comparable(Object cp)
e. public boolean compareTo(Object cp)

3 points
QUESTION 11
1. Which ones of the following statements are legal? Choose all that apply.
a. Integer n = new Integer(77);
b. int m = 77
c. n = m;
d. m = n;
e. None of these are legal

1 points
QUESTION 12
1. Given the following partial class definition:
public class A1
{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1
{
protected int a;
private int b;
...
}
public class A3 extends A2
{
private int q;
...
}

Which of the following lists of instance data are accessible in class A2?

a. x, z, a, b
b. x, y, z, a
c. x, y, z, a, b
d. a, b
e. z, a, b

3 points
QUESTION 13
1. One operation that we might want to implement on a stack and a queue is full, which determines
if the data structure has room for another item to be added. This operation would be useful
a. only for a queue
b. only if the queue or stack is implemented using an array
c. only if the queue or stack is implemented using a linked list
d. only for a stack
e. None of these; a full operation is not useful at all

1 points
QUESTION 14
1. Assume a stack class stores int values. Consider the following sequence of instructions

Stack s = new Stack();


s.push(16);
s.push(12);
s.push(19);
int x = s.pop();
s.push(5);
s.push(9);
s.push(4);
int y = s.pop();
int z = s.pop();

After the instructions execute, z has the value

a. 9

b. 5

c. 4

d. 12

e. 16

3 points
QUESTION 15
1. The linked list is superior in all ways to the array when it comes to implementing a list
True

False

1 points
QUESTION 16
1. The only difference between a stack and a queue is that stacks operate using FIFO and queues
operate using LIFO
True

False

1 points
QUESTION 17
1. Any class can implement an interface, but no classes can implement more than a single interface
True

False

1 points
QUESTION 18
1. A finally clause will execute
a. only if the try statement that precedes it throws an exception, regardless of whether or not
it is caught
b. only if the try statement that precedes it does not throw an exception
c. only if the try statement that precedes it throws an exception that is caught
d. only if the try statement that precedes it throws an exception that is not caught
e. in any circumstance

1 points
QUESTION 19
1. What kind of exception might be thrown by the following statement:

PrintWriter outputStream = new PrintWriter(“out.txt”);

a. FileNotFound
b. RuntimeException
c. IllegalArgumentException
d. NullPointerException
e. IOException

1 points
QUESTION 20
1. Which of the following is an example of multiple inheritance?
a. Macintosh and IBM PC are both types of PCs
b. a PC can be a desktop or a laptop
c. a laptop is both a PC and a portable device
d. a portable device is a lightweight device
e. a computer can be a mainframe or a PC

1 points
QUESTION 21
1. Given the following partial class definition:

public class A1
{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1
{
protected int a;
private int b;
...
}
public class A3 extends A2
{
private int q;
...
}
Which of the following is true with respect to A1, A2, and A3?

a. A2 and A3 are both subclasses of A1


b. A1, A2 and A3 are all subclasses of the class A
c. A3 is a subclass of A2 and A2 is a subclass of A1
d. A1 and A2 are both subclasses of A3
e. A1 is a subclass of A2 and A2 is a subclass of A3

3 points
QUESTION 22
1. Given the following code:

public static void main(String[] args)


{
try
{
ExceptionThrowerCode etc = new ExceptionThrowerCode();
etc.m1();
etc.m2();
}
catch (ArithmeticException ae) { ... }
}
public class ExceptionThrowerCode
{
...
public void m1()
{
...
}
public void m2()
{
try
{
m3();
}
catch(ArithmeticException ae) {...}
catch(NullPointerException npe) {...}
}
public void m3()
{
try
{
...
}
catch(ArithmeticException ae) {...}
}
}

If an NullPointerException arises in the try statement in m3

a. it is caught in m1
b. it is caught in main
c. it is not caught and the program terminates
d. it is caught in m3
e. it is caught in m2

3 points
QUESTION 23
1. Queues and stacks can be implemented using either arrays or linked lists
True

False

1 points
QUESTION 24
1. A linked list that contains six Nodes will have six reference pointers
True

False

1 points
QUESTION 25
1. Which of the following criticisms of an array is applicable to a Java array?
a. It only supports first-in first-out types of accesses.
b. It is fixed in size (static)
c. It cannot be used to create an Abstract Data Type such as a queue or stack
d. It is an inefficient structure to access random elements.
1 points
QUESTION 26
1. Formal parameters are those that appear in the method call and actual parameters are those that
appear in the method header
True

False

1 points
QUESTION 27
1. Consider the following class definition:

public class AClass


{
protected int x;
protected int y;
public AClass(int a, int b)
{
x = a;
y = b;
}
public int addEm()
{
return x + y;
}
public void changeEm()
{
x++;
y--;
}
public String toString()
{
return "" + x + " " + y;
}
}

You want to extend AClass to BClass. BClass will have a third int instance
data, z. Which of the following would best define BClass's constructor?

a. public BClass(int a, int b, int c)


{
z = c;
}
b. public BClass(int a, int b, int c)
{
super();
}

c. public BClass(int a, int b, int c)


{
super(a, b);
z = c;
}
d. public BClass(int a, int b, int c)
{
super(a, b, c);
}

e. public BClass(int a, int b, int c)


{
x = a;
y = b;
z = c;
}
3 points
QUESTION 28
1. The most important decisions regarding the development of a system are made during the
implementation phase while code is actively being written
True

False

1 points
QUESTION 29
1. Assume a stack class stores int values. Consider the following sequence of instructions

Stack s = new Stack();


s.push(16);
s.push(12);
s.push(19);
int x = s.pop();
s.push(5);
s.push(9);
s.push(4);
int y = s.pop();
int z = s.pop();

After the instructions execute, x has the value

a. 19

b. 0

c. 16

d. None, the instruction int x = s.pop() results in an exception being thrown


e. 12

3 points
QUESTION 30
1. A truth table shows, for the various true or false values of boolean variables, what the
result of a boolean condition is. Fill in the following table. Assume a, b, and c are
boolean variables
x y z !(x && y) || !(x && z)
false false false
false false true
false true false
false true true
true false false
true false true
true true false
true true true
2 points
QUESTION 31
1. Consider the following class definition:

public class AClass


{
protected int x;
protected int y;
public AClass(int a, int b)
{
x = a;
y = b;
}
public int addEm()
{
return x + y;
}
public void changeEm()
{
x++;
y--;
}
public String toString()
{
return "" + x + " " + y;
}
}

You want to extend AClass to BClass. BClass will have a third int instance
data, z. You want addEm to now add all three values, return the sum, and changeEm to
change x and y, but leave z alone. Which should you do?

a. Redefine addEm to return the value of z+super.addEm() and redefine changeEm to


call super.changeEm() and then set z = x + y
b. Redefine changeEm to call super.changeEm() and then set z = x + y, but
leave addEm alone
c. Redefine addEm to return the value of z+super.addEm(), but
leave changeEm alone
d. Redefine changeEm to call super.changeEm() without doing anything to z, and
redefine addEm to return super.addEm()
e. Redefine addEm and changeEm without
referencing super.addEm() or super.changeEm()
3 points
QUESTION 32
1. It is easier to correct errors found in a program if:
a. they are identified early in the development cycle
b. they are identified during testing
c. they are identified during program use
d. they are identified during maintenance
e. All of the above are equally true; errors are easily corrected at any of these stages

1 points
QUESTION 33
1. An example of an aggregation relationship is:
a. teacher and computer
b. All of these
c. Human body and heart
d. phone and fax machine
e. animal and dog

1 points
QUESTION 34
1. Given the following partial class definition:

public class A1
{
public int x;
private int y;
protected int z;
...
}
public class A2 extends A1
{
protected int a;
private int b;
...
}
public class A3 extends A2
{
private int q;
...
}

Which of the following is true regarding the use of instance data y of class A1?
a. It is accessible in A1 and A2
b. It is accessible only in A1
c. It is accessible only in A3
d. It is accessible to any of the three classes
e. It is accessible in A1 and A3

3 points
QUESTION 35
1. What is the output of the following code?

public class Inherit


{
abstract class Speaker
{
abstract public void speak();
}
class Cat extends Speaker
{
public void speak()
{
System.out.println("Woof!");
}
}
class Dog extends Speaker
{
public void speak()
{
System.out.println("Meow!");
}
}
Inherit()
{
Speaker d = new Dog();
Speaker c = new Cat();
d.speak();
c.speak();
}
public static void main(String[] args)
{
new Inherit();
}
}

a. Meow!
Meow!
b. Woof!
Meow!
c. Meow!
Woof!
d. Woof!
Woof!
3 points
QUESTION 36
1. In order to determine the type that a polymorphic variable refers to, the decision is made
a. by the compiler at compile time
b. by the Java run-time environment at run time
c. by the programmer at the time the program is written
d. by the user at run time
e. by the operating system when the program is loaded into memory

1 points
QUESTION 37
1. Assume Exceptionname is a checked exception. If a method uses a class that can
generate Exceptionname, then either the method must include try and catch statements
where a catch statement catches Exceptionname, or the method header must include the
statement

a. catch Exceptionname
b. catches Exceptionname
c. throw Exceptionname
d. throws Exceptionname
e. implements Exceptionname

3 points
QUESTION 38
1. If an exception arises in a catch statement, and the exception is of the type caught by
the catch statement, then the catch statement catches the same exception. For instance, if the
following catch statement first catches an ArithmeticException and, while executing,
throws another ArithmeticException, it is able to catch the
second ArithmeticException as well the first.
catch (ArithmeticException ex) {...}
True

False

1 points
QUESTION 39
1. A linked list that stores int values would be comprised of a group of Nodes. We might define
the Node by

a. class Node
{
int next;
}

b. class Node
{
int data;
Node next;
}

c. class Node
{
int[ ] data;
Node next;
}

d. class Node
{
int data;
}
e. class Node
{
Node next;
}
3 points
QUESTION 40
1. The versions of an overloaded method are distinguished by the number, type, and order of their
parameters
True

False

1 points
QUESTION 41
1. Java does not support multiple inheritance but some of the abilities of multiple inheritance are
available by
a. implementing interfaces
b. using public rather than protected or private modifiers
c. importing classes
d. overriding parent class methods
e. creating aliases

1 points
QUESTION 42
1. Assume you have a text file with these two lines of data:
55.6 hi
there
Assume you have successfully opened this text file for input and given the connection the
name fileIn. Assume the next lines of code are:

double num = fileIn.nextDouble();


String name = fileIn.nextLine();

What is the length of the final string in name?

a. 5

b. 8

c. 3

d. 7

e. 2

3 points
QUESTION 43
1. All run-time errors throw exceptions
True

False

1 points
QUESTION 44
1. The Abstract Data Type (ADT) is thought of as abstract because the operations that are to be
implemented are separated from the actual implementation; that is, an ADT can be implemented in
more than one way and that implementation is separate from how we might use the ADT
True

False

1 points
QUESTION 45
1. The advantage of creating a BookList for a list of books using a linked list instead of using an
array is that the linked list
a. is dynamic and so can be any size needed
b. offers easier access to a random element in the list
c. is easier to implement and debug
d. uses less memory
e. can store types other than Books, unlike the array

1 points
QUESTION 46
1. Once we have implemented the solution, we are not done with the problem because
a. the solution may not be the best or most efficient
b. the solution may have errors and need testing and fixing before we are done
c. the solution may, at a later date, need revising to handle new specifications
d. the solution may, at a later date, need revising because of new programming language
features
e. All of these

1 points
QUESTION 47
1. Given the following code:

public static void main(String[] args)


{
try
{
ExceptionThrowerCode etc = new ExceptionThrowerCode();
etc.m1();
etc.m2();
}
catch (ArithmeticException ae) { ... }
}
public class ExceptionThrowerCode
{
...
public void m1()
{
...
}
public void m2()
{
try
{
m3();
}
catch(ArithmeticException ae) {...}
catch(NullPointerException npe) {...}
}
public void m3()
{
try
{
...
}
catch(ArithmeticException ae) {...}
}
}

If an ArithmeticException arises in the try statement in m3

a. it is not caught and the program terminates


b. it is caught in m2
c. it is caught in m1
d. it is caught in main
e. it is caught in m3

3 points
QUESTION 48
1. An example of an dependency relationship is:
a. All of these
b. teacher and computer
c. Human body and heart
d. animal and dog
e. phone and fax machine

1 points
QUESTION 49
1. Assume that Student, Employee and Retired are all extended classes of Person, and all
four classes have different implementations of the method getMoney. Consider the following
code where ... indicates the required parameters for the constructors:

Person p = new Person(...);


int m1 = p.getMoney(); // assignment 1
p = new Student(...);
int m2 = p.getMoney(); // assignment 2
if (m2 < 100000)
p = new Employee(...);
else if (m1 > 50000)
p = new Retired(...);
int m3 = p.getMoney(); // assignment 3

The reference to getMoney() in assignment 3 is to the class

a. Employee
b. Student
c. This cannot be determined by examining the code
d. Person
e. Retired

3 points
QUESTION 50
1. If classes C1 and C2 both implement an interface Cint, which has a method, whichIsIt, and
if C1 c = new C1(); is performed at one point of the program, then a later
instruction c.whichIsIt(); will invoke the whichIsIt method defined in C1.
True

False

1 points
QUESTION 51
1. A polymorphic reference can refer to different types of objects over time
True

False

1 points
QUESTION 52
1. Given the following code, class Aggregate is incorrect. Choose the correct line so that this
program prints: Granite: weight = 25.0 value = 4 numKind = 7

public class Inherit


{
abstract class Stone
{
protected float weight = 13;
protected int value = 4;
abstract public String toString( );
}
class Aggregate
{
protected int numKind;
}
class Granite extends Aggregate
{
Granite()
{
weight = 25; numKind = 7;
}
public String toString()
{
return "Granite: weight = " + weight + " value = " +
value + " numKind = " + numKind;
}
}
Inherit()
{
Granite g = new Granite( );
System.out.println(g);
}
public static void main(String[] args)
{
new Inherit();
}
}

a. abstract class Aggregate extends Granite {


b. abstract class Aggregate {
c. abstract class Aggregate extends Stone {
d. class Aggregate extends Stone {

3 points
QUESTION 53
1. To swap the 3rd and 4th elements in the int array values, you would do
values[3] = values[4];
values[4] = values[3];

True

False

1 points
QUESTION 54
1. A variation of a linked list is a circular linked list where the last Node in the list has next =
head rather than next = null. One problem with this type of list is that

a. it is more difficult to traverse the list since the old terminating condition, (next == null), is
no longer true for the last node
b. there is no ability to add a new Node at the end of the list since the last Node points at the
first Node
c. a header Node for this type of list is more complex
d. it
wastes memory space since head already points at the first Node, so the last one does
not need to
3 points
QUESTION 55
1. Consider a class called ChessPiece. This class has two instance data, String
type and int player. The variable type will store "King", "Queen", "Bishop",
etc. and the int player will store 0 or 1 depending on whose piece it is. We wish to
implement Comparable for the ChessPiece class. Assume that, the current ChessPiece is
compared to a ChessPiece passed as a parameter. Pieces are ordered as follows:
 "Pawn" is a lesser piece to a "Knight"
 a "Bishop" and "Knight" are equivalent for this example
 both "Bishop" and "Knight" are lesser pieces to a "Rook"
 a "Rook" is a lesser piece to a "Queen"
 a "Queen" is a lesser piece to a "King".
Which of the following pieces of logic could be used in the method that
implements Comparable? Assume that ChessPiece has a method
called returnType which returns the type of the given piece:

a. if (a.returnType().equals("Pawn"))
return 1;
b. if (this.type < a.returnType())
return -1;
c. if (a.returnType().equals("King"))
return -1;
d. if (this.type.equals(a.returnType())
return 0;
e. if (this.type == a.returnType())
return 0;
3 points
QUESTION 56
1. It is not possible to test out any single method or class of a system until the entire system has been
developed, and so all testing is postponed until after the implementation phase.
True

False
1 points
QUESTION 57
1. In which phase of program development would you expect the programmer(s) to determine the
classes and objects needed?
a. software requirements
b. software design
c. this could occur at any time
d. software implementation
e. software testing

1 points
QUESTION 58
1. Assume that a linked list is implemented using the Node class where a Node contains instance
data of int info; and Node next; where next references the next Node in the linked
list. Also assume that head references the first Node in the list. Assume Node temp is
currently set equal to head. Which of the following while loops could be used to iterate through
each element of a linked list?

a. while (temp != null)


temp = temp.next;

b. while (head != null)


head = head.next;

c. while (head != null)


head = temp.next;
d. while (head != null)
temp = temp.next;

e. while (temp != null)


head = head.next;

3 points
QUESTION 59
1. Assume that a linked list is implemented using the Node class where a Node contains instance
data of int info; and Node next; where next references the next Node in the linked
list. Also assume that head references the first Node in the list. Assume Node temp references the
last element of the linked list. Which of the following conditions is true about temp?
a. (temp.next == null)

b. (temp.info == 0)

c. (temp.next == null && temp.info == null)


d. (temp == null)

e. (temp == head)

3 points
QUESTION 60
1. In order to input a list of values and output them in order, you could use a queue. In order to input
a list of values and output them in opposite order, you could use a stack
True

False

1 points
QUESTION 61
1. Consider the following operations on a queue data structure that stores int values:

Queue q = new Queue();


q.enqueue(3);
q.enqueue(5);
q.enqueue(9);
System.out.println(q.dequeue());
q.enqueue(2);
q.enqueue(4);
System.out.println(q.dequeue());
System.out.println(q.dequeue());
q.enqueue(1);
q.enqueue(8);

After this code executes, how many elements would remain in q?

a. 7

b. 0
c. 4

d. 5

e. 6

3 points

Click Save and Submit to save and submit. Click Save All Answers to save all answers.

You might also like