1. What is printed by this program? Show intermediate steps separate from the final answer for partial credit. Be sure to follow this code one step at a time! All needed code is in this one class X.
public class X
{
private string s;
private string t;
public static void Main(string[] args)
{
X a = new X("the"); //1
X b = new X("an"); //2
b.Change("oh"); //3
Console.WriteLine(a.ToString()); //4
Console.WriteLine(b.ToString()); //5
}
public X(string str) //6
{
s = str; //7
t = ""; //8
}
public void Change(string z) //9
{
s = s.ToUpper() + z; //10
t = z + s; //11
}
public override string ToString() //12
{
return "s:" + s + " t:" + t; //13
}
}
2. What is printed by this code fragment?
int[] a = {3, 1, 2}; //1
foreach (int n in a) { //2
for (int j = 0; j < n; j++) { //3
Console.Write(n); //4
}
Console.WriteLine(); //5
}
3. Complete the method definition.
/** Print the elements of a in reverse order, with a blank after each element.
For example, if a contains {7, 2, 8, 6}, then the method prints: 6 8 2 7 */
public static void PrintReversed(int[] a)
{
4. Complete the method definition.
/** Consider each element of the List words, and add each element that has length less than tooBig to the
List small. For example, if tooBig is 4, and the List words contains the elements
"an", "elephant", "was", and "tall", then "an" and "was" are added to the List small. */
public static void CopySmall(List< String> words, int tooBig,
List< String> small)
{
5. Complete the method definition.
/** Read pairs of lines from reader to make entries in lookup, until the end of the file is
reached The two lines in a pair become the key and value for an entry in lookup. Assume
there are an even number of lines remaining to be read by reader. For example, if the
remaining lines are the four at the right, then two entries would be made in lookup, with key
“up” associated with value “down”, and one with key “in” associated with value “out”. */
up
down
in
out
public static void FileToDict(StreamReader reader,
Dictionary< string, string> lookup)
{
6. Suppose you are given the C# interface:
interface MakesSound {
void Speak(); // print sound made
}
Complete the indicated 3 lines of a minimal but complete class Dog with one method, that would compile to satisfy the interface MakesSound. Have a Dog speak by printing "BowWow!". You may skip any constructor.
public class________________________________
{
_____________________________________
{
__________________________________
}
}
7. Consider this example code illustrating the constructor and methods of the class WorkRecord, that stores a name and a number of hours worked:
WorkRecord wA = new WorkRecord("Andy"); // hours 0
WorkRecord wG = new WorkRecord("George"); // hours 0
wG.AddHours(3); //update hours: 0+3=3
Console.WriteLine(wG.GetHours()); // prints 3
wG.AddHours(2); //update hours: 3+2=5
Console.WriteLine(wG); // prints George: 5 hours
Console.WriteLine(wA); // prints Andy: 0 hours
Complete the code for the constructor and the three methods in the class WorkRecord below:
public class WorkRecord
{
private String name;
private int hours;
/** Construct a WorkRecord for the specified name, with initial hours 0.
public WorkRecord(string name) // code it!
{
}
/** Return the number of hours in this WorkRecord. */
public int GetHours() //code it!
{
}
/** Update the number of hours when moreHours further hours are worked. */
public void AddHours(int moreHours) //code it!
{
}
/** Return a string in the form like the example: Andy: 15 hours */
public override string ToString() //code it!
{
}
}