Professor Mustard Programming
 

Programming 101 - Unit 01 - The Basics

01z) Review
 
To keep these concepts fresh in your mind, answer the following questions, and then check your answers at the bottom. For best results as a memory tool, answer these questions a few hours after reading the original material.

1) Given the following output...

Hello,
world!


...which of the following code segments could have produced it?

a)
Console.Write("Hello," + Environment.NewLine + "world!");

b)
Console.Write("Hello,");
Console.WriteLine("world!");


c)
Console.WriteLine("Hello,");
Console.Write("world!");


d)
Console.WriteLine("Hello,");
Console.Write(Environment.NewLine + "world!");



2) Why won't the following code compile?

string myName = "Ralph";
Console.WriteLine("My name is " + myName);
string myName = "Fred";
Console.WriteLine("My name's changed; it is now " + myName);



3) Which of these code lines could potentially cause trouble with your program's math?

a) double height = 10;
b) double average = 15 / 4;
c) double age = 10 * 5;


4) What operator might you use to quickly determine if a given number was divisible by 3?


5) Given this code...

string city = "Springfield";
city.Substring(4, 2);


...what will happen to the city variable?

a) Nothing
b) It will store a "substring" of its original contents, starting at index 4 and 2 characters long.
c) It will store a "substring" of its original contents, starting at index 2 and 4 characters long.



Answers
1) a) & c)
a) places an Environment.NewLine in between the two words, which will separate them into two separate lines.
b) uses Console.Write to place the first word, which means the second word will be placed on the same line.
c) uses Console.WriteLine to place the first word, which means that the second word will be placed on a new line.
d) is almost like c), but because of the Environment.NewLine, there will actually be a blank line in between the first and second words.


2) The code won't compile because the variable myName is declared twice. We don't use the "string" keyword to assign a new value to the variable; we only use it on the initial declaration. The corrected code would look like this...
string myName = "Ralph";
Console.WriteLine("My name is " + myName);
myName = "Fred";
Console.WriteLine("My name's changed; it is now " + myName);



3) b)
All three answers are assigning integer values to a double. Since doubles can store more information than integers, however, this is not a problem. The problem with b) is that it's dividing with two integers, which will cause the remainder to drop out before the result can be stored in the double. Losing the remainder, of course, could result in inaccuracy. Placing decimals in the numbers will force C# to perform "proper" divison:
double average = 15.0 / 4.0;


4) One way is to use the % operator, which gives you the whole number remainder of a division operation. If the result from the % operation is 0, then there is no remainder, and therefore, the number is divisible. In the next unit, we'll learn about the "if" statement, which could then be used to make a decision based on whether or not the result is 0.


5) a)
Nothing will happen to the variable if we don't assign the result of Substring back to the original variable, with "=", like so:
city = city.Substring(4, 2);