Organization of Programming Languages

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the output of the following Perl code: $x = 0;$y = 1;$z = 2;if ($z/$y || $z/$x) { print "hello";}else { print "bye";}

"hello"

Variables that are defined inside subprograms are called "______ variables".

"local" or "method"

With respect to language evaluation critieria, select all language characteristics that affect readability.

- Data types - Syntax Design - Orthogonality - Simplicity

What are the three general characteristics of subprograms?

- Each program has a single entry point. - The calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time. - Control always returns to the caller when the subprogram execution terminates.

With respect to language evaluation critieria, select all language characteristics that affect reliability.

- Exception handling - Type checking - Data types - Restricted aliasing - Syntax design - Orthogonality - Support for abstraction - Simplicity - Expressivity

With respect to language evaluation critieria, select all language characteristics that affect writability.

- Expressivity - Syntax Design - Data types - Orthogonality - Support for abstraction - Simplicity

What are the three general methods of implementing a programming language?

- Hybrid (compiled-interpreted) - Interpreted - Compiled

Select all that are constructed languages.

- Loglan - Esperanto - Na'vi - lojban - Klingon

What are three reasons why lexical analysis is separated from syntax analysis? (one word each)

- Simplicity - Efficiency - Portability

Consider the following grammar: S → aScB | A | b A → cA | c B → d | A Which of the following sentences are in the language generated/recognized by this grammar?

- abcd - accc - cd

Consider the following grammar: <S> → <A> a <B> b <A> → <A> b | b <B> → a <B> | a Which of the following strings are in the language generated by this grammar?

- bbaab - baab

What are the three general methods of implementing a programming language?

- compilation - pure interpretation - hybrid implementation

According to your textbook, what are the two distinct kinds of uses for pointers?

Pointers provide some of the power of indirect addressing. Pointers provide a way to manage dynamic storage.

What are the advantages of named constants?

Readability Used to parameterize programs

What is the value of $x after the following after the execution of the following Perl code? $x = 0;$y = 1;$z = 2;($x ? $y : $z) = 3;

0

What is the output of the following C++ code? int i[] = {3,6,9}; cout << (i+1);

0x7f3e5029254c i.e. some hexadecimal memory address

What is the output of the following C++ code? int i[] = {3,6,9}; cout << *&i;

0x7f3e5029254c i.e. some hexadecimal memory address

What is the output of the following Python code? x = 1while(x<10): x += xprint x

16

What is evaluation of this Racket code? (define z 2) (let ((x 3)(y 5)) (+ (* x y) z))

17

What is the value of x after the following C-style code snippet? int y = 2;int z = 4 - y;x = (y == z) ? 3 : 1

3

What is the output of the following C++ code? int i[] = {3,6,9}; cout << (*i)+1;

4

What is the output of the following C++ code? int i[] = {3,6,9}; cout << *i+1;

4

Consider the execution of sub2 when it is called in sub4. Note that subx is a formal parameter of sub4. What the value of x as output by alert(x) in sub2 in the case of shallow binding x = [a] deep binding, x = [b] ad hoc binding x = [c]

4 1 3

What is the value of x after the following guarded if terminates? x := 2 y := 3 if x > y -> x := x + y [] x < y -> x := x + 3 [] x = y -> x := x - y fi

5

What is the output of the following Python code? x = 3y = 4z = 5x,y,z = y,z,xy,x,z = y,z,zx,x,y = z,y,xprint x,y,z

5 3 3

What is the output of the following C++ code? int i[] = {3,6,9}; cout << *(i+1);

6

What is the value of x after the following guarded do loop terminates? x := 3 y := 7 do x > y -> y := y + 1 [] x < y -> x := x + 1 od

7

Consider the following C program: int fun(int *i) {*i += 5;return 4; } int main() {int x = 3;x = x + fun(&x); std::cout << x << std::endl;} What is the value of x displayed in the std::cout line after the assignment statement in main, assuming a. operands are evaluated left to right. [a] b. operands are evaluated right to left. [b]

7 then 12

Write EBNF descriptions (i.e. rules) for the a Java class definition header statement.

<class_head> → {<modifier>} class <class_name> [extends class_name] [implements <interface_name> {, <interface_name>}] <modifier> → public | abstract | final

In C-family for loop syntax, match the elements to their description. for ( A ; B ; C ) D

A - initial value B - Terminal value C - Step size interval D - Statement(s) to be executed

Define static binding and dynamic binding.

A binding is static if it first occurs before run time and remains unchanged throughout program execution A binding is dynamic if it first occurs during execution or can change during execution of the program

What is a closure?

A closure is a subprogram and its referencing environment. Closures are useful in languages that allow nested subprograms, are static scoped, and allow subprograms to be returned from functions and assigned to variables. This is common to Functional Programming.

What is a coroutine?

A coroutine is a special subprogram that has multiple entries. It can be used to provide interleaved execution of subprograms.

What is a block?

A method of creating static scopes inside program units

Reserved word

A special word of a programming language that can- not be used as a name.

Name

A string of characters used to identify some entity in a program.

What is an overloaded subprogram?

An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment.

Although the + addition operator may be chained with multiple operands, it is actually a binary operator. Therefore, to evalute 2+3+5 we must first either (a) add 2+3 and combine the result with 5, or (b) add 3+5 and combine the result with 2.

Associativity

In what language is most of UNIX written?

C

Range-based loops are not possible in which of the following languages?

C

What are the design issues for names?

Case sensitivity Whether special words are reserved words are keywords Special characters Length Limit

Designed the first general purpose computing device, the "difference engine".

Charles Babbage

List five implementation models of parameter passing that have been developed by language designers to guide the implementation of the three basic parameter transmission modes.

Pass-by-Value When a parameter is passed by value, the value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram, thus implementing in-mode semantics. Pass-by-Result Pass-by-result is an implementation model for out-mode parameters. Pass-by-value-result Pass-by-value-result is an implementation model for inout-mode parameters in which actual values are copied. It is in effect a combination of pass-by-value and pass-by-result. The value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable. Pass-by-Reference Pass-by-reference is a second implementation model for inout-mode parameters. Rather than copying data values back and forth, however, as in pass-by- value-result, the pass-by-reference method transmits an access path, usually just an address, to the called subprogram. This provides the access path to the cell storing the actual parameter. Pass-by-Name Pass-by-name is an inout-mode parameter transmission method that does not correspond to a single implementation model. When parameters are passed by name, the actual parameter is, in effect, textually substituted for the corresponding formal parameter in all its occurrences in the subprogram.This method is quite different from those discussed thus far; in which case, formal parameters are bound to actual values or addresses at the time of the subprogram call. A pass-by-name formal parameter is bound to an access method at the time of the subpro- gram call, but the actual binding to a value or an address is delayed until the formal parameter is assigned or referenced. Implementing a pass-by-name parameter requires a subprogram to be passed to the called subprogram to evaluate the address or value of the formal parameter. The referencing environment of the passed subprogram must also be passed. This subprogram/referencing environment is a closure.

The process of placing a memory cell that has been unbound from a variable back into the pool of available memory.

Deallocation

Which of the following is NOT a possible binding time?

Debug time

Which language below allows arrays to have negative integer indices?

Perl Python

Select all that are examples of dynamic type binding.

Perl, Python

The first high-level language, designed by Konrad Zuse.

Plankalkul

The Guarded Command Language was introduced by which famous computer scientist?

Edsger Dijkstra

What is the purpose of the let constructs in functional languages?

Enclosing static scopes (to a specific scope) are called its static ancestors. dynamic ancestors are those that are called to reach the subprogram in question.

What is the primary use of attribute grammars?

Enforcing type compatibility

Which of the following is NOT a Characteristic of Language Evalutation criteria associated with Writability?

Exception handling

Variables that are nameless (abstract) memory cells allocated and deallocated by explicit run-time instructions written by the programmer.

Explicit heap-dynamic variables

A C# switch allow more than one case to be executed.

False

A Language is a collection of syntax rules that describe how to generate valid sentences.

False

In Java, String objects are mutable.

False

In Racket, a cond control statement requires an else option.

False

In Racket, a when control statement permis an else option.

False

In programming languages, the use of keywords is more restrictive than reserved words.

False

Like the if/else statement in Java and C++, the Racket else statement is optional.

False

Python data types are declared explicitly.

False

What is the boolean evalution of the following expressions in PHP? (Note: triple equal sign operator) 2 === 2.0

False

Using the algorithm described in Section 4.4.2, remove direct left recursion form the following grammar rules. X → Xyy | Xz | Yx | w Y → Yy | Yw | zy | x

For the first grammar rule: a1 = yy, a2 = z, b1 = Yx, b2 = w X -> YxX' | wX' X' -> yyX' | zX' | ε For the second grammar rule: a1 = y, a2 = w, b1 = zy, b2 = x Y -> zyY' | xY' Y' -> yY' | wY' | ε Answer: X -> YxX' | wX' X' -> yyX' | zX' | ε Y -> zyY' | xY' Y' -> yY' | wY' | ε Correct Answer: X → YxX' | wX'X' → yyX' | zX' | ε Y -> zyY' | xY'Y' → yY' | wY' | ε

On what branch of mathematics is axiomatic semantics based?

Formal Logic

What are the three distinct semantic models (i.e. transmissions modes) of subprogram formal parameters?

Formal parameters are characterized by one of three distinct semantics models: They can receive data from the corresponding actual parameter; They can transmit data to the actual parameter; or They can do both. These models are called in mode, out mode, and inout mode, respectively.

With respect to subprograms, what are formal parameters?

Formal parameters are the names that subprograms use to refer to the actual parameters given in subprogram calls.

An early programming language whose control statements were based on the IBM 704 harware.

Fortran I

Breifly describe three advantages of LR parsers.

From Textbook p.193 "There are three advantages to LR parsers:" They can be built for all programming languages. They can detect syntax errors as soon as it is possible in a left-to-right scan. The LR class of grammars is a proper superset of the class parsable by LL parsers (for example, many left recursive grammars are LR, but none are LL).

In Perl, and associative array is called a

Hash

What is an alias?

If two variable names can be used to access the same memory location, they are called aliases

What is the name of the category of programming languages whose structure is dictated by the von Neumann computer architecture?

Imperative Programming Languages

Variables bound to heap storage only when they are assigned values.

Implicit heap-dynamic variables

Designed the first widely-used high-level language (Fortran)

John Backus

Designed the hardware architecture used in most modern computers

John von Neuman

Racket is a dialect of which older language?

LISP

At which possible binding time is the Java + operator symbol bound to a memory location?

Language design time

Which of the following is NOT an attribute of a variable?

Length

The time during which the variable is bound to a specific memory location.

Lifetime

Which of the following is NOT a category of variable with respect to storage bindings and lifetime?

Memory locked

Variables can be characterized as a sextuple (6-tuple) of which six attributes?

Name, address, value, type, lifetime, and scope.

What is the potential danger of case-​sensitive names?

Names that look alike are different

Are the unions of Ada always type checked?

No

Are tuples in Python mutable?

No

Which of the following are categories of Dynamic Semantics?

Operational Semantics Axiomatic Semantics Denotational Semantics

What is the scope of a named constant?

Package

What is parametric polymorphism?

Parametric polymorphism is provided by a subprogram that takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram. Different instantiations of such subpro- grams can be given different generic parameters, producing subprograms that take different types of parameters. Parametric definitions of subprograms all behave the same. Parametrically polymorphic subprograms are often called generic subprograms. Ada, C++, Java 5.0+, C# 2005+, and F# provide a kind of compile-time parametric polymorphism.

Which of the following is not a value used by a loop variable in counter-controlled loops?

Recursion value

On what branch of mathematics is denotational semantics based?

Recursive Function Theory

Which one of the following is NOT a reason why lexical analysis is separated from syntax analysis?

Reliability

Unlike the C-family of languages that use curly braces to delineate blocks of code, Python uses _____ to indicate a statement's membership in a block.

indent

In functional programming languages loops are implemented using ______.

induction or recursion

In Perl it is possible to unconditionally exit a loop with which keyword?

last

If the then reserved word or some other syntactic marker is not used to introduce the 'then' clause, the control expression is placed in ______.

parentheses

Which of the following are signed integers in Java?

short long int byte

A programming language is strongly typed if

type errors are always checked

A programming language is strongly typed if

type errors are always detected.

The primary limiting factor in the speed of von Neumann architecture computers is called the [v]

von Neumann bottleneck

A union is a data type

whose variables may store different type values at different times during program execution.

Compute the weakest precondition given the following assignment statement and its postcondition: x = (y + 2) * 3 {x > 6}

x = (y + 2) * 3 {x > 6}

Compute the weakest precondition given the following assignment statement and its postcondition: x = (y + 2) * 3 {x > 6}

x = (y + 2) * 3 {x > 6} 6 < (y + 2) * 3 (divide each side by 3) 2 < y + 2 (subtract 2) 0 < y (done)

Consider the following grammar: S → xSzB | A | y A → zA | z B → w | A Which of the following strings are in the language generated by this grammar?

xzzz xyzw xzzzzw

Convert the following EBNF rules to BNF: S → A{bA} A → a[b]A

S → A | AB B → bA | bAB A → aA | abA

Write a BNF grammar which generates the language described as: "The set of strings with a single x followed by an odd number of y's" You may NOT use the EBNF empty string symbol (ε) to terminate recursion. Your grammar should begin with the start symbol S and avoid left recursion. Your answer will be graded on both correctness and simplicity.

S → xY Y → yyY | y

The first language designed specifically for text and string processing.

SNOBOL

The Readability of a program is affected by which Characteristics of Language Evaluation? (check all that apply)

Simplicity Syntax design

Modify one or more rules of the following grammar so that the + operator has precedence over the * operator. Note: Do not introduce extra terminal symbols, like parentheses, into the grammar. That would change the language, not the operator precedence. S ⟶ X X ⟶ Y + X | Y Y ⟶ Z * Y | Z Z ⟶ 1 | 2 | 3 | 4

Simply transpose the + and * symbols. S ⟶ X X ⟶ Y * X | Y Y ⟶ Z + Y | Z Z ⟶ 1 | 2 | 3 | 4

Variables whose storage bindings are created when their declaration statements are elaborated, but whose types are statically bound.

Stack-dynamic variables

Variables that are bound to memory cells before program execution begins and remain bound to those same memory cells until program execution terminates.

Static variables

Why are named constants used, rather than numbers, for token codes?

Textbook p.170 "Although tokens are usually represented as integer values, for the sake of readability of lexical and syntax analyzers, they are often referenced through named constants."

What are the two distinct goals of syntax analysis?

Textbook p.178 "There are two distinct goals of syntax analysis: "First, the syntax analyzer must check the input program to determine whether it is syntactically correct. When an error is found, the analyzer must produce a diagnostic message and recover. In this case, recovery means it must get back to a normal state and continue its analysis of the input program. This step is required so that the compiler finds as many errors as possible during a single analysis of the input program. If it is not done well, error recovery may create more errors, or at least more error messages. "The second goal of syntax analysis is to produce a complete parse tree, or at least trace the structure of the complete parse tree, for syntactically correct input. The parse tree (or its trace) is used as the basis for translation."

Define lifetime, scope, static scope, and dynamic scope.

The lifetime of a variable is the time during which it is bound to a particular memory cell The scope of a variable is the range of statements over which it is visible Static scope of a variable can be statically determined(prior to execution.) Dynamic scoping is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus, the scope can be determined only at run time

In Java, the else option of an if control statement is bound to _____.

The nearest previoius if.

What two programming language deficiencies were discovered as a result of the research in software development in the 1970s?

The primary programming language deficiencies that were discovered were (a) incompleteness of type checking and (b) inadequacy of control statements (requiring the extensive use of gotos). [p.20]

An algorithm was described to eliminate direct left recursion in a BNF grammar by replacing a left recursive LHS rule with two new LHS rules. X ⟶ Xx | Xy | XY | zY | WY | Wx

The solution works by converting direct left recursion into direct right recursion. α1 = x α2 = y α3 = Y β1 = zY β2 = WY β3 = Wx X → zYX′ | WYX′ | WxX′ X′ → xX′ | yX′ | YX′ | ε

A C++ switch allow more than one case to be executed.

True

A nested if is one way to implement a multiple-way selection statement.

True

A selection statement provides the means of choosing between two or more paths of execution.

True

A tuple is a data type that is similar to a record, except that the elements are not named.

True

In Racket, an if control statement requires an else option.

True

It is possible to use a loop counter variable in a recursive loop.

True

It is possible to use a loop counter variable in a while loop.

True

What is the boolean evaluation of the following C++ expression? 3 < 2 < 1

True

What is the boolean evaluation of this C-style expression in red? int x = 3;int y = 2;if (y++ && y++==x) { ... }

True

For what application area is JavaScript most widely used?

Web Programming

Compute the weakest precondition for each of the following sequences of assignment statements and their postconditions:a) a = 2 * b + 1; b = a - 3 {b < 0} b)a = 3 * (2 * b + a); b = 2 * a - 1 {b > 5}

a) b < 1 b) b > (1 - a) / 2

Perform the pairwise disjointness test for each of the following grammar rules. Your answer should show the FIRST() function for each RHS, and then state whether the LHS rule passes or fails pairwise disjointness by examining all pairs of RHS FIRST()'s. To get you started, the first RHS is: FIRST(aB) = {a} a. A → aB | b | cBB b. B → aB | bA | aBb c. C → aaA | b | caB

a. A → aB | b | cBB FIRST(aB) = {a}FIRST(b) = {b}FIRST(cBB) = {c} FIRST(aB) ∩ FIRST(b) ∩ FIRST(cBB) = Ø {a} ∩ {b} ∩ {c} = Ø Passes b. B → aB | bA | aBb FIRST(aB) = {a}FIRST(bA) = {b}FIRST(aBb) = {a} FIRST(aB) ∩ FIRST(bA) ∩ FIRST(aBb) = {a}{a} ∩ {b} ∩ {a} = {a} Fails c. C → aaA | b | caB FIRST(aaA) = {a}FIRST(b) = {b}FIRST(caB) = {c} FIRST(aaA) ∩ FIRST(b) ∩ FIRST(caB) = Ø{a} ∩ {b} ∩ {c} = Ø Passes

Perform the pairwise disjointness test for each of the following grammar rules. Your answer should show the FIRST() function for each RHS, and then state whether the LHS rule passes or fails pairwise disjointness. a. S → aSb | bAA b. A → b{aB} | a c. B → aB | a

a. S → aSb | bAA FIRST(aSb) = {a}FIRST(bAA) = {b} FIRST(aSb) ∩ FIRST(bAA) = Ø{a} ∩ {b} = Ø Passes b. A → b{aB} | a FIRST(b{aB}) = {b}FIRST(a) = {a} FIRST(b{aB}) ∩ FIRST(a) = Ø{b} ∩ {a} = Ø Passes c. B → aB | a FIRST(aB) = {a}FIRST(a) = {a} FIRST(aB) ∩ FIRST(a) = {a}{a} ∩ {a} = {a} Fails

Using the algorithm described in Section 4.4.2, remove direct left recursion form the following grammar rules. A → Aa | Abc | bc | d

a1 = a, a2 = bc, b1 = bc, b2 = d A -> bcA' | dA' A' -> aA' | bcA' | ε Feedback: A → bcA' | dA' A' → aA' | bcA' | ε

An associative array is an unordered collection of data elements that are indexed by

an equal number of values called keys, which may be strings or numerics.

Compute the weakest precondition for each of the following assignment statements and postconditions: a = 2 * (b - 1) - 1 {a > 0} b = (c + 10) / 3 {b > 6} a = a + 2 * b - 1 {a > 1} x = 2 * y + x - 1 {x > 11}

b > 3/2 c > 8 b > 1 - a/2 y > 6 - x/2

In C++ and Java it is possible to unconditionally exit a loop with which keyword?

break

The values of enumeration constants

can be explicitly assigned any integer literal in the type's definition.

An explicit type conversion is called

casting

In C++ and Java it is possible to skip the remainder of the current iteration (but not exit the loop) with which keyword?

continue

The switch keyword that introduces a clause to handle unrepresented case values in a C++ switch is

default

The binding of more than one variable to the same memory cell.

Aliasing

Describe, in English, the language defined by the following grammar: S → ABC A → aA | a B → bB | b C → cC | c

All strings that contain one or more a's followed by one or more b's followed by one or more c's. The following strings are all in the language: abc abbbbbbbbbbc aaaaaaaaaabbcc aabcc

The process of taking a memory cell from a pool of available memory and binding it to a variable.

Allocation

Variable

An abstraction of a computer memory cell or collection of cells.

A [p] is a program that processes a program immediately before the program is compiled.

[p] - preprocessor

A simple trade-off can be made between compilation cost and execution speed of the compiled code. [x] is the name given to the collection of techniques that compilers may use to decrease the size and/or increase the execution speed of the code they produce.

[x] - optimization

Static array

An array in which the subscript ranges are statically bound and storage allocation is static (done before run time).

Stack-dynamic array

An array in which both the subscript ranges and the storage allocation are dynamically bound at elaboration time.

Heap-dynamic array

An array in which the binding of subscript ranges and storage allocation is dynamic and can change any number of times during the array's lifetime.

Fixed stack-dynamic array

An array in which the subscript ranges are statically bound, but the allocation is done at declaration elaboration time during execution.

With respect to compilation, the syntax analyzer takes the lexical units from the lexical analyzer and uses them to construct hierarchical structures called [p].

[p] - fetch-execute

Fixed heap-dynamic array

An array in which the subscript ranges and the storage binding are both fixed after storage is allocated. Additionally, both the subscript ranges and storage bindings are done when the user program requests them during execution, and the storage is allocated from the heap.

What are the advantages and disadvantages of dynamic scoping?

Advantage: convenience Disadvantages: 1. While a subprogram is executing, its variables are visible to all subprograms it calls 2. Impossible to statically type check 3.Poor readability - it is not possible to statically determine the type of a variable

Keyword

A word of a programming language that is special only in certain contexts.

Convert the following EBNF grammar rules to BNF. Avoid left recursion. Your answer will be graded on both correctness and simplicity. A → B {(x|y) B} B → w[z]

A → BxA | ByA | B B → w | wz

Convert the following BNF rule with three RHSs to an EBNF rule with a single RHS. Note: Conversion to EBNF should remove all explicit recursion and yield a single RHS EBNF rule. A ⟶ B + A | B - A | B

A ⟶ B { (+|-) B }

What are some of the features of Functional Programming Languages? (select all that apply)

Ability to pass functions as arguments to other functions Elimination (or minimization) of side effects The presence of anonymous (unnamed) functions Ability to return a function as a valid return type of a function

What are some characteristics of the Functional Programming paradigm? (check all that apply)

Ability to pass functions as arguments to other functions The presence of anonymous (unnamed) functions Elimination (or minimization) of side effects Ability to return a function as a valid return type of a function

Wrote the first software program

Ada Lovelace

At which possible binding time is a C or C++ static variable bound to a memory location?

Load time

What is the only contemporary languages that fully supports coroutines?

Lua

Which languages do not support coercion in expressions?

ML F#

Which of the following A-rules fail the Pairwise Disjointness Test?

X → xy | Yy Y → yz | xz A → aB | BAb B → aB | b

Are the unions of F# discriminated?

Yes

The very first program was written in 1843 by [a], which was designed to execute on the theoretical hardware platform called the Analytical Engine, designed in 1842 by [b].

[a] - Ada Lovelace [b] - Charles Babbage

Initial implementations of Java were all hybrid. Its intermediate form, called [b], provides portability to any machine that has an interpreter and an associated runtime system.

[b] - byte code

The first successful high-level language for business was [c]

[c] - COBOL

The first successful high-level language for scientific applications was [f]

[f] - Fortran


Set pelajaran terkait

Exam 2- Nervous System Prep/Quiz

View Set

에너지관리기능사 필기 (2011년 1회 기출문제)

View Set

Chapter 3 Developing Management Skills

View Set

Vocabulary Workshop (Grade 4) Unit 12

View Set

Chapter 6 - Bones and Skeletal Tissues

View Set