Vous êtes sur la page 1sur 6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

Week 2: Core C# Programming Constructs


Topics
Constants & Variable Statements and Expressions Control Flow

PART I: C # BASIC
Exercise 1
Which letters represent valid identifiers? a) Abc e) $$$ i) a_1 b) M/H f) 25or6to4 j) Student Number c) Main g) 1_time k) string d) double h) first-Name Valid identifiers are ______________________________________________ Invalid identifiers are ____________________________________________

Exercise 2
using System; class Class1 { static void Main() { const int y = 1,z = 2; const string x = "Hello"; string X = "World"; Console.WriteLine(y + z); Console.WriteLine(x + " " + X); Console.ReadLine(); } } 2.1 What is written by this program?

1/6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

2.2 List all variables in this program.

Exercise 3
using System; class Class1 { static void Main() { const int b=a+1; const int d=c+3; const int c=b+2; const int a=1; Console.WriteLine(a + b Console.ReadLine(); } }

//1 //2 //3 //4 + c + d);

3.1 From this program, when compiled, there are some errors. Why?

3.2 This program can be corrected by rearrangement of lines (1) to (4). Rewrite CONSTANT DECLARATION PART of this program to correct it. ________________________ ________________________ ________________________ ________________________

2/6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

Exercise 4
static void Main() { string s = "abc"; int n = 0; double x = 0.0; s n n x x s s n } = n; = 1.0; = x; = 999; += n; = "abc" + 1; -= 1; = 1 + 1.5; //1 //2 //3 //4 //5 //6 //7 //8 //9 //10 //11

In which line of this program (4-11) did errors occur? Line The problem is

3/6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

PART II: Operators and Priorities


Precedence Order 1 2 3 4 5 6 Operator () ++(x),--(x),+(x),-(x) *,/,% +,=,+=,-=,*=,/=,%= (x)++,(x)--

Exercise 5
class Test { static void Main() { double x = 3.0, y = 2.0; int a = 10, b = 2; Console.ReadLine(); } } (1)

Insert the line numbered (1) using instructions from the following table. Fill the column output.

(1)
Console.WriteLine(x+a); Console.WriteLine(a/b); Console.WriteLine(y/x); Console.WriteLine(y%x); Console.WriteLine(++y%x-1); Console.WriteLine(y--%x+1); Console.WriteLine(a+b%b); Console.WriteLine(x/y*b); Console.WriteLine((a+b)/b%a); Console.WriteLine(9.0/5.0*(a-x)); Console.WriteLine(x+y-x*y%x); Console.WriteLine(57%50/25);

Output

4/6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

PART III: Control Flow


Exercise 6
class IfElseStruct { static void Main() { int x = 15; int y=20 ; if(x>y)Console.WriteLine(x); else Console.WriteLine(y) ; Console.ReadLine(); } }

What is written from this program?

Exercise 7
class ForStruct { static void Main() { for(int i=1 ;i<10 ;i++){ Console.WriteLine(i*i); } Console.ReadLine(); } } What is written from this program?

Exercise 8
using System; { class WhileStruct

5/6

HaNoi University of Technology

Week 2:Core C# Programming Constructs Mastering C# 2008

{ static void Main() { int n=10; while(true){ if(n>0){ Console.WriteLine(n); n--; } else break; } Console.ReadLine(); } } }

What is written from this program?

Exercise 9
using System; { class Foreach { static void Main() { int[] A={10,9,8,7,6,5,4,3,2,1}; foreach(var x in A){ Console.WriteLine(x); } Console.ReadLine(); } } } What is written from this program?

6/6

Vous aimerez peut-être aussi