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

Object type as universal base 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 declare a variable that can hold any type of value.

C Sharp (C#)
object [1] = 42;
Drag options to blanks, or click blank then click option'
Aobj
Bany
Cdata
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a reserved keyword as variable name
Using a type name instead of a variable name
2fill in blank
medium

Complete the code to assign a string value to an object variable.

C Sharp (C#)
object obj = [1];
Drag options to blanks, or click blank then click option'
Anull
B100
C"hello"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings
Assigning a number instead of a string
3fill in blank
hard

Fix the error in the code by completing the cast from object to string.

C Sharp (C#)
object obj = "test";
string s = ([1])obj;
Drag options to blanks, or click blank then click option'
Astring
Bint
Cbool
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to a wrong type causes runtime errors
Forgetting to cast causes compile errors
4fill in blank
hard

Fill both blanks to create an object array and assign different types of values.

C Sharp (C#)
object[] arr = new object[] { [1], [2] };
Drag options to blanks, or click blank then click option'
A"hello"
B42
Ctrue
D3.14
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers
Mixing up boolean and numeric values
5fill in blank
hard

Fill all three blanks to create a method that returns an object and assigns different types inside.

C Sharp (C#)
public object GetValue(int choice) {
    return choice switch {
        1 => [1],
        2 => [2],
        _ => [3]
    };
}
Drag options to blanks, or click blank then click option'
A"apple"
B100
Cfalse
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around booleans or numbers
Returning incompatible types without casting