CMPS 258 Final, Spring 2008
American University of Beirut · Programming Languages · 8 pages
More CMPS 258 files
Show the text of this final exam
American University of Beirut
Faculty of Arts and Science,
Department of Mathematics and Computer Science
CMPS2358 final exam
Programming Languages
Exam duration: 2 hours
Exam guidelines:
a. students must submit this booklet as their answer sheet.
b. students must use ink
c. exam total marks is 51/50
Part J: (12 marks) Tick True (T) or False (F) each question is worth 0.5 mark.
Any object file contains a symbol table
Lazy evaluation is essential to implement functions
The linking process generates an executable file
C++ references are easier to use than C pointers
A symbol table in code.o has a list of the names of all variables declared in code.c
Not all object files become input to the linking process.
The linking process builds a combined symbol table
All pointers to void need to be cast before they can be used to reference an object
The linking process searches library files for undefined symbols
10. The default access modes for C++ structures and C++ classes are different
1].Currying is using partially implemented functions
12.C++ references are used to pass parameters to functions
13. The linking process checks for storage space
14. 'Upcasting’ is a mechanism underlying inheritance in any OO language
15.C ++ structures cannot be used to implement inheritance
16.C++ references are a useful alisasing mechanism
17. Pointers to void are the same size as any other pointer
18, Under some conditions an object file can be executed
19,.C++ references are generally easier to use than C pointers
20. Pointers to void can be used as an information hiding mechanism
21,C++ structures are used less often than C++ classes
22. Type variables in Haskell implement polymorphism
23. ‘Downcasting' allows access of a base class through a pointer to a derived class
24. Any C++ reference requires four bytes of memory
DISBAND TN AAT DTH
|
|
|
|
1.
2.
3.
4,
5.
6.
7,
8.
9.
|
|
|
|
|
|
|
|
|
|
9393949599949 4345395359859549535553584
Part 2: Short questions on C++ (7 marks) only one answer for a multiple choice question must
be chosen. Each question is ] mark
1. C++ uses static binding for a function f of a derived class D if
e f is declared virtual
ef in the base class is virtual
e fis virtual in both, D and the base class of D
e fis not virtual anywhere
2. A function F in a base class B is always overridden by all derived classes. Therefore:
« F should not be in included in the interface of B
« F should be declared as a virtual function in B
e Suggests that B is an abstract class
¢ None of the above
3. Which of these is false about ‘Downcasting?
* Allows access of a derived class using a pointer to a base class.
« Is not an implicit mechanism in C++
* Jsa default feature of pointers to objects in a class hierarchy in C ++
¢ None of the above
4. Consider the following C++ declarations:
int a=I, b=2; /* Tine ] */
int &r_b, &r_a= a, /* line 2 */
int*a_ptr=&r_a; /* line 3*/
e Lines 2 and 3 are faulty.
« Line 2 is only faulty.
« Line 3 is only faulty.
All lines are correct
5. Ifno access specifier is used, the default access mode of members in C++ classes is
6. Which C++ programming mechanism is not used to 1mplement polymorphism:
e Inheritance
e References
« Function overloading
* Operator overloading
7. Given the following inheritance relationships:
class A {... };
class B: public A {... };
class C; public Bf... };
Consider the object declarations:
Aol; /*line ] */
Bo3; /*line 2 */
Cos; /*line 3 */
Which statement is false:
« Line | causes the constructor of A to be called first.
e Line 2 causes the constructor of A to be called first.
e Line 3 causes the constructor of B to be called first
¢ None of the above
{aemican NIVE!
| LIBRARY
|__OF BEIRUT
Part 3: Short answers on Haskell (7 marks)
1.Consider the following function: (7 mark)
my_length :: [a] > Int
my_length list
| rest==Q) = 27?
| otherwise = 1+ my_length rest
where
rest = = tail list
What is missing at ??? in my_length to calculate the jJength of a non-empty list?
2. What is the type of each of the following expressions: (2 marks)
a. ({(5, 6, ‘E’,F”)):0)
b. ({('a", g), ("b", 2)}, ‘c)
3. Consider these functions: (4 marks)
fib :: Int > Int
fib 0 = 1
fib 1 =1
fib n = (fib (n-1)) + (fib (n-2))
sleep (first : rest) = first : (sleep rest)
sleep [] ={]
even x = x ‘mod’ 2 = =
What are the results of the following:
a. fib (-3)
b. map fib [1..4]
c. map sleep [[1..4], [1, 2]]
d. filter even (1, 2, 3, 4, 5] ++ filter (not even) [4, 3, 2, 1]
Libaoo
| OF BEIRUT |
Part 2_C ++ programming (12 marks)
For this question, use the space provided for your answers (and the back of your question sheets if needed).
Consider the following C++ class interfaces that depict a simplified University structure. The university is
organized as a collection of faculties. Each faculty is a collection of departments.
‘ class University { ' ‘sity ; University {
: protected: | private:
: Faculty * faculties [10]; ' int students_to_teachers = 25;
: public: ‘ char * mission_motto;
Faculty * FindFaculty (char * faculty_name}; ' public:
~ University (); } American_University (int, char *); }
: private: | public:
Department * departments [26];
char faculty_name [50];
int free_slot; // this contains the next free slot in departmenis | }
array '
char department_name[50];
int total_majors;
: /* declaration of a university */
public:
Department * FindDept (char * dept_name),;
Department * AddDept (char * dept_name),;
~ Faculty(}; }
: American_Universit: AUB(20, “We grow as we
: serve\n”);
Given the faculty name, FindFaculty retums a pointer to this faculty within an object University.
Given the department name, FindDept returns a pointer to this department within an object faculty.
Given a new department name, AddDept adds this department to the faculty (if possible) and it returns a
pointer to this department in case it succeeds.
a. Implement each of the three above methods (6 marks)
b. Implement the destructors of the classes University and Faculty, (2 marks)
MEMCAN UNIVERSIT
LIBRARY
OF HEIRUT
c. Implement the constructor of the class American_University. (2 marks)
d. Does the class American _University require its own destructor? If yes, implement it; if not, why
not? (2 marks)
ERICAN UNTVER’
LIBRARY
OF BEIRUT
Part 3 Haskell programming (13 marks)
QI. (6 marks) write a function which takes a string and gives you the mirror image of a string. A mirror image of
a string is produced by reversing the string, and by also converting each character in the string as follows: a>? z
bPy CPx
e.g. mirror “abcd”
“woayz”
Your ‘mirror’ function should start here (Use the back of the sheet if you need more space) D>
AMERICAN CNIVERSIT
LIBRARY
OF BLIRUT
Q2. (7 marks) A Haskell function interleave interleaves three strings. The function type looks as follows:
interleave :: String > String D String > String
Examples:
interleave “abcd” ”1234” “ABCD”
“al Ab2Bc3Cd4D”
interleave “abcdefg” “1234” “ABCD”
“al Ab2Bc3Cd4Nefg”
interleave “ab” “1234” “ABC”
“al Ab2B3C4”
The extent of the interleaving is determined by the length of the shortest string. Implement the interleave
function.
End of exam