Computing - C# Test 2
Question 1
Write down what will appear on the console for each of these code fragments:
a) [2 marks]
Console.Write("Hello...");
Console.Write("...World!");
As Console.Write() is used, a new line is not generated. Therefore, the output will appear on one line. Make sure you include the right number of full stops.
b) [2 marks]
string name = "Bobby";
Console.WriteLine("Hello {0}!", name);
This is computing! You must show exactly what will appear on the console. Don't forget the exclamation mark.
c) [3 marks]
for (int i = 3; i <= 5; i++)
{
Console.WriteLine("Number {0} = {1}", i, i*2);
}
i starts at 3 and counts to 6. However, the code block in the loop only executes when i <= 5. It executes when i is 3, 4 and 5.
Number 3 = 6
Number 4 = 8
Number 5 = 10
d) [2 marks]
class Program
{
static void Main(string[] args)
{
Say("Hello there.");
}
static void Say(string message)
{
Console.WriteLine("{0} is the message.", message);
}
}
The program starts with the Main method which calls the Say method, passing Hello there. as the argument. The Say method inserts the passed text at the start of its text and displays the result.
Hello there. is the message.
e) [4 marks]
class Program
{
static void Main(string[] args)
{
string message = GetLaugh(3);
Console.WriteLine("Hee{0}", message);
}
static string GetLaugh(int numTimes)
{
string laugh = "";
for (int i = 0; i <= numTimes; i++)
{
laugh += "HeeHee";
}
return laugh;
}
}
The program starts with the Main method which creates a variable called message. message is set to the result of calling GetLaugh(3).
The GetLaugh() method builds up a string of repeated "HeeHee". It starts with an empty string, "", and adds on "HeeHee" the number of times specified by numTimes, the integer passed to the method when it is called, plus 1 extra.
In the question, 3 is passed to GetLaugh, so it returns the string "HeeHeeHeeHeeHeeHeeHeeHee". That string is added to "Hee" for the final answer.
HeeHeeHeeHeeHeeHeeHeeHeeHee
Writing Code
When asked to write code, you should follow the instructions carefully. Don't do more than you are asked in the question.
Question 2
Write a code fragment that will ask the user for a word and then display the word on the console. [4 marks]
string userWord;
Console.Write("Enter a word: ");
userWord = Console.ReadLine();
Console.WriteLine(userWord);
Question 3
Write a code fragment that creates an array called myNumbers to hold 5 numbers that may have decimal parts. [3 marks]
double[] myNumbers = new double[5];
// or
// decimal[] myNumbers = new decimal[5];
Question 4
Write a list of the C# data types that you know. [3 marks]
You should know...
- int
- byte
- double
- decimal
- string
- char
- bool
- DateTime
...and maybe long and float and ...
Question 5
Write a function that accepts two integers and returns the smaller of the two. [4 marks]
static int Min(int num1, int num2)
{
int smallerNumber = num1;
if (num2 < num1)
{
smallerNumber = num2;
}
return smallerNumber;
}
Question 6
Write a complete program that asks the user for a direction (up, down left or right) and then displays a different description depending on their choice. E.g. "You chose Up. You find a site of wondrous revision materials." [6 marks]
class Program
{
static void Main(string[] args)
{
string userChoice;
Console.Write("Which direction (up, down, left, right) ? ");
userChoice = Console.ReadLine();
switch (userChoice)
{
case "up":
Console.WriteLine("You chose Up. You find a site of wondrous revision materials.");
break;
case "down":
Console.WriteLine("You chose Down. You find a shoe house.");
break;
case "left":
Console.WriteLine("You chose Left. You find a lost valley of dinosaurs.");
break;
case "right":
Console.WriteLine("You chose Right. You are lost in a vortex.");
break;
default:
Console.WriteLine("Invalid choice!");
break;
}
}
}