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
Q

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

A

32
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double’ to ‘char *’

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

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);
}

A

33
error C2664: ‘int Math::Inc(char *)’:
cannot convert argument 1 from
‘double *’ to ‘char *’

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

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 );
}

A

34
yes

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

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);
}

A

35
any non-constant pointer can be converted to “void *”. The next
example will compile.

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

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);
}

A

37
int Math::Inc(int v1)

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

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’);
}

A

38
int Math::Inc(int v1)

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

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);
}

A

39
int Math::Inc(int v1)

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

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);
}

A

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)

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

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”);
}

A

41
int Inc(…);

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

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);
}

A

42
int Math::Inc(…)

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

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);
}

A

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
Q

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);
}

A

44
ambiguu

37
Q

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”);
}

A

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
Q

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);
}

A

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
Q

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);
}

A

47
int Math::Inc(int v1,…)

40
Q

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);
}

A

48
int Math::Inc(…)

41
Q

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);
}

A

50
void Math::Add(char x, int y)

42
Q

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);
}

A

51
void Math::Add(char x, int y)

43
Q

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);
}

A

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
Q

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);
}

A

53
void Math::Add(int x, char y)

45
Q

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);
}

A

54
int Add(char x, int y, int z) { return 1; }

46
Q

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);
}

A

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
Q

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);
}

A

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
Q

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)

A

57
ambiguu

49
Q

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)

A

58
3

50
Q

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)

A

59
ambiguu

51
Q

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)

A

60
3

52
Q

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)

A

61
ambiguu

53
Q

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’)

A

62
ambiguu

54
Q

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)

A

63
ambiguu

55
Q

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)

A

64
2

56
Q

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)

A

65
ambiguu

57
Q

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)

A

66
ambiguu

58
Q

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)

A

67
2

59
Q

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);
}

A

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
Q

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);
}

A

70
int Inc(int * x)

61
Q

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);
}

A

71
int Inc(const int * x)

62
Q

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);
}

A

72
int Inc(const int & x)

63
Q

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);
}

A

72
int Inc(int & x)

64
Q

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);
}

A

73
int Inc(const int & x)

65
Q

class Math
{
public:
int Inc(int & x)
{
return x + 2;
}
};
void main()
{
Math m;
m.Inc(100);
}

A

74
eroare
error C2664: ‘int Math::Inc(int &)’: cannot
convert argument 1 from ‘int’ to ‘int &’

66
Q

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’);
}

A

75
int Inc(const int & x)

67
Q

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);
}

A

78
Number: 10
Text: C++ test
Number: 0

68
Q

class Date
{
private:
int x;
public:
int& GetX();
};
int& Date::GetX()
{
x = 0;
return x;
}
void main()
{
Date d;
d.GetX()++;
}

A

85
The following code compiles without problem. At the end of execution
member x from object “d” will be 1;

69
Q

class Date
{
private:
int x;
public:
const int& GetX();
};
const int& Date::GetX()
{
x = 0;
return x;
}
void main()
{
Date d;
d.GetX()++;
}

A

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
Q

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++;
}

A

87
yes

71
Q

void main()
{
int x;
const int * ptr;
ptr = &x;
*ptr = 1;
}

A

88
This code will not compile as ptr points to a
constant int that CAN NOT BE modified.

72
Q

void main()
{
int x;
const int * ptr;
ptr = &x;
ptr += 1;
}

A

88
This code will run as we DO NOT modify the
actual value, we just modify the pointer.

73
Q

void main()
{
int x;
int * const ptr;
ptr = &x;
}

A

89
This code will not compile as ptr is a
constant pointer that points towords a nonconstant value.

74
Q

void main()
{
int x;
int * const ptr = &x;
*ptr = 1;
}

A

89
yes

75
Q

void main()
{
int x;
int * const ptr;
ptr = &x;
}

A

90
This code will not compile as ptr is a
constant pointer that points towords a nonconstant value.

76
Q

void main()
{
int x;
int * const ptr = &x;
ptr += 1;
}

A

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
Q

void main()
{
int x;
const int * const ptr = &x;
*ptr = 1;
ptr += 1;
}

A

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
Q

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++;
}

A

94
error C3490: ‘x’ cannot be modified because
it is being accessed through a const object

79
Q

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++;
}

A

96
yes

80
Q

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++;
}

A

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
Q

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++;
}

A

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
Q

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++;
}

A

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
Q

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++;
}

A

101
static const int& GetX() const
eroare

84
Q

class Date
{
private:
int x;
public:
void ModifyX(Date * d) const
{
d->x = 0;
}
};
void main()
{
Date d1,d2;
d1.ModifyX(&d2);
}

A

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
Q

class Date
{
private:
int x;
public:
void ModifyX(Date * d) const
{
this->x = 0;
}
};
void main()
{
Date d1,d2;
d1.ModifyX(&d2);
}

A

103
this->x = 0;
This code will NOT compile as a “const” method refers to the current instance
(“this” pointer).

86
Q

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);
}

A

104
Compile error, d is
const