CSC220 Review for Final Exam 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;
-
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
-
0
-
2
-
3
-
4
-
5
-
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
-
0
-
2
-
3
-
4
-
5
-
The break statement does which of the following?
-
ends the program
-
transfers control out of the current control structure such as a loop or switch statement
-
ends the current line of output, returning the cursor
-
denotes the ending of a switch statement
-
indicates the end of line when using System.out.print
4) If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2;
a) 2
-
64
-
100
-
128
-
None of the above, this is an infinite loop
5) How many times will the following loop iterate?
int x = 10;
while (x > 0)
{
System.out.println(x);
x--;
}
-
0 times
-
1 time
-
9 times
-
10 times
-
11 times
6) 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));
-
it prints s out backwards
-
it prints s out forwards
-
it prints s out backwards after skipping the last character
-
it prints s out backwards but does not print the 0th character
-
it yields a run-time error because there is no character at s.charAt(j-1) for j = 0
7) 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++;
-
100
-
200
-
10,000
-
20,000
-
1,000,000
8) In order to create a constant, you would use which of the following Java reserved words?
-
private
-
static
-
int
-
final
-
class
Part 2
-
An exception can produce a “call stack trace” which lists
-
the active methods in the order that they were invoked
-
the active methods in the opposite order that they were invoked
-
the values of all instance data of the object where the exception was raised
-
the values of all instance data of the object where the exception was raised and all local variables and parameters of the method where the exception was raised
-
the name of the exception thrown
-
A finally clause will execute
-
only if the try statement that precedes it does not throw an exception
-
only if the try statement that precedes it throws an exception that is caught
-
only if the try statement that precedes it throws an exception that is not caught
-
only if the try statement that precedes it throws an exception, whether it is caught or not
-
in any circumstance
-
Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException?
-
str.length( )
-
str.charAt(2);
-
str.replace(‘a’, ‘A’);
-
str.equals(str);
-
any of the above could throw a StringIndexOutOfBoundsException
-
Character streams manage
-
byte-sized data
-
binary data
-
Unicode characters
-
ASCII characters
-
compressed data
-
The Scanner class provides an abstraction for input operations by
-
using try and catch statements to catch any IOException instead of throwing the Exception elsewhere
-
parsing input lines into individual tokens
-
performing conversion operations from String to the appropriate type as specified in the Scanner message
-
inputting from the standard input stream if create is called using System.in
-
all of the above
-
The difference between a checked and an unchecked exception is:
-
checked exceptions need not be listed in a throws clause
-
unchecked exceptions must be listed in a throws clause
-
neither kind of exception follows the rules of exception propagation
-
an unchecked exception requires no throws clause
-
a checked exception always must be caught by a try block; an unchecked exception does not
public int question(int x, int y)
{
if (x == y) return 0;
else return question(x-1, y) + 1;
}
7) If the method is called as question(8, 3), what is returned?
-
11
-
8
-
5
-
3
-
24
8) Calling this method will result in infinite recursion if which condition below is initially true?
-
(x = = y)
-
(x != y)
-
(x > y)
-
(x < y)
-
(x = = 0 && y != 0)
9) The following method should:
return true if the int parameter is even and either positive or 0, and false otherwise.
Which set of code should you use to replace … so that the method works appropriately?
public boolean question(int x) { … }
a) if (x = = 0) return true;else if (x < 0) return false;else return question(x – 1);
b) if (x = = 0) return false;else if (x < 0) return true;else return question(x – 1);
c) if (x = = 0) return true;else if (x < 0) return false;else return question(x – 2);
d) if (x = = 0) return false;else if (x < 0) return true;else return question(x – 2);
e) return(x = = 0);
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) The length of String a
b) The length of String a concatenated with char b
c) The number of times char b appears in String a
d) Returns 1 if char b appears in String a at least once, and 0 otherwise
e) The char which appears at location i in String a
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);
else return foo (a, b, j+1) + 1;
else return 0;
}
public int bar(int[ ] a, int j)
{
if (j < a.length)
return a[ j ] + bar(a, j+1);
else return 0;
}
11) What is the result of calling foo(a, 2, 0);?
a) 0
b) 1
c) 2
d) 3
e) 4
12) What is the result of calling bar(a, 0);?
a) 0
b) 5
c) 6
d) 12
e) 34
13) What is the result of bar(a, 8);?
a) 0
b) 5
c) 6
d) 12
e) 34
14) What does the following recursive method determine?
public boolean question(int[ ]a, int[ ] b, int j)
{
if (j = = a.length) return false;
else if (j = = b.length) return true;
else return question(a, b, j+1);
}
a) Returns true if a and b are equal in size, false otherwise
b) Returns true if a is larger than b, false otherwise
c) Returns true if b is larger than a, false otherwise
d) Returns true if a and b have no elements
e) Returns the length of array a + length of array b
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;
…
}
-
Which of the following is true with respect to A1, A2 and A3?
-
A1 is a subclass of A2 and A2 is a subclass of A3
-
A3 is a subclass of A2 and A2 is a subclass of A1
-
A1 and A2 are both subclasses of A3
-
A2 and A3 are both subclasses of A1
-
A1, A2 and A3 are all subclasses of the class A
-
Which of the following lists of instance data are accessible in class A2?
-
x, y, z, a, b
-
x, y, z, a
-
x, z, a, b
-
z, a, b
-
a, b
-
Which of the following lists of instance data are accessible in A3?
-
x, y, z, a, b, q
-
a, b, q
-
a, q
-
x, z, a, q
-
x, a, q
-
Which of the following is true regarding the use of instance data y of class A1?
-
it is accessible in A1, A2 and A3
-
it is accessible in A1 and A2
-
it is accessible only in A1
-
it is accessible only in A3
-
it is not accessible to any of the three classes
-
The instruction super( ); does which of the following?
-
calls the method super as defined in the current class
-
calls the method super as defined in the current class’ parent class
-
calls the method super as defined in java.lang
-
calls the constructor as defined in the current class
-
calls the constructor as defined in the current class’ parent class
-
Inheritance through an extended (derived) class supports which of the following concepts?
-
interfaces
-
modulary
-
information hiding
-
code reuse
-
correctness
-
Aside from permitting inheritance, the visibility modifier protected is also used to
-
permit access to the protected item by any class defined in the same package
-
permit access to the protected item by any static class
-
permit access to the protected item by any parent class
-
ensure that the class can not throw a NullPointerException
-
define abstract elements of an interface
-
Which of the following is an example of multiple inheritance?
-
a computer can be a mainframe or a PC
-
a PC can be a desktop or a laptop
-
a laptop is both a PC and a portable device
-
a portable device is a lightweight device
-
Macintosh and IBM PC are both types of PCs
-
Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by
-
importing classes
-
implementing interfaces
-
overriding parent class methods
-
creating aliases
-
using public rather than protected or private modifiers
-
If a programmer writes a class wanting it to be extended by another programmer, then this programmer must
-
change private methods and instance data to be protected
-
change public methods and instance data to be protected
-
change all methods to be protected
-
change the class to be protected
-
none of the above, the programmer does not have to change anything
-
All classes in Java are directly or indirectly subclasses of the _______ class.
-
Wrapper
-
String
-
Reference
-
this
-
Object
-
Which of the following is not a method of the Object class?
-
clone
-
compareTo
-
equals
-
toString
-
Which of the following is true regarding Java classes?
-
All classes must have 1 parent but may have any number of children (derived or extended) classes.
-
All classes must have 1 child (derived or extended) class but may have any number of parent classes.
-
All classes must have 1 parent class and may have a single child (derived or extended) class.
-
All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes.
-
All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes.
-
In order to determine the type that a polymorphic variable refers to, the decision is made
-
by the programmer at the time the program is written
-
by the compiler at compile time
-
by the operating system when the program is loaded into memory
-
by the Java run-time environment at run time
-
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
-
The reference to getMoney( ) in assignment 1 is to the class
-
Person
-
Student
-
Employee
-
Retired
-
none of the above, this cannot be determined by examining the code
16) The reference to getMoney( ) in assignment 2 is to the class
-
Person
-
Student
-
Employee
-
Retired
-
none of the above, this cannot be determined by examining the code
17) The reference to getMoney( ) in assignment 3 is to the class
-
Person
-
Student
-
Employee
-
Retired
-
none of the above, this cannot be determined by examining the code
18) Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.
-
equals (String)
-
toUpperCase (String)
-
sqrt (Math)
-
format (DecimalFormat)
-
paint (Applet)
19) An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
-
inner
-
i
-
private
-
this
-
static
20) Java does not support multiple inheritance, but some of the abilities of multiple inheritance are available by
-
importing classes
-
implementing interfaces
-
overriding parent class methods
-
creating aliases
-
using public rather than protected or private modifiers
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:
-
What is returned by values[3]?
-
9
-
12
-
2
-
6
-
3
-
What is the value of values.length?
-
0
-
5
-
6
-
7
-
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++;
-
It finds the smallest value and stores it in temp
-
It finds the largest value and stores it in temp
-
It counts the number of elements equal to the smallest value in list
-
It counts the number of elements in list that are less than temp
-
It sorts the values in list to be in ascending order
An int array stores the following values. Use the array to answer questions 4 – 7
-
Which of the following lists of numbers would accurately show the array after the first pass through the Selection Sort algorithm?
-
9, 4, 12, 2, 6, 8, 18
-
4, 9, 12, 2, 6, 8, 18
-
2, 4, 12, 9, 6, 8, 18
-
2, 4, 6, 8, 9, 12, 18
-
2, 4, 9, 12, 6, 8, 18
-
Which of the following lists of numbers would accurately show the array after the second pass of the Selection Sort algorithm?
-
9, 4, 12, 2, 6, 8, 18
-
2, 4, 9, 6, 12, 8, 18
-
2, 4, 12, 9, 6, 8, 18
-
2, 4, 6, 8, 9, 12, 18
-
2, 4, 12, 6, 8, 9, 18
-
How many passes will it take in all for Selection Sort to sort this array?
-
2
-
4
-
5
-
6
-
7
7) What are the main programming mechanisms that constitute object-oriented programming?
-
Encapsulation, inheritance, polymorphism
-
Encapsulation, abstraction, inheritance
-
Inheritance, polymorphism, recursion
-
Polymorphism, recursion, abstraction
-
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");
}
}
class Rectangle extends Figure
{
void display( )
{
System.out.println("Rectangle");
}
}
class Box extends Figure
{
void display( )
{
System.out.println("Box");
}
}
Inherit( )
{
Figure f = new Figure( );
Rectangle r = new Rectangle( );
Box b = new Box( );
f.display( );
f = r;
f.display( );
f = b;
f.display( );
}
public static void main(String[ ] args)
{
new Inherit( );
}
}
-
Figure
Rectangle
Box
-
Rectangle
Box
-
Figure
Figure
Figure
-
Syntax error. This code won't compile or execute
-
None of the above
9) One way polymorphism is achieved is by
-
extending
-
overriding
-
embedding
-
abstraction
-
encapsulation
10) Which of these methods will sort an array of floats into ascending order?
-
void arrange(double[ ] ary)
{
for (int n=0; n
for (int k=n; k
if (ary[n] > ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
-
void arrange(double [ ] ary)
{
for (int n=0; n
for (int k=n; k
if (ary[n] < ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
-
void arrange(double [ ] ary)
{
for (int n=1; n<=ary.length; n++)
for (int k=n; k
if (ary[n] > ary[k])
{
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
}
-
void arrange(double [ ] ary)
{
for (int n=0; n
for (int k=n; k
if (ary[n] > ary[k])
double x = ary[n];
ary[n] = ary[k];
ary[k] = x;
}
None of the above
ANSWERS:
Part 1
1.C 2 B 3 B 4 D 5 D 6 A 7 C 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).
2 E 3 B
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.
7 C 8 D 9 C 10 C 11 D 12 E 13 B 14 B
Part 3
1 B 2 C 3 D 4 C 5 E 6 D
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.
9 B 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 Person’s getMoney method.
16 B Explanation: At this point of the program, p is a Student, and so getMoney is a reference to Student’s getMoney method.
17 E 18 C 19 D 20 B
21 B Answer: Pseudocode is a description of an algorithm written in an English-like way rather than in a specific programming language. This is part of the program’s design. In the implementation phase, the programmer(s) translates the pseudocode into the programming language being used.
Part 4
1 C 2 D 3 D 4 C 5 C 6 D 7 A 8 A 9 B 10 A
Share with your friends: |