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

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

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a method that prints a greeting.

C Sharp (C#)
public void [1]() {
    Console.WriteLine("Hello, friend!");
}
Drag options to blanks, or click blank then click option'
APrint
BMain
CGreet
DStart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Main' as method name here causes confusion with program entry point.
Choosing unrelated names like 'Start' or 'Print' does not match the greeting.
2fill in blank
medium

Complete the code to call the method that prints a greeting.

C Sharp (C#)
class Program {
    static void Main() {
        [1]();
    }

    static void Greet() {
        Console.WriteLine("Hello, friend!");
    }
}
Drag options to blanks, or click blank then click option'
APrint
BGreet
CStart
DMain
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'Main' inside Main causes recursion error.
Using 'Print' or 'Start' which are not defined methods.
3fill in blank
hard

Fix the error in the method definition by completing the missing return type.

C Sharp (C#)
public [1] Greet() {
    Console.WriteLine("Hello!");
}
Drag options to blanks, or click blank then click option'
Avoid
Bbool
Cstring
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'string' without returning a value causes errors.
Forgetting to specify 'void' when no return is needed.
4fill in blank
hard

Fill both blanks to define and call a method that adds two numbers and returns the result.

C Sharp (C#)
public static int Add(int a, int b) {
    return a [1] b;
}

static void Main() {
    int sum = Add(3, 4);
    Console.WriteLine(sum [2] 0);
}
Drag options to blanks, or click blank then click option'
A+
B>
C<
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' in addition.
Using '<' instead of '>' in comparison.
5fill in blank
hard

Fill all three blanks to define a method that checks if a number is even and call it in Main.

C Sharp (C#)
public static bool IsEven(int num) {
    return num [1] 2 [2] 0;
}

static void Main() {
    bool result = IsEven([3]);
    Console.WriteLine(result);
}
Drag options to blanks, or click blank then click option'
A%
B==
C4
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong result.
Passing wrong number to IsEven.