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

Expression-bodied methods in C Sharp (C#) - Interactive Code Practice

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

Complete the code to define an expression-bodied method that returns the square of a number.

C Sharp (C#)
public int Square(int x) => x [1] x;
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' will add the number to x instead of squaring.
Using '-' or '/' will not calculate the square.
2fill in blank
medium

Complete the expression-bodied method to return the length of a string parameter.

C Sharp (C#)
public int GetLength(string s) => s.[1];
Drag options to blanks, or click blank then click option'
ALength
BLength()
CCount
DSize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Length()' causes a syntax error because Length is a property, not a method.
Using 'Count' or 'Size' are not valid string properties in C#.
3fill in blank
hard

Fix the error in the expression-bodied method that returns the uppercase version of a string.

C Sharp (C#)
public string ToUpperCase(string s) => s.[1]();
Drag options to blanks, or click blank then click option'
AToUpper
Btoupper
Ctouppercase
DUpperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'toupper' causes a compile error.
Using 'touppercase' or 'UpperCase' are not valid method names.
4fill in blank
hard

Fill both blanks to create an expression-bodied method that returns true if a number is even.

C Sharp (C#)
public bool IsEven(int num) => num [1] 2 [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 '%' will not give the remainder.
Using '!=' instead of '==' will return true for odd numbers.
5fill in blank
hard

Fill all three blanks to create an expression-bodied method that returns a dictionary with words as keys and their lengths as values for words longer than 3 characters.

C Sharp (C#)
public Dictionary<string, int> WordLengths(List<string> words) => words.Where(w => w.[1] > [2]).ToDictionary(w => w, w => w.[3]);
Drag options to blanks, or click blank then click option'
ALength
B3
DCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' instead of 'Length' for strings causes errors.
Using a number other than 3 changes the filter condition.