Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS 326 FINAL EXAM 2025 | ALL QUESTIONS AND CORRECT ANSWERS | ALREADY GRADED A+ | VERIFIED, Exams of Programming Languages

CS 326 FINAL EXAM 2025 | ALL QUESTIONS AND CORRECT ANSWERS | ALREADY GRADED A+ | VERIFIED ANSWERS | LATEST EXAM | JUST RELEASED

Typology: Exams

2024/2025

Available from 02/14/2025

rex-smith-1
rex-smith-1 🇺🇸

783 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 326 FINAL EXAM 2025 | ALL QUESTIONS AND
CORRECT ANSWERS | ALREADY GRADED A+ |
VERIFIED ANSWERS | LATEST EXAM | JUST
RELEASED
Generate a Grammar that is Syntactically Valid, but show an input string
that would not type check.
(test question from subprograms slide part 2) ---------CORRECT ANSWER-
----------------e = expression
f = factor
start -> e ';'
e -> e +/- f | f
f -> f */ / atom
| atom
atom -> string
| int
"Bob" / 26 ; //could be valid string if you want it to be
it is syntactically valid but semantically drugs.
What is "auto-boxing" ---------CORRECT ANSWER-----------------Conversion
between primitive types and the corresponding wrapper classes is
automatic.
ie. int a = 10;
Integer b = a; //Auto boxing (int to Integer)
int c = b; // Unboxing (Integer to int)
ie.
Primitive Data Types:
These are the basic data types in Java such as 'int' , 'double', 'char', and
'boolean'. They are not objects and do not belong to any class. They are
stored efficiently and have a lower memory footprint.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download CS 326 FINAL EXAM 2025 | ALL QUESTIONS AND CORRECT ANSWERS | ALREADY GRADED A+ | VERIFIED and more Exams Programming Languages in PDF only on Docsity!

CS 326 FINAL EXAM 2025 | ALL QUESTIONS AND

CORRECT ANSWERS | ALREADY GRADED A+ |

VERIFIED ANSWERS | LATEST EXAM | JUST

RELEASED

Generate a Grammar that is Syntactically Valid, but show an input string that would not type check. (test question from subprograms slide part 2) ---------CORRECT ANSWER- ----------------e = expression f = factor start - > e ';' e - > e +/- f | f f - > f */ / atom | atom atom - > string | int "Bob" / 26 ; //could be valid string if you want it to be it is syntactically valid but semantically drugs. What is "auto-boxing" ---------CORRECT ANSWER-----------------Conversion between primitive types and the corresponding wrapper classes is automatic. ie. int a = 10; Integer b = a; //Auto boxing (int to Integer) int c = b; // Unboxing (Integer to int) ie. Primitive Data Types: These are the basic data types in Java such as 'int' , 'double', 'char', and 'boolean'. They are not objects and do not belong to any class. They are stored efficiently and have a lower memory footprint.

Wrapper Classes: for every primitive data type in Java, there is a corresponding wrapper class, like 'Integer' for 'int', 'Double' for 'double', 'Character' for 'char', and 'Boolean' for 'boolean'. These classes encapsulate the primitive data type in an object allowing them to be used in places where only objects can be utilized. Auto Boxing - is the process where the Java compiler automatically converts the primitive type into its corresponding wrapper class object. For example, when you type int, to an Integer object, Java automatically converts the int to an Integer. Unboxing the reverse process called unboxing, where the object of a wrapper class is converted back to a primitive type What is the potential negative about "auto boxing" in relation to Java? Think int - > Integer (Lec 10.19.23) Subprograms 3 ---------CORRECT ANSWER----------------- Has to be an object because Java weaves it into the bytecode that way. Under the hood, when calling a generic, the generic is called an object type. Thus, it has to be that way or the program does not work - mathematically required. The problem is objects are generally bigger and slower. Instead of using ALU - you have to make an object to put in a variable which all takes time.. size of object + heap + field of actual 32 bits and something to access (getter/setter/caller) which all takes up some space. Generic is called The syntax of a programming language is?'

What are Tokens? ref ch 3 ---------CORRECT ANSWER-----------------Each lexeme group that is represented by a name or token. So, a token of a language is a category of its lexemes. What is a grammar? ref ch 3 ---------CORRECT ANSWER-----------------Grammars are formal language-generation mechanisms. They commonly are used to describe the syntax of a programming language Who created Context-Free Grammars? ref ch 3 ---------CORRECT ANSWER-----------------Noam Chomsky Who created BNF? ref ch 3 ---------CORRECT ANSWER-----------------John Backus and Peter Naur What is BNF? ref ch 3 ---------CORRECT ANSWER-----------------It is a natural notation for describing syntax. What is a meta-language?

ref ch 3 ---------CORRECT ANSWER-----------------A metalanguage is a language that is used to describe another language. BNF is a metalanguage for programming languages. - > = Define the LHS and RHS ref ch3 ---------CORRECT ANSWER-----------------LHS of arrow (->) is the abstraction being defined RHS of arrow (->) is the definition of the LHS. RHS consists of some mixture of tokens, lexemes, and references to other abstractions. (Tokens are also abstractions) In reference to the RHS, what is the definition called? ref ch 3 ---------CORRECT ANSWER-----------------definition is called a rule or production. ie. a rule is just given, the abstraction and obviously must be defined for the definition to be useful. This particular rule specified that the abstraction is defined as an instance of the abstraction , followed by the lexeme " = ", followed by an instance of the abstraction What are abstractions called in BNF description or grammar? ref ch 3 ---------CORRECT ANSWER-----------------non-terminal symbols or simply nonterminals - and the lexemes and tokens of rules are called terminal symbols or simply terminals. Note: BNF description or grammar is a collection of rules.

ref ch 3 ---------CORRECT ANSWER-----------------Attribute grammars are a formal approach both to describing and checking the correctness of the static semantics rules of a program. What are dynamic semantics? ref ch 3 ---------CORRECT ANSWER-----------------Dynamic semantics, which is the meaning of expressions, statements, and program units Synthesized attributes do what? ref ch 3 ---------CORRECT ANSWER-----------------They pass semantic information up the parse tree Inherited attributes do what? ref ch 3 ---------CORRECT ANSWER-----------------They pass semantic information down and across the tree What is a "fully attributed" parse tree? ref ch 3 ---------CORRECT ANSWER-----------------If all the attributes in a parse tree have been computed What are intrinsic attributes? ref ch 3 ---------CORRECT ANSWER-----------------Intrinsic attributes are synthesized attributes of leaf nodes whose values are determined outside the parse tree.

What are operational semantics? ref ch 3 ---------CORRECT ANSWER-----------------To describe the meaning of a statement or program by specifying the effects of running it on a machine. In relation to denotational semantics, what is the domain and what is it also called? ref ch 3 ---------CORRECT ANSWER-----------------The domain is the collection of values that are legitimate parameters to the function. The domain is also called syntactic domain, because it is the syntactic structures that are mapped. In relation to denotational semantics, what is the range and what is it also called? ref ch 3 ---------CORRECT ANSWER-----------------The range is the collection of objects to which the parameters are mapped. The range is called the semantic domain. How is Denotational semantics related to operational semantics? ref ch 3 ---------CORRECT ANSWER-----------------In operational semantics, programming language constructs are translated into simpler programming constructs, which become the basis of the meaning of the construct. In denotational semantics, programming language constructs are mapped to mathematical objects, either sets or more often functions. However, unlike operational semantics, denotational semantics do not model the step-by-step computational processing of programs.

For two adjacent statements, the postcondition of the first serves as the precondition for the second. Developing an axiomatic description or proof of a given program requires that every statement in the program has both a precondition and a postcondition. What is the "weakest precondition"? ref ch 3 ---------CORRECT ANSWER-----------------The weakest precondition is the least restrictive precondition that will guarantee the validity of the associated postcondition. What is an "inference rule"? ref ch 3 ---------CORRECT ANSWER-----------------An inference rule is a method of inferring the truth of one assertion on the basis of the values of other assertions. ie. (S1, S2...Sn)/S this rule states that if S1, S2, ...Sn are true, then the truth of S can be inferred. What is the top part of an inference rule called? What is the bottom? (S1, S2...Sn)/S ref ch3 ---------CORRECT ANSWER-----------------The top part is called the antecedent and the bottom is the consequent.

Define the term "axiom" ref ch 3 ---------CORRECT ANSWER-----------------An Axiom is a logical statement that is assumed to be true. Therefore, an axiom is an inference rule without an antecedent. Let x = E be a general assignment statement and Q be its postcondition. Then, its weakest precondition, P, is defined by the axiom P = Qx→E which means that P is computed as Q with all instances of x replaced by E. For example, if we have the assignment statement and postcondition a = b / 2 - 1 {a < 10} Compute the precondition. ref ch 3 ---------CORRECT ANSWER-----------------First substitute the b/2 - 1 for "a" in the post condition {a < 10 } b / 2 - 1 < 10 b/2 < 11 // 1 is added to both sides b/2 (*2) < 11 *2 //2 is multiplied by both sides b < 22 //ANSWER When will an assignment statement have "side effects"? ref ch 3 ---------CORRECT ANSWER-----------------It only has side effects if it changes some variable other than its target Compute the precondition: x = 2 * y - 3 {x > 25}

Evaluate the pre and post condition below, what is the precondition of the second assignment? y = 3 * x + 1; x = y + 3; {x < 10} ---------CORRECT ANSWER-----------------The precondition of the second assignment is y = 3 * x + 1 3 * x + 1 What is the strategy for a proof? lec slides Axiomatic Semantics PT1 ---------CORRECT ANSWER-------------- ---Strategy for Proof:

  1. Replace the post-condition, with the expression
  2. Solve it
  3. Check if it matches the precondition Evaluate the statement { x > 3 } x = x - 3 { x > 0 } ref lec slides Axiomatic Semantics PT1 ---------CORRECT ANSWER--------- --------{ x > 3 } x = x - 3 { x > 0 }
  4. x - 3 > 0
  5. x > 3
  6. it matches the precondition {x > 3} Use the Rule of Consequences (in axiomatic semantics) to solve this one: { x > 5 } x = x - 3 { x > 0 } Does it hold?

ref lec slides Axiomatic Semantics PT1 ---------CORRECT ANSWER--------- --------{ x > 5 } x = x - 3 { x > 0 }

  1. precondition: x > 5
  2. statement: x = x - 3
  3. Postcondition to prove: x > 0 We substitute the statement into the precondition and see if it implies the post condition x = x - 3 value of x is x - 3 we replace the x with x - 3 in the precondition x > 5 becomes (x - 3) > 5 which simplifies to x - 3 > 5 x > 8 Now check to see if x > 8 implies x > 0. Since 8 is greater than 0. The post condition x > 0 is true whenever the precondition is x > 5 is true, and the statement x = x - 3 is executed. Thus the rules of consequence holds Consider y = 3 * x + 1; x = y + 3; {x < 10} Find the weakest precondition? ref lec slides Axiomatic Semantics PT2 ---------CORRECT ANSWER--------- --------Step 1: y = 3 * x + 1; x = y + 3 {x <10} y + 3 < 10 //substitute {x < 10} here y < 7

while y <> x do y = y + 1 end {y = x} One Iteration: wp(y = y + 1, {y = x}) {y = x - 1} ---------CORRECT ANSWER----------------- Second Iteration: wp(y = y + 1, {y = x - 1} ) = { y + 1 = x - 1 } or { y = x - 2} Third Iteration: wp( y = y + 1, { y = x - 2} ) = { y + 1 = x - 2 } or { y = x - 3} It is obvious that { y < x } will suffice for cases of one or more iterations, combining with { y = x } for the zero iterations case we get { y <= x } which can be used as the loop invariant. The precondition for the while statement can be determined from the loop invariant. For example {y <= x and y <> x} y = y + 1 {y <= x} Applying the assignment axiom to y = y + 1 {y <= x} we get {y + 1 <= x}, which is equivalent to {y < x}, which is implied by {y <= x and y <> x}. So, the earlier statement is proven Describe the subprogram definition. ref ch 9 ---------CORRECT ANSWER-----------------A subprogram definition describes the interface to and actions of the subprogram abstraction. What is a subprogram call?

ref ch 9 ---------CORRECT ANSWER-----------------A subprogram call is the explicit request that a specific subprogram be executed What does it mean for a subprogram to be active? ref ch 9 ---------CORRECT ANSWER-----------------A subprogram is said to be active if, after being called, it has begun execution but not yet completed its execution What are the two fundamental kinds of subprograms? ref ch 9 ---------CORRECT ANSWER-----------------procedures and functions Subprogram side-effects define them lec slides subprogram 3 ---------CORRECT ANSWER-----------------A function side effect is when a callee can modify a parameter passed from the caller (in-out mode or out mode) What does the parameter profile of a subprogram contain? ref ch 9 ---------CORRECT ANSWER-----------------It contains the number, order and types of formal parameters What is a protocol in a subprogram? ref ch 9 ---------CORRECT ANSWER-----------------A protocol of a subprogram is its parameter profile plus, if it is a function, its return type.

ref ch 9 ---------CORRECT ANSWER-----------------A closure is a nested subprogram and its referencing environment, which together allow the subprogram to be called from anywhere in the program. Semantic models of parameter passing are formal parameters characterized by one of the three semantic models, name the three ref ch 9 ---------CORRECT ANSWER-----------------1. they can receive data from the corresponding actual parameter.

  1. they can transmit data to the actual parameter
  2. they can do both The 3 modes are also called ref ch 9 ---------CORRECT ANSWER-----------------in mode out mode in-out mode Define Pass-By-Result ref ch 9 ---------CORRECT ANSWER-----------------Pass-by-result - is an implementation model for out-mode parameters. When a parameter is passed by result, no value is transmitted to the subprogram. The corresponding formal parameter acts as a local variable, but just before control is transferred back to the caller, its value is transmitted back to the caller's actual parameter, which obviously must be a variable. What are the advantages and disadvantages of "Pass-By-Result"? ref ch 9 ---------CORRECT ANSWER-----------------Advantages:

--isolation of function and the caller -- changes made to the parameter inside the function do not affect the actual parameter until the function exits --avoids unintended side-effects -- during function execution since the actual parameter is not directly modified until function completes --Easier to understand flow -- for some programmers its easier to understand the flow of data as changes are only reflected after the function execution making it simpler to trace and debug sometimes. Disadvantages: --- potential for inefficiency -- esp if its a large structure or object -- Overhead on function exit -- since results are copied back to actual parameters at end of the function it can create additional overhead esp if parameters are large --incompatibility with concurrency -- it can lead to inconsistencies or race conditions as the actual parameter updated after function execution Define Pass-by-Value-Result ref ch 9 ---------CORRECT ANSWER-----------------Pass-by-value-result is an implementation model for inout-mode parameters in which actual values are copied. It combines pass-by-value and pass-by-result. (also sometimes called Pass-by-Copy), because the actual parameter is copied to the formal parameter at subprogram entry and then copied back at the subprogram termination. What are the shared disadvantages of pass-by-value-result and pass-by- value and pass-by-result? ref ch 9 ---------CORRECT ANSWER-----------------Disadvantages shared are the requiring of multiple storage for parameters and time for copying values. It also shares the pass-by-result problem associated with the order in which actual parameters are assigned. Define Pass-by-Reference