COMP103A Introduction to Computer Science 1

Programming Revision Questions 2006

The following questions may not bear any resemblance to past or future examination questions or cover all aspects of any exam. They relate only to course material and are for revision purposes only.

Where is the bug?

For each of the following pieces of C# code, try to identify the programming error.
NB: In some cases there is more than one error.

1. label1.text = "Hello World";

2. label1.Text = "Hello" "World";

3. label1.Text = 'Hello World';

4. label1.Text = Hello World;

5. "Hello World" = label1.Text;

6. int value = label1.Text;

7. (x+1)++;

8. x+=;

9. 5--;

10. if (c < 7);
label1.Text = "c is less than 7";

11. if (c < 7)
label1.Text = "c is ";
label1.Text = label1.Text + "less than 7";

12. if (5+5=10)
label1.Text = "Big deal";

13. switch (n) {
case 0: label1.Text = "zero";
case 1: label1.Text = "one";
case 2: label1.Text = "two";
else: label1.Text = "Something else";
}

14. while (c <= 5) {
product *= c;
++c;

15. while (z >= 0)
sum += z;

16. x=1;
while (x <= 10);
x++;

17. while () {
if (x<10)
x++;
else
break;
}

18. int x=1, total;
while (x <= 10) {
total += x;
++x;
}

19. int x=1, sum=0;
while (x<10)
sum += x;
x++;

20. int i = 5;
while (i > 0) {
label1.Text = label1.Text + " " + i;
i++;
}

21. label1.Text = "";
for (x=0, x<100, x++)
label1.Text = label1.Text + " " + x ;

22. int sum(int x, int y)
{
int result;
result = x+y;
}

23. int sum(int x, int y)
{
sum = x+y;
return sum;
}

24. void doit(int x)
{
int x=0;
label1.Text = x.ToString();
}

25. void sum(int x, int y)
{
return x+y;
}

26. void show(int x)
{
show(x);
return;
}

What does the code do?

For each of the following pieces of code, explain what the program does and what, if any, output is generated.

1. label1.Text = "He" + 'l' + "lo W"
+ "orld\nTh" + "is is line " + 10/5 + '\n';

2. int x=1;
label1.Text = "x" + x + 'x' + "x + x" + x + x + (x + x);

3. int x;
x = Convert.ToInt32(textbox1.Text);
if (x>100)
label1.Text = "Big";
else
label1.Text = "Small";

4. int x;
x = Convert.ToInt32(textbox1.Text);
label1.Text = ((x>100) ? "Big" : "Small");

5. int count=1, total=0;
while (count<=10)
total += count++;
label1.Text = total;

6. int i;
string output = "";
for (i=1; i<=100; i++) {
output = output + "*";
if (i % 10 == 0) {
output = "";
}
}

7. int y, x=1, total=0;
while (x<=10) {
y = x * x;
total += y;
++x;
}
listbox1.Items.Add ("Total is: " + total);

8. int x=9, y=11;
if (x<10)
if (y>10)
label1.Text = "***";
else
label1.Text = "###";
label1.Text = "\$\$\$"

Could this code be written in a clearer manner?

9. Random rand = new Random();
int x = rand.Next(1, 5);
switch(x) {
case 1: label1.Text = "1"; break;
case 2: label1.Text = "2"; break;
case 3: label1.Text = "3"; break;
case 4: label1.Text = "4"; break;
}

Is there a better way of writing this code?

10. int x=5;

11. int[] array = new int[10];
int i;
for (i=0; i<10; i++)
array[i] = i+1;
for (i=1; i<10; i++)
array[i] = array[i-1] + array[i];
for (i=0; i<10; i++)

Write code to...

The following questions require you to actually write code. These will probably take you longer to complete than the previous questions.

1. Write code which outputs numbers from 0 up to 100. Each number must be three digits so numbers with less than three digits must have leading 0's. ie.

000
001
002
:
010
011
:
098
099
100

2. Modify your program in question 1 to output the squares of the numbers from 1 to 100 with each output number having 5 digits. ie.

00000
00001
00004
00009
00016
:
10000

3. Write a program to prompt the user for two integer values and calculate the first number to the power of the second number.

4. Write a program which prompts the user for two values, the first a number and the second a character. Read in the two values and then draw a square on the screen with each sides length being the number read in, and the square drawn using the character read. i.e. input of 5 and '+' would produce

+++++
+++++
+++++
+++++
+++++

5. Write a function which calculates the factorial of a given number.

6. Use the function you have written in question 5 to calculate the value of

1    1    1
e = - + - + - + ...
1!   2!   3!

7. Write a program which generates 1000 random numbers in the range 0-9 and then outputs the count for each digit. ie.

0  118
1  99
2  96
:
8  85
9  112

Do not store the 1000 random numbers.

8. Write a program which calculates the average speed of a runner given the distance travelled and the time taken. You can decide what units of distance, time, and speed are appropriate.