COMP103A Introduction to Computer Science 1

TIM'S EXAM-STYLE REVISION QUESTIONS

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. There are more questions here than there are in the exam, and some of the questions are a little more challenging than a typical exam question (but are still very good practice). We will not provide model answers to these questions.

Q1. Selection:

  1. What is the output of the following code fragment? What values would you use to test it?

      int a = Convert.ToInt32(textbox1.Text);
      int b = Convert.ToInt32(textbox2.Text);
      string output = "";

      if (a>5)
          output = output + "triangle ";
      if (a<5)
          output = output + "square ";

      if (a>5)
          output = output + "triangle ";
      else
          output = output + "square ";

      if (a>=5)
      if (b>=10)
          output = output + "cross ";
      else
          output = output + "circle ";

      if ((a>=5) && (b>=10))
          output = output + "cross ";
      else
          output = output + "circle ";
      MessageBox.Show(output);

  2. Write C# code that uses a switch statement to set label1 to the name of the playstation symbol given an integer called symbol, where 2 = triangle, 4 = circle, 6 = cross, 8 = square.

Q2. Iteration:

  1. Compare the three iteration constructs in C#: for, while and do/while. Give a situation which is best suited to each of the constructs.

  2. Rewrite the following code using a for loop:

      int i = 99;
      while(i>=0)
      {
          MessageBox.Show("half of this is: " + i.ToString() );
          i=i/2;
      }

  3. Write a loop to generate 50 random test scores between 0 and 100. Keep track of the minimum, maximum and overall average score generated. You do not need to keep the individual scores.

Q3. Methods:

  1. Why might you create a method of a piece of code that will only ever be called once?

  2. Write C# code to define a method that will accept an integer and return an integer which is half the original value.

  3. Write C# code showing how the above method would be called from within a button click event to make the value of y half that of x.

  4. Write a method called Move which has two integer reference parameters, x and y, and an integer parameter called direction, and updates the values of x and y according to the following table:

      directionxy
      2 add 1
      4subtract 1 
      6add 1 
      8 subtract 1

Q4. Data Types:

  1. Define the type of value stored in the following data types:

    What range of values can be stored in each?

  2. What is the difference between 'a' and "a"? Write C# code to declare a variable to store each of the two values.

  3. Write C# code to declare an array of 10 integers and then initialise them all to 15 (i.e. it should contain 10 copies of the value 15). You may want to use constants.

Q5. Variables and Scope:

  1. What is the difference between a local variable and an instance variable? Give an example of each.

  2. What are the keywords "public" and "private" used for? What is the difference between them. Give an example showing when they are used?

  3. What is the "namespace" of a program. How can you tell which namespace you are in?

Q6. Events and Controls:

  1. What is an "event" in a C# program? How is this different from an "event handler"?

  2. Steve wants a command button to only be enabled when an item is typed into the textbox1 text box. The text box control has the following events:

    Explain when each event is triggered and which would be the best to use to code the enable action.

  3. "All Visual C# controls are objects." Using the listbox as an example of a control, explain this statement. Give examples using methods and properties of the listbox.

  4. Write code to move a textbox to the centre of the form, and set its size to be half the width and height of the form. Set it's text to be "comp103 is cool!" and it's backcolour to be red.

Q7. Files:

  1. Write a fragment of code to open a file called "products.csv" and check that it has successfully opened.

  2. Assuming the file has been opened from part a), write code to find product number 10091 in the file and displays the product information. Each line in the file contains three items separated by commas: a product number (int), a price (double) and a description (up to 50 chars including spaces). Display an appropriate message if the product is not in the file.

  3. Describe how you would handle errors such as a missing file, or a line not having all three values.

Q8. Lists:

Imagine someone has created a Wagon struct to represent goods wagons in a train. The struct has a Destination field that holds a string of where the goods are going, a Description field describing what the goods are and a boolean field that specifies if the goods are Hazardous or not.

  1. Write a C# method called Shunting that accepts a list of wagons (i.e. a train) and a string holding the current station name. It should produce two OUT parameters: the first is a list holding the wagons whose destinations are the current station, the second is a list of the wagons to stay on the train to stations further down the line.

  2. Rewrite a) so that the train parameter is a reference parameter holding the wagons to be left on the train, and the method returns a list holding the wagons to be left at the current station (i.e. it has no OUT parameters).

  3. Write a C# method called Safe that accepts a reference parameter of a list of wagons (i.e. a train) and makes sure that there are not two hazardous goods wagons in a row. It should rearrange the wagons if it is possible to make the train "safe". The method should also return a boolean value stating if the train has been made safe, or if there were too many hazardous goods wagons to put a non-hazardous goods wagon between each one.

Q9. Structs:

Write C# statements for the following:

  1. Design a Wagon struct to store information about railway wagons. Each struct repesents a goods wagon in a railway train. The struct will store the destination (a string), a description string, and a boolean value that specifies if the goods are hazardous or not.

  2. Create a constant to store the string "empty", which will be used to show that the wagon is empty. Declare a wagon variable and set it to be "empty" and non-hazardous. Set the destination to be empty string "".

  3. Write code to check if a wagon variable has a description string that is the empty string ("", or has a length of 0), and if it is, sets the description to "empty" and hazardous to false.

  4. Write a method called Load that is passed three parameters (a destination string, a description string and a boolean value indicating if the goods are hazardous), and returns a wagon struct with the contents set to the values passed in. Show how you would use your method to load a wagon with "paint" for "Hamilton", which is classified as hazardous.

  5. Write a method called IsEmpty that is passed a wagon struct and returns a boolean value depending on whether the wagon is empty or not. Show how you would call this method.

  6. Write a method called ToString that is passed a wagon struct and returns a string value describing the wagons current contents, e.g. "Hazardous paint bound for Hamilton". Show how you would use this method to add a description of the wagon to a listbox.

Q10. Programming Problem:

There is an ancient Greek problem of a man who wishes to buy exactly 100 animals with exactly 100 dollars. Each horse costs $12, each sheep costs $5 and you get 8 rabbits for every $1 spent. You cannot buy a single rabbit, only in lots of 8. The man must have at least one of each animal.

Write a program to find all combinations of about 100 animals that cost about $100, i.e. a program to display all combinations of between 95 to 105 animals that cost between $95 to $105.