|
1. Сколько операторов выполняется в теле оператора switch(i):
switch(i) {
case -1: a++; break;
case 0: z++; breаk;
case 1: p++; break; }
Если исходить из того, что в C# оператором считается только ++, то 1, но можно сказать что case и break тоже операторы, соответственно 3 оператора для каждого case, в общем – 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i=-1, z, p, a;
Console.ReadKey(i);
switch (i) {
case -1: a++; break; // case, ++, break
case 0: z++; break;
case 1: p++; break;
}
}
}
}
2. Чему будет равно d?
main()
{int k=65,d; switch (k%12) { case 0: d=2; break; case 5: d=5; break; case 7: d=k; break; case 11: d=3; break; default: d=1;} }
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int k = 65, d;
switch (k % 12)
{
case 0:
d = 2;
break;
case 5:
d = 5;
break;
case 7:
d = k;
break;
case 11:
d = 3;
break;
default:
d = 1;
break;
}
Console.WriteLine(d);
Console.ReadKey();
}
}
}
3. Чему равно z?
main()
{int z=1; do {z=z+2;} while (z<10); Console.WriteLine (“\nz=”+z);}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int z=1;
do {
z=z+2;
}
while (z<10);
Console.WriteLine ("\nz="+z);
Console.ReadKey();
}
}
}
6. Сколько раз выполнится тело цикла в программе?
main()
{ int m=36, n=56; while (m!=n) { if (m>n) m=m-n; else n=n-m;}}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int m = 36, n = 56, i=0;
while (m!= n) {
if (m > n) m = m - n;
else n = n - m;
i++;
}
Console.WriteLine("Loop Counter: " + i);
Console.ReadKey();
}
}
}
8. Аналогично 2 задаче
9. Сколько раз выполнится тело цикла в программе?
main()
{ int m=36, n=56; do { if (m>n) m=m-n; else n=n-m;} while (m==n) }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int m=36, n=56, i=0;
do
{
i++;
if (m > n)
m = m - n;
else n = n - m;
} while (m == n);
Console.WriteLine("Loop Counter: " + i);
Console.ReadKey();
}
}
10. Если класс classA определен следующим образом
class classA
{ public int field1;
public int field2;
public classA(int a, int b)
{ field1 = a; field2 = b; }
public classA(): this(0, 0) { }
public classA(classA obj): this(obj.field1, obj.field2) { }}
То каков будет результат работы следующего фрагмента
classA ob1 = new classA();
classA ob2 = new classA(10, 10);
classA ob3 = new classA(ob2);
Console.WriteLine(ob1.field1 + " " + ob1.field2);
Console.WriteLine(ob2.field1 + " " + ob2.field2);
Console.WriteLine(ob3.field1 + " " + ob3.field2);
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class classA
{
public int field1;
public int field2;
public classA(int a, int b)
{
field1 = a;
field2 = b;
}
public classA(): this(0, 0) { }
public classA(classA obj): this(obj.field1, obj.field2) { }
}
class Program
{
static void Main(string[] args)
{
classA ob1 = new classA();
classA ob2 = new classA(10, 10);
classA ob3 = new classA(ob2);
Console.WriteLine(ob1.field1 + " " + ob1.field2);
Console.WriteLine(ob2.field1 + " " + ob2.field2);
Console.WriteLine(ob3.field1 + " " + ob3.field2);
Console.ReadKey();
}
}
}
12.
Массив определен следующим образом:
int [][]a=new int [4][];
for (int i=0; i<a.Length; ++i)
a[i]=new int [2*i-1]
В строке с номером 2 содержится _____ элементов.
Вопрос не верный!! Так как при i=0, 2*i-1 = -1, в массиве не может быть -1 элемент!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int [][]a=new int [4][];
Console.WriteLine(a.Length);
for (int i = 0; i < a.Length; ++i)
{
a[i] = new int[2 * i - 1];
Console.WriteLine("В строке " + (2*i-1) + " элементов");
}
Console.ReadKey();
}
}
}
13. Пусть определены два класса
class classA
{ int field1; int field2; }
class classB:classA
{ public int field3;
public classB(int a, int b)
{ field1 = a;
field2 = b;
field3 = field1 + field2; }
public void Show()
{ Console.WriteLine(field3); } } }
Что будет на экране после выполнения следующего фрагмента
classB ob1 = new classB(2,3); ob1.Show();
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class classA
{
public int field1;
public int field2;
}
class classB:classA
{
public int field3;
public classB(int a, int b){
field1 = a;
field2 = b;
field3 = field1 + field2;
}
public void Show()
{
Console.WriteLine(field3);
}
}
class Program
{
static void Main(string[] args)
{
classB ob1 = new classB(2, 3);
ob1.Show();
Console.ReadKey();
}
}
}
14.. В результате выполнения фрагмента программы:
class Program
{ static void F(int a)
{ ++a; }
static void Main()
{ int a=5;
F(a);
Console.WriteLine(a); } }
на экран будет выведено значение
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void F(int a) {
++a;
}
static void Main()
{
int a = 5;
F(a);
Console.WriteLine(a);
Console.ReadKey();
}
}
}
16. В результате выполнения фрагмента программы:
string a = "кол около колокола";
char[] b = {'о'};
string[] c = a.Split(b);
Console.WriteLine(c.Length);
на экран будет выведено: __
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
string a = "kol okolo kolokola";
char b = 'o';
string[] c = a.Split(b);
foreach (string s in c) {
Console.WriteLine(s);
}
Console.WriteLine(c.Length);
Console.ReadKey();
}
}
}
17. После выполнения следующего фрагмента кода на экран будет выведено
Hashtable hash = new Hashtable();
hash.Add(1, "gold medal");
hash.Add(2, "silver medal");
hash.Add(3, "bronze medal");
hash.Add(4, "no medal");
hash.Remove(2);
foreach (DictionaryEntry d in hash)
{ Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
Hashtable hash = new Hashtable();
hash.Add(1, "gold medal");
hash.Add(2, "silver medal");
hash.Add(3, "bronze medal");
hash.Add(4, "no medal");
hash.Remove(2);
foreach (DictionaryEntry d in hash){
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
Console.ReadKey();
}
}
}
18. В результате выполнения фрагмента программы:
int []a={1, 2, 4, 3, 1, 2};
int s=0;
for (int i=0; i<a.Length; i+=2)
s+=a[i];
Console.WriteLine(s);
на экран будет выведено значение: ________
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
int[] a = { 1, 2, 4, 3, 1, 2 };
int s = 0;
for (int i = 0; i < a.Length; i += 2)
s += a[i];
Console.WriteLine(s);
Console.ReadKey();
}
}
}
19. В результате выполнения фрагмента программы:
class Program
{ static void F(ref int a)
{ ++a; }
static void Main()
{ int a=5;
F(ref a);
Console.WriteLine(a); } }
на экран будет выведено значение______________________________________________________
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void F(ref int a){
++a;
}
static void Main()
{
int a = 5;
F(ref a);
Console.WriteLine(a);
Console.ReadKey();
}
}
}
20. После выполнения следующего фрагмента кода на экран будет выведено:
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(4);
queue.Enqueue(8);
queue.Enqueue(16);
foreach (int i in queue)
{
Console.Write("{0} ", i);
}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
Queue queue = new Queue();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(4);
queue.Enqueue(8);
queue.Enqueue(16);
foreach (int i in queue)
{
Console.Write("{0} ", i);
}
Console.ReadKey();
}
}
}
21. После выполнения следующего фрагмента кода на экран будет выведено:
Stack stack = new Stack();
stack.Push("son");
stack.Push("father");
stack.Push("grandfather");
while (stack.Count!=0)
{
Console.WriteLine("{0} ", stack.Pop().ToString());
}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Push("son");
stack.Push("father");
stack.Push("grandfather");
while (stack.Count!= 0)
{
Console.WriteLine("{0} ", stack.Pop().ToString());
}
Console.ReadKey();
}
}
}
22.
В результате выполнения фрагмента программы
FileStream f = new FileStream("t.dat", FileMode.Create);
BinaryWriter fOut = new BinaryWriter(f);
for (float i = -3; i <=3; i+=0.5f)
fOut.Write(i);
fOut.Close();
f = new FileStream("t.dat", FileMode.Open);
BinaryReader fIn = new BinaryReader(f);
long m = f.Length;
for (long i = 0; i < m; i += 16)
{ f.Seek(i, SeekOrigin.Begin);
float a = fIn.ReadSingle();
Console.Write("{0:f} ", a); }
fIn.Close();
f.Close();
на экран будут выведены числа:
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
FileStream f = new FileStream("t.dat", FileMode.Create);
BinaryWriter fOut = new BinaryWriter(f);
for (float i = -3; i <= 3; i += 0.5f)
fOut.Write(i);
fOut.Close();
f = new FileStream("t.dat", FileMode.Open);
BinaryReader fIn = new BinaryReader(f);
long m = f.Length;
for (long i = 0; i < m; i += 16)
{
f.Seek(i, SeekOrigin.Begin);
float a = fIn.ReadSingle();
Console.Write("{0:f} ", a);
}
fIn.Close();
f.Close();
Console.ReadKey();
}
}
}
23. Предположим, что коллекция SortedList хранит пары наименование товара (ключ)/вид товара (значение), например:
SortedList goods = new SortedList();
goods.Add("кукла", "игрушка");
goods.Add("тетрадь", "канцтовары");
goods.Add("лото", "игра");
Каким из предложенных способов можно получить значение для "лото":
a. s1["лото"]
b. s1["игра"]
c. s1[goods]
d. s1[3]
e. s1.value
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
SortedList goods = new SortedList();
goods.Add("kukla", "igrushka");
goods.Add("tetrad", "kanctovari");
goods.Add("loto", "igra");
// for (int i = 0; i < goods.Count; i++)
// {
// Console.WriteLine("\t{0}:\t{1}", goods.GetKey(i), goods.GetByIndex(i));
// }
Console.WriteLine(goods.GetKey(1));
Console.ReadKey();
}
}
}
24. Что напечатает программа
class Class1
{ [STAThread]
static void Main(string[] args)
{ System.Collections.Queue queue = new System.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
while (queue.Count > 0)
{Console.WriteLine(queue.Dequeue());}
Console.ReadLine(); } }
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Collections.Queue queue = new System.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);
queue.Enqueue(3);
while (queue.Count > 0)
{
Console.WriteLine(queue.Dequeue());
}
Console.ReadLine();
}
}
}
25. Что напечатает программа
class Class1
{ [STAThread]
static void Main(string[] args)
{System.Collections.Stack stack = new System.Collections.Stack();
for (int i=0; i<8; i++)
{ stack.Push(i); }
while (stack.Count > 0)
{Console.WriteLine(stack.Pop()); }
Console.ReadLine(); }}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
[STAThread]
static void Main(string[] args)
{
System.Collections.Stack stack = new System.Collections.Stack();
for (int i = 0; i < 8; i++)
{
stack.Push(i);
}
while (stack.Count > 0)
{
Console.WriteLine(stack.Pop());
}
Console.ReadLine();
}
}
}
26. Что напечатает программа
class Class1
{ [STAThread]
static void Main(string[] args)
{ System.Collections.Stack stack = new System.Collections.Stack();
for (int i=0; i<10; i++)
{ stack.Push(i); }
for (int i=0; i<5; i++)
{ Console.WriteLine(stack.Pop());}
Console.ReadLine(); }}
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Stack stack = new Stack();
for (int i = 0; i < 10; i++)
{
stack.Push(i);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(stack.Pop());
}
Console.ReadLine();
}
}
}
}
27. Что напечатает программа
int *p;
int [] i={25,6,47,-5,12};
; p = i; *p = 333;
p++;
Console.WriteLine(i[0]); Console.WriteLine(p[3]);
28. Что напечатает программа
int *p;
int i; i = 10; p = &i; *p = 333;
i = *(&i) + 10;
Console.WriteLine(i); Console.WriteLine((*p)--);
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
class Class1
{
unsafe static void Main(string[] args)
{
int* p;
int i; i = 10; p = &i; *p = 333;
i = *(&i) + 10;
Console.WriteLine(i);
Console.WriteLine((*p)--);
Console.ReadKey();
}
}
}
}
30. Что напечатает программа
int *p;
int i; i = 20; p = &i; *p = 22;
i = *(&i) + 80;
Console.WriteLine(i++); Console.WriteLine(--(*p));
Sol:_
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
class Class1
{
unsafe static void Main(string[] args)
{
int *p;
int i;
i = 20;
p = &i;
*p = 22;
i = *(&i) + 80;
Console.WriteLine(i++);
Console.WriteLine(--(*p));
Console.ReadKey();
}
}
}
}
Дата добавления: 2015-10-21; просмотров: 29 | Нарушение авторских прав
<== предыдущая лекция | | | следующая лекция ==> |
Инструкция по перенастройке почты butovo.com | | | Официальный сайт группы «CENTR» |