"Computer Programming II", Mid-term Exam, Spring 2013 Student ID

advertisement
"Computer Programming II", Mid-term Exam, Spring 2013
Student ID________Name__________Score ___________
Note: please write down your answers on the answer page!
1. MULTIPLE CHOICE (45 points).
Choose the alternative(s) that complete the statements or answer the questions.
1) Which of the following statements are correct?
A) Circle pObject = Circle();
B) Circle *pObject = new Circle();
C) Circle *pObject = new Circle;
D) Circle pObject = new Circle();
2) Analyze the following code:
#include <iostream>
int main(){
using namespace std;
A a;
class A{
a.print();
public:
}
int s;
A(int newS){ s = newS; }
void print(){ cout << s; }
};
A) The program compiles and runs fine and prints nothing.
B) The program has a compilation error because class A does not have a default constructor.
C) The program has a compilation error because class A is not a public class.
D) The program would compile and run if you change A a to A a(5).
3) Show the output of the following code:
class A{
int main(){
public:
A a;
int x;
A *p1 = &a;
int y;
a.x = 2;
int z;
A a1;
A(): x(1), y(2), z(3){
p1 = &a1;
}
cout << p1->x << " " << (*p1).y << " " << p1->z;
};
return 0;
}
A)1 1 1 B) 1 1 2 C) 3 3 3 D) 2 2 2 E) 1 2 3
4) When invoking a function with a reference object parameter, ________ is passed.
A) the reference of the object
B) a copy of the object
C) the object is copied, then the reference of the copied object
D) the contents of the object
5) Which of the following statements are true about an immutable object?
A) All properties of an immutable object must be private.
1
B) The contents of an immutable object cannot be modified.
C) An object type property in an immutable object must also be immutable.
D) An immutable object contains no mutator functions.
6) Variables that are shared by every instances of a class are ________.
A) public variables B) instance variables C) private variables D) static variables
7) Which of the following statements are true?
A) By default, the copy constructor performs a shallow copy.
B) By default, the copy constructor simply copies each data field in one object to its counterpart in the other object.
C) The copy constructor can be used to create an object initialized with another object’s data.
D) Every class has a copy constructor with the signature ClassName(ClassName &).
8) Suppose class X is derived from Y and both X and Y have no-arg constructors. To invoke Y's constructor from X, use
________.
A) X(): { Y(); ... }
B) X(): Y() { ... }
C) Y(): X() { ... }
D) Y(): { X(); ... }
9) What is wrong in the following code?
class Fruit{
class Apple: public Fruit{
public:
public:
Fruit(int id) { }
Apple() { }
};
};
A) The program will compile if you add a no-arg constructor for Fruit.
B) The program has a syntax error because Fruit does not have a no-arg constructor.
C) The program will compile if you replace Apple() by Apple(): Fruit(4).
D) The program will compile if you delete the constructor in Fruit.
10) Which of the following statements are true?
A) To redefine a function, the function must be defined in the derived class using the same signature and return
type as in its base class.
B) It is a compilation error if two functions differ only in return type.
C) Overloading a function is to provide more than one function with the same name but with different signatures to
distinguish them.
D) A private function cannot be redefined. If a function defined in a derived class is private in its base class, the two
functions are completely unrelated.
11) Suppose you declared GeometricObject *p = &object. To cast p to Circle, use ________.
A) Circle *p1 = dynamic_cast<Circle*>(p);
B) Circle *p1 = dynamic_cast<Circle>(p);
C) Circle p1 = dynamic_cast<Circle>(p);
D) Circle p1 = dynamic_cast<Circle*>(p);
12) To add Rational objects r1 to r2, use ________.
A) r2 += r1;
B) r2 = r2.add(r1);
C) r2 = r1.operator+=(r2);
D) r2 = r2.operator+=(r1);
2
13) Which of the following statements are correct?
A) The try block contains the code that are executed in normal circumstances.
B) You can throw a value of any type.
C) The execution of a throw statement is called throwing an exception.
D) The catch block contains the code that is executed when an exception occurs.
14) Which of the following statements are true?
A) A custom exception class must always be derived from class runtime_error.
B) A custom exception class must always be derived from a derived class of class exception.
C) A custom exception class must always be derived from class exception.
D) A custom exception class is just like a regular class.
15) Which of the following statements are true?
A) The statement Circle circle = Circle(5) creates a Circle object with radius 5 and copies its contents to circle.
B) Circle circle = Circle() should be replaced by Circle circle.
C) The statement Circle circle = Circle() creates a Circle object using the no-arg constructor and copies its contents to
circle.
D) Circle circle = Circle(5) should be replaced by Circle circle(5).
2. PRINTOUT ANALYSIS (20 points). What is the printout of the following code?
1) What is the printout of the following code?
#include <iostream>
void increment(Count c, int times) {
using namespace std;
c.count++; times++;
class Count {
}
public:
int main() {
int count;
Count myCount;
Count(int c) { count = c; }
int times = 0;
Count() { count = 0;}
for (int i = 0; i < 100; i++)
};
increment(myCount, times);
cout << "myCount.count is " << myCount.count;
cout << " times is " << times;
return 0;
}
2) What is the output of the following code?
#include <iostream>
using namespace std;
class B {
public:
~B() {
cout << "B";
}
};
class A: public B {
public:
~A(){
cout << "A";
}
};
int main() {
A a;
return 0;
}
3
3) What is the printout of the following code?
#include <iostream>
#include <string>
using namespace std;
class C {
public:
virtual string toString() { return "C";}
};
class B: public C {
string toString(){ return "B"; }
};
class A: public B {
string toString() { return "A"; }
};
void displayObject(C *p) {
cout << p->toString();
}
int main() {
displayObject(&A());
displayObject(&B());
displayObject(&C());
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class C {
public:
virtual string toString() { return "C"; }
};
class B: public C {
string toString() {return "B"; }
};
class A: public B {
string toString() { return "A"; }
};
void displayObject(C p) {
cout << p.toString();
}
int main() {
displayObject(A());
displayObject(B());
displayObject(C());
return 0;
}
4) If you enter 1 and 0, what is the output of the following code?
#include <iostream>
try {
using namespace std;
if (number2 == 0) throw number1;
int main(){
cout << number1 << " / " << number2 << " is "
// Read two intergers
<< (number1 / number2) << endl;
cout << "Enter two integers: ";
cout << "stat 1" << endl;
int number1, number2;
} catch (int e) {
cin >> number1 >> number2;
cout << "stat 2" << endl;
}
cout << "stat 3" << endl;
return 0;
}
3. SHORT ANSWER (35 points).
answers the question.
1)
2)
3)
4)
5)
6)
7)
Write the word or phrase that best completes each statement or
What’s the major difference between an instance variable and a static variable?
Please compare shallow copy and deep copy.
Is it good to declare all the classes in a program as friends each other? Why?
What is the major difference between overloading and overriding?
What is “abstract class”? Why do you need it?
What is the difference between dynamic casting and static casting?
What is “the rule of three”?
4
Student ID
________Name__________Score ___________
Answers:
5
1.
1) ABC 2) B D 3) E 4) A 5) A, B, C, D
6) D 7) A, B, C, D 8) B 9) A, B, C, D 10) A, B, C, D
11) A 12) A, B, C, D 13) A, B, C, D 14) D 15) A, B, C, D
2.
1)
2)
3)
4)
myCount.count is 0 times is 0
AB
ABC CCC
stat 2
stat 3
3.
1) What’s the major difference between an instance variable and a static variable?
 Associated with instance vs. class
 Location: static region vs. dynamic region
 Life time: global vs. local
2) Please compare shallow copy and deep copy.
 Memberwise copy vs. copy with creation of reference target
 Used for copy constructor, “=”: shallow copy is the default way; deep copy is customized way by users
3) Is it good to declare all the classes in a program as friends each other? Why?
 Not good.
 This will destroy the encapsulation of data.
4) What is the major difference between overloading and overriding?
 Overloading: function versions to handle different parameters
 Overriding : redefining a virtual function in a derived class, especially for polymorphism.
5) What is “abstract class”? Why do you need it?
 Classes with pure virtual function.
 Used to be derived and can’t not be directly used to create an object.
 Used to define the behaviors of derived classes that can’t be implemented in the base class.
6) What is the difference between dynamic casting and static casting?
 The moment to do: compiling vs. running
 The effect: convert rigidly vs. flexibly (check the real data type)
 Target: simple data vs. object
7) What is “the rule of three”?
 The overloading of three functions: copy constructor, destructor and “=”
 Usually atomically (One or none) overloaded
6
Download