3.6. Chapter Review
Prepare this assignment using a Word document.
Write answers under each question.
Paste code screenshots when required.
Write the method definition for a static method called
Q1
which has twoint
parameters,x
andy
, and returns adouble
.The method above must have what kind of statement in its body? (see Statements for a formal description of different types of statements for a formal answer or just use what you have learned so far to answer this question.)
Each of these lines below has a call to the method above,
Q1
. Which are legal? Explain:double d = Q1(2, 5); int x = Q1(2, 5); double y = Q1(2) + 5.5; Console.WriteLine(Q1("2", "5")); Console.WriteLine(Q1(2.5, 5.5)); Q1(10, 20);
Suppose
Q1
does nothing except produce the value to return, like most methods returning adouble
. Which line in the previous problem is legal (does not produce error), but has no effect?Write the method header for a static method called
Q4
which has onestring
parameter,s
, and returns nothing.Which of these lines with a call to the method above,
Q4
, is legal? Explain:Q4("hi"); string t = Q4("hi"); Console.WriteLine(Q4("hi")); Q4("hi" + "ho"); Q4("hi", "ho"); Q4(2);
Can you have more than one function/method in the same class definition with the same name? Explain why. (Google this one for an answer. This question will be covered in the future but is directly related to our topic here.)
What is a function/method signature? Can you have more than one function/method declared in the same class definition with the same signature? (Same note as above)
In each part, is this a legal program? If so, what is printed? If not, why not?
For the code blocks below, each version uses the same code, except for different versions of
Main
. Here is the common code with the body ofMain
omitted:using System; class Local1 { static int Q(int a) // 1 { // 2 int x = 3; // 3 x = x + a; // 4 return x; // 5 } // 6 static void Main() { // see each version } }
Insert:
static void Main() { Q(5); Console.WriteLine(x); }
Insert instead:
static void Main() { int x= 1; Q(5); Console.WriteLine(x); }
Insert instead:
static void Main() // 7 { // 8 int x = 1, y = 2; // 9 y = Q(5); // 10 Console.WriteLine(x + " " + y); // 11 } // 12
In the previous problem consider the common code with part c. Note the line numbers as comments.
In what line(s) is
Q
being defined?In what line(s) is
Q
called?What is the return type of
Q
?What is a formal parameter to
Q
?What is used as an actual parameter to
Q
?What is the scope of the
x
in line 3?What is the scope of the
x
in line 9?