Professional Documents
Culture Documents
Spring 2005
P. Van Hise
Part 1
Given the nested if-else structure below, answer questions 1-2.
if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
1)
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
2)
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a) 0
b) 2
c) 3
d) 4
e) 5
3)
4)
5)
6)
7)
c) 9 times
d) 10 times
e) 11 times
Given that s is a String, what does the following loop do?
for (int j = s.length( ); j > 0; j--)
System.out.print(s.charAt(j-1));
a) it prints s out backwards
b) it prints s out forwards
c) it prints s out backwards after skipping the last character
d) it prints s out backwards but does not print the 0th character
e) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
The following nested loop structure will execute the inner most statement (x++) how many times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)
x++;
a) 100
b)200
c) 10,000
d)20,000
e) 1,000,000
8)
words?
In order to create a constant, you would use which of the following Java reserved
a)
b)
c)
d)
e)
private
static
int
final
class
Part 2
1)
2)
3)
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 the above could throw a StringIndexOutOfBoundsException
4)
5)
6)
e)
a checked exception always must be caught by a try block; an unchecked exception does not
10) What does the following method compute? Assume the method is called initially with i = 0
public int question(String a, char b, int i)
{
if (i = = a.length( )) return 0;
else if (b = = a.charAt(i)) return question(a, b, i+1) + 1;
else return question(a, b, i+1);
}
a)
b)
c)
d)
e)
Assume that int[ ] a = {6, 2, 4, 6, 2, 1, 6, 2, 5} and consider the two recursive methods below, foo and bar.
public int foo(int[ ] a, int b, int j)
{
if (j < a.length)
if (a[j] != b) return foo (a, b, j+1);
0
5
6
12
34
Part 3
For questions 1 4, use the following partial class definitions:
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;
}
1)
2)
Which of the following lists of instance data are accessible in class A2?
a) x, y, z, a, b
b) x, y, z, a
c) x, z, a, b
d) z, a, b
e) a, b
3)
4)
Which of the following is true regarding the use of instance data y of class A1?
a) it is accessible in A1, A2 and A3
b) it is accessible in A1 and A2
c) it is accessible only in A1
d) it is accessible only in A3
e) it is not accessible to any of the three classes
5)
6)
Inheritance through an extended (derived) class supports which of the following concepts?
a) interfaces
b) modulary
c) information hiding
d) code reuse
e) correctness
7)
Aside from permitting inheritance, the visibility modifier protected is also used to
a) permit access to the protected item by any class defined in the same package
b) permit access to the protected item by any static class
c) permit access to the protected item by any parent class
d) ensure that the class can not throw a NullPointerException
e) define abstract elements of an interface
8)
9)
Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by
a) importing classes
b) implementing interfaces
c) overriding parent class methods
d) creating aliases
e) using public rather than protected or private modifiers
10)
If a programmer writes a class wanting it to be extended by another programmer, then this programmer
must
a) change private methods and instance data to be protected
b) change public methods and instance data to be protected
c) change all methods to be protected
d) change the class to be protected
e) none of the above, the programmer does not have to change anything
11)
All classes in Java are directly or indirectly subclasses of the _______ class.
a) Wrapper
b) String
c) Reference
d) this
e) Object
12)
13)
14)
In order to determine the type that a polymorphic variable refers to, the decision is made
a) by the programmer at the time the program is written
b) by the compiler at compile time
c) by the operating system when the program is loaded into memory
d) by the Java run-time environment at run time
e) by the user at run time
For questions 15-17, 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 are 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
15)
21) In which phase of program development would you expect the programmer(s) to create the pseudocode?
a) Software requirements
b) Software design
c) Software implementation
d) Software testing
e) Could occur in any of the above
Part 4
For questions 1-4, assume values is an int array that is currently filled to capacity, with the following values:
9
12
1)
2)
3)
18
What does the following code do? Assume list is an array of int values, temp is some previously initialized
int value, and c is an int initialized to 0.
for (j=0; j < list.length; j++)
if (list[j] < temp) c++;
a)
b)
c)
d)
e)
An int array stores the following values. Use the array to answer questions 4 7
9
12
18
4)
Which of the following lists of numbers would accurately show the array after the first pass through the
Selection Sort algorithm?
a) 9, 4, 12, 2, 6, 8, 18
b) 4, 9, 12, 2, 6, 8, 18
c) 2, 4, 12, 9, 6, 8, 18
d) 2, 4, 6, 8, 9, 12, 18
e) 2, 4, 9, 12, 6, 8, 18
5)
Which of the following lists of numbers would accurately show the array after the second pass of the
Selection Sort algorithm?
a) 9, 4, 12, 2, 6, 8, 18
b) 2, 4, 9, 6, 12, 8, 18
c) 2, 4, 12, 9, 6, 8, 18
d) 2, 4, 6, 8, 9, 12, 18
e) 2, 4, 12, 6, 8, 9, 18
6)
How many passes will it take in all for Selection Sort to sort this array?
a) 2
b) 4
c) 5
d) 6
e) 7
7) What are the main programming mechanisms that constitute object-oriented programming?
a) Encapsulation, inheritance, polymorphism
b) Encapsulation, abstraction, inheritance
c) Inheritance, polymorphism, recursion
d) Polymorphism, recursion, abstraction
e) None of the above
8) What is printed by the following code? Consider the polymorphic invocation.
public class Inherit
{
class Figure
{
void display( )
{
System.out.println("Figure");
}
}
Figure
Rectangle
Box
b) Rectangle
Box
c)
Figure
Figure
Figure
e)
encapsulation
10) Which of these methods will sort an array of floats into ascending order?
a)
void arrange(double[ ] ary)
{
for (int n=0; n<ary.length; n++)
for (int k=n; k<ary.length; k++)
if (ary[n] > ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
b)
void arrange(double [ ] ary)
{
for (int n=0; n<ary.length; n++)
for (int k=n; k<ary.length; k++)
if (ary[n] < ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
c)
void arrange(double [ ] ary)
{
for (int n=1; n<=ary.length; n++)
for (int k=n; k<ary.length; k++)
if (ary[n] > ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
d)
void arrange(double [ ] ary)
{
for (int n=0; n<ary.length; n++)
for (int k=n; k<ary.length; k++)
if (ary[n] > ary[k])
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
e)
ANSWERS:
Part 1
1.C
2B
3B
4D
5D
6A
7C
8D
Part 2
1. B Explanation: The call stack trace provides the names of the methods as stored on the run-time stack. The
method names are removed from the stack in the opposite order that they were placed, that is, the earliest method
was placed there first, the next method second, and so forth so that the most recently invoked method is the last item
on the stack, so it is the first one removed. The stack trace then displays all active methods in the opposite order that
they were called (most recent first).
2E
3B
4.C Explanation: Character streams are used to manage 16-bit Unicode characters. This differs from a byte stream
that is used to manage any kind of byte-sized data, including ASCII characters and binary data of other types.
5 E Explanation: Input into a Java program is difficult because it requires a lot of overhead. The Scanner class
implements all of that overhead so that the programmer does not have to see it. Thus, Scanner is an abstraction for
performing input operations without the details. These details include importing java.io classes, handling
IOExceptions in some way, inputting from the standard input stream, dividing the input into individual tokens and
converting each token as needed into the requesting form.
6 D Explanation: A checked exception must either be caught or it must be listed in a throws clause. An unchecked
exception requires no throws clause. Both kinds of exceptions follow the rules of exception propagation.
7C
8D
9C
10 C
11 D
12 E
2C
3D
4C
5E
6D
13 B
14 B
Part 3
1B
7 A Explanation: The visibility modifier protected is used to control access to the item in a protected (guarded)
manner. The protection is that access is restricted to the current class (like private items), classes in the same
package, or extended classes of this class.
8 C Explanation: Multiple inheritance means that a given class inherits from more than one parent class. Of
those listed above, a laptop computer inherits properties from both PCs and portable devices. The answers in a, b
and e are all examples of single inheritance where a class has at least two children (in a, computer has children
mainframe and PC, in b, PC has children desktop and laptop, in e, PC has children Macintosh and IBM PC).
Answer d denotes a property of a class.
9B
10 A
11 E
12 B
13 A
14 D
15 A Explanation: At this point of the program, p is a Person, and so getMoney is a reference to Persons
getMoney method.
16 B Explanation: At this point of the program, p is a Student, and so getMoney is a reference to Students
getMoney method.
17 E
18 C
19 D
20 B
2D
3D
4C
5C
6D
7A
8A
9B
10 A