Assorted Questions and Concepts – Part 1 – Answers
Cpt S 321 Washington State University
Question 3
• Find at least 3 compilation errors from the following code and explain them. Find 1 in Main and at least 2 in MyFunction.
class Program
{
}
static void Main(string[] args)
{
MyFunction(3.3, 4.4);
MyFunction(5.5, 6.6);
}
int MyFunction(double a, double b)
{
int x, y, z;
z = a * a;
y = b * b;
if (z > 100 || y > 100 || x > 100)
{
x = Math.Sqrt(y + z);
}
return x; }
Question 3 – Error 1
• Assigning doubles to ints on the highlighted lines. Can’t do this because C# is strongly typed.
class Program
{
}
static void Main(string[] args)
{
MyFunction(3.3, 4.4);
MyFunction(5.5, 6.6);
}
int MyFunction(double a, double b)
{
int x, y, z;
if (z > 100 || y > 100 || x > 100)
{
}
return x; }
z = a * a;
y = b * b;
x = Math.Sqrt(y + z);
Question 3 – Error 2
• Local variable used without being initialized
class Program
{
static void Main(string[] args)
{
MyFunction(3.3, 4.4);
MyFunction(5.5, 6.6);
}
int MyFunction(double a, double b)
{
int x, y, z;
z = a * a;
y = b * b;
if (z > 100 || y > 100 || x > 100)
{
x = Math.Sqrt(y + z);
}
return x; }
}
Question 3 – Error 3
• Calling a non-static member function from a static function.
class Program
{
static void Main(string[] args)
{
}
int MyFunction(double a, double b)
{
int x, y, z;
z = a * a;
y = b * b;
if (z > 100 || y > 100 || x > 100)
{
x = Math.Sqrt(y + z);
}
return x; }
}
MyFunction(3.3, 4.4);
MyFunction(5.5, 6.6);
Question 5
• What is the output from the following code?
static void Main(string[] args)
{
string s = “Hello World!”;
s.Replace(‘H’, ‘J’);
Console.WriteLine(s);
}
Answer to Question 5
static void Main(string[] args)
{
string s = “Hello World!”;
s.Replace(‘H’, ‘J’);
Console.WriteLine(s);
}
• You absolutely have to understand why. This is trivial, but if you are still in the C++ world then you might make a mistake with your assumption of the output.
• The output is:
Hello World!
Question 6 Primer
• Note: you can use the ref keyword to pass a parameter by reference
• Use the ref keyword both in the parameter declaration in the function
AND in the call to the function
• An example in the question 6 code
• Can pass a structure by reference instead of by value by using ref
• Can pass a reference to a reference by using ref with a class object
Question 6
• What is the output of the following code?
}
static void Main(string[] args)
{
string s = “Hello World!”;
DoReplacement(ref s);
Console.WriteLine(s);
static void DoReplacement(ref string s)
{
}
s.Replace(‘H’, ‘J’);
Question 6
• What is the output of the following code?
}
It’s still “Hello World!”
static void Main(string[] args)
{
string s = “Hello World!”;
DoReplacement(ref s);
Console.WriteLine(s);
static void DoReplacement(ref string s)
{
}
s.Replace(‘H’, ‘J’);
Question 7
• If we wanted the following code to display “Jello World!” how can we alter it by changing DoReplacement and leaving Main alone?
}
static void Main(string[] args)
{
string s = “Hello World!”;
DoReplacement(ref s);
Console.WriteLine(s);
}
static void DoReplacement(ref string s)
{
s.Replace(‘H’, ‘J’);
Question 7
• You have to set s to something in order for it to change the s back in Main. Note that we couldn’t do this without the ref keyword.
}
Add this
static void Main(string[] args)
{
string s = “Hello World!”;
DoReplacement(ref s);
Console.WriteLine(s);
}
static void DoReplacement(ref string s)
{
s=
s.Replace(‘H’, ‘J’);
Question 8A
• What is the output of program A? • What is the output of program B?
Program A
Program B
static void Main(string[] args)
{
for (int i = 0; i < 5; i++) {
Console.WriteLine(i.ToString()); }
}
static void Main(string[] args)
{
for (int i = 0; i < 5; ++i) {
Console.WriteLine(i.ToString()); }
}
Answer to Question 8A
3 4
• The output of both are the same: 0
1
2
Program A
Program B
static void Main(string[] args)
{
for (int i = 0; i < 5; i++) {
Console.WriteLine(i.ToString());
}
}
static void Main(string[] args)
{
for (int i = 0; i < 5; ++i) {
Console.WriteLine(i.ToString());
}
}
Question 8B
• What is the output of program A? • What is the output of program B? • Do these even compile?
Program A
Program B
static void Main(string[] args)
{
for (int i = 0; i++ < 5; ) {
Console.WriteLine(i.ToString());
}
}
static void Main(string[] args)
{
for (int i = 0; ++i < 5; ) {
Console.WriteLine(i.ToString());
}
}
Answer to Question 8B
• Yes, they both compile and run just fine.
• The output of • The output of
program A: 11 2 2 33 44 5
program B:
Program A
Program B
static void Main(string[] args)
{
for (int i = 0; i++ < 5; ) {
Console.WriteLine(i.ToString());
}
}
static void Main(string[] args)
{
for (int i = 0; ++i < 5; ) {
Console.WriteLine(i.ToString());
}
}
Question 9
• Write a function to multiply 2 ushort values without using the * operator
• It must run in (very close to) constant time
• Multiplying 10,000 and 20,000 should not result in thousands
more operations than multiplying 10 and 20
• In other words, don’t implement it by looping from 1 to operand A and adding operand B to itself that many times
• Addition and a few bitwise operators are all you should need • What should the return type of the function be?
static uint Multiply(ushort multiplier, ushort multiplicand) {
}
uint result = 0;
int shiftAmount = 0;
while (multiplier != 0)
{
if (1 == (multiplier & 1))
{
result += (uint)(multiplicand << shiftAmount);
}
shiftAmount++;
multiplier >>= 1;
}
return result;
Answer to Question 9
Summary
• EVERYTHING within these slides should make good sense to you
• These are the types of things that you’ll need to know how to do for homework assignments, exams, and many practical software engineering problems
• These are just a small handful of examples. We will have more questions throughout the semester and we’ll look at several that are much more difficult.