Module 6 Quiz
What is recursion in programming?
A method that calls itself.
A method call must occur outside the body of another method.
False
A method can only have 1 parameter, or void.
False
Every method must have a unique name.
False
This C# code snippet outputs the number 99. { int x = 99; } Console.WriteLine(x.ToString() );
False
A variable called myValue has been declared and initialized on line 3. What is the value of the variable myValue on line 9 in the following C# code? 1 static void Main( ) 2 { 3 int myValue = 17; 4 myMethod( ); 5 } 6 static void myMethod( ) 7 { 8 int myValue; 9 Console.WriteLine( myValue.ToString() ); 10 }
It is uninitialized
Using the C# code fragment below, choose all the locations (Lines 1-7) where you could correctly enter a statement to call a method: (Note: the method doesn't exist in this question.) 1 public class Program 2 { 3 public static void Main() 4 { 5 } 6 } 7
Line 5 Line 6
What can you tell by looking at this method signature? static int DoSomething( string[ ] param1, double param2)
The method takes a string array and a real number as arguments, and returns an integer number.
A variable called myValue has been declared on line 3. What is the value of that variable on line 8 in the following C# code? 1 static void Main( ) 2 { 3 int myValue = 17; 4 myMethod( ); 5 } 6 static void myMethod( ) 7 { 8 Console.WriteLine( myValue.ToString() ); 9 }
The variable myValue can't be accessed.
A method can only have 1 return value, or void.
True
When you call a method, you must always put a pair of parentheses after the name of the method, even if the method has no parameters.
True
Choose all of the following reasons why you would put methods (functions, subroutines) into a program.
You want to make the code easier to read by separating it into more manageable sections. You want to reduce the number of places you have to look for errors. You have multiple programmers, and want them to work on parts of the entire program. You have code you want to execute from multiple places in your program.
A method parameter variable can be used _______. Choose all that apply.
as a local variable in the method in which it is declared as a return expression in the method in which it is declared
Look at this code: static void DoSomething( int[ ] param1, int param2 ) { param1[0] = 100; param2 = 100; } public static void Main( ) { int[] myArray = {50}; int myVar = 50; DoSomething(myArray, myVar); //What are the values? } After the call to DoSomethinig, which of the following is true?
myArray will be 100 myVar will be 50
Identify everything this method signature tells a programmer: static void DoSomething( double param1, int param2)
param2 can be modified in this method. There is a method called DoSomething. There is a local variable called param2. A call to this method takes 2 arguments. There is a local variable called param1.
Which of the following is a valid C# method call (or invocation)?
perhapsAMethod(5)
Which is a valid statement that calls a method with this method signature? void thisIsMyMethod( string value )
thisIsMyMethod("hello");
Which of the following is a valid C# method implementation (or method body)?
void isThisAMethod( double value ) { if(value < 5) return; else value *= 2; }
Which of the following is a valid C# method signature (or method declaration)?
void perhapsAMethod( int value )