CSc 10: Introduction to Computing
Final Exam sample questions and answers
Answer the following T or
F. Explain why or why not. (3 each, 45 total)
See http://www.cse.lehigh.edu/~glennb/um/book/exsolnew.htm.
What is an advantage of value
parameters?
Avoiding hidden side effects,
because pass by value copies an actual parameter to a formal parameter, any
changes to the formal parameter in the callee method have no effect on the
actual parameter in the caller.
What are two
advantages of reference
parameters?
1) Allowing multiple outputs from a method, as side
effects of any changes to a parameter within a callee (unlike pass by value).
2) Passing large objects efficiently, since no copying of
values occurs.
Write a method which, given an String, converts all the ‘.’ (periods) into ‘.’ (commas) and returns the new String. Note: do not use any library functions other than length, charAt and setCharAt.
public
class test
{
/** convert commas to periods */
static String convert(String in)
{ StringBuffer out = new StringBuffer(in);
//Modify as a StringBuffer
int i=0;
while (i < out.length())
//Loop boundary invariant: 0 <= i <
in.length
{ if (out.charAt(i) == ',') //Is it a comma?
out.setCharAt(i,'.'); //Make it a period
i++;
}
return out.toString(); //Return as String
}
public static void main(String args[])
{
String test="1,000,000";
System.out.println("test="+test
+ " convert="+convert(test));
int stop = Input.getInt();
}
}
In each of the following code
fragments: a) find and explain a bug, b) fix it (by marking up the code on this
sheet), and c) show your corrected version's output.
const
int Sentinel = -1;
int count=0;
float sum=0;
int score;
System.out.println("Enter scores, stopping with
–1:");
while (score != Sentinel)
{ score = Input.getInt();
count =
count + 1;
sum = sum +
score;
}
System.out.println(count + " scores averaging
" + sum / count);
//Assume user types in: 70 81 -1
a)
The
erronerous output is: 3 scores averaging 53.333333
The bug is that the sentinel value, -1, gets counted
as an actual score.
b)
The
fix is to use the sentinel loop idiom, moving the input statement so that it
occurs just before the while loop test:
score = Input.getInt();
while (score != Sentinel)
{ count = count + 1;
sum = sum +
score;
score =
Input.getInt();
}
c)
2
scores averaging 75.5
static void switchEm(int
x,int y)
{ int temp=x;
x = y;
y = temp;
}
public static void
main(String args[])
{ int a=1,b=2;
switchEm(a,b);
System.out.println("Now " + a + " is bigger than
" + b);
}
a)
The erroneous output is: Now 1 is bigger than 2
The bug is that the parameters get
passed by value, so the changes to the formal parameters, x and y, don’t affect the actual parameters a and b.
b) Without explicit pass by reference, the fix isn’t as easy
in Java as in C++. Pass in an object,
such as an array, and set its values (passing an object just copies the
object’s handle, not the values):
static void
switchEm(int a[])
{ int temp=a[0];
a[0] = a[1];
a[1] = temp;
}
public static void main(String
args[])
{ int a[] = new int[2];
a[0] = 1; a[1] = 2;
switchEm(a);
System.out.println("Now " + a[0] +
" is bigger than " + a[1]);
}
Explain why multiprogramming
can improve the performance or Athroughput@ of each of the following types of systems:
a)
Batch operating systems
While one job is waiting for file processing, another job
can run, maximizing CPU usage.
b)
Time-sharing operating systems
While
one user is inactive, the processor can switch to another user, maximizing
usage.
6. Consider the following
snippet from the atm_oop program:
public class Account
{ public Account() {}
public int getPIN() { return PIN; }
void setPIN(int pin) { PIN = pin; }
protected int PIN; //Personal Identification Number
}
a) Given the above class
definition, define a variable, myAccount,
of this type, then assign it a PIN.
Account
myAccount = new Account(); //construct
an Account
myAccount.setPIN(2468);
b) Why is the following
snippet illegal? Why is making it
illegal a good idea?
if
(myAccount.PIN == 2468)
Because PIN is private.
Making it illegal protects the meaning of the data. Though it doesn’t in the implementation
above, setPIN could
include an assertion that makes sure that PIN only gets a valid value.
c) Derive another class, Savings, from class Account. Declare a
constructor for your new class which requires a PIN number.
class
Savings extends Account
{ Savings(int pin) { PIN = pin; }
//Note that the above statement assumes that PIN is protected, not private in Account.
d) How do classes illustrate
the power of abstraction?
Classes let their clients
focus on a public interface, while suppliers can worry about implementation
details.
e) How does class inheritance
further illustrate abstraction?
Inheritance pools everything
(methods an variables) that a set of classes may have in common in one place,
so implementors subclasses don’t have to keep re-inventing the wheel.
Th.. th.. th.. that's all folks!