CS225 FINAL MC EXAM QUESTIONS WITH
COMPLETE ANSWERS
int main()
{
int p = 6;
int *q;
q = new int(p);
// here {{#line}}
delete q;
return 0;
}
Suppose that variable q has location 0xdeadbeef, variable p has location 0xcafebabe,and
the memory address of the new int is 0x00bae000.
What is the value of *q at line {{@line}}?
A. None of these.
B. 0xdeadbeef
C. 0x00bae000
D. [Correct Answer] [Your Answer] 6
E. The default value of an integer.
,F. 0xcafebabe - ANSWER D. 6
class Foo
{
public:
Foo();
private:
int bar;
};
Foo::Foo() { bar = 0; }
int main()
{
Foo *x = new Foo();
Foo *y = new Foo(12);
return 1;
}
What is the result when this code is compiled and run?
A. A run time error, because the proper constructor doesn't exist for the assignment to y.
B. No output
,C. A compiler error, because the proper constructor doesn't exist for the assignment to y.
D. The number 1 is printed to the screen.
E. A compiler error, because bar is private.
F. A run time error, because bar is private - ANSWER C. A compiler error, because the
proper constructor doesn't exist for the assignment to y.
What is one way that C++ enforces encapsulation?
A. By convention, the main function is put in a separate file.
B. Creating private member variables and public functions to alter the variables in a
controlled manner.
C. Compilation is orchestrated via a Makefile.
D. By using pointers, rather than - ANSWER B. Creating private member variables and
public functions to alter the variables in a controlled manner.
Suppose you have the following code:
class Milkshake
{
public:
bool awesome;
void setTogo();
, private:
char size;
bool togo;
};
void Milkshake::setTogo() { // code code code }
void serveShake() { // code code code }
int main()
{
Milkshake m;
return 0;
}
Where could the assignment awesome = true; occur?
A. In the setTogo function.
B. In the serveShake function.
C. None of the other options is correct.
D. Only in the constructor if we were to write one.
E. In the main function - ANSWER A. In the setTogo Function.
COMPLETE ANSWERS
int main()
{
int p = 6;
int *q;
q = new int(p);
// here {{#line}}
delete q;
return 0;
}
Suppose that variable q has location 0xdeadbeef, variable p has location 0xcafebabe,and
the memory address of the new int is 0x00bae000.
What is the value of *q at line {{@line}}?
A. None of these.
B. 0xdeadbeef
C. 0x00bae000
D. [Correct Answer] [Your Answer] 6
E. The default value of an integer.
,F. 0xcafebabe - ANSWER D. 6
class Foo
{
public:
Foo();
private:
int bar;
};
Foo::Foo() { bar = 0; }
int main()
{
Foo *x = new Foo();
Foo *y = new Foo(12);
return 1;
}
What is the result when this code is compiled and run?
A. A run time error, because the proper constructor doesn't exist for the assignment to y.
B. No output
,C. A compiler error, because the proper constructor doesn't exist for the assignment to y.
D. The number 1 is printed to the screen.
E. A compiler error, because bar is private.
F. A run time error, because bar is private - ANSWER C. A compiler error, because the
proper constructor doesn't exist for the assignment to y.
What is one way that C++ enforces encapsulation?
A. By convention, the main function is put in a separate file.
B. Creating private member variables and public functions to alter the variables in a
controlled manner.
C. Compilation is orchestrated via a Makefile.
D. By using pointers, rather than - ANSWER B. Creating private member variables and
public functions to alter the variables in a controlled manner.
Suppose you have the following code:
class Milkshake
{
public:
bool awesome;
void setTogo();
, private:
char size;
bool togo;
};
void Milkshake::setTogo() { // code code code }
void serveShake() { // code code code }
int main()
{
Milkshake m;
return 0;
}
Where could the assignment awesome = true; occur?
A. In the setTogo function.
B. In the serveShake function.
C. None of the other options is correct.
D. Only in the constructor if we were to write one.
E. In the main function - ANSWER A. In the setTogo Function.