0
0
C Sharp (C#)programming~20 mins

Why methods are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Method Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding method reuse in C#
What will be the output of this C# program that uses a method to greet a user?
C Sharp (C#)
using System;
class Program {
    static void Greet(string name) {
        Console.WriteLine($"Hello, {name}!");
    }
    static void Main() {
        Greet("Alice");
        Greet("Bob");
    }
}
AHello, Alice!\nHello, Bob!
BHello, Alice!\nHello, Alice!
CHello, Bob!\nHello, Bob!
DCompilation error due to missing return type
Attempts:
2 left
💡 Hint
Look at how the method Greet is called with different names.
🧠 Conceptual
intermediate
1:30remaining
Why use methods in programming?
Which of the following is the main reason to use methods in programming?
ATo make the program run slower
BTo repeat the same code multiple times without rewriting it
CTo increase the size of the program unnecessarily
DTo confuse other programmers
Attempts:
2 left
💡 Hint
Think about how methods help with code repetition.
Predict Output
advanced
2:00remaining
Effect of methods on program structure
What will be the output of this C# program that uses methods to calculate and print the area of a rectangle?
C Sharp (C#)
using System;
class Program {
    static int CalculateArea(int width, int height) {
        return width * height;
    }
    static void Main() {
        int area = CalculateArea(5, 3);
        Console.WriteLine(area);
    }
}
ACompilation error due to missing return statement
B8
C15
D53
Attempts:
2 left
💡 Hint
Multiply width and height inside the method.
🧠 Conceptual
advanced
1:30remaining
How methods improve code maintenance
Which statement best explains how methods help maintain code?
AMethods allow fixing a bug in one place instead of many places
BMethods increase the chance of errors by duplicating code
CMethods make code harder to read and understand
DMethods prevent the program from running
Attempts:
2 left
💡 Hint
Think about how changing code in one method affects the whole program.
Predict Output
expert
2:00remaining
Understanding method parameters and output
What will be the output of this C# program that uses a method with parameters and prints results?
C Sharp (C#)
using System;
class Program {
    static void PrintSum(int a, int b) {
        Console.WriteLine(a + b);
    }
    static void Main() {
        int x = 4;
        int y = 7;
        PrintSum(x, y);
        PrintSum(y, x);
    }
}
A47\n74
BCompilation error due to missing method return type
C4\n7
D11\n11
Attempts:
2 left
💡 Hint
The method adds two numbers and prints the result.