MidtermB Flashcards
- Which of the following commands is an example of linking?
(a) g++ -c Student.cc
(b) g++ -o p1 main.o Student.o
(c) g++ -o p1 main.cc Student.cc
(d) g++ main.cc
(b) g++ -o p1 main.o Student.o
include < iostream >
- What would be the output of the following code assuming the user entered a 5?
~~~
# include < string >
using namespace std ;
void doubleNum ( int * num ){
* num *= 2;
}
int main (){
int num ;
cout «” Enter an integer : “ ;
cin»_space; num ;
doubleNum ( num );
cout «_space;” Your number doubled is : “ «_space;num «_space;endl ;
}
~~~
(a) 2
(b) 5
(c) 10
(d) Error
(c) 10
- Which of these functions could return an int as an output parameter?
(a) int& getValue();
(b) int getValue(int);
(c) void getValue(int&);
(d) int* getValue();
(c) void getValue(int&);
- Assuming that Book and Movie are both classes, which of these has the proper signature
for a conversion constructor?
(a) Movie(Book&);
(b) Movie(Book*);
(c) Movie(Book);
(d) None of these.
(a) Movie(Book&);
- What is the output of the following code?
~~~
# include < string >
# include < iostream >
using namespace std ;
class Egg
{
public :
Egg ( string size = “ mini “ ){ cout < < “ Egg ctor “ ; }
private :
string size ;
};
class Chicken {
public :
Chicken (){ cout < < “ Chicken ctor “ ; }
private :
Egg * egg ;
};
int main (){
Chicken chicky ;
}
~~~
(a) Egg ctor Chicken ctor
(b) Chicken ctor Egg ctor
(c) Chicken ctor
(d) None of these.
(c) Chicken ctor
- Which of these code snippets would produce an error?
(a) int x = 0, y = 1; int& z = x;
(b) int x, y; x = 1, y = 1; int& z = 1;
(c) int x, y; x = 1; y = 1; int& z = x;
(d) All of these.
(e) None of these.
(b) int x, y; x = 1, y = 1; int& z = 1;
- Which of these variables could be used to refer to a dynamically allocated array of
Student pointers?
(a) Student** studentArray;
(b) Student* studentArray;
(c) Student* studentArray[10];
(d) Student studentArray[10];
(a) Student** studentArray;
- Which of these variables could be used to refer to a statically allocated array of Student
pointers?
(a) Student** studentArray;
(b) Student* studentArray;
(c) Student& studentArray;
(d) Student studentArray;
(e) None of these.
(a) Student** studentArray;
include < iostream >
- How many int variables are allocated on the function call stack in this program?
using namespace std ; int main (){ int x = 0; int * y = & x ; int & z = x ; z = 10; int w = * y ; cout << w <<" , " << x <<" , " << y <<" , " << z ; }
(a) 1
(b) 2
(c) 3
(d) 4
(e) None of these.
(b) 2
- Which of the following is NOT an Object-Oriented design principle?
(a) Data abstraction
(b) Encapsulation
(c) Principle of least privilege
(d) Model-View-Controller
(d) Model-View-Controller
- Assuming these are functions in an Employee class with a pay member variable, which
of these functions should be made const? Select all that apply.
(a) float getPay(){
return pay;
}
(b) void setPay(float pay){
this->pay = pay;
}
(c) void printPay() {
cout «_space;pay;
}
(d) Employee (float pay){
this->pay = pay;
}
(a) float getPay(){
return pay;
}
(c) void printPay() {
cout «_space;pay;
}
- When running the following piece of code, what part of memory would the Weapon
object be stored?
~~~
# include < iostream >
# include < string >
using namespace std ;
class Weapon {
public :
setDamage ( int damage ){ this - > damage = damage ;}
private :
int damage ;
};
class Character {
public :
Character ( const string & n , int h , int damage ){
name = n ;
health = h ;
weapon . setDamage ( damage );
}
private :
string name ;
int health ;
Weapon weapon ;
};
int main (){
Character joe ( “ Joe “ , 20 , 20);
}
~~~
(a) No Weapon object was created.
(b) The heap.
(c) The function call stack.
(d) The heap and the function call stack.
(c) The function call stack.