assignment no. 1 - philadelphia university jordan | home page files/cs/750321pw.pdf ·...

18
1 Philadelphia University Faculty of Information Technology For Students of CS, CIS, SE, ACS Departments Module: Concepts of Programming Languages (750321) Lecturer: Dr. Nadia Y. Yousif Assignment No. 1 Writing: 10% of course total Date Set: Wednesday 30 th November 2005 Date Due in: Monday 12 th December 2005 Submission Notes: Completed work must be handed in to my office (room IT 332) by 15:00 on the due date. After the deadline “zero” will be awarded. You must keep a duplicate copy of your work because it may be needed while the original is being marked. You should hand in: 1- A printed listing of your test programs (for Q1). 2- A brief report to explain your findings about the type compatibility rules (as required in Q1) 3- Your solution of questions 2-4. Objectives: On completion of this assignment, you should be able to: Understand different data types, Understand different storage structures and work with them easily. Design algorithms for new operations for different data structures The Problems: Q.1- Design a set of simple test programs to determine the type compatibility rules of a C++ or Java compiler. Write a report of your finding. Q.2- Consider the following declaration in Pascal language: type cities = ( Amman, Jarash, Irbid, Aqaba); var area: array [cities] of real; population : array [cities] of integer; This declaration allows storing the area and population of the cities in vectors such as A-PDF MERGER DEMO A-PDF MERGER DEMO

Upload: lykhanh

Post on 11-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Philadelphia University Faculty of Information Technology For Students of CS, CIS, SE, ACS Departments Module: Concepts of Programming Languages (750321) Lecturer: Dr. Nadia Y. Yousif

Assignment No. 1

Writing: 10% of course total Date Set: Wednesday 30th November 2005 Date Due in: Monday 12th December 2005 Submission Notes: Completed work must be handed in to my office (room IT 332) by 15:00 on the due date. After the deadline “zero” will be awarded. You must keep a duplicate copy of your work because it may be needed while the original is being marked. You should hand in: 1- A printed listing of your test programs (for Q1). 2- A brief report to explain your findings about the type compatibility rules (as required in Q1) 3- Your solution of questions 2-4. Objectives: On completion of this assignment, you should be able to: • Understand different data types, • Understand different storage structures and work with them easily. • Design algorithms for new operations for different data structures

The Problems: Q.1- Design a set of simple test programs to determine the type compatibility rules of a C++ or Java compiler. Write a report of your finding. Q.2- Consider the following declaration in Pascal language: type cities = ( Amman, Jarash, Irbid, Aqaba); var area: array [cities] of real; population : array [cities] of integer; This declaration allows storing the area and population of the cities in vectors such as

A-PDF MERGER DEMOA-PDF MERGER DEMO

Page 2: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

2

area := (5590.5, 4340.2, 2347.3, 3425.9); population := ( 3, 1, 2, 1 );

- Give the storage representation of the variables area and population. - Restate the above declaration in order to store the same information in a record of vectors.

Give its storage representation. Q.3- Suppose that you have a language that does not support a structured operation (for example, an operation on arrays). Design an add operation that adds two arrays and an assign operation. For example, if A, B, and C are three arrays of equal sizes, then you have to design the add and assign operations so that C = A + B. Q.4- A language designer proposes to extend PASCAL language by allowing recursive type definitions without explicit use of pointers, as in the following type string = record case isnull : boolean of true : ( ) ; false : ( first : char; rest : string ) end; - Compare this definition with the basic string type. - How would a variable of this type be represented?

Page 3: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Philadelphia University Faculty of Information Technology For Students of CS, CIS, SE, ACS Departments Module: Concepts of Programming Languages (750321) Lecturer: Dr. Nadia Y. Yousif

Assignment No. 2 Writing: 5% of course total Date Set: Monday 16th January 2006 Date Due in: Monday 22nd January 2006 Submission Notes: Completed work must be handed in to my office (room IT 332) by 14:00 on the due date. After the deadline “zero” will be awarded. You must keep a duplicate copy of your work because it may be needed while the original is being marked. You should hand in: 1- A printed listing of your programs. 2- A brief report to explain your practice. Objectives: On completion of this assignment, you should be able to: • Understand different control structures and different subprograms handling. • Design programs for new different subprograms and templates.

The Problems: Q.1- Suppose you wish to write a subprogram that prints a heading on a new output page, along with a page number that is 1 in the first activation and that increases by 1 with each subsequent activation. Can this be done without parameters and without reference to nonlocal variables in Pascal? Can it be done in FORTRAN? Can it be done in C++? Q.2- (a) What is a generic function? (b) Write a template function in C++ that searches for a given data in an array of items. Test your function with an array of integer values and an array of character values. Q.3- Consider the following C++ program in which the function factorial is recursive. #include <iostream.h>

int factorial (int n) { if (n <= 1) return 1; else return (n * factorial(n - 1)); //function factorial is called recursively } void main( ) { int value; value = factorial(3); //first call to function factorial cout << “ Result = “ << value << endl; }

(a) By using diagrams, show the activation records on the stack that will be produced on running the above program.

(b) Explain what is wrong with recursive functions. (c) Rewrite the above program by implementing the factorial function without recursion (i.e.,

implementing it iteratively).

Page 4: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

Philadelphia University Faculty of Information Technology For Students of CS, CIS, SE, ACS Departments Module: Concepts of Programming Languages (750321) Lecturer: Dr. Nadia Y. Yousif

Tutorial No. 2 Objectives: This tutorial aims to test your understanding in the concepts of different control structures. Deadline: You should prepare the answers for the following problems on papers and bring them to discuss on next Wednesday 28 / 12 / 2005.

The Problems: Exercise 1 Consider the following Pascal case statement. Rewrite it using only two-way selection. case index –1 of 2, 4: even := even + 1; 1, 3: odd := odd + 1; 0: zero := zero + 1; else error := true; end ………………………………………………………………………………………… Exercise 2 Consider the following ALGOL 60 style for statement: for i := y + 1 step i * j until 3 * j do j := j + 1; Assume that the initial value of j is 1. List the sequence of values for the variable i used, assuming the following semantics:

(a) All expressions are evaluated once at the loop entry. (b) All expressions are evaluated before each iteration. (c) Step expressions are evaluated once at loop entry, and until expressions are

evaluated before each iteration. ………………………………………………………………………………………… Exercise 3 Rewrite the following code segment using a loop structure in the languages (a) Pascal, (b) Ada, (c) C++ or Java: K := (j + 13 ) / 27 Loop: If k > 10 then goto out K := k + 1 I := 3 * k – 1 Goto loop Out:

Page 5: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

Philadelphia University Faculty of Information Technology For Students of CS, CIS, SE, ACS Departments Module: Concepts of Programming Languages (750321) Lecturer: Dr. Nadia Y. Yousif

Tutorial No. 1 Date: Tuesday, 20th March 2007 Date Due: Sunday, 25th March 2007 Objectives: This tutorial aims to test your understanding of the concepts of binding and type checking. Note: You should bring your answers for the following problems with a covering sheet written on it your name, number, Department, Title and number of the course, the title of the tutorial. We will discuss the solution in the lecture hour of the due date.

The Problems: Problem 1: Review Questions (a) What are the design issues for names? (b) What are the advantages and disadvantage of implicit declarations? Problem 2: Declaration/Free/Bound Occurrences Consider the following program in a lexically-scoped C++-like language. Note that the numbers are used to refer to the statement number in your answer. 1: int x = 0; // x1 2: class A { // A1 3: private: 4: int x; // x2 5: public: 6: int f (int y) { // f1 y1 7: return f (x + y); // f2 x3 y2 8: } 9: }; 10: class B : public A { // B1 A2 11: private: 12: int y; // y3 13: public: 14: int g (int y) { // g1 y4 15: A a; // A3 a1 16: return a.f (this->y + y); // a2 f3 y5 y6 17: } 18: }; For each name occurrence (they are identified in the comments to the right by numbers indicting their order of occurrence), tell whether it is a declaration occurrence, a free, or a bound occurrence. If bound, indicate which declaration binds it. To do this, complete the following table:

Page 6: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

FREE : ... DECLARATION : x1, ... BOUND to x1 : none BOUND to .. : ...

... Here is an example of the kind of answer I prefer to do. 1: int h (int i) { // h1 i1 2: i = i + j; // i2 i3 j1 3: }

FREE : j1 DECLARATION : h1, i1 BOUND to h1 : none BOUND to i1 : i2, i3

………………………………………………………………………………………………………… Problem 3: Static/Dynamic Scope Do Question 5.9 in Sebesta 5th edition (p. 214). Hand in your answer. The problem is stated below: Assume the following program was compiled and executed using static scoping rules. What value of x is printed in procedure sub1? Under dynamic scoping rules, what value of x is printed in procedure sub1?

program main; var x : integer; procedure sub1; begin {sub1} writeln('x=',x) end; {sub1} procedure sub2; var x : integer; begin {sub2} x:= 10; sub1 end; {sub2} begin {main} x:= 5; sub2 end. {main}

…………………………………………………………………………………………………….. Problem 4: Dynamic Scope Do Question 5.14 in Sebesta 5th edition (p. 217). Hand in your answer. The problem is stated below: Consider the following program:

program main; var x, y, z: integer; procedure sub1; var a,y,z :integer; begin {sub1} ... end; {sub1} procedure sub2; var a,b,z :integer;

Page 7: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

begin {sub2} ... end; {sub2} procedure sub3; var a,x,w : integer; begin {sub3} ... end; {sub3} begin {main} ... end. {main}

Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the unit where it is declared. a. main calls sub1; sub1 calls sub2; sub2 calls sub3. b. main calls sub1; sub1 calls sub3. c. main calls sub2;sub2 calls sub3;sub3 calls sub1. d. main calls sub3; sub3 calls sub1; e. main calls sub1; sub1 calls sub3; sub3 calls sub2. f. main calls sub3;sub3 calls sub2; sub2 calls sub1. Here is an example of an answer to a similar question: Suppose we have the following delcarations.

procedure sub1; var y,z :integer; begin {sub1} ... end; {sub1} procedure sub2; var x,z :integer; begin {sub2} ... end; {sub2}

If sub1 calls sub2, then what is visible under dynamic scope? There are four variables, one of which is hidden, so the visible variables are: sub1.y sub2.x sub2.z (sub1.z is hidden by sub2.z) If instead sub2 calls sub1, the the visible variables are: sub2.x sub1.y sub1.z (sub2.z is hidden by sub1.z)

Page 8: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Concepts of Programming Language (750321)– Tutorial (1) ========================================================================= Referencing Environments -Def: The referencing environment of a statement is the collection of all names that are visible in the statement -In a static-scoped language, it is the local variables plus all of the visible variables in all of the enclosing scopes -A subprogram is active if its execution has begun but has not yet terminated -In a dynamic-scoped language, the referencing environment is the local variables plus all visible variables in all active subprograms Named Constants -Def: A named constant is a variable that is bound to a value only when it is bound to storage� -Advantages: readability and modifiability -Used to parameterize programs -The binding of values to named constants can be either static (called manifest constants) or dynamic Languages: Pascal: literals only, FORTRAN 90: constant-valued expressions, Ada, C++, and Java: expressions of any kind

Variable Initialization -Def: The binding of a variable to a value at the time it is bound to storage is called initialization -Initialization is often done on the declaration statement e.g., Java int sum = 0; General Information: 1. What is the name of the first programming language designed by an international committee, and not just by a single computer manufacturer? 2. In what country was the language Plankalkül created? 3. What organization sponsored the development of the language Ada? 4. The people who created the C programming language worked for what company? 5. What programming paradigm was invented with the creation of the programming language Prolog? 6. Name another functional programming language besides Lisp. 7. What do the letters BNF stand for? 8. What problem domain was the creator of Lisp interested in? 9.What problem area was COBOL designed to be used for? 10. Name two object-oriented programming languages. 11. One early computer language is described as an “ancestor” of most modern languages, including C, Java, Pascal, Ada, Smalltalk, and Visual Basic. What is that language?

Page 9: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

2

Question – Chapter 1

Q1. List three reasons why it is important to study programming languages.

Q2. Why is the readability of programs an important concern?

Q3. How is readability of a programming language different from its writability

Q4. What are some of the costs involved in using a new programming language?

Questions – Chapter 2 1. Define syntax and semantics. 2.1. Which produces faster program execution, a compiler or a pure interpreter? Why is the other option any good? 2.2. Determine whether each of the following is a property of compilation or interpretation. Put a C for compilation, and I for interpretation.

C or I Property Programs translated into machine code. Bottleneck: Statement decoding. No translation into machine language. Easy implementation. Fast execution. Bottleneck: Difference between memory fetch and CPU speeds. Slow execution.

3.1. (T/ F) The six attributes of a variable are name, alias, type, lifetime, scope and address 3.2. What are the six attributes of a variable? 4. What is an alias? 5.1. Define binding and binding time. 5.2. Define static binding and dynamic binding. 5.3. What are the advantages and disadvantages of dynamic type binding? 5.4. Dynamic type binding is closely related to implicit heap-dynamic variables? Explain this relationship. 6.1. Define static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic variables. What are the advantages and disadvantages of these? 6.2. What is the difference between a static variable and an explicit heap dynamic variable? 7. Define coercion, type error, type checking, and strong typing. 8.1. Define lifetime, scope, static scope and dynamic scope. 8.2. What is the general problem with static scoping? 8.3. What are the advantages and disadvantages of dynamic scoping? 8.4. What is a disadvantage of static scoping? What is a disadvantage of dynamic scoping? 9. What is the referencing environment of a statement? 10. What is the l-value of a variable? What is the r-value? 11. What is the difference between name type compatibility and structure type compatibility? List one advantage of each.

Page 10: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

3

12. What is the difference between the lifetime of a variable and the scope of a variable? Give one example where they are not the same. 13. Name three different possible binding times.

Question Type B: Q1. What value is printed if A is statically typed in the first column and dynamically typed in the second?

int A A = 10 A = A / 4 Print A

Q2. Here is a program written in a Pseudo code. Explain what the result would be printed under static scoping, and what the result would be under dynamic scoping. procedure one ( ) var x;

procedure two ( ) begin

if (x < 5) then print (x + 1);

else print (x - 1);

end

procedure three ( ) var x begin

x = 4; two();

end; begin x = 7; three(); end Q3. Given the following program: procedure main { int a;

procedure mysterious { a = … // variable a is assigned some value } procedure strange { int a; // code for strange }

// code for main } Assume the two call sequences as: i. main calls mysterious, mysterious calls strange ii. main calls strange, strange calls mysterious

Page 11: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

4

Determine where the reference to variable a is declared for each call sequence, under: a.) Static scoping b.) Dynamic scoping Q4. Consider the following Pascal skeleton. Program main; var x, y, z : integer;

procedure sub1; var a, b, y : integer;

procedure sub2; var a, c, y, z : integer; begin {sub2} ... end; {sub2}

begin {sub1} ... end; {sub1} procedure sub3; var a, w, x : integer; begin {sub3} ... end; {sub3}

begin {main} ... end; {main} List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2 and sub3 assuming static scoping is used. Q5. Assume the following program was compiled and executed using static scoping rules. What value of x is printed in procedure sub1? Under dynamic scoping rules, what value of x is printed in procedure sub1? Program main var x : integer;

procedure sub1 beginsub1 writeln('The value of x is : ', x) endsub1

procedure sub2 var x: integer; beginsub2 x := 10; sub1 endsub2

beginmain x:= 5; sub2 endmain

Page 12: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Concepts of Programming Language (750321)– Tutorial (1) ========================================================================= Question – Chapter 1

Q1. List three reasons why it is important to study programming languages.

1. (T/ F) Variable is a data object with a name which is bound to a value permanently during its life time.

2. (T/ F) Compiler languages have fast execution than interpreter languages.

3. (T/ F) Interpreter languages have fast execution than complier languages. 4. (T/ F) Interpreter languages have slow execution than complier languages.

5. (T/ F) Compiler languages have slow execution than interpreter languages.

(5 Marks) Match

Seq Stmt Seq Stmt 1 Type A is the contents of the memory cell or cells associated

with the variable.

B Is a word of programming language that can not be used as user defined name.

2 Location

C Those are bound to heap storage only when they are assigned values.

3 Keyword D Is bound to a value only at the time it is bound to storage

4 Binding E Are not defined in terms of other types. 5 Static

variables F is a word of programming language that is special only

in certain contexts. 6 Local

variable G The collection of the attributes of a variable.

7 Constant H Association 8 Descriptor I Symbolic constant 9 Primitive

data types J Designed to support business system

10 Subrange type

K

11 Enumeration type

L Contiguous subsequence

12 Decimal M Determine the range of values the variable can have. 13 Scope of

program variable

N is the memory address with which it is associated.

14 Float-point O Those that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates.

15 Value P Designed to support scientific notation Is the range of statements in which the variable is

visible.

Page 13: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

2

By using Type inference, F(a: integer): float { a * 2;} F will be of type __________. By using Type inference, F(a): integer { a * 2.5;} F will be of type __________. By using Type inference, F(a) { a * 2.5;} F will be of type __________. By using Type inference, F(a) { a * a;} F will be of type __________. Q. By using C-Syntax

A. (2 Marks) Declare a variable x of type integer and value 5 using explicit declaration, implicit declaration.

B.

Q. For the C-syntax code: int x = 7; float y = 2.7;

a. (1.5 Marks) Assume a language L, use Name type compatibility, can we write x = y; why/ why

not. b. (1.5 Marks) Assume a language L, use structure type compatibility, can we write x = y; why/

why not.

a. (1.5 Marks) For the statement x = y, by using structure type compatibility, the value of x will be _________.

b. (1.5 Marks) For the statement x = y, by using Name type compatibility, the value of x will be _________.

C or I Property Programs translated into machine code. Bottleneck: Statement decoding. No translation into machine language. Easy implementation. Fast execution. Bottleneck: Difference between memory fetch and CPU speeds. Slow execution.

Q. (2 Marks) Assume the language L use static length string, for the following C-Syntax: String S[10]; what is the minimum number of characters S can holds? What is the maximum number of characters S can holds? Q. (2 Marks) Assume the language L use dynamic length string, for the following C-Syntax: String S[10]; what is the minimum number of characters S can holds? What is the maximum number of characters S can holds?

Page 14: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

3

(2 Marks) What is an alias? To have multiple identifiers reference the same address. (2 Marks) Some programming languages are typeless . list an obvious advantage and disadvantage of having no types in a language? 8.2. What is the general problem with static scoping? 8.3. What are the advantages and disadvantages of dynamic scoping? 8.4. What is a disadvantage of static scoping? What is a disadvantage of dynamic scoping? 9. What is the referencing environment of a statement? 10. What is the l-value of a variable? What is the r-value? 13. Name three different possible binding times. Question Type B: Q1. What value is printed if A is statically typed in the first column and dynamically typed in the second?

int A A = 10 A = A / 4 Print A

Q2. Here is a program written in a Pseudo code. Explain what the result would be printed under static scoping, and what the result would be under dynamic scoping. procedure one ( ) var x;

procedure two ( ) begin

if (x < 5) then print (x + 1);

else print (x - 1);

end

procedure three ( ) var x begin

x = 4; two();

end; begin x = 7; three(); end Q3. Given the following program: procedure main { int a;

Page 15: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

4

procedure mysterious { a = … // variable a is assigned some value } procedure strange { int a; // code for strange }

// code for main } Assume the two call sequences as: i. main calls mysterious, mysterious calls strange ii. main calls strange, strange calls mysterious Determine where the reference to variable a is declared for each call sequence, under: a.) Static scoping b.) Dynamic scoping Q5. Assume the following program was compiled and executed using static scoping rules. What value of x is printed in procedure sub1? Under dynamic scoping rules, what value of x is printed in procedure sub1? Program main var x : integer;

procedure sub1 beginsub1 writeln('The value of x is : ', x) endsub1

procedure sub2 var x: integer; beginsub2 x := 10; sub1 endsub2

beginmain x:= 5; sub2 endmain

Page 16: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

Good Luck 1

PPhhiillaaddeellpphhiiaa UUnniivveerrssiittyy Lecturer: Miss. Dead Al-Halabi

========================================================== Concept of programming Language (750321) 1st Homework 2nd Semester 2005- 2006 Date: 18/5/2005 ========================================================== Assignment Weighting: 10% of the module total Date Set: 18/5/2006 Date Due in: 27-18/5/2006 =============================

Submission Notes: Completed work must be handed into my office. Sessions for oral discussion will be held in the class room. After the deadline a nil mark will be awarded. The only exceptions are where you have permission to hand in later or have been ill and can produce appropriate evidence. In both cases a form (available from the Dean Secretary) must be completed and submitted to the module teacher. Objectives: The objectives of this assignment are to evaluate the:

1- Ability to search and find information concerning about language design and evaluation feature 2- Ability to submit a well formed report.

__________________________________________________________________________ Task: Submit a specific programming language report consist of:

Brief history of its creation, what area it support(procedural, functional, object oriented, logic) Describe the syntax and semantic Names, binding, type checking and scoping Data types (primitive, arrays, user define data types…etc) Expressions and assignment statement Statement-level control structure (one and two selection, iteration, looping) Subprograms (syntax, passing parameter methods)

__________________________________________________________________________ Assessment Criteria 40%: Evaluation of the report contents (1st objective) 40%: Evaluation of the structure and organization of the report. (2nd objective) 20%: Oral discussion __________________________________________________________________________ The cover Sheet of the handled work must contain the following items:

1. The Date Due in, The Name and code of the Module, Your Name and Student number, Section Number, Number of the chosen project, Text of the chosen project.

2. Every member in the project group, should write a note on the topics that had covered and written by the member.

FFaaccuullttyy ooff IInnffoorrmmaattiioonn TTeecchhnnoollooggyy DDeeppaarrttmmeenntt ooff CCoommppuutteerr SScciieennccee Homework Paper

Page 17: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Concepts of programming Languages (750321) – Tutorial =========================================================================== Q1.

a) What are the three general characteristic of subprograms? b) What it does it mean for a subprogram to be active? c) (T/ F) a polymorphic subprogram takes parameters of different types on different activations. d) (T/ F) generic subprograms provide a particular kind of polymorphism called ad hoc

polymorphism. e) For the following definition considered as generic

#define Max (a, b) ( (a)> (b) ) ? (a) : (b) In the since that it works for any numeric type. Does the following call Max ( x++, y) support side effect, why?

f) Consider the following program written in C Syntax void fun ( int F, int S) {F+ =F; S+=S;}

void main () {int L[2] = {1, 3}; Fun (L[0}, L[1]);} What are the values of the L array after executing the main, use each of the following passing parameter methods?

a) passed by value? b) passed by reference? c) passed by value result? Q2. Consider the following skeletal C- program:

void A(int x) { int y; ... 2 C(y); ...} void B(float r) { int s, t; ... 1 A(s); ...} void C(int q) { ... 3 } void main() { float p; ... B(p); ...}

The sequence of procedure calls in this program is: main calls B B calls A A calls C

Show the stack and activation record instance contents for the points 1, 2, and 3. Q3.

a) Floating-point types in High-level languages employee a key concept in data abstraction, which is: __________________.

b) (T/ F) in C++, all of the instances for the same class share a single set of member functions, but each instance gets its own set of the class's data members.

c) (T/ F) C++ always contains both hidden and visible entities. d) (T/ F) All heap dynamic objects live until explicitly de-allocated with delete operators. e) Suppose someone designed a stack abstract data type in which the function, top, returned an access

path (or Pointer) rather than returning a copy of the top element. This is not a true data abstraction. Why? Give an example that illustrate the problem.

Page 18: Assignment No. 1 - Philadelphia University Jordan | Home Page Files/cs/750321pw.pdf · 2013-11-05 · 2- A brief report to explain your findings about the type compatibility rules

1

Concepts of Programming Language – 750321 Tutorial – chapter 6: expressions and assignment statements

Q. Consider the following c-syntax: int a =5; int f1() { a= 17; Return 3;} void f2() { a = a + f1():} void main() { F2(); } What is the of "a" if we:

1. allowing function side effect ___________________ 2. disallow function side effect ___________________

Q. Consider the following C program: Int f(int *i) { *I = *I +5; return 4; } Void main(){ Int x =3; x = x+f(&x);} What is the value of x after the assignment statement in main, assuming

a. Operands are evaluated left to right. __________________________ b. Operands are evaluated right to left. __________________________

Coercion is an implicit type conversion that is initiated by the compiler. An arithmetic expression said to be a Mixed-mode expression, is whether an operator can have operands of different types. Q. Consider the following c-syntax Int ; Float b, c, d; …

D = b * a; What is the compiler behavior if

1. The language support mixed-mode expression. 2. The language does not support mixed-mode expression.

A relational expression is an operator that compares the values of its two operands. The value of relation operator is ___________ . Unary assignment statement Write an equivalent compound sequence of statements to give the same result as the following statement that had written in C++: Sum = ++ count;