0% found this document useful (0 votes)
5 views

C Sharp tutorials

The document contains a series of questions and code snippets related to C# programming concepts, including the use of main static classes, constants, value and reference types, and control flow structures. It also explores function overloading, polymorphism, and the compilation behavior of abstract classes and derived classes. The questions require explanations and analysis of code execution results in various scenarios.

Uploaded by

Awal Mamane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Sharp tutorials

The document contains a series of questions and code snippets related to C# programming concepts, including the use of main static classes, constants, value and reference types, and control flow structures. It also explores function overloading, polymorphism, and the compilation behavior of abstract classes and derived classes. The questions require explanations and analysis of code execution results in various scenarios.

Uploaded by

Awal Mamane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C SHARP TUTORIALS

1. Every C# program requires at least one main static class. True or False

2. When is it appropriate or desirable to use constants in your programs? Why?

3. How do value and reference types differ in their use of memory? Why might this be
important?

4. Is the following code valid? If not, why not?

int x = 10;
float y = 20;
x = y;

5. What are the values of the variables w, x, y and z after this code has executed?
Explain.

int c = 0;
int w = 0, x = 0, y = 0, z = 0;
switch( c ) {
case 0:
w = 10;
goto case 1; // this is required in the current version of C#
case 1:
x = 10;
goto case 2; // as is this
case 2:
y= 10;
break;
case 3:
z= 10;
break;
// the “goto” is not required for empty cases, e.g.,
//case 4:
//case 5:
//case 6:
//case 7: ...some code...
}

6. Now assume that c is initialized to 2 instead of 0, and the code from Question 6 is run
again. What are the values of the variables w, x, y, and z after execution?

7. When the following code has finished executing, what is the value of x?

SWE2 Page 1 on 3
int x = 0;
while( x < 10 )
x++;

8. What is the value of x after the following code executes?

int x = 0;
for( int y = 0; y < 10; y += 2 ) {
if( y == 4 )
break;
x++;
}

9. What is the value of x after the following code is executed?

int x = 10;
int y = x;
y = 20;

10. Can a class have the following two functions defined at the same time?

int function1(int x, int y)


float function1(int x, int y)

11. Can a class have the following two functions defined at the same time?

int function2(int x, int y)


int function2(int x, float y)

12. Give a C# programming example, including any needed explanation, of the use of
polymorphism.

13. Consider the following code fragment:

abstract class Spaceship{


abstract public void MissileHit();
};
class CargoShip : Spaceship{
override public void MissileHit(){
// some code here
}
};
class CombatShip : Spaceship{
public void MissileHit(){
// some code here
}
};

a. Can you create instances of Spaceship?

SWE2 Page 2 on 3
b. Will the CombatShip class compile? Explain.

c. Will the following code compile? Explain.


Spaceship s = new CargoShip();

d. Will the following code compile? Explain.


CargoShip s = new CombatShip();

SWE2 Page 3 on 3

You might also like