Pointers¶
Q) Which of the following tells you where name
is?¶
Given:
string name = "waldo";
a) cout << &name << endl;
b) cout << name << endl;
c) cout << *name << endl;
d) cout << &"waldo" << endl;
Q) Given the following code:¶
int main()
{
int happiness = 0;
// XXXXXXXXX
*pFeeling = 100;
cout << "Happiness is: " << happiness << endl;
}
Which of the following best replaces the line XXXXXXXX to allow this code to compile, run, and print out the happiness of 100?
a) int pFeeling = &happiness;
b) int *pFeeling = &happiness;
c) int &pFeeling = *happiness;
d) int *pFeeling = *happiness;
Q) Given the following code:¶
int multiply(int *pOne, int *pTwo)
{
// XXXXXXXX
}
Which of the following best replaces line XXXXXXXX so the function multiplies the two values which the parameters point to?
a) return pOne * pTwo;
b) return &pOne * &pTwo;
c) return *pOne * *pTwo;
d) return *(pOne * pTwo);
Q) Which of the following is true about the size of pointer variables?¶
If a
and b
are defined as:
int *a = nullptr;
double *b = nullptr;
Which of the following is true?
a) sizeof(a) > sizeof(b);
because a
is declared before b
.
b) sizeof(a) < sizeof(b);
because double
s are bigger than int
s.
c) sizeof(a) == sizeof(b);
because both point to nullptr
.
d) sizeof(a) == sizeof(b);
because all pointers are the same size.
Answers¶
1 = a) cout << &name << endl;
2 = b) int *pFeeling = &happiness;
3 = c) return *pOne * *pTwo;
4 = d) sizeof(a) == sizeof(b);
because all pointers are the same size.