





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Programming Languages finals exam
Typology: Exams
Uploaded on 05/14/2023
10 documents
1 / 9
This page cannot be seen from the preview
Don't miss anything!






American University of Beirut
Fall 2005-
Instructor: Chiraz Ben Abdelkader
float C[3] = {0.1,0.2,0.3}; int i; void foo(float x) { i++; x = 0.7; C[1] = 0.5; i--; }
void main() { for (i=0; i<3; i++) foo(C[i]); printf(“%d, %d, %d, %d \n”, C[0], C[2], C[3]); }
(a) What is the output if function parameters are passed by value?
(b) What is the output if function parameters are passed by reference?
(c) What is the output if function parameters are passed by value-result?
#define N 100000 _/ this is how constants are defined in C /_ int A[N]; void Initialize() { int i; for (i=0; i<N; i++) A[i] = 1; } int main() { float *x; Init(); }
Program 1
‘(QUOTE QUOTE) =>
‘QUOTE =>
((if ‘A CAR CDR) (list A B)) =>
((CAR ‘(+ -)) 5 2) =>
((CAR (list / *)) 5 2) =>
(define foo(x,y)
( (lambda(a b) (if (< a b) a b)) (- (* x x 7) 10) (* y y y) ) )
(foo -1 5) =>
(map list? ‘( (A B C) D 3 (()) ) ) =>
(map (lambda (z) (floor (/ z 5))) (list (+ 26 3) -1) ) =>
(define foo(i)
(cond ( (not (integer? i) ‘() )
( (< i 0) ‘() )
( else (cons i (foo (- i 1))) ) ))
(a) Describe in one sentence what this function computes in general.
(b) What is the value of this s-expression: (foo 7) =>
Note: You can check whether an element is a number using the function number?
Hint: you may assume you have a function last that returns the last element of a list, and a function reverse that returns the elements of a list in reverse order.
Hint: you might want to start by writing a helper function that is called in remove.
class Dog: public Pet { public: void Add() { “Adding a Dog” << endl; } void printinfo() { cout << ID << ": a dog of type " << breed << endl; } };
void main() { Pet *c, *d; c = new Cat(98182,“Siamese”); d = new Dog(10293,”Labrador”); c->Add(); d->Add(); c->printinfo(); d->printinfo(); }
class A { public: void f1(); virtual void f2(); void f3(); };
class B : public A { public: void f2(); void f3(); protected: void f4(); };
Indicate in the table below which methods are visible to objects a , b , c, and d in main when: (a) they are accessed directly, and (b) they are accessed through the pointer pA. Use the scope operator to indicate the class of each method; for example A::f1().
Object a Object b Object c Object d
(a)
(b)
class C: public B { public: void f1(); void f2(); }; class D: private B { public: void f1(); void f2(int, float); friend class C; };
void main() { A a; B b; C c; D d; A *pA; ... }
Be careful to type of inheritance
(i) Programmer mistyped a keyword, for e.g. esle instead of else.
(ii) Programmer mistyped if(x=y) instead of if(x==y)
(iii) Programmer mistyped if(x <> y) instead of if(x < y)
(iv) Programmer forgot to initialize a variable to zero before using it.
(v) Program contains an infinite loop.
(vi) Programmer forgot the right quotation mark of a string, such as:
char s[] = “Hello;