Docsity

Sample Midterm Exam 1 - Software Object-Oriented Programming | ECS 040, Exams of Computer Science

Partial preview of the text

Download Sample Midterm Exam 1 - Software Object-Oriented Programming | ECS 040 and more Exams Computer Science in PDF only on Docsity!

Software Development and Object-Oriented Programming

ECS 40

Summer Session I 2004

Sample MidTerm Exam I

Total: 50 points

Time: 50 minutes

Open book and open notes

Monday., July 6, 2004

Name:ANSWERS

Student Id:

1 (5 points)

  1. (1 point) Give an example of a ternary operator.

?:

  1. (2 points) Clearly show an example of passing an argument to a function by using a reference?

Sample: int main() { void increment (int &); int x = 2;

increment(x); //function call

return 0; }

void increment(int & y) //function prototype { y++; }

  1. (2 points) Give a reason why returning a reference to a private data member of a class is not recommended.

To maintain the integrity of private data members by preventing them from clients modifying through reference.

48 Student:: Student(char * n, int id) 49 { 50 name = new char [strlen(n) +1]; 51 name = n; 52 studentid = id; 53 count ++; 54 } 55 56 const char *Student:: GetName() const 57 { 58 return name; 59 } 60 61 void Student:: SetName(char * n) 62 { 63 name = new char [strlen(n) +1]; 64 name = n; 65 } 66 67 int Student:: GetId() const 68 { 69 return studentid; 70 } 71 72 void Student::SetId(int id) 73 { 74 studentid= id; 75 } 76 77 Student :: ~Student() 78 { 79 cout << "Destroying " << name << endl; 80 delete [] name; 81 count--; 82 } 83 84 //-File "main.cpp" begins here 85 86 #include "Student.h" 87 88 int main() 89 { 90 91 Student *sPtr = new Student("Phil Mickelson", 987654321); 92 93 sPtr->SetName("Tiger Woods"); 94 95 Student s2; 96 s2.SetName("Ernie Els"); 97 s2.SetId(123454321); 98 99 cout << "Number of Students:" << sPtr->count << endl; 100

101 cout << *sPtr << s2; 102 delete sPtr; 103 sPtr =0; 104 105 cout << "Number of Students:" << Student::count << endl; 106 107 return 0; 108 }

  1. (1 point) Give a reason for including preprocessor directives in the header file (Sentence.h), e.g., lines 2, 3, and 33.

So that even if a programmer includes the header file multiple times, the compiler includes it only once.

  1. (1 point) Give a reason why Get() member functions, e.g., on lines 20 and 21 are declared as const?

Because such functions are not supposed to modify class members.

  1. (1 point) Give a reason why certain members of a class, e.g., on lines 30 and 31 are declared as private members?

To hide the actual implementation; a notion popularly known as information hiding.

  1. (1 point) Give a reason why count has been declared as a static member of the class Student, e.g., on line 26.

So that every object of Student class can access and modify the shared variable count.

  1. (2 points) What is the purpose of the operator new[] (e.g., on lines 50 and 63)?

To allocate memory to the character array dynamically.

  1. (1 point) Name a function used in the above program from header file < cstring >.

strlen()

  1. (1 point) The constructor on lines 14-17 is the default constructor of class Student.
  2. (1 point) The function declared at line 28 in class Student is the destructor of the class Student.

3 Writing Program (15 points)

Based on Programming Assignment #2, define a function in the class Sentence which overloads operator() to return a subsentence consisting of words from index begin through end, e.g., A subsentence of sentence “Alexander Graham Bell invented telephone.” with begin=2 and end=4 is “Bell invented telephone.”

Prototype: const Sentence operator() (int begin, int end) Usage: s(2, 4);

Assume valid inputs. You may reuse the functions already defined for the class Sentence. You must create a Sentence object in your function definition.

const Sentence Sentence:: operator ()(int begin, int end) { Sentence s; for (int i=begin; i <= end; i++) s+= word[i]; return s; }