"Teach The Topic" Example Answer

This document provides a sample question, and a sample answer.  It is intended for you to examine so that you know what is expected of a 'good answer' on the actual exam.

The Question

Given the following code, what will be printed to the screen?
  1. using System;
  2. namespace ExamCheck
  3. {   class Program
  4.     {   static void Main(string[] args)
  5.         {
  6.             int input = 10;
  7.            // Code to get input is ommitted, but imagine that it's here
  8.            if (input >= 1 && input < 20 && input != 0)
  9.            {
  10.                Console.WriteLine("Your input is valid");
  11.            }
  12.            else
  13.                Console.WriteLine("INVALID INPUT!");
  14. }   }   }

An Example Answer

(Your Solution - this is you simply solving the problem/question listed above)

WRITE THIS OUT BY HAND

Your input is valid

Optionally, you can also include the "Press any key to continue . . ." text that you'll see if you run this without debugging, but there's no expectation that you will include it.

Sample Explanation
(This is where you explain the concepts needed to understand the program, and the output)

Even though this short program produces only a single line of output, there's at least four important concepts from this course that are needed to fully understand this program.  In this explanation I will show how knowledge of variables (including data types), console input and output (or I/O, for short), expression evaluation, and if/else statements are all used in the above program.

Let's start with variables.  Since the program seeks to simulate getting user input (but does not actually get user input) it'll need a location in memory to store the value that the program pretends to get from the user.  It'll then need a name to refer to that memory location so that it can talk about that value later on.  A variable is pretty much defined as "a memory location that has a name", so the program will clearly need to use a variable to store the user's input.  Line 10 declares a variable of type int named input, and immediately assigns it the value 10.  It's worth noting that variables of type int  can only store integers - whole numbers, including negative whole numbers, zero, and positive whole numbers.  It's also worth noting that

    int input = 10;

is really just an abbreviation for

    int input;
    input = 10;

In both cases the first part/line asks the C# compiler to create a space in memory named input that can only hold integer values.  The second part/line then assigns 10 to that variable by storing the value 10 in the memory location identified by the name input.

Next, let's examine how expressions are evaluated.  Technically, the line input = 10; contains an expression ( input = 10 - note that there's no semicolon at the end), but it's simple enough to evaluate that we don't need to think through all the details.  Since there's just one operator (the = operator) then we clearly need to evaluate that one operator.  The assignment operator ( = ) just takes whatever's on the right-hand side of the equals sign and stores it into the memory location identified by the variable input.

For more complicated expressions (such as input >= 1 && input < 20 && input != 0, on line 8), we refer to the "Operator precedence and associativity" table (available online at http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx).  The basic approach is to do the following until you run out of operators:

  1. Find highest priority unevaluated expression (based on precedence - operators with higher precedence are evaluated first)
    1. When we have more than one of a particular operator, we figure out whether we should evaluate the left-most or right-most on associativity
  2. Evaluate that operator, using whichever operands (data) it needs
  3. Rewrite the expression so that you substitute the result of the prior step for the operator.

In the above example we start with input >= 1 && input < 20 && input != 0.  Looking at the Operator precedence and associativity table, we see that the "Relational and type testing" operators have the highest precedence in our expression, and therefore are evaluated first.  This category includes both the >= and < operator (but NOT the != operator - that's in the "Equality" category).  When we have two (or more) operators in the same category we use the associativity of the category to determine which one is evaluated first.  Near the bottom of the table is a note that says "Except for the assignment operators, all binary operators are left-associative, meaning that operations are performed from left to right" (binary operators are operators that take 2 operands/pieces of data, like addition ( + ) and comparison (>= and <).  This means that we'll be evaluating the >= first, since it's left-most.  Since input has the value 10, input >= 1 must therefore be true, and we can substitute that result into the rewritten expression: true && input < 20 && input != 0.

Next we'll evaluate <, since it's the only "Relational and type testing" operator (which has precedence over the inequality operator).  Since input has the value 10, input < 20 must therefore be true, and we can substitute that result into the rewritten expression: true && true && input != 0.

Next we'll evaluate !=, since it's the only "Equality" operator (which has precedence over the "Conditional AND" operators).  Since input has the value 10, input != 0 must therefore be true, and we can substitute that result into the rewritten expression: true && true && true.

Again, we're faced with a choice: which of the two && operators do we evaluate next?  As we established earlier, the leftmost && will be evaluated first (because it's a binary operator).  The left-most true && true will evaluate to true, which we can substitute into the expression, leading to  true && true.  At this point we've got a single operator ( && ) left, which evaluates to the singular value true. 

So when all is said and done, the complicated line if (input >= 1 && input < 20 && input != 0) will evaluate to if ( true ).   But what does that mean?  In order to understand what effect if ( true ) will have on the program's execution we need to delve into the workings of the if/else statement.....

<the if/else explanation and the I/O explanations would be similar, but are left out for brevity - the above two topics should lend clarity to what you're expected to do for the exam>


If you're curious, here's the outline that I created first, in order to generate the above explanation.

  1. Four big areas that we covered in class: variables (including data types), I/O, expression evaluation, and if/else statements/p>

    1. Variables

      1. Intuition about name corresponding to a memory location

      2. int stores integers

      3. int x = 10; as shorthand for int x; x = 10;

    2. expression evaluation

      1. Reference that table

      2. Explain algorithm: find highest priority unevaluated expression (based on precedence)(watch for left-to-right issues when we have duplicates, using associativity), evaluate it & substitute result back into overall expression

      3. Explain what >= is/does, show expression after it's evaluated

      4. Explain what < is/does, show expression after it's evaluated

      5. Explain what != is/does, show expression after it's evaluated

      6. Explain what && is, which one goes first and why

      7. Next one must go next (COMMENTARY : note the abbreviated explanation)

      8. End result

    3. if/else

      1. Explain the logic

      2. explain the {} vs. no {} thing

    4. I/O

      1. Mostly O, talk about C.WL producing a newline (return key) at the end of the line

Commentary On The Sample Answer

  1. Do I want this here? 

  2. DO NOT COPY MY WORK FOR YOUR ANSWER!!! EVEN IF YOU'VE GOT A QUESTION WHICH IS SIMILAR YOU MUST WRITE OUT YOUR OWN UNDERSTANDING, IN YOUR OWN WORDS