Docsity

Programming Languages: Final Exam Review Guide, Exams of Programming Languages

Toggle sidebar
bg1
Programming Languages: Final Exam
Review Guide/ Questions and Answers/
2024-2025.
Polymorphism. (Definition. Examples) - Answer: Allows a single body of code to
work with objects of multiple types. It may or may not need run-time type
checking.
For example, integers and floats are implicitly polymorphic since you can add,
subtract, multiply and so on, irrespective of the fact that the types are different.
They're rarely considered as objects in the usual term.
Discrete types and scalar types (Examples) - Answer: Discrete Data Type -
represents items that can be counted. They can be classified as integers, Booleans,
characters, enumerations, or subranges. Must have a clear successor and
predecessor.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Programming Languages: Final Exam Review Guide and more Exams Programming Languages in PDF only on Docsity!

Programming Languages: Final Exam

Review Guide/ Questions and Answers/

Polymorphism. (Definition. Examples) - Answer: Allows a single body of code to work with objects of multiple types. It may or may not need run-time type checking. For example, integers and floats are implicitly polymorphic since you can add, subtract, multiply and so on, irrespective of the fact that the types are different. They're rarely considered as objects in the usual term. Discrete types and scalar types (Examples) - Answer: Discrete Data Type - represents items that can be counted. They can be classified as integers, Booleans, characters, enumerations, or subranges. Must have a clear successor and predecessor.

Scalar Type - It is a simple single numeric value (i.e. 1, 2/3, 3.14, etc.) usually an integer, fixed point, or float, as opposed to an array, structure, object, or complex vector which contain more than one single numeric value. Alignment of struct/record data types. - Answer: • ORTHOGONALITY is a useful goal in the design of a language, particularly its type system. It means that features can be used in any given combination, the combinations all make sense, and the meaning of a given feature is consistent, regardless of the other features with which it is combined.

  • A collection of features is orthogonal if there are no restrictions on the ways in which the features can be combined (analogy to vectors)
  • Pascal is more orthogonal than Fortran, (because it allows arrays of anything, for instance), but it does not permit variant records as arbitrary fields of other records (for instance)
  • Orthogonality is nice primarily because it makes a language easy to understand, easy to use, and easy to reason about Type equivalence and compatibility. - Answer: Type Equivalence - Type Compatibility - A compatible type is one that is either legal for the operator, or is allowed under language rules to be implicitly converted, by compiler-generated code, to a

In the first statement, the right hand side of the statement refers to the value of a, which we wish to place into d. In the second statement, the left hand side refers to the location of a, where we want to put the sum of b and c. •Variables as values vs. variables as references -value-oriented languages •C, Pascal, Ada -reference-oriented languages •most functional languages (Lisp, Scheme, ML) •Clu, Smalltalk -Algol-68 kinda halfway in-between -Java deliberately in-between •built-in types are values •user-defined types are objects - references Name equivalence and structure equivalence. - Answer: Name Equivalence - based on declarations. Two types are the same is they have the same name. Each type declared introduces a new type distinct from all others. Used in Java and Ada. Structural Equivalence - Based on some notion of meaning behind those declarations. Two types are equivalent if they have the same structure. Used in Algol-68, Modula-3, and ML. Depends on simple comparison of type descriptions substitute out all names

  • expand all the way to built-in types

Type conversion and coercion. - Answer: Type Conversion (Explicit) - Converting one type of data to another type. It is also known as type casting. It can be used to pass a value to a function where the parameter is of a different data type or to return a value from a function where the function return type is of a different data type. Implicit - performed whenever one fundamental data type is expected but a different fundamental data type is supplied and the user does not explicitly tell the compiler how to perform this conversion.


Type Coercion (Implicit) - it is the conversion of one type of object to a new object of a different type with similar content. A common example is the coercion of string "5" into an integer 5 or a double 5.0. Coerces an expression to be of the proper type

  • Coercion can be based just on types of operands, or can take into account expected type from surrounding context as well
  • Fortran has lots of coercion, all based on operand type
  • C has lots of coercion, too, but with simpler rules:
  • all floats in expressions become doubles - short intand charbecome intin expressions
  • if necessary, precision is removed when assigning into LHS Type casting in C++. - Answer: • C++ has 4 types of casting:
  • static_cast:
  • Almost equivalent to type conversion in C. No type checking.

Associative Array. (Definitions. Implementations. Challenges. Associative arrays in languages). - Answer: An abstract data type composed of a collection of (key, value) pairs, such that each possible key appears with this data type. It is a non discrete index type. Must be implemented with hash tables or search trees. They resemble the dictionary or map types supported by the standard libraries of many object oriented languages. In C++, they are typically defined by overloading operator[]. C#, like C++, provides extensive facilities for operator overloading but it does not use these facilities to support associative arrays. Instead, the language provides a special indexer mechanism, with its own unique syntax. Most script languages, such as Python, PHP, Javascript support associative array.


They are arrays that can use non-integer type values as indices:

  • example: age["John"] Strings. - Answer: In many languages, a string is simply an array of characters. In other languages, strings have special status, with operations that are not available for arrays of other sorts. They are often special-cased, to give them flexibility (like polymorphism or dynamic sizing) that is not available for arrays in general. It's easier to provide these things for strings than for arrays in general because strings are one-dimensional and (more important) noncircular. Pascal and C/C++ implement strings in different ways. Pascal uses length+array. C/C++ uses null- terminator to indicate the end of a string. Prologue and epilogue of function/procedure calling. - Answer: Maintenance of stack is responsibility of

calling sequence and subroutine prologue and epilogue. space is saved by putting as much in the prologue and epilog as possible. Tasks that must be accomplished on the way into a subroutine include passing parameters, saving the return address, changing that stack pointer to allocate space, saving registers that contain important values and that may be overwritten by the callee, changing the frame pointer to refer to the new frame, and executing initialization code for any objects on the new frame that require it. Time may be saved by putting stuff in the caller instead, where more information may be known.

  • e.g., there may be fewer registers IN USE at the point of call than are used SOMEWHERE in the callee
  • Common strategy is to divide registers into caller-saves and callee-saves sets -caller uses the "callee-saves" registers first -"caller-saves" registers if necessary •Common strategy is to divide registers into caller-saves and callee-saves sets -caller uses the "callee-saves" registers first -"caller-saves" registers if necessary •Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile time -some storage layouts use a separate arguments pointer
  • Caller

proc(&a,b); -or directly passed by reference (C++) void proc(int& x, int y) {x = x + y } proc(a,b); Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value. public void badSwap(int var1, int var2){ int temp = var1; var1 = var2; var2 = temp; } When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well. What is the accessibility rules for C++ inheritance classes for example, class circle:protect shape { ....... } - Answer: C++ allows members of a class to be designated as Public, Private, or Protected. Public members are visible anywhere the class declaration is in scope. Private members are only visible inside the class's method. Protected members are visible inside methods of the class or its descendants.


Visibility rules

  • Public and Private parts of an object declaration/definition
  • 2 reasons to put things in the declaration
  • so programmers can get at them • so the compiler can understand them
  • At the very least the compiler needs to know the size of an object, even though the programmer isn't allowed to get at many or most of the fields (members) that contribute to that size
  • That's why private fields have to be in declaration
  • C++ distinguishes among
  • public class members
  • accessible to anybody
  • protected class members
  • accessible to members of this or derived classes
  • private
  • accessible just to members of this class
  • A C++ structure (struct) is simply a class whose members are public by default
  • C++ base classes can also be public, private, or protected

  • Example: class circle : public shape { ... anybody can convert (assign) a circle* into a shape* class circle : protected shape { ... only members and friends of circle or its derived classes can convert (assign) a circle* into a shape* class circle : private shape { ...
  • Member functions are passed an extra, hidden, initial parameter: this Prolog: a. Write a Prolog program to solve the following problems: i. john is taller than josh ii. josh is taller than kim iii. josh is taller than sam iv. sam is taller than Edward v. Find all people that is taller than sam. b. Write a Prolog program to do simple arithmatics (see slides) c. Write a Prolog program to calculate Fibonacci series. - Answer: a. relations.pl: taller (john, josh). taller (josh, kim). taller (josh, sam). taller (sam, edward). b. start:- sum,nl. sum:- write('X= '),read(X), write('Y= '),read(Y), S is X+Y, write('Sum is '),write(S).

c. fib(0, 0). fib(1, 1). fib(X, Y) :- X > 1, X2 is X - 2, fib(X2, Y2), X1 is X - 1, fib(X1, Y1), Y is Y1 + Y2. Understand vtable and dynamic method binding. - Answer: - They are implemented by creating a dispatch table (vtable) for the class and putting a pointer to that table in the data of the object

  • Objects of a derived class have a different dispatch table
  • In the dispatch table, functions defined in the parent come first, though some of the pointers point to overridden versions
  • You could put the whole dispatch table in the object itself
  • That would save a little time, but potentially waste a lot of space Of course you only have a vtable in C++ if your class has virtual functions
  • That's why you can't do a dynamic_cast on a pointer whose static type doesn't have virtual function Does C++ support multiple inheritance? How about Java? What are potential problems for multiple inheritance? - Answer: • In C++, you can say

c. 2*2 = 4 - Answer: - λ-calculus is a formal system in Mathematical Logic -Natural numbers (0, 1, 2, ....) are defined as functions in λ−calculus.

  • I is defined as: λx.x , the identity function. Note that Ix ≡ x for any x.
  • 0 is defined as: λsz.z ≡ λs.(λz.z) ≡ λs.I • 1 ≡ λsz.s(z)
  • 2 ≡ λsz.s(s(z))
  • 3 ≡ λsz.s(s(s(z)))
  • Successor functions: S ≡ λwyx.y(wyx) Exercise: Prove: 0 ≡ 1. prove: S(n) ≡ n + 1 (λwyx.y(wyx)) ≡λyx.y(λsz.z)yx) ≡λyx.y(x) ≡λyx.y(x) = λsz.s(z)
  • Addition: λxyzw.xz(yzw)
  • Verify that (λxyzw.xz(yzw) 2 3 ≡ 5 ≡λzw.2z(3zw) ≡λzw.2z(λsz.s(s(s(z))).zw) ≡λzw.2z(z(z(z(w))) ≡λzw.(λsz.s(s(z))) \ z(z(z(z(w)))) ≡λzw.(z(z(z(z(z(w))))) ≡λsz.(s(s(s(s(s(w)))))
  • Multiplication: λxyz.z(yz)

a. ≡ (λwyx.y(wyx))(λsz.s(sz)) ≡ (λyx.y((λsz.s(s(z))) ≡ (λyx.y(y(y(x))) ≡ 3 b. ≡ (λwyx.y(wyx))(λsz.s(sz)) ≡ (λyx.y((λsz.s(s(z))) ≡ (λyx.y(y(y(x))) ≡ 3 c. ≡ (λxyz.x(yz))2 2 ≡ (λz.2(2z)) ≡ 4 Scheme Language - Answer: •One of two dialects of LISP language (the other one is Common Lisp) •It is a functional programming language •It was developed at the MIT AI Lab by Guy Steele and Gerald Gussman. •The language is case insensitive •Its theoretical foundation is Lambda-calculus •It uses recursion heavily •It uses prefix convention •There is no iteration in Scheme. You have to use recursion to implement iteration


•"hello" →"hello"

(3) Write code to get the third element of a list. You code must be able to apply to the list '(a b c d e) and return c. (4) Write code to get the list that removes the first two items of the input argument. In other words, you code must be applied to '(a b c d) and return (c d). (5) Write code to define fibonacci function: f(0) = 0, f(1)=1, f(2)=f(1)+f(0), f(3)=f(2)+f(1) ..... (6) Write a function that accept low and high as input and print from low to high (include both low and high). For example, if low is 4, high is 7, your program should print 4, 5, 6, 7. - Answer: (1) (+ 3(*4 (- 5 6))) (2) (define (solve-equation x) (define a (^ 4x + 2 - 3x 6) (display a)) (3) (define (third list) (map (map cadr list)) (4) (define delete (lambda (item list) (cond ((equal? Item (car list) (cdr list))

(else (cons (car list) (delete item (cdr list))))))) (5) (define fibonacci n) (if (< n 2) n (+ fibonacci (- n 1)) (fibonacci (- n 2))))) (6) (define func (low, high) (cond (< low high) (display "low < high") (newline) (do ((nums (list low)) (i 0 (+ i 1))) ((= i low) num) (numlist! num i i)) (display numlist) (newline) (else (display "high > low")))