0% found this document useful (0 votes)
35 views4 pages

Programming Languages Final Exam Guide

The document is a final exam simulation for programming languages, containing various types of questions including explanations, drawings, fill-in-the-blanks, and true/false questions. Key concepts covered include referential transparency, pass-by-value vs. pass-by-reference, runtime stack structure, activation records, and control flow statements. Each question is accompanied by correct answers and notes for clarification.

Uploaded by

82dn66qxff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Programming Languages Final Exam Guide

The document is a final exam simulation for programming languages, containing various types of questions including explanations, drawings, fill-in-the-blanks, and true/false questions. Key concepts covered include referential transparency, pass-by-value vs. pass-by-reference, runtime stack structure, activation records, and control flow statements. Each question is accompanied by correct answers and notes for clarification.

Uploaded by

82dn66qxff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming Languages - Final Exam

Simulation (with Answers)


A. Explanation Questions (2 Questions)
1. Explain the concept of Referential Transparency. Give an example.
✅ Answer: Referential transparency means an expression always returns the same
value for the same input and has no side effects.
📝 Note: Enables easier reasoning about program behavior.
📘 Chapter: 7 – Final %70

2. Describe the difference between pass-by-value and pass-by-reference.


✅ Answer: Pass-by-value sends a copy of the variable, while pass-by-reference sends
the actual variable address allowing modification.
📝 Note: Reference passing changes original value; value passing does not.
📘 Chapter: 9 – Final %70

B. Drawing Questions (2 Questions)


3. Draw the runtime stack if the function call order is: main → process() → compute() →
print()
✅ Answer:
Top of Stack
- print Activation Record
- compute Activation Record
- process Activation Record
- main Activation Record
Bottom of Stack
📝 Note: Stack operates in LIFO order; last called is on top.
📘 Chapter: 10 – Final %70

4. For the following function, draw the activation record layout:


void sub(float total, int part) {
int list[5];
float sum;
}
✅ Answer:
- sum
- list[4] to list[0]
- part
- total
- dynamic link
- return address
📝 Note: Local variables go on top, then parameters, then control info.
📘 Chapter: 10 – Final %70

C. Fill in the Blanks (10 Questions)


5. Short-circuit evaluation is used with _________ and _________ operators.
✅ Answer: && and ||
📝 Note: Stops evaluation early.
📘 Chapter: 7

6. The method of evaluating expressions without changing variable state is called


________.
✅ Answer: Referential Transparency
📝 Note: No side effects.
📘 Chapter: 7

7. In C, assigning a float to an int involves ________.


✅ Answer: Coercion
📝 Note: Type conversion with possible truncation.
📘 Chapter: 7

8. if-else and switch statements are types of ________ statements.


✅ Answer: Selection
📝 Note: Control flow choice.
📘 Chapter: 8

9. The runtime memory area for function calls is the ________.


✅ Answer: Stack
📝 Note: Holds activation records.
📘 Chapter: 10

10. Pass-by-________ allows a function to modify the caller’s variable.


✅ Answer: Reference
📝 Note: Uses variable address.
📘 Chapter: 9

11. A CFG defines the ________ of a programming language.


✅ Answer: Syntax
📝 Note: CFG = Context-Free Grammar.
📘 Chapter: 3 – Vize %30
12. { } in EBNF means zero or more ________.
✅ Answer: Repetitions
📝 Note: Loop-like grammar.
📘 Chapter: 3 – Vize %30

13. An activation record includes parameters and ________.


✅ Answer: Local Variables
📝 Note: Temporary and fixed data.
📘 Chapter: 10

14. The operator == has ________ precedence than +.


✅ Answer: Lower
📝 Note: Comparison after addition.
📘 Chapter: 7

D. True / False Questions (10 Questions)


15. Short-circuit evaluation always evaluates all conditions.
❌ False
📝 Note: Stops early if result is known.
📘 Chapter: 7

16. Pass-by-value allows the function to change the caller’s variable.


❌ False
📝 Note: Only modifies a copy.
📘 Chapter: 9

17. CFG defines the semantics of a language.


❌ False
📝 Note: CFG defines syntax, not meaning.
📘 Chapter: 3 – Vize %30

18. [x] in EBNF means x occurs zero or one time.


✅ True
📝 Note: Optional element in grammar.
📘 Chapter: 3 – Vize %30

19. Pass-by-reference allows external variable modification.


✅ True
📝 Note: Directly changes caller’s data.
📘 Chapter: 9

20. Stack is FIFO (First In First Out) structure.


❌ False
📝 Note: Stack is LIFO (Last In First Out).
📘 Chapter: 10
21. A function call always creates a new activation record.
✅ True
📝 Note: Needed for tracking context.
📘 Chapter: 10

22. else always binds to the farthest if.


❌ False
📝 Note: Binds to nearest if.
📘 Chapter: 8

23. All operators in C have the same precedence.


❌ False
📝 Note: Precedence varies (e.g., * > +).
📘 Chapter: 7

24. Assignment operators in C associate right to left.


✅ True
📝 Note: a = b = c; is valid.
📘 Chapter: 7

Common questions

Powered by AI

LIFO, or Last In First Out, is essential in stack memory structures because it aligns with the nature of function calls and returns, where the most recent call must be completed before returning to the previous one. This order ensures that the return address and local data of the most recent call are preserved at the top, ready for immediate access and restoration after the function execution completes. This characteristic simplifies and expedites function management, allowing for efficient use and release of stack memory .

The Extended Backus-Naur Form (EBNF) notation {} is used to indicate zero or more repetitions of an element, akin to a loop in syntax specifications. This makes it easier to represent repetitive structures such as lists or block contents, thereby enhancing parser generation efficiency. By simplifying syntax rules, it allows language designers to build grammars that are concise and capable of expressing complex constructs necessary for parsing programming languages .

Coercion is important in programming languages because it allows operations to proceed even when operands are of different types, by converting one type to another. When assigning a float to an int in C, coercion happens automatically, converting the float to an integer by truncating any fractional part. While this facilitates harmonious operations between different data types, it can also lead to a loss of precision, necessitating cautious implementation to prevent unintended data loss .

Activation records in a stack-based memory layout are used to store information about active subroutines, including their parameters, local variables, return addresses, and dynamic linking data. They follow a Last In First Out (LIFO) order, where the most recently called function's activation record is on top, ensuring that function calls and returns are managed efficiently. For instance, when a function is called, its activation record is pushed onto the stack, and when it returns, the record is popped off, maintaining proper flow control .

The inclusion of parameters and local variables within activation records is significant because it facilitates the organized storage and access of data necessary for function execution. During function calls, this data structure enables the runtime environment to manage scopes and lifetimes of data efficiently, ensuring that each function can execute with its own set of variables without interfering with others. Additionally, this separation maintains data integrity and aids in debugging by making memory footprints predictable .

Context-free grammars (CFGs) are a type of formal grammar that is crucial for defining the syntax of programming languages. They consist of a set of production rules that describe all possible strings (or sequences of symbols) in a language, ensuring each string's validity within that language. CFGs enable systematic description and interpretation of programming constructs, allowing language parsers to determine valid source code, making them essential for compiler and interpreter design .

Referential transparency refers to the property of an expression in a program that, given the same inputs, always produces the same output without causing any side effects. This characteristic allows for easier reasoning about the behavior of a program because each expression can be replaced with its value without changing the program's meaning, enhancing predictability and debug-ability .

Operator precedence dictates the order in which parts of an expression are evaluated. In languages like C, an operator with higher precedence will be evaluated before one with lower precedence, regardless of its position in the expression. For example, in the expression '3 + 4 * 5', multiplication has a higher precedence than addition, so '4 * 5' is evaluated first. Understanding precedence is crucial for writing correct and intended expressions, as improper usage can lead to logical errors and unexpected results .

Short-circuit evaluation in programming languages like C optimizes the evaluation of logical expressions by stopping as soon as the outcome is determined. For example, in the expression 'A && B', if A evaluates to false, evaluating B is unnecessary because the whole expression can only result in false. Similarly, for 'A || B', if A is true, B is not evaluated. This reduces unnecessary computations and enhances performance by avoiding potentially costly operations .

Pass-by-value involves copying the actual value of an argument into the formal parameter of the function. This means any changes made within the function do not affect the original variable. In contrast, pass-by-reference involves passing the address of the variable, allowing the function to modify the original variable directly. Consequently, changes made to the parameter affect the argument used to call the function .

You might also like