Docsity

Midterm Exam Solution for Software and Object-Oriented Programming | ECS 040, Exams of Computer Science

Toggle sidebar
bg1
Name: Student Id:
Introduction to Software Development and Object-Oriented
Programming
ECS 40 Summer Session II 2003
Midterm Exam Solutions(50 points)
Time: 50 mins
Open book and open notes
August 20, 2003
1 (21 points)
1. (1 point) Give an example of a ternary operator.
? :
2. (3 points) What are the three ways of passing arguments to a function?
passing by value
passing by reference with pointer argument
passing by reference with reference argument
3. (15 points) Based on Programming Assignment #2, add a function to the class Sentence which overloads
operator() to return a subsentence of words from indices begin through end e.g., A subsentence of sentence
“Alexander Graham Bell invented telephone.” with begin=3 and end=5 is “Bell invented telephone.”
Prototype: const Sentence operator() (int begin, int end)
Usage: s(3, 5);
Assume valid inputs. You may reuse functions already defined in the class Sentence.
const Sentence Sentence:: operator ()(int begin, int end)
{
Sentence s;
for (int i=begin; i <= end; i++)
s+= word[i];
return s;
}
4. (2 points) Why are the functions, operator << and operator >>, declared as friend in the class
Sentence.
If an overloaded operator function is a member of class, by default, the left operand
is the object of the class. But for the operators << and >>, the left operand is a
reference to an object of class istream or ostream.
2(29 points) Please read the code below answer the questions that follow.
The code compiles correctly.
1 // --file "Student.h" begins here --
2 #ifndef STUDENT_H
3 #define STUDENT_H
4
5 #include <iostream>
6 #include <cstring>
1
pf3
pf4
pf5

Partial preview of the text

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

Name: Student Id:

Introduction to Software Development and Object-Oriented

Programming

ECS 40 Summer Session II 2003

Midterm Exam Solutions(50 points)

Time: 50 mins

Open book and open notes

August 20, 2003

1 (21 points)

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

? :

  1. (3 points) What are the three ways of passing arguments to a function?

passing by value passing by reference with pointer argument passing by reference with reference argument

  1. (15 points) Based on Programming Assignment #2, add a function to the class Sentence which overloads operator() to return a subsentence of words from indices begin through end e.g., A subsentence of sentence “Alexander Graham Bell invented telephone.” with begin=3 and end=5 is “Bell invented telephone.” Prototype: const Sentence operator() (int begin, int end) Usage: s(3, 5); Assume valid inputs. You may reuse functions already defined in the class Sentence.

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

  1. (2 points) Why are the functions, operator << and operator >>, declared as friend in the class Sentence.

If an overloaded operator function is a member of class, by default, the left operand is the object of the class. But for the operators << and >>, the left operand is a reference to an object of class istream or ostream.

2 (29 points) Please read the code below answer the questions that follow.

The code compiles correctly.

1 // --file "Student.h" begins here -- 2 #ifndef STUDENT_H 3 #define STUDENT_H 4 5 #include 6 #include

8 using namespace std; 9 10 class Student 11 { 12 public: 13 Student(){ cout << "Inside Student Constructor"<< endl; } 14 Student(char *, int); 15 16 const char * GetName() const; 17 void SetName(char *); 18 int GetId() const; 19 void SetId(int); 20 21 void print() const; 22 23 private: 24 char * name; 25 int studentid; 26 }; 27 #endif 28 29 // --file Student.cpp" begins here 30 #include "student.h" 31 32 Student:: Student(char * n, int id) 33 { 34 name = new char [strlen(n) +1]; 35 name = n; 36 studentid = id; 37 } 38 39 const char *Student:: GetName() const 40 { 41 return name; 42 } 43 44 void Student:: SetName(char * n) 45 { 46 name = new char [strlen(n) +1]; 47 name = n; 48 } 49 50 int Student:: GetId() const 51 { 52 return studentid; 53 } 54 55 void Student::SetId(int id) 56 { 57 studentid= id; 58 } 59 60 void Student :: print() const

117 Student s1("Peter Gallager", 123456789); 118 s1.print(); 119 s1.SetName("Robert Horry"); 120 s1.print(); 121 122 UndergraduateStudent ug1("John Smith", 615182223, ’U’); 123 ug1.print(); 124 cout << ug1.GetId() << endl; 125 126 return 0; 127 }

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

Because these functions do not modify class members.

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

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

  1. (1 point) The constructor on line 13 is the default constructor of class Student.
  2. (2 points) The function on line 13 is a(n) inline function. Give an advantage of such functions.

Reduces function call overhead.

  1. (2 points) The class UndergraduateStudent is a base class whereas class Student is a derived class.
  2. (1 point) Give an advantage of inheritence (e.g., on line 70)

Reduces coding overhead and eases management for the derived class by reusing member definitions of the base class.

  1. (1 point) s1 is an object of class Student.
  2. (2 points) Public members of class Student are public members of class UndergraduateStudent and private members are hidden in class UndergraduateStudent.
  3. (2 points) What is the purpose of the operator new[] (e.g., on lines 34 and 46)?

To allocate memory for character array dynamically.

  1. (2 points) What is the purpose of line 89?

To initialize base class data members.

  1. (4 points) What is the output of the program?

Student Name:Peter Gallager Student id: Student Name:Robert Horry Student id: Student Name:John Smith Student id: Code: U 615182223

  1. (4 points) Remove line 89 and add appropriate code in the Undergraduate class constructor to initiliaze all its data members.

UndergraduateStudent :: UndergraduateStudent(char *n, int id, char cd) { SetName(n); SetId(id); SetCode(cd); }

  1. (2 points) Does the output of the program change? If yes, show the new output.

Student Name:Peter Gallager Student id: Student Name:Robert Horry Student id: Inside Student Constructor Student Name:John Smith Student id: Code: U 615182223

  1. (4 points) Write a makefile for compiling the above five files.

student: Student.o UndergraduateStudent.o main.o g++ -o student Student.o UndergraduateStudent.o main.o

Student.o: Student.cpp Student.h g++ -c Student.cpp

UndergraduateStudent.o: UndergraduateStudent.cpp UndergraduateStudent.h g++ -c UndergraduateStudent.cpp

main.o: main.cpp UndergraduateStudent.h Student.h g++ -c main.cpp

clean: rm Student.o UndergraduateStudent.o main.o student