Inner Workings C++ Class - Quiz Explanation
The correct answers are indicated below, along with the text that explains the correct answers.
1.
The expression
cout << ::i
means:
Please select the best answer.
A.
print the global variable
i
B.
print the local variable
i
C.
print the value of
i
in the nearest enclosing scope
D.
none of these
The correct answer is A.
The use of the scope resolution operator without a class name refers to a variable in global scope.
2.
An
int i
is a static data member of class
X
if which condition is met:
Please select the best answer.
A.
it is declared
static
within the class
B.
it is shared by all variables of that class
C.
it is accessible as
X::i
D.
all of the above
The correct answer is D.
All of these are properties of a static data member.
3.
If class
IN
is declared in class
OUT
and
int i
is in
IN
, how would you reference
i
unambiguously?
Please select the best answer.
A.
IN::OUT::i
B.
OUT::i
C.
OUT::IN::i
D.
::i
The correct answer is C.
If class
IN
is declared in class
OUT
and
int i
is in
IN
then
OUT::IN::i
is how to reference
i
unambiguously.
4.
The
this
pointer:
Please select the best answer.
A.
is
const
and therefore cannot have its value altered
B.
is available for native types
C.
is mutable and can have its value altered
D.
must be explicitly declared in a class
The correct answer is A.
In the earlier versions of C++, the
this
pointer was mutable, but that led to buggy code. The
this
pointer is unavailable for native types.
5.
A
static
member function:
Please select the best answer.
A.
does not have access to
this
pointer arguments
B.
must be public
C.
is a class variable
C.
none of the above
The correct answer is A.
The
static
member function does not have access to this pointer arguments. Any class variables must be passed in through its argument list. A
static
member function can be private as well as public. When a
static
member function is defined outside the class declaration, it must not include the
static
modifier.