The method PrintMessage prints "Hello World" when called.
Step 2: Check the method call
The method is called correctly with PrintMessage();, so it runs and prints the message.
Final Answer:
Hello World -> Option D
Quick Check:
Method call prints message = D [OK]
Hint: Method call runs code inside method [OK]
Common Mistakes:
Thinking method name prints instead of output
Assuming no output without return
Confusing syntax error with correct code
4. Identify the error in this C# method and how to fix it:
void ShowNumber()
{
Console.WriteLine(number);
}
int number = 5;
ShowNumber();
medium
A. The variable 'number' is used before declaration; move declaration above method
B. Method must return int instead of void
C. Missing semicolon after method call
D. Method name should be lowercase
Solution
Step 1: Check variable scope
The variable 'number' is declared after the method, so it's not visible inside the method.
Step 2: Fix variable scope
Move the declaration of 'number' before the method or pass it as a parameter.
Final Answer:
The variable 'number' is used before declaration; move declaration above method -> Option A
Quick Check:
Variable scope error = B [OK]
Hint: Variables must be declared before use in methods [OK]
Common Mistakes:
Thinking method return type causes error
Missing semicolon is not the issue here
Ignoring case sensitivity of method names
5. You want to reuse code that calculates the area of a rectangle multiple times with different sizes. How should you use methods in C# to do this efficiently?
hard
A. Write a method that prints the area directly without parameters
B. Copy and paste the area calculation code everywhere you need it
C. Write a method that takes width and height as parameters and returns the area
D. Use a global variable to store area and update it each time
Solution
Step 1: Identify the need for reusable code
Calculating area multiple times means code should be reusable with different inputs.
Step 2: Use method with parameters and return value
A method that takes width and height and returns area allows reuse without copying code.
Final Answer:
Write a method that takes width and height as parameters and returns the area -> Option C
Quick Check:
Reusable method with parameters = A [OK]
Hint: Use parameters and return to reuse code easily [OK]
Common Mistakes:
Copy-pasting code instead of using methods
Printing inside method limits reuse
Using global variables causes confusion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.