程序代写代做代考 Assorted Questions and Concepts – Part 1

Assorted Questions and Concepts – Part 1
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 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);
}

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 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 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()); } } 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()); } } 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? 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.