Страница 1 из 1

Тесты по С и С++

Добавлено: 19 ноя 2005, 18:39
Sneg
Закидайте плиз ссылками по сабжу.
Необходимо потренироваться перед тестированием при принятии
на работу. Надеюсь это пригодится и тем, кто просто желает проверить
свой уровень знаний.

Добавлено: 19 ноя 2005, 19:53
BreakPointMAN
Тест по C: http://www.chugainov.narod.ru/ourprogs/teaching.rar

По C++ я как-то находил тесты, остались у меня в виде текстового файлика. Вот он:
1) Определите значение переменной x после выполнения серии команд:
int x,y;
y = 10;
x = ++y;

------------------------------------------------------------
2) Определите значение переменной x после выполнения серии команд:
int x,y,z;
y = 10;
z = 11;
x = ++y + z++;

------------------------------------------------------------
3) Определите значение переменной x после выполнения серии команд:
int x,y,z;

y = 8;
z = 2;
x = y<<2 + z>>3;



------------------------------------------------------------
4) Определите значение переменной x после выполнения серии команд:
int x,y,z;

y = 8;
z = 2;
x = (y=2) + (z+=3);

------------------------------------------------------------
5) Определите значение переменной x после выполнения серии команд:
int x=9;
int y=x+2,z=x;
x = x-2 - y;

------------------------------------------------------------
6) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int &V( void ) { return value; }
};

Определите значение переменной x после выполнения серии команд:
First a(10);
First b,c;
c.V() = b.V() + 1 + (b.V()=a.V());
int x = a.V() + (b.V()*c.V() );


------------------------------------------------------------
7) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int &V( void ) { return value; }
};

Определите значение переменной x после выполнения серии команд:
First a(10);
First b(a.V()=1),c(a.V()==b.V());
int x = a.V() + (b.V()*c.V() );



------------------------------------------------------------
8 ) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int &V( void ) { return value; }
};

class Second : public First
{
public:
Second( int _value ) : First( _value ) {}
Second( void ) : First(0) {}
};

Определите значение переменной x после выполнения серии команд:
First a(10);
Second b(1),c(3);
int x = a.V() + (b.V()*c.V() );

------------------------------------------------------------
9) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
};

class Second : public First
{
int appendix;
public:
Second( int _value ) :
First( _value ),
appendix(0) {}
Second( void ) :
First(0),
appendix(0) {}
virtual int V( void )
{ return First::V()+appendix; }
virtual int &A( void )
{ return appendix; }
};

Определите значение переменной x после выполнения серии команд:
First a(10);
Second b(5);
b.A() = a.V()+1;
int x = a.V() + b.V();


------------------------------------------------------------
10) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
};

class Second : public First
{
int appendix;
public:
Second( int _value ) :
First( _value ),
appendix(_value) {}
Second( void ) :
First(0),appendix(0) {}
virtual int V( void )
{ return First::V()+appendix; }
virtual int &A( void )
{ return appendix; }
};

Определите значение переменной x после выполнения серии команд:
First *a,*b;
a = new First(3);
b = new Second(4);
int x = a->V() + b->V();
------------------------------------------------------------
11) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
};

class Second : public First
{
int appendix;
public:
Second( int _value ) :
First( _value ),
appendix(_value) {}
Second( void ) :
First(0),
appendix(0) {}
virtual int V( void )
{ return First::V()+appendix; }
virtual int &A( void )
{ return appendix; }
};

Определите значение переменной x после выполнения серии команд:
First *a,*b;
a = new First(3);
b = a;
int x = a->V() + b->V();

------------------------------------------------------------

12) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
int S( void )
{
int s=0;
for ( int i=this- >V(); i; s+=i--); return s;
}
};

Определите значение переменной x после выполнения серии команд:
First *a = new First(3);
int x = a- >S();



------------------------------------------------------------

13) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
int S( void )
{
int s=0;
for ( int i=this- >V(); i; s+=i--); return s;
}
};

class Second : public First
{
int appendix;
public:
Second( int _value ) :
First( _value ),
appendix(_value) {}
Second( void ) :
First(0),
appendix(0) {}
virtual int V( void )
{ return First::V()+appendix; }
virtual int &A( void )
{ return appendix; }
};


Определите значение переменной x после выполнения серии команд:
First *a = new Second(3);
int x = a->S();


------------------------------------------------------------

14) Дано следующее описание:
class First
{
int value;
public:
First( int _value ) : value( _value ) {}
First( void ) : value(0) {}
virtual int V( void ) { return value; }
int S( void )
{
int s=0;
for ( int i=First::V(); i; s+=i--); return s;
}
};

class Second : public First
{
int appendix;
public:
Second( int _value ) :
First( _value ),
appendix(_value) {}
Second( void ) :
First(0),
appendix(0) {}
virtual int V( void )
{ return First::V()+appendix; }
virtual int &A( void )
{ return appendix; }
};


Определите значение переменной x после выполнения серии команд:
First *a = new Second(3);
int x = a->S();


********************
Ответы:
11) 6; 13) 21; 5) -4 ; 1) 11; 9) 26; 7) 2; 12) 6; 3) 16; 2) 22; 8 ) 13; 10) 11; 14) 6; 6) 220; 4) 7
А вообще, посмотри на разных сайтах, которые занимаются он-лайн сертификацией. Типа http://www.brainbench.com, http://www.retratech.ru, http://www.specialist.ru и т.д. - там бывают бесплатные.

Добавлено: 20 ноя 2005, 04:19
Sneg
кто еще? ;-)

2 BreakPointMAN: благодарю.

Вот еще нарыл по С может кому понадобиться. копирайт Aldec-Polska

1. Declare t as a 10-element table of pointers to function returning long

2. What value will be assigned to the variable x?
int a = 2;
int b = 3;
int x = sizeof("abc") + (a&b);

3. The function f is defined as follows:
void f(int *x)
{
*x = *x > 0 ? 1 : -1;
}

What value will be assigned to the variable y?
int a = -2;
int b = 4;
f(&a);
f(&b);
int y = a+b;

4. What will be the value of x after execution of the following program:
int arr[5];
int *p = arr;
while (p < arr+5)
*p++ = 1;
int x = 0;
for (p = arr; p < arr+5; p++)
x += *p;

5. What will be the output of the following program:
int a = 2;
int b = 3;
printf("%d%d%s%d",a,++b,"2",a/3);

6. The functions f and g are defined as follows:
int f()
{
return 2;
}
int g()
{
return 6;
}

What value will be assigned to the variable x?
typedef int (*FPTR)();
FPTR p = NULL;
if (p)
p = &f;
else
p = &g;
int x = (*p)();


7. The function f is defined as follows:
void f(int *p)
{
static int i = 0;
i += 3;
*p = i;
}
What will be the value of x after execution of the following instructions:
int x;
f(&x);
f(&x);

8. The following function has been written to free the list pointed by the head variable. Is it OK?

#include <stdlib.h>

struct list { struct list *next; } *head = NULL;
void freeup()
{
struct list *p;
for (p = head; p; p = p->next)
free(p);
}

9. What is the output of the following program

#include <stdio.h>
char *days[] =
{
"Mon"
"Tue"
"Wed"
"Thu"
"Fri"
"Sat"
"Sun"
};
int size = sizeof(days)/sizeof(char *);

void main()
{
int k;
for (k = 0; k < size; k++)
{
printf("%d-%s",k+1, days[k]);
}
}


10. Which of the following sentences concerning two following programs are correct? Select all aplicable answers.
#include <stdio.h>
#define MAX 10
char buff [MAX];

/* One */
void main()
{
fgets(buff, MAX, stdin); // (*)
printf ("%s",buff);
}

/* Two */
void main()
{
gets(buff); // (**)
printf ("%s",buff);
}

a) Programs are equivalent
b) First program may work incorrectly if user will enter more than 10 characters
c) Second program may work incorrectly if user will enter more than 10 characters
d) First program replaces \n character with \0
e) Second program replaces \n character with \0

11. Which of the assignments a), b) is/are incorrect and why?

void main(void)
{
int *px, *pxx;
{
static int x;
int xx;
px = &x;
pxx = &xx;
}
{
static double x;
*px = 6;
*pxx = 7; // a)
px = &x; // b)
}
}


12. Is the following assignment legal in C? If not explain why. Otherwise supply a piece of code where this assignment is correctly used
a[-3] = -5;

13. Add necessary declarations to the following program so the res variable will be initialized with value true (1).

void main()
{
// Add your declarations here...
// ...
int res = a.b->a == 5;
}


14. Is the following program correct? If not explain why?

struct tag_SomeStruct
{
int x;
double y;
}
main()
{
struct tag_SomeStruct x, y;
/* .... */
return x = y, 0;
}



Ну и тут еще чуток:

1. Which of the following lines is not correct:
1 class Parent { int x; };
2 class Child : public Base {};
3 Parent *p = new Child;
4 Child *c = new Parent;

2. Assuming the following declarations:
class Parent { public: virtual int f() { return 1; } };
class Child : public Parent { public: virtual int f() { return 2; } };
what will be assigned to v:
Child c;
Parent *p = &c;
int v = p->f();

3. Is this code correct? If not, which line contains error?
1 template<int x> void f() {}
2 template<> void f<3>() {}
3 void g()
4 {
5 int x = 3;
6 f<x>();
7 }

4. Is the following assignment legal in C? If not explain why. Otherwise supply a piece of code where this assignment is correctly used.
a[-3] = -5;

5. Assuming followed program
#include <iostream>
using namespace std;
struct A { int operator()(int i) const {static int val = 0; return val += i;} };
int main() {
A a;
A b;
cout << a(3); cout << b(4); cout << a(6);
}
What will program do:
a) print 3713
b) print 349
c) print 61013
d) print 943
e) Syntax error: struct cannot contain member function
f) Syntax error: operator () cannot be applied to struct
g) Syntax error: struc A have not constructor so expressions a(3),b(4),a(6) are not defined.

6. Assuming followed program
#include <iostream>
#include <cstring>
using namespace std;
class A {
enum {MAX_SIZE=1000};
char* s;
int size;
public:
A(int sz): size(sz) { s=new char[size]; }
A(const char* str)
{
size = strlen(str);
s=new char[size+1];
strcpy(s, str);
}
~A() { cout << " ~" << s; delete[] s; }
};

A f(const char* sa) {
A sb("one");
A se("two");
A sc = A(sa);
A sd = sb;
return sb;
}
int main() {
A foo = f("three"); cout << " done." << endl; return 0;
}

What will program print?
a) ~one ~two ~three done.
b) ~one ~three ~two done.
c) ~three ~two ~one done.
d) ~one ~three ~two ..... and possible run-time error at returning from f
e) ~three ~two ~one ..... and possible run-time error at returning from f
f) ~one ~two ~three ..... and possible run-time error at returning from f
g) Syntax error : enum cannot be member of class

7. Is this code correct:
int Inc(int &x) { return x+1; }
int f() { return Inc(10); }

8. Is this code correct? If not, which line contains error?
1 struct Block {
2 int d;
3 Block(int x) : d(x) {}
4 };
5 int f()
6 {
7 Block *x = new Block(4);
8 Block *y = x;
9 delete x;
10 x = new Block(5);
11 return y->d;
12 }
9. Which of the following sentences correctly expresses properties of class and struct declarations in C++? Select all applicable answers.
a) Cannot use access control keywords (public, protected, private) inside a struct definition.
b) All class members are private by default.
c) All class members are protected by default.
d) All struct members are public by default.
e) Struct is a synonym of the class keyword.
f) Struct can’t inherit from other struct or class
g) Class cannot be initialized (using initializer list: {})

10. What will be the value of x after the following:
int x = 2;
try { x++; throw 1; x++; }
catch(int) { x += 2; }
catch(int*) { x -= 2; }[quote][/quote]

Добавлено: 21 ноя 2005, 14:04
Kolinus
посмотри на Retratech.ru и brainbench.com