MidtermA Flashcards
- Which of the following commands would you use to create an object file Student.o
for a Student class (assuming there is a Student.h and a Student.cc file)?
(a) g++ -c Student.cc
(b) g++ -o Student.o Student.cc
(c) g++ -c Student.h
(d) g++ Student.cc Student.h
(a) g++ -c Student.cc
- What would be the output of the following code assuming the user entered a 5?
# include < iostream > # include < string > using namespace std ; void doubleNum ( int num ){ num *= 2; } int main (){ int num ; cout <<" Enter an integer : " ; cin >> num ; doubleNum ( num ); cout << " Your number doubled is : " << num << endl ; }
(a) 2
(b) 5
(c) 10
(d) Error
(b) 5
- Which of these functions could return an int as an output parameter?
(a) int& getValue();
(b) void getValue(int*);
(c) void getValue(int);
(d) int* getValue();
(b) void getValue(int*)
- Assuming there is a Date class, which of these functions would result in the copy
constructor of the Date class being called?
(a) bool beforeDate(Date&);
(b) bool beforeDate(Date*);
(c) bool beforeDate(Date);
(d) None of these.
(c) bool beforeDate(Date);
- What is the output of the following code?
# 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.
(a) Egg ctor Chicken ctor
- If Student* stu is a pointer to a Student object, which of these is the proper way to
assign this pointer to a double pointer output parameter?
(a) void getStudent(Student** student){
*student = stu;
}
(b) void getStudent(Student** student){
student = &stu;
}
(c) void getStudent(Student** student){
&student = *stu;
}
(d) void getStudent(Student** student){
**student = *stu;
}
(a) void getStudent(Student** student){
*student = stu;
}
- 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;
- Which of these variables could be used to refer to a dynamically allocated array of
Student objects?
(a) Student** studentArray;
(b) Student* studentArray;
(c) Student& studentArray;
(d) Student studentArray;
(e) None of these.
(b) 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) 2
(b) 1
(c) 3
(d) 4
(e) 10
(f) None of these.
(a) 2
- Which of the following is NOT an example of an object category?
(a) Entity objects
(b) Control objects
(c) Boundary objects
(d) Dynamically allocated objects
(d) Dynamically allocated objects
- Which of these is NOT shown in a UML diagram? (select all that apply)
(a) Constructors
(b) Collection classes
(c) Member variables
(d) Private functions
(a) Constructors
(b) Collection classes
- 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) bool lessThan(float pay) {
return this->pay < pay;
}
(d) Employee (Employee& e){
pay = e.pay;
}
(a) float getPay(){
return pay;
}
(c) bool lessThan(float pay) {
return this->pay < pay;
}
- Which of these are valid ways to initialize the nextId variable in the Employee class
above (select all that apply)?
class Employee { private : static int nextId ; }
(a) static int nextId = 100;
(b) static int Employee::nextId = 100;
(c) Employee::Employee(int nextId): nextId(nextId)
(d) int Employee::nextId = 100;
(d) int Employee::nextId = 100;
- 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 : Weapon ( int damage ){ this - > damage = damage ;} private : int damage ; }; class Character { public : Character ( const string & n , int h , int damage ){ name = n ; health = h ; weapon = new Weapon ( 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) Both the heap and the function call stack.
(b) The heap.