CMPS 258 Final, Fall 2004

American University of Beirut · Programming Languages · 14 pages

All CMPS 258 files
CMPS 258 Final, Fall 2004, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 258 Final, Fall 2004, page 2 CMPS 258 Final, Fall 2004, page 3 CMPS 258 Final, Fall 2004, page 4 CMPS 258 Final, Fall 2004, page 5 CMPS 258 Final, Fall 2004, page 6 CMPS 258 Final, Fall 2004, page 7 CMPS 258 Final, Fall 2004, page 8 CMPS 258 Final, Fall 2004, page 9 CMPS 258 Final, Fall 2004, page 10

Showing the first 10 of 14 pages. Download the PDF to see all of it.

More CMPS 258 files

Show the text of this final exam
—eaareemnentne fas: \ Co VERSIT LIBRARY OP EXIRUT January 25, 2005 CMPS 258 Final Exam CMPS 258 Final Exam January, 25, 2005 Student Name: Student ID: Signature: Section: There are forteen pages, including this one. The test is out of 100 marks, and the value of each question is provided. Please use this information to manage your time effectively. Question 1:___—/10 Question 2:__/05 Question 3:___/15 Question 4:____/05 Question 5:___/10 Question 6:______/05 | Question 7:__/05 Question 8:__/05 Question 9:___/05 Question 10:___/05 Question 11:_____/10 Question 12:____—/10 Question 13:____—/10 Total: /100 Instructor: Marcel R. karam 1/14 January 25, 2005 CMPS 258 Final Exam Question |: imperative and Others [10 marks] A) What is the difference between the Von Neuman machine and the logical declarative lan- guages, when it comes to assignment statements? Explain. [2 marks]. B) The four main language design criteria are: Readability; Writability, Reliability; and Cost. Based on the languages we have studied, give a specific example where two criteria work against each other, and two examples of languages which chose to favor opposite criteria. Use a short code example if it will help support your answer. (In other words, say “In Language X and Lan- guage Y, the “foo” construct creates tension between Criterion A and Criterion B. Language X chose to favor A at the detriment of B, while Y favors B over A. Here's some code demonstrat- ing this.”) There are many examples you could cite here--pick a strong example. [6 marks] C) What are the six main attributes of variables? [2 marks] Instructor: Marcel R. karam January 25, 2005 CMPS 258 Final Exam Question 2: Grammar and parsing [5 marks] A) Here is a somewhat peculiar expression grammar: <exp> -> <factor> * <exp> | <factor> / <exp> | <factor> <factor> -> <var> + <factor> | <var> - <factor> | <var> <var> -> Aj BICI|OD Show a parse tree for the expression: A*B-C-D Instructor: Marcel R. karam January 25, 2005 CMPS 258 Final Exam Question 3: Program Execution [15 marks] A) The following prograph code depicts a number to the right of each operation in function “main”. Assuming that the execution order starts at (7/), and sequentially ends at (7/2), what is the output of the following program? [5 marks] CLL | aList {100 100 100) CLL B) What is the output of the following visual code? [5 marks]. Zask 4 A fossume 3is entered continue on fakure Instructor: Marcel R. karam January 25, 2005 CMPS 258 Final Exam C) What is the output of the following visual code? [5 marks] 2 Question 4: Features of Visual Dataflow Languages [5 marks] A) Discuss the possibility of creating a dataflow language that is textual rather than visual. Describe how you would go about doing this. [4 marks] B) It is said that visual dataflow languages help programmers create a mental image of the pro- gram execution thread. True or False? [1 mark] Instructor: Marcel R. karam 5/14 January 25, 2005 CMPS 258 Final Exam Question 5: Inheritance, Substitutability and Polymorphism [10 marks] A) Describe briefly two problems with multiple inheritance in C++. What is the role of virtual inheritance in C++, and what problem does it solve. Give a C++ example to demonstrate. Write code that makes sense. [4 marks] B) Advanced features in C++ allow the following declarations in structures. This code does not compile. What would be the problem? [3 marks] Note that you need to discuss the concept behind lines 4, 5, and 6 in details for more marks. For each problem in the code suggest a solu- tion. #include <iostream> using namespace std; struct S { Instructor: Marcel R. karam 6/14 January 25, 2005 CMPS 258 Final Exam virtual void f1() {cout << "fl() in S!" << endl; } class C: private S { void f1() { cout << "f1() in Cc!" << endl; } int i; Me 1- int main() 2- { 3- Cc; 4- S$ *sp = 4c; 5- sp->f1(); 6- sp->i = 10; 7- return 0; } C) Give a C++ example to illustrate the concept of (Class instance Record) (CIR) and their vir- tual tables or Vtables. Draw the necessary graphical representation and the C++ code to illus- trate the concept of substitutability, and it’s effect on the Virtual table pointer. [3 marks] Instructor: Marcel R. karam TA4 January 25, 2005 CMPS 258 Final Exam Question 6: General OO [5 marks] A) Discuss briefly the advice that “all objects initialized by a constructor must be explicitly deleted somewhere”. [3 marks] B) What is the difference between a struct and a class in C++? [2 marks] Question 7: Template metaprogramming [5 marks] In the following code, the compiler attempts to instantiate Factorial<6>, it finds it must also instantiate Factorial<5>, which requires Factorial<4>, and so on. Eventually the recursion ends with the specialization Factorial<!>, and the computation unwinds. Eventually, Factorial<6>: :val is replaced by the integral constant 720, and compilation ends. Since all the computation is done by the compiler, the values involved must be compile-time constants, hence the use of enum. When the program runs, the only work left to do is print that constant followed by a new line. Discuss the advantages and disadvantages of such programming approach. #include <iostream> using namespace std; template <int n> struct Factorial { enum { val = Factorial<n-1>::val * n }; MF template <> struct Factorial<0> { enum { val = 1}; i int main() { cout << Factorial<6>::val << endl; // 720 return 0; } Instructor: Marcel R. karam January 25, 2005 CMPS 258 Final Exam Question 8: Private Data Members Scope [5 marks] What is the output of the following code? And how was the concept of private data violated? class A { int X; static int Y; public: A(int x): X(x) {} int& foo() {return X;} inté bar() {return Y;} void foobar(A x) {cout << x.X << endl; cout << x.Y << endl; } ye int A::Y = 10; int main (void) { A a(5); int &6i = a.foo(); itt; a. foobar (a); int &j = a.bar(); jtt; a. foobar (a); return 0; Instructor: Marcel R. karam 9/14 January 25, 2005 CMPS 258 Final Exam Question 9: Inheritance in C++ [5 marks] what is the output of the following code?. What would happen and why, when we replace the line 20 with the line number 21. i.e remove line 20 and replace it with line 21. #include <iostream> using namespace std; //introduces namespace std class Base { public: Base(){ pub = 1; pro int pub; protected: int pro; private: int pri; Ul 2; cout << pub <<" " << pro << endl; } Me class Derived: private Base{ public: Derived() {pub=10; pro=20; cout<<pub<<" "<< pro << endl; } Me class Derived_1: private Derived { public: Derived _1() {pub = 10; cout << pub << endl; } // line 20 // Derived_1() {} replaces the constructor. // line 21 hi int main( void ) { Base b; Derived d; Derived_1l d_l; return 0; } Instructor: Marcel R. karam 10/14