Sample 1303 Midterm (Fall 2007)

  1. Write a snippet of code that asks the user for their name and prints out "Hello X" where X is their name.
  2. What does the following code do?

    for(int i=0; i < 3000; i += 3)
    {
      Console.WriteLine(i);
    }

  3. Write a snippet of code that reads in four numbers from the user and displays the smallest of the four numbers.
  4. Describe the difference between the WHILE, FOR, and DO-WHILE loops.  When is it appropriate to use each?
  5. Describe the similarities between the WHILE, FOR, and DO-WHILE loops.
  6. What does the following code do?

    int i = 4;
    int j = 10;

    if ((i < 7) && (j < 10))
    {
      if (i + j < 20)
        Console.WriteLine("A");
      else
        Console.WriteLine("B");
    }
    else
    {
      if (i + j < 20)
        Console.WriteLine("C");
      else
        Console.WriteLine("D"); 
    }
  7. Write a code snippet that repeatedly prompts the user to type "QUIT" and reads in a string from the user until the user types "QUIT".  You must use a do-while loop in your solution.
  8. Describe the intent/purpose of methods and the use of methods in a program.