Curs 6 Flashcards

1
Q

class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
class C : private A, private B { public: int c1, c2; };
void main(void)
{
C c;
A* a = &c;
}

A

11
no
derivarea e privata
error C2243: ‘type cast’: conversion from
‘C *’ to ‘A *’ exists, but is inaccessible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
class C : private A, private B { public: int c1, c2; };
void main(void)
{
C c;
A* a = reinterpret_cast<A*> (&c);
}

A

25
yes
This code will compile. It is important to understand the reinterpret_cast is
not bounded by class type or access modifier type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

int x = reinterpret_cast <int>(1000);</int>

A

26
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

int y = 100;
int x = reinterpret_cast <int>(y);</int>

A

26
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

int x = reinterpret_cast<int>(“test”);</int>

A

26
yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

char x = reinterpret_cast<char>(“test”);</char>

A

26
yes cu warning trunchiere date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

float x = reinterpret_cast<float>(1.2f);</float>

A

27
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

double x = reinterpret_cast<double>(1.2);</double>

A

27
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

int number = 10;
reinterpret_cast<int &>(number) = 20;

A

27
yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

int number = 10;
reinterpret_cast<char &>(number) = 20;

A

27
yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

int x = static_cast<int>(1000);</int>

A

30
yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

char x = static_cast<char>(1000);</char>

A

30
yes
232=1000%256

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

char x = static_cast<char>(3.75);</char>

A

30
yes
3=int(3.75)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

char x = static_cast<char>("test");</char>

A

30
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

const char * x = static_cast<const char *>(9);

A

30
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

class A { public: int a1, a2, a3; };
class B { public: int b1, b2; };
void main(void)
{
B b;
A* a = static_cast<A *>(&b);
}

A

31
eroare

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
operator A* () { return new A(); }
};
void main(void)
{
B b;
A* a = static_cast<A *>(&b);
}

A

32
eroare
A* a = static_cast<A *>(&b); are parametru &b in loc de b

18
Q

class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
operator A* () { return new A(); }
};
void main(void)
{
B b;
A* a = static_cast<A *>(b);
}

A

33
yes

19
Q

int Add(int x, char y)
{
return x + y;
}
int Add(char x, int y)
{
return x + y;
}
void main(void)
{
int suma = static_cast<int(*) (int, char)> (Add)(100, 200);
}

A

35
yes

20
Q

class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
B b;
C c = dynamic_cast<C>(&b);
}

A

38
eroare
‘dynamic_cast’: ‘B’ is not a polymorphic type
note: see declaration of ‘B’

21
Q

class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
B b;
C c = dynamic_cast<C>(&b);
}

A

39
yes
c este nullptr pt ca b este obiect de sine statator

22
Q

class A { public: int a1, a2, a3; };
class B
{
public:
int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void)
{
C c;
B b = (B)&c;
C c2 = dynamic_cast<C>(b);
}

A

40
yes

23
Q

class A {
public: int a1, a2, a3;
virtual void f2() {};
};
class B {
public: int b1, b2;
virtual void f() {};
};
class C : public A, public B { public: int c1, c2; };
void main(void) {
C c;
A a = (A)&c;
B b = dynamic_cast<B>(a);
}

A

41
This code also compiles.
 While there is no relationship between A and B , since local variable “a”
points to an object of type C , that contains a “B” component, this conversion
is possible.

24
Q

void main(void)
{
int x = 100;
const int * ptr = &x;
int * non_const_pointer = const_cast <int *>(ptr);
*non_const_pointer = 200;
printf(“%d”, x);
}

A

43
This code will compile and will convert ptr const pointer to a non-constant
pointer that can be used to change the value of “x”
 As a result, the program will print “200” on the screen

25
Q

void main(void)
{
const char * txt = “C++ exam”;
char * non_const_pointer = const_cast <char *>(txt);
non_const_pointer[0] = ‘c’;
printf(“%s”, non_const_pointer);
}

A

44
This code will compile but it will most likely produce a run-time error.

26
Q

void main(void)
{
const int x = 100;
(const_cast<int>(&x)) = 200;
}

A

Now , this code will compile (using the const_cast we can remove the const
attribute of “x”) allowing one to change “x” value.
 The behavior of the program is however , undefined → in this context
undefined means that the actual value of “x” may not be 200 !!!

27
Q

void main(void)
{
const int x = 100;
(const_cast<int>(&x)) = 200;
printf(“%d”, x);
}

A

47
nu stim ce se va afisa

28
Q

void main(void)
{
const int x = 100;
(const_cast<int>(&x)) = 200;
printf(“%d”, (const_cast<int>(&x))) ;
}

A

48
200

29
Q

class Test
{
public:
const int x;
Test(int value) : x(value) { }
void Set(int value) { (const_cast<int>(&x)) = value; }
};
void main(void)
{
Test t(100);
printf(“%d “, t.x);
t.Set(200);
printf(“%d “, t.x);
}

A

49
100 200

30
Q

define PRINT(format,…) \

{ \
printf(“\nPrint values:”); \
printf(format, __VA_ARGS__); \
}
void main(void)
{
int v1, v2;
v1 = 100;
v2 = 200;
PRINT(“%d,%d”, v1, v2);
}

A

57
yes
void main(void)
{
int v1, v2;
v1 = 100;
v2 = 200;
printf(“\nPrint values:”);
printf(“%d,%d”, v1, v2);
}

31
Q

define CHECK(condition) { \

if (!(condition)) { printf(“The condition ‘%s’ wasn’t evaluated correctly”, #condition);
};
void main(void)
{
int v1, v2;
v1 = 100;
v2 = 200;
CHECK(v1 > v2);
}

A

58
void main(void)
{
int v1, v2;
v1 = 100;
v2 = 200;
if (!(v1>v2)) { printf(“The condition ‘%s’ wasn’t evaluated correctly”, “v1 > v2”); };
}

32
Q

define SUM(a,b) a+b

#define SUM(a,b,c) a+b+c
void main(void)
{
int x = SUM(1, 2);
}

A

61
error C2059: syntax error: ‘;’

33
Q

void main(void)
{
#define SUM(a,b) a+b
int x = SUM(1, 2);
#define SUM(a,b,c) a+b+c
x = SUM(1, 2, 3);
}

A

61
yes

34
Q

define DIV(x,y) x/y

void main(void)
{
int x = DIV(10 + 10, 5 + 5);
}

A

62
incorect
void main(void)
{
int x = 10 + 10 / 5 + 5;
}

35
Q

define DIV(x,y) ((x)/(y))

void main(void)
{
int x = DIV(10 + 10, 5 + 5);
}

A

62
corect
void main(void)
{
int x = ((10 + 10) / (5 + 5));
}

36
Q

define PRINT(x,y) \

printf(“X=%d”,x);printf(“Y=%d”,y);
void main(void)
{
int x = 10, y = 20;
if (x > y)
PRINT (x, y);
}

A

63
incorect
void main(void)
{
int x = 10, y = 20;
if (x > y)
printf(“X=%d”,x);printf(“Y=%d”,y);
}

37
Q

define PRINT(x,y) \

{ printf(“X=%d”,x);printf(“Y=%d”,y); }
void main(void)
{
int x = 10, y = 20;
if (x > y)
PRINT (x, y);
}

A

63
corect
void main(void)
{
int x = 10, y = 20;
if (x > y)
{printf(“X=%d”,x);printf(“Y=%d”,y);}
}

38
Q

define set_min(result,x,y) \

result = ((x)>(y)?(x):(y));
void main(void)
{
int x = 2, y = 1;
int res;
set_min(res, x++, y);
}

A

64
incorect
res will be 3

39
Q

define set_min(result,x,y) {\

int t_1 = (x); \
int t_2 = (y); \
result = ((t_1)>(t_2)?(t_1):(t_2)); \
}
void main(void)
{
int x = 2, y = 1;
int res;
set_min(res, x++, y);
}

A

64
corect
void main(void)
{
int x = 2, y = 1;
int res;
{ int t_1 = (x); int t_2 = (y);
result = ((t_1)>(t_2)?(t_1):(t_2)); }
}

40
Q

void main(void)
{
int x = __COUNTER__;
int y = __COUNTER__;
int z = __COUNTER__;
int t = __COUNTER__;
}

A

66
void main(void)
{
int x = 0;
int y = 1;
int z = 2;
int t = 3;
}