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 Midterm Exam: Comprehensive Guide with Questions and Answers, Exams of Programming Languages

A comprehensive guide to cs 326 midterm exam, covering key concepts and providing answers to common questions. It delves into various aspects of compilation, programming languages, and computer science fundamentals, offering valuable insights for students preparing for the exam.

Typology: Exams

2024/2025

Available from 02/14/2025

rex-smith-1
rex-smith-1 ๐Ÿ‡บ๐Ÿ‡ธ

783 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 326 MIDTERM EXAM (NEWEST EXAM 2025) |
ALL QUESTIONS AND CORRECT ANSWERS |
GRADED A+ | VERIFIED ANSWERS | LATEST
VERSION
List the phases of front end compilation ---------CORRECT ANSWER---------
--------Character stream => scanner(lexical analysis) => token stream =>
parser(syntax analysis) =>parse tree => semantic analysis and
intermediate code generation
List the phases of back end compilation ---------CORRECT ANSWER---------
--------Semantic analysis and intermediate code generation => -abstract
syntax free or other intermediate form => machine independent code
improvement(optional) => modified intermediate form => target code
generation => assembly or machine language or other target language =>
machine specific code improvement (optional) => modified target language
== symbol table
What are regular languages? ---------CORRECT ANSWER-----------------A
subset of context free ones
What is a formal language? ---------CORRECT ANSWER-----------------A set
of strings of symbols drawn from a finite alphabet
Name the qualities of a scanner ---------CORRECT ANSWER-----------------โˆ’
ignores white space (blanks, tabs, new-lines)
โˆ’ ignores comments
โˆ’ recognizes tokens
โˆ’ implemented as a function that returns next token every time it is called
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS 326 Midterm Exam: Comprehensive Guide with Questions and Answers and more Exams Programming Languages in PDF only on Docsity!

CS 326 MIDTERM EXAM (NEWEST EXAM 2025) |

ALL QUESTIONS AND CORRECT ANSWERS |

GRADED A+ | VERIFIED ANSWERS | LATEST

VERSION

List the phases of front end compilation ---------CORRECT ANSWER--------- --------Character stream => scanner(lexical analysis) => token stream => parser(syntax analysis) =>parse tree => semantic analysis and intermediate code generation List the phases of back end compilation ---------CORRECT ANSWER--------- --------Semantic analysis and intermediate code generation => - abstract syntax free or other intermediate form => machine independent code improvement(optional) => modified intermediate form => target code generation => assembly or machine language or other target language => machine specific code improvement (optional) => modified target language == symbol table What are regular languages? ---------CORRECT ANSWER-----------------A subset of context free ones What is a formal language? ---------CORRECT ANSWER-----------------A set of strings of symbols drawn from a finite alphabet Name the qualities of a scanner ---------CORRECT ANSWER-----------------โˆ’ ignores white space (blanks, tabs, new-lines) โˆ’ ignores comments โˆ’ recognizes tokens โˆ’ implemented as a function that returns next token every time it is called

Name the qualities of a parser ---------CORRECT ANSWER-----------------โˆ’ calls the scanner to obtain tokens โˆ’ builds parse tree โˆ’ passes it to the later phases (semantic analysis, code generation and improvement) What can expressions be in Scheme ---------CORRECT ANSWER------------ -----atoms or lists What is an atom in Scheme? ---------CORRECT ANSWER-----------------a number, string, identifier, character, or boolean What is a list in Scheme? ---------CORRECT ANSWER-----------------a sequence of expressions separated by spaces, between parentheses What to constant atoms evaluate to? ---------CORRECT ANSWER------------ -----themselves What do identifiers (symbols) evaluate to? ---------CORRECT ANSWER----- ------------the value bound to them What do lists evaluate to? ---------CORRECT ANSWER----------------- evaluate as "function calls"

Name the two storage allocation mechanisms ---------CORRECT ANSWER- ----------------Static and Dynamic Describe static allocation ---------CORRECT ANSWER-----------------each object is given an address before execution begins and retains that address throughout execution Describe Dynamic allocation ---------CORRECT ANSWER-----------------โˆ’ Stack - objects are allocated (on a stack), and bindings are made when entering a subroutine โˆ’ Heapโˆ’ Explicit - allocated and deallocated by explicit directives at arbitrary times, specified by the programmerโˆ’ Implicit - allocation and deallocation are implicit (transparent for the programmer); requires garbage collection Describe and give examples of languages that are Statically (also called lexically) scoped ---------CORRECT ANSWER-----------------โˆ’ Bindings are determined by examining the program text โˆ’ Can be determined at compile time โˆ’ Examples: C, Pascal, Scheme Describe and give examples of languages that are dynamically scoped ----- ----CORRECT ANSWER-----------------โˆ’ Bindings depend on the flow of control at run time (on the dynamic sequences of calls) โˆ’ Choose the most recent active binding (at run time) โˆ’ Examples: APL, Snobol, early Lisp

explain the differences between shallow vs. deep binding --------- CORRECT ANSWER-----------------โˆ’ deep binding - use the environment from the moment when the function was passed/stored/returnedโˆ’ shallow binding

  • use the environment from the moment of function call What are the 2 iteration mechanisms? ---------CORRECT ANSWER----------- ------โˆ’ Enumeration-controlled loops (for) - executed once for every value in a given finite set โˆ’ Logically-controlled loops (while) - executed until some Boolean condition changes Describe the qualities of Enumeration-controlled loops (for) --------- CORRECT ANSWER-----------------โˆ’ Changes to the loop index (i), step or bounds (first and last) โˆ’ Empty bounds โˆ’ Direction of step โˆ’ Jumps in and out of the loop A function is tail recursive if: ---------CORRECT ANSWER-----------------โˆ’ no additional computation follows the recursive call โˆ’ returns what the recursive call returns Why are the front and back end of a compiler usually implemented as separate passes? ---------CORRECT ANSWER-----------------The front is what makes the source languge grogram understandable for the compiler, while the back end mqps programs to the target machine.

What is syntax? ---------CORRECT ANSWER-----------------What a program looks like What are semantics? ---------CORRECT ANSWER-----------------What a program means What is implementation? ---------CORRECT ANSWER-----------------How a program executes What languages are used for systems programming? ---------CORRECT ANSWER-----------------C, Modula- 3 What languages are used for scientific computations? ---------CORRECT ANSWER-----------------Fortean, Ada What languages are used for embedded systems? ---------CORRECT ANSWER-----------------C,Ada,Modula- 2 What languages are used for symbolic computing? ---------CORRECT ANSWER-----------------Lisp,Scheme,ML What does imperative mean? ---------CORRECT ANSWER----------------- How should the computer do it

What does declarative mean? ---------CORRECT ANSWER----------------- What should the computer do What types of programming are imperative? ---------CORRECT ANSWER--- --------------Von Neumann, Object-oriented What types of programming are Declarative? ---------CORRECT ANSWER-- ---------------Functional, logic and constraint based Name von Neumann languages ---------CORRECT ANSWER----------------- Fortran, Basic, Pascal, C Name object-oriented languages ---------CORRECT ANSWER----------------- Smalltalk,Eiffel, C++, Java Name functional languages ---------CORRECT ANSWER-----------------Lisp, Scheme, ML, Haskell Name logic and constraint based languages ---------CORRECT ANSWER--- --------------Prolog Explain machine language ---------CORRECT ANSWER------------------ sequence of bits that directly controls the processor

  • add, compare, move data, etc.
  • computer's time more valuable than programmer's time

When are arguments evaluated? ---------CORRECT ANSWER----------------- โˆ’ Before being passed to the function (applicative-order evaluation): in most languages, safer, more clear โˆ’ Pass a representation of unevaluated parameters to the function; evaluate them only when needed (normal-order evaluation): typical for macros, can be faster Why are the front end and the back end of a compiler usually implemented as separate passes? ---------CORRECT ANSWER-----------------The front makes the source language program understandable, the back maps programs to the target machine Is C++ a weekly typed language? ---------CORRECT ANSWER----------------- Yes, because it has many features that deliberately weaken its type system. For example, type casting allows you to interpret a field of a structure that was an integer as a pointer. Is the syntax of a language the grammar rules of that language? --------- CORRECT ANSWER-----------------Yes, syntax is generated by the languages under the grammar. Is C++ a static/lexical scoped language? ---------CORRECT ANSWER------- ----------Yes Explain orthogonality in the context of a language design. Give an example. ---------CORRECT ANSWER-----------------Orthogonality in a programming language means that a relatively small set of primitive constructs can be

combined in a relatively small number of ways to build the control and data structures of the language. It is associated with simplicity. Eg. Inheritance, pointers in c++, function, etc. Explain why type checking impacts the reliability of a language. Give an example. ---------CORRECT ANSWER-----------------Type checking impacts the reliability of a language because it helps prevent certain types of errors that can occur during program execution. When a programming language has strong type checking it means the compiler or interpreter can catch errors related to type mismatches before program execution. This can help in avoiding runtime errors that can be difficult to debug and cause the program to crash. x = "5" y = 3 z = x + y print(z) Explain static semantics and why it cannot be enforced by the parser. Provide an example to justify your answer. ---------CORRECT ANSWER----- ------------Static semantics are thing like scoping of a variable which are determined during compile time. This is because static semantics often require contextual information that is not present in the syntax of the program such as the types of variables or the scope of identifiers. An example is inheritance since the parser doesn't know how this works. public class Example { public static void main(String[] args) { int x = 5; boolean b = x; // ERROR System.out.println(b); } }

TEXTBOOK and SLIDES ---------CORRECT ANSWER----------------- Feature Multiplicity ---------CORRECT ANSWER-----------------having more than one way to accomplish a particular operation Problem with operator overloading (a single operator has more than one meaning) ---------CORRECT ANSWER-----------------it can lead to reduced readability if users are allowed to create their own overloading and do not do it sensibly. Type checking ---------CORRECT ANSWER-----------------simply testing for type errors in a given program, either by the compiler or during program execution. Type checking is an important factor in language reliability. Aliasing ---------CORRECT ANSWER-----------------having two or more distinct names in a program that can be used to access the same memory cell. Categories for evaluating programming languages ---------CORRECT ANSWER-----------------1. Simplicity

  1. Orthogonality
  2. Data Types
  3. Syntax Design
  4. Support for Abstraction
  5. Expressivity
  6. Exception Handling
  7. Restricted Aliasing

Application Domains of Programming Languages ---------CORRECT ANSWER-----------------1. Scientific Programming (e,g. R, Fortran)

  1. Business Applications (e.g. COBOL)
  2. Artificial Intelligence (e.g. Lisp)
  3. Web Software (e.g. HTML, JavaScript) Categories of Languages ---------CORRECT ANSWER-----------------1. Imperative (e.g. C, Java)
  4. Functional (e.g. Scheme, Lisp)
  5. Logic (e.g Prolog)
  6. Object-oriented (e.g., Smalltalk, Java)
  7. Scripting languages (e.g. Ruby, JavaScript)
  8. Markup (e.g. HTML, CSS) Compiler implementation ---------CORRECT ANSWER-----------------When programs are translated into machine language, which can be executed directly on the computer. Pros: very fast program execution once translation process is complete the process is done Lexical units ---------CORRECT ANSWER-----------------lexical units of a program are identifiers, special words, operators, and punctuation symbols Compilation phases ---------CORRECT ANSWER-----------------1. Lex
  9. Parse
  10. Semantic Analysis
  11. Optimize (or not)
  12. Generate code (or interpret)