• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/12

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

12 Cards in this Set

  • Front
  • Back
Under what circumstances can a local variable be declared within a switch without being inside a control block?
If no labels can skip over it (i.e., there are no case labels following it).
In a class definition, how do you give an overloaded == operator precedence over an overloaded + operator?
Contrary to the premise of this question, you cannot change the precedence of operators.
int SomeFunction( int input )
{
if (input > 0)
if (input > 100)
return 100;
else
return input;
return 0;
}

What will the value of SomeFunction (-1) be after execution of the sample code above?
0
The else actually matches with the second if.
namespace {
int i;
}

Is this code equivalent to: static int i; ?
No, because using an unnamed namespace, "i" has external linkage, and a static declaration would result in internal linkage.
class Foo {
public:
Foo();
Foo(const Foo& f);
Foo(double x);
private:
Foo& operator=(const Foo&);
};
Which one of the following statements, when written in the body of a non-member function, does NOT create an instance of class Foo?

1) Foo* object = new Foo;
2) static volatile Foo f;
3) Foo f = 3;
4) extern Foo f;
5) const Foo& f = Foo();
Choice 1: incorrect
Foo* object = new Foo;
This statement creates an instance of class Foo on the free store through operator new.

Choice 2 Incorrect (selected)
static volatile Foo f;
The statement creates an instance of class Foo in static storage. volatile is completely irrelevant here.

Choice 3: incorrect
Foo f = 3;
The statement creates an auto instance of class Foo through its conversion constructor, Foo(double x). Actually, it creates a temporary Foo instance through the conversion constructor, and then uses the copy constructor to create instance f. The fact that the assignment operator is declared as private in class Foo is a red herring; this is a construction operation, not an assignment operation.

Choice 4: correct
The statement only tells the compiler that somewhere, there exists a variable of type Foo called f. It does not create an instance of class Foo by itself.

Choice 5: incorrect
const Foo& f = Foo();
An instance of Foo is indeed created. It may seem that the instance does not exist for very long because the Foo() syntax appears to create a temporary. However, the temporary is guaranteed to exist for the scope of the reference variable f.
Assume "pointer" is a pointer to a void member function of type "SomeClass" with one int parameter and "someobject" is a pointer to a "SomeClass" object. Which one of the following correctly calls the member function referenced by "pointer"?

1) pointer (1)
2) SomeClass::pointer (29)
3) someobject->pointer (10)
4) someobject->*pointer) (2)
5) (someobject->*pointer) (2)
Choice 5 Correct (not selected)
(someobject->*pointer) (2)
This is correct. The "->*" is a newly introduced operator which binds the member function pointer on the right with the object pointer on the left (C++ Standard 5.5/3).
class A{
public:
A();
A (int x);
operator int&();
};

----> A object = 7;

What happens to "object" on the line above?

1) "object" is constructed using the default constructor. A temporary object of type A constructed with the conversion constructor is then assigned to it.
2) The line will not compile because there is no operator=(int).
3) Line 8 will not compile since no copy constructor was defined.
4) "object" is constructed with the default constructor. It is then converted to "int&", using the user-defined conversion operator. That "int&" is then assigned the value 7.
5) "object" is constructed directly through the conversion constructor.
Choice 5 Correct
There is no ambiguity because all other possibilities require several conversions, so the conversion constructor is preferred.
struct X {
virtual ~X(){}
};

class Y : public X {};

int main()
{
X* p = new Y[2];
delete [] p;
return 0;
}

What, if anything, is an error with the sample code above?

1) A "class" cannot inherit from a "struct."
2) It invokes undefined behavior.
3) "main" requires arguments.
4) "Y" requires a destructor.
5) Nothing is wrong.
Choice 2: correct
It invokes undefined behavior.
When deleting an array, the dynamic and the static type of the object must be the same, or the behavior is undefined
struct X {
int foo() { return 0; }
} x;

struct Y {
static int (X::*p)();
};

int (X::*Y::p)() = X::foo;

Given the declarations in the sample code above, which one of the following invocations is correct?

1) (x.*Y::p)();
2) (x->*Y::p)();
3) (Y::p)(x);
4) (Y::x.*p)();
5) (x.Y::p)();
Choice 1 Correct (not selected)
(x.*Y::p)();
This is correct. The ".*" is the operator that binds object "x" to member function "Y::p" (C++ Standard 5.5/2).

Choice 2
(x->*Y::p)();
This is incorrect. The "->*" operator would be correct if "x" were a pointer to" X" (C++ Standard 5.5/3).

Choice 3 Incorrect (selected)
(Y::p)(x);
This is incorrect. This could only be used if "Y::p" were a pointer to a non-member function expecting an object or reference of type "X".

Choice 4
(Y::x.*p)();
This is incorrect; "x" is not a static member of "Y," even though this answer uses the correct operator.

Choice 5
(x.Y::p)();
This is incorrect. If the class of "x" were derived from class "Y," and class "Y" had access to a static member named "p," which was a pointer to a non-member function, this would invoke that function.
Which one of the following declarations is valid in a function body?

1) auto static volatile int j;
2) volatile const int j = 0;
3) const int const * j = 0;
4) const int j;
5) void* j, k;
Choice 1
auto static volatile int j;
This is incorrect. A variable cannot have storage class auto and static at the same time.

Choice 2 Correct (not selected)
volatile const int j = 0;
This is the correct answer. As strange as it may seem, it is possible for a variable to be const and volatile at the same time. This essentially means that the variable cannot be changed by the programmer through C++ code, but it can be changed behind your back by something outside the program's control (an external device, for example).

Choice 3
const int const * j = 0;
This is incorrect. The declaration may appear to be a const pointer to a const int. However, it is actually an invalid declaration because the second const in on the wrong side of the asterisk "*".

Choice 4
const int j;
This is incorrect. A const variable must be initialized. Note that in C, this declaration would be possible if it were for a global variable because const variables are extern by default, which means they could be defined (and initialized) somewhere else.. However, in C++, such a declaration is never valid since const variables are static by default.

Choice 5 Incorrect (selected)
void* j, k;
This is incorrect. Despite appearances, the second variable, k, is not declared to be a "void*." It is actually declared to be a void. It is not possible to have a variable of type void (you cannot have a variable with no type).
int main()
{
string line1(3, '*'), line2(5, ';');
cin >> line1;
getline(cin, line2);

cout << "Line 1: " << line1 << endl
<< "Line 2: " << line2 << endl
<< "EOF\n";
return 0;
}
Given the program above, what happens if, when the program is run, the user enters "Hello world!" followed by a newline?
It will print out:
Line 1: Hello
Line 2: world!
EOF
Although std::skipws is issued in the stream, std::getline() bypasses that mechanism, and the space that terminated the extraction will be read in. In addition, std::getline() never keeps the terminating newline in the destination string.
Can a destructor be pure virtual?
Yes, and this is a common method of making a class abstract. It MUST still be defined (i.e., have an implementation)>