Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The expression-bodied method uses the multiplication operator '*' to return the square of x.
2fill in blank
mediumComplete 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'
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#.
✗ Incorrect
The Length property returns the number of characters in the string.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'toupper' causes a compile error.
Using 'touppercase' or 'UpperCase' are not valid method names.
✗ Incorrect
The correct method name is 'ToUpper' with capital T and U.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' will not give the remainder.
Using '!=' instead of '==' will return true for odd numbers.
✗ Incorrect
The method uses the modulus operator '%' to get the remainder and checks if it equals zero with '=='.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The method filters words with Length > 3 and creates a dictionary with word lengths using the Length property.