Curs 2 Flashcards

1
Q

int i = 10;
int *p = &i;

A

6
yes

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

int i = 10;
int &refI = i;

A

6
yes

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

int i = 10;
int &refI = i;

A

6
yes

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

int i = 10;
int *p;

A

6
yes

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

int i = 10;
int &refI;

A

6
Compile error –
uninitialized reference

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

int i = 10;
int j = 20;
int *p = &i;
p = &j;

A

7
yes

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

int i = 10;
int j = 20;
int &refI = i;
&refI = j;

A

7
Compiler error – trying
to change a reference
that was already
initialized.

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

int i = 10;
int j = 20;
int p = &i;
p++;
(
p) = 30;

A

8
yes

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

int i = 10;
int j = 20;
int &refI = i;
refI++;
(&refI)++;

A

8
eroare
ultimul rand

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

int i = 10;
char *p = (char *)&i;

A

10
yes
dar trebuie facut cast explicit!

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

int i = 10;
int &refI = i;
int & &ref_to_refI = refI;

A

10
eroare

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

int *p[100];

A

11
yes

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

int &ref[100];

A

11
no

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

int *p = &int(10);

A

11
yes

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

const int &refI = int(12);

A

11
yes
This code will not compile if the
“const” specifier is not used as it
refers to a constant numerical
value.

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

void Compute(int x, double y, char z)
Compute(true, 1.5f, ‘A’)

A

21
promotie
Compute(1, 1.5, ‘A’)

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

void Compute(int x, double y, char z)
Compute(3.5, 1.5, ‘A’)

A

22
conversie
Compute(3, 1.5, ‘A’)

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

void Compute(int x, const void* y, char z)
Compute(NULL, “C++”, ‘A’)

A

23
cast
Compute(0, (const void*)”C++”, ‘A’)

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

class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(100);
}

A

25
int Math::Inc(int v1)

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

class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0f);
}

A

26
float Math::Inc(float v1)

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

class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}

A

27
int Math::Inc(int v1)

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

class Math
{
public:
int Inc(short v1);
float Inc(float v1);
};
int Math::Inc(short v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(‘a’);
}

A

28
ambiguu

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

class Math
{
public:
int Inc(int v1);
float Inc(float v1);
};
int Math::Inc(int v1)
{
return v1 + 1;
}
float Math::Inc(float v1)
{
return v1+1.0f;
}
void main()
{
Math m;
m.Inc(1.0);
}

A

30
ambiguu

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

class Math
{
public:
int Inc(char v1) {
return v1 + 1;
}
};
void main()
{
Math m;
m.Inc(1.0);
}

A

31
compile
warning C4244: ‘argument’:
conversion from ‘double’ to
‘char’, possible loss of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
class Math { public: int Inc(char* v1) { return 1; } }; void main() { Math m; m.Inc(1.0); }
32 error C2664: 'int Math::Inc(char *)': cannot convert argument 1 from 'double' to 'char *'
26
class Math { public: int Inc(char* v1); }; int Math::Inc(char* v1) { return 1; } void main() { Math m; double d = 1.0; m.Inc(&d); }
33 error C2664: 'int Math::Inc(char *)': cannot convert argument 1 from 'double *' to 'char *'
27
class Math { public: int Inc(char* v1); }; int Math::Inc(char* v1) { return 1; } void main() { Math m; double d = 1.0; m.Inc( (char *)&d ); }
34 yes
28
class Math { public: int Inc(void* v1); }; int Math::Inc(void* v1) { return 1; } void main() { Math m; double d = 1.0; m.Inc(&d); }
35 any non-constant pointer can be converted to “void *”. The next example will compile.
29
class Math { public: int Inc(int v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(123); }
37 int Math::Inc(int v1)
30
class Math { public: int Inc(int v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(‘a’); }
38 int Math::Inc(int v1)
31
class Math { public: int Inc(int v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(1.0); }
39 int Math::Inc(int v1)
32
class Math { public: int Inc(int v1); int Inc(float v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(float v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(1.0); }
40 error C2668: 'Math::Inc': ambiguous call to overloaded function note: could be 'int Math::Inc(float)' note: or 'int Math::Inc(int)' note: while trying to match the argument list '(double)
33
class Math { public: int Inc(int v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(“test”); }
41 int Inc(...);
34
class Math { public: int Inc(int v1); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(...) { return 1; } void main() { Math m; m.Inc(1.0,2); }
42 int Math::Inc(...)
35
class Math { public: int Inc(int v1); int Inc(int v1,...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } void main() { Math m; m.Inc(123); }
43 error C2668: 'Math::Inc': ambiguous call to overloaded function note: could be 'int Math::Inc(int,...)' note: or 'int Math::Inc(int)' note: while trying to match the argument list '(int)
36
class Math { public: int Inc(int v1); int Inc(int v1,...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } void main() { Math m; m.Inc(true); }
44 ambiguu
37
class Math { public: int Inc(int v1); int Inc(int v1,...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } void main() { Math m; m.Inc(“test”); }
45 This code will NOT compile. None of the methods Inc(int) and Inc(int,…) matches the const char * parameter and there is no fallback method.
38
class Math { public: int Inc(int v1); int Inc(int v1,...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } void main() { Math m; m.Inc(‘a’,true); }
46 This code will compile. ‘a’ (char) is promoted to int and since there is only one method that accepts two parameters, the compiler will use it.
39
class Math { public: int Inc(int v1); int Inc(int v1,...); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } int Math::Inc(...) { return 2; } void main() { Math m; m.Inc(‘a’,true); }
47 int Math::Inc(int v1,...)
40
class Math { public: int Inc(int v1); int Inc(int v1,...); int Inc(...); }; int Math::Inc(int v1) { return v1 + 1; } int Math::Inc(int v1,...) { return 1; } int Math::Inc(...) { return 2; } void main() { Math m; m.Inc(“test”,true); }
48 int Math::Inc(...)
41
class Math { public: void Add(char x, int y); void Add(int x, char y); }; void Math::Add(char x, int y) { printf("Add(char,int)"); } void Math::Add(int x, char y) { printf("Add(int,char)"); } void main() { Math m; m.Add(‘a’,100); }
50 void Math::Add(char x, int y)
42
class Math { public: void Add(char x, int y); void Add(int x, char y); }; void Math::Add(char x, int y) { printf("Add(char,int)"); } void Math::Add(int x, char y) { printf("Add(int,char)"); } void main() { Math m; m.Add(‘a’,true); }
51 void Math::Add(char x, int y)
43
class Math { public: void Add(char x, int y); void Add(int x, char y); }; void Math::Add(char x, int y) { printf("Add(char,int)"); } void Math::Add(int x, char y) { printf("Add(int,char)"); } void main() { Math m; m.Add(100,200); }
52 error C2666: 'Math::Add': 2 overloads have similar conversions note: could be 'void Math::Add(int,char)' note: or 'void Math::Add(char,int)' note: while trying to match the argument list '(int, int)'
44
class Math { public: void Add(char x, int y); void Add(int x, char y); }; void Math::Add(char x, int y) { printf("Add(char,int)"); } void Math::Add(int x, char y) { printf("Add(int,char)"); } void main() { Math m; m.Add(100,1.5); }
53 void Math::Add(int x, char y)
45
class Math { public: int Add(char x, int y, int z) { return 1; } int Add(int x, char y, int z) { return 2; } int Add(float x, bool y, int z) { return 3; } }; void main() { Math m; int x = m.Add('a', 1.5, 2.5); }
54 int Add(char x, int y, int z) { return 1; }
46
class Math { public: int Add(char x, int y, int z) { return 1; } int Add(int x, char y, int z) { return 2; } int Add(char x, bool y, int z) { return 3; } }; void main() { Math m; int x = m.Add('a', 1.5f, 2.5); }
55 error C2668: 'Math::Add': ambiguous call to overloaded function note: could be 'int Math::Add(char,bool,int)' note: or 'int Math::Add(char,int,int)' note: while trying to match the argument list '(char, float, double)
47
class Math { public: int Add(char x, int y, int z) { return 1; } int Add(int x, char y, int z) { return 2; } int Add(char x, bool y, int z) { return 3; } }; void main() { Math m; int x = m.Add('a’, ‘a’, 2.5); }
56 error C2666: 'Math::Add': 3 overloads have similar conversions note: could be 'int Math::Add(char,bool,int)' note: or 'int Math::Add(int,char,int)' note: or 'int Math::Add(char,int,int)' note: while trying to match the argument list '(char, char, double)'
48
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(1.5, true, 1.5)
57 ambiguu
49
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(false, true, 1.5)
58 3
50
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(1.5, true, 1.5)
59 ambiguu
51
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(‘a’, true, 1.5)
60 3
52
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(‘a’, true, 100)
61 ambiguu
53
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(1.5, true, ‘a’)
62 ambiguu
54
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(100, 1.5, 1.5)
63 ambiguu
55
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, char z) { return 3; } }; Math m; m.Add(1.0f, 1.5, 1.5)
64 2
56
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, double z) { return 3; } }; Math m; m.Add(1.0f, 1.5, 1.0f)
65 ambiguu
57
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, double z) { return 3; } }; Math m; m.Add(1.0f, 1.5, 1.5)
66 ambiguu
58
struct Math { int Add(char x, int y, int z) { return 1; } int Add(double x, int y, int z) { return 2; } int Add(char x, bool y, double z) { return 3; } }; Math m; m.Add(1.0f, 1.5, 100)
67 2
59
class Math { public: int Inc(int x) { return x + 2; } int Inc(const int x) { return x + 1; } }; void main() { Math m; int x = 10; m.Inc(x); }
69 This case will NOT compile – but not due to a ambiguity problem, but rather to the fact that both Inc(int) and Inc(const int) are considered to have the same signature: Inc(int)
60
class Math { public: int Inc(int * x) { return *x + 2; } int Inc(const int * x) { return *x + 1; } }; void main() { Math m; int x = 10; m.Inc(&x); }
70 int Inc(int * x)
61
class Math { public: int Inc(int * x) { return *x + 2; } int Inc(const int * x) { return *x + 1; } }; void main() { Math m; const int x = 10; m.Inc(&x); }
71 int Inc(const int * x)
62
class Math { public: int Inc(int & x) { return x + 2; } int Inc(const int & x) { return x + 1; } }; void main() { Math m; const int x = 10; m.Inc(x); }
72 int Inc(const int & x)
63
class Math { public: int Inc(int & x) { return x + 2; } int Inc(const int & x) { return x + 1; } }; void main() { Math m; int x = 10; m.Inc(x); }
72 int Inc(int & x)
64
class Math { public: int Inc(int & x) { return x + 2; } int Inc(const int & x) { return x + 1; } }; void main() { Math m; m.Inc(100); }
73 int Inc(const int & x)
65
class Math { public: int Inc(int & x) { return x + 2; } }; void main() { Math m; m.Inc(100); }
74 eroare error C2664: 'int Math::Inc(int &)': cannot convert argument 1 from 'int' to 'int &'
66
class Math { public: int Inc(int & x) { return x + 2; } int Inc(const int & x) { return x + 1; } }; void main() { Math m; m.Inc(‘a’); }
75 int Inc(const int & x)
67
void Print(int value) { printf("Number: %d\n", value); } void Print(const char* text) { printf("Text: %s\n", text); } void main() { Print(10); Print("C++ test"); Print(NULL); }
78 Number: 10 Text: C++ test Number: 0
68
class Date { private: int x; public: int& GetX(); }; int& Date::GetX() { x = 0; return x; } void main() { Date d; d.GetX()++; }
85 The following code compiles without problem. At the end of execution member x from object "d" will be 1;
69
class Date { private: int x; public: const int& GetX(); }; const int& Date::GetX() { x = 0; return x; } void main() { Date d; d.GetX()++; }
86 This code will not compile because GetX function () returns a constant reference to a number. This means that the operator "++" from "d.GetX () ++" has to modify a number that is considered constant.
70
class Date { private: int x; public: const int& GetX(); }; const int& Date::GetX() { x = 0; return x; } void main() { Date d; int x = d.GetX(); x++; }
87 yes
71
void main() { int x; const int * ptr; ptr = &x; *ptr = 1; }
88 This code will not compile as ptr points to a constant int that CAN NOT BE modified.
72
void main() { int x; const int * ptr; ptr = &x; ptr += 1; }
88 This code will run as we DO NOT modify the actual value, we just modify the pointer.
73
void main() { int x; int * const ptr; ptr = &x; }
89 This code will not compile as ptr is a constant pointer that points towords a nonconstant value.
74
void main() { int x; int * const ptr = &x; *ptr = 1; }
89 yes
75
void main() { int x; int * const ptr; ptr = &x; }
90 This code will not compile as ptr is a constant pointer that points towords a nonconstant value.
76
void main() { int x; int * const ptr = &x; ptr += 1; }
90 linia 2: This code will run as it initialize the constant pointer from the beginning. linia 3: This code will NOT run as we try to modify a constant pointer.
77
void main() { int x; const int * const ptr = &x; *ptr = 1; ptr += 1; }
91 In this case both the pointer and the value it points to are constant. The code will not compile – one can not modify the pointer or the value.
78
class Date { private: int x; public: const int& GetX() const; }; const int& Date::GetX() const { x = 0; return x; } void main() { Date d; int x = d.GetX(); x++; }
94 error C3490: 'x' cannot be modified because it is being accessed through a const object
79
class Date { private: mutable int x; int y,z,t; public: const int& GetX() const; }; const int& Date::GetX() const { x = 0; return x; } void main() { Date d; int x = d.GetX(); x++; }
96 yes
80
class Date { private: const mutable int * x; int y,z,t; public: const int& GetX() const; }; const int& Date::GetX() const { x = &y; return *x; } void main() { Date d; int x = d.GetX(); x++; }
97 yes “const” can be used with “mutable”. In the previous example mutable refers to the value of the pointer and does not interfere with the const qualifier. This translates that you can modify the pointer (through the mutable qualifier) but you can not modify the value (due to the const qualifier at the end of the GetX() method).
81
class Date { private: const mutable int * const x; int y,z,t; public: const int& GetX() const; }; const int& Date::GetX() const { x = &y; return *x; } void main() { Date d; int x = d.GetX(); x++; }
98 const mutable int * const x This code will not compile as “x” being a const pointer (not a pointer to a const value) can not be mutable at the same time (it will imply that it can be changed). At the same time, “x=&y” can not run as “x” is a const pointer.
82
class Date { private: static int x; public: const int& GetX() const; }; int Date::x = 100; const int& Date::GetX() const { x = 0; return x; } void main() { Date d; int x = d.GetX(); x++; }
100 The code compiles correctly because "x" is no longer a member of an instance but a global static member (it does not belong to the object).
83
class Date { private: static int x; public: static const int& GetX() const; }; int Date::x = 100; static const int& Date::GetX() const { x = 0; return x; } void main() { Date d; int x = d.GetX(); x++; }
101 static const int& GetX() const eroare
84
class Date { private: int x; public: void ModifyX(Date * d) const { d->x = 0; } }; void main() { Date d1,d2; d1.ModifyX(&d2); }
102 This code compiles. “const” specifier refers to the current object/instance alone. It does not apply to another instance of a different type (it will only apply to the instance represented by “this”)
85
class Date { private: int x; public: void ModifyX(Date * d) const { this->x = 0; } }; void main() { Date d1,d2; d1.ModifyX(&d2); }
103 this->x = 0; This code will NOT compile as a “const” method refers to the current instance (“this” pointer).
86
class Date { private: int x; public: void Inc(); }; void Date::Inc() { x++; } void Increment(const Date &d) { d.Inc(); } void main() { Date d; Increment(d); }
104 Compile error, d is const