Sem 1 Practice Final 1-2016
Sem 1 Practice Final 1-2016
1. What is the value of product a0er the following code segment is executed?
(A) 2
(B) 3
(C) 4
(D) 5
(E) 9
(A) 0
(B) 0 5
(C) 0 5 10
(D) 0 5 10 15
(E) 0 5 10 15 20
I. An interface contains only public abstract methods and public staHc final fields.
II. If a class implements an interface and then fails to implement any methods in that interface, then the class must be
declared abstract.
III. While a class may implement just one interface, it may extend more than one class.
(A) I only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III
1
4. Which of the following declaraHons will cause an error?
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only
5. Consider the following code fragment. You may assume that wordList has been declared as ArrayList<String>.
What is the maximum number of Hmes that "This word is short." can be printed?
(A) 3
(B) 4
(C) wordList.size()
(D) wordList.size() - 1
(E) s.length()
(A) 2 bytes
(B) 4 bytes
(C) 8 bytes
(D) It depends on the compiler seSng
(E) It depends on the operaHng system
are declared, which of the following statements will cause a syntax error?
(A) p = cw;
(B) cw = new Puzzle();
(C) p = new Crossword(10, 20);
(D) Solvable x = new Crossword(10, 20);
(E) All of the above will compile with no errors
2
8. The expression !((x <= y) && (y > 5)) is equivalent to which of the following?
(A) 1
1 4
1 4 9
1 4 9 16
1 4 9 16 25
(B) 1 4 9 16 25
1 4 9 16
1 4 9
1 4
1
(C) 25 16 9 4 1
25 16 9 4
25 16 9
25 16
25
(D) 25
25 16
25 16 9
25 16 9 4
25 16 9 4 1
(E) 1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
1 4 9 16 25
3
10. The following method is intended to remove from List<Integer> list all elements whose value is less than zero:
while (i < n)
{
if (list.get(i) < 0)
{
list.remove(i);
n--;
}
i++;
}
}
For which list of Integer values does this method work as intended?
(A) I
(B) IO
(C) ION
(D) ONI
(E) NION
4
QuesHons 13 and 14 refer to the following class, SortOf:
13. The sorHng algorithm implemented in the sort method can best be described as:
If SortOf.sort(names) is running, what is the order of the values in names a0er two complete iteraHons through the
while loop in the sort method?
5
15. Consider the following class definiHons:
For which integer values of n do fun1(n) and fun2(n) return the same result?
6
17. Consider the following class:
If ClassList myClass is declared and iniHalized in some other client class, which of the following correctly assigns to
name the name of the first student in the myClass list?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) II and III only
(A) 21
(B) 15
(C) 5
(D) 3
(E) 1
7
For quesHons 19 and 20, consider the following interfaces and classes:
public ClassB ( )
8
19. What is the minimum requirement for method definiHons in ClassB for it to compile with no errors?
(A) System.out.println("2/3");
(B) System.out.println("2" + "/" + "3");
(C) System.out.println(2/3);
(D) System.out.println(2 + "/" + 3);
(E) System.out.println((int)2 + "/" + (int)3);
9
Which of the following methods inside the TestPow2 class will compile with no errors?
(A) I only (B) II only (C) I and II only (D) II and III only (E) I, II, and III
10
25. What output will be produced by calling firstTestMethod for a Tester object (assuming that testArray
contains 3 4 5)?
(A) 3 4 5
(B) 4 5 6
(C) 5 6 7
(D) 0 0 0
(E) No output will be produced. An ArrayIndexOutOfBoundsExcep`on will be thrown.
26. What output will be produced by invoking secondTestMethod for a Tester object (assuming that testArray
contains 3 4 5)?
(A) 3 4 5
(B) 4 5 6
(C) 5 6 7
(D) 0 0 0
(E) No output will be produced. An ArrayIndexOutOfBoundsExcep`on will be thrown.
27. What happens if you forget to iniHalize a field that is an object, and start calling its methods?
(A) A syntax error is reported
(B) A run-Hme "null reference excepHon" error is reported
(C) A new object is created with the default values for its fields
(D) A new object is created with unpredictable values for its fields
(E) Mr. DeRuiter will tap you on the shoulder and say "bad programmer!" (this is not the correct answer . . .)
double a = 1.1;
double b = 1.2;
if ((a + b) * (a - b) != (a * a) - (b * b))
{
System.out.println("Mathema`cal error!");
}
Which of the following best describes why the phrase "Mathema`cal error!" would be printed?
(Remember that mathemaHcally (a+b)*(a-b) = a2 - b2 .)
29. Which of the following is a good stylisHc rule for naming fields?
11
30. Consider the following code segment:
int x = 0, y = 3;
String op = new String("/");
(A) There will be an error at compile Hme, because String and int variables are intermixed in the same condiHon.
(B) There will be an error at run Hme, due to division by zero.
(C) The code will compile and execute without error. The output will be OK.
(D) The code will compile and execute without error. The output will be Failed.
(E) The code will compile and execute without error. There will be no output (nothing printed).
12
public class Code extends Secret
{
public Code (int c)
{
super(c + 1);
}
(A) Secret - 0
(B) Secret - 1
(C) Secret - 2
(D) Code - 1
(E) Code - 2
Which of the following represents the contents of array as a result of execuHng the code segment?
(A) 2, 4, 6, 8, 10, 12
(B) 2, 6, 6, 8, 10, 12
(C) 2, 4, 8, 8, 10, 12
(D) 2, 4, 8, 10, 12, 12
(E) 2, 6, 8, 10, 12, 12
13
33. Consider the following class definiHons.
Now, consider the following declaraHons in a client class. You may assume that ClassOne and ClassTwo have default
constructors.
I. c1.methodTwo();
II. c2.methodTwo();
III. c2.methodOne();
14
Which of the following method signatures of compareTo will saHsfy the Comparable interface requirement?
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
35. A car dealership needs a program to store informaHon about the cars for sale. For each car, they want to
keep track of the following informaHon: number of doors (2 or 4), whether the car has air condiHoning, and its
average number of miles per gallon. Which of the following is the best design?
(A) Use one class, Car, which has three data fields: int numDoors, boolean hasAir, and double milesPerGallon.
(B) Use four unrelated classes: Car, Doors, AirCondi`oning, and MilesPerGallon.
(C) Use a class Car which has three subclasses: Doors, AirCondi`oning, and MilesPerGallon.
(D) Use a class Car, which has a subclass Doors, with a subclass AirCondi`oning, with a subclass MilesPerGallon.
(E) Use three classes: Doors, AirCondi`oning, and MilesPerGallon, each with a subclass Car.
36. Which of the following statements about interfaces and abstract classes is TRUE?
(A) Methods in a derived class that redefine a method from the base class
(B) Methods of a class that create objects of the same class
(C) Methods of the same class that have the same name but different numbers or types of parameters
(D) Methods of different classes that have the same name
(E) Methods that are taking too many final exams
15
Which of the following code segments correctly computes the sum of all prime numbers from 2 to 101?
(A) while (n != 2)
{
n--;
if (isPrime(n))
{
sum += n;
}
}
(C) while (n != 2)
{
if (isPrime(n))
{
sum += n;
}
n--;
}
(A) n
(B) n log n
(C) (log n)2
(D) n2
(E) n3
16
40. Consider the following statements about the Java programming language:
(A) I only
(B) I and II only
(C) II and III only
(D) I and III only
(E) I, II, and III
I. System.out.println(obj.substring(3,9));
(A) I only (B) II only (C) I and II only (D) II and III only (E) I, II, and III
43. The class CourseList provides methods that allow you to represent and manipulate a list of high school courses, but
you are not concerned with how these operaHons work or how the list is stored in memory. You only know how to
iniHalize and use CourseList objects and have no direct access to the implementaHon of the CourseList class or its
private data fields. This is an example of
(A) encapsulaHon
(B) overriding
(C) inheritance
(D) polymorphism
(E) method overloading
17
44. What is the output of the following code segment?
45. What is the result when the following code segment is compiled and executed?
int m = 4, n = 5;
double d = Math.sqrt ((m + n) / 2);
System.out.println (d);
(A) the following syntax error occurs: "sqrt(double) in java.lang.Math cannot be applied to int"
(B) 1.5 is printed
(C) 2.0 is printed
(D) 2.1213203435596424 is printed
(E) a ClassCastExcep`on is thrown
while (x > y)
{
x--;
y++;
}
System.out.println(x - y);
Assume that x and y are int variables and their values saHsfy the condiHons 0 <= x <= 2 and 0 <= y <= 2 before the
start of the while loop. Which of the following describes the set of all possible outputs?
(A) 0
(B) -1, 1
(C) -1, -2
(D) 0, -1, -2
(E) 0, -1, 1, -2, 2
18
FREE RESPONSE
Note that a Cat “is-a” Pet, a Dog “is-a” Pet, and a LoudDog “is-a” Dog. The class Pet is shown in the following
declaraHon.
The subclass Dog has the parHal class declaraHon shown below.
{
public Dog (String name)
{ /* implementa`on not shown */ }
19
(a) Given the class hierarchy shown above, write a complete class declaraHon for the class Cat, including
implementaHons of its constructor and method(s). The Cat method speak returns “meow” when it is invoked. Note
that the method speak will need to be overridden in Cat.
(b) Assume that class Dog has been declared as shown at the beginning of the quesHon. If the String dog-sound is
returned by the Dog method speak, then the LoudDog method speak returns a String containing dog-sound
repeated two Hmes. (note that we do not know the String for dog-sound)
Given the class hierarchy shown previously, write a complete class declaraHon for the class LoudDog, including
implementaHons of its constructors and method(s).
20
2. Consider a class, NoteKeeper, that is designed to store and manipulate a list of short notes. Here are some typical
notes:
pick up drycleaning
special dog chow
dog registraHon
denHst Monday
study for APCS quiz
// Postcondi`on: All notes with specified word have been removed from noteList, leaving the order of the
// remaining notes unchanged. If none of the notes in noteList contains word, the list remains
// unchanged.
public void removeNotes(String word)
{
/* to be implemented in part (b) */
}
(a) Write the NoteKeeper method printNotes. The printNotes method prints all of the notes in noteList, one per line,
and numbers the notes, starHng at 1. The output should look like this:
1. pick up drycleaning
2. special dog chow
3. dog registraHon
4. denHst Monday
5. study for APCS quiz
21
Complete method printNotes below.
(b) Write the NoteKeeper method removeNotes. Method removeNotes removes all notes from noteList that contain
the word specified by the parameter. The ordering of the remaining notes should be le0 unchanged. For example,
suppose that a NoteKeeper variable, notes, has a noteList containing
[pick up drycleaning, special dog chow, dog registraHon, denHst Monday, study for APCS quiz]
// Postcondi`on: All notes with specified word have been removed from noteList, leaving the order of the
// remaining notes unchanged. If none of the notes in noteList contains word, the list remains
// unchanged.
public void removeNotes(String word)
22
3. Consider the class hierarchy diagram shown to the right.
PersonalInfo is an interface that is implemented by Employee. A PartTimeEmployee is-a Employee. Here is the
declaraHon for PersonalInfo.
(a) Given the class hierarchy diagram shown above, write a complete class declaraHon for the class Employee, including
implementaHon of methods and a constructor with parameters. An Employee, in addiHon to implemenHng
PersonalInfo, has a data field to store annual salary, and an accessor method that returns the annual salary of the
Employee. Write the Employee class below, or on the mulHple choice answer sheet.
23
(b) Write a complete class declaraHon for the PartTimeEmployee class, given the class hierarchy shown above. A
PartTimeEmployee has two addiHonal data fields:
- A floaHng point number that indicates the fracHon that the part-Hme employee works (for example, 0.5, 0.8, etc.)
- A boolean field that stores whether the employee is a union member or not.
- An accessor that returns the floaHng point number fracHon that the PartTimeEmployee works.
- An accessor that returns a value indicaHng whether the PartTimeEmployee is a union member or not.
- A mutator that switches the status of that employee’s union membership. If the employee’s salary is
greater than $15,000, then employee is not a union member. Otherwise, the employee is a union member.
Write the PartTimeEmployee class below, or on the mulHple choice answer sheet.
24