# exercise-week-2-core-c-progr

THPT Gò Vấp
18 de May de 2015
1 de 7

### exercise-week-2-core-c-progr

• 1. 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
• 2. 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; //1 const int d=c+3; //2 const int c=b+2; //3 const int a=1; //4 Console.WriteLine(a + b + c + d); Console.ReadLine(); } } 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
• 3. HaNoi University of Technology Week 2:Core C# Programming Constructs – Mastering C# 2008 Exercise 4 static void Main() { string s = "abc"; //1 int n = 0; //2 double x = 0.0; //3 s = n; //4 n = 1.0; //5 n = x; //6 x = 999; //7 x += n; //8 s = "abc" + 1; //9 s -= 1; //10 n = 1 + 1.5; //11 } In which line of this program (4-11) did errors occur? Line The problem is … 3/6
• 4. HaNoi University of Technology Week 2:Core C# Programming Constructs – Mastering C# 2008 PART II: Operators and Priorities Precedence Order Operator 1 () 2 ++(x),--(x),+(x),-(x) 3 *,/,% 4 +,- 5 =,+=,-=,*=,/=,%= 6 (x)++,(x)-- Exercise 5 class Test { static void Main() { double x = 3.0, y = 2.0; int a = 10, b = 2; (1) Console.ReadLine(); } } Insert the line numbered (1) using instructions from the following table. Fill the column output. (1) Output 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); 4/6
• 5. 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
• 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
• 7. 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