click below
click below
Normal Size Small Size show me how
CECS 342 Midterm SG2
| Question | Answer |
|---|---|
| All computer languages use compilation to create executable machine code | False |
| What is a clear benefit for compilation? | Better performance |
| What is the process that divides a program into tokens? | Scanning |
| What is the growth rate (Big O) for the Fibonacci function? | O(2^n) |
| What is the most popular programming language as of 2024? | Python |
| What can be said about C++ vs Python? | They both allow recursion |
| What can be said about C++ vs Python? | All of the following are false: The performance of both languages appears to be similar Both languages are interpreted Both languages are compiled |
| The C++ program I gave to you in assignment 1 had a problem that you had to fix. What was the problem? | Integer overflow |
| The slide deck for chapter one grouped languages into 2 groups. What are the groups? | Imperative & Declarative |
| According to the instructor (and many other references) what was the first high-level computer language (created in 1957)? | FORTRAN |
| Which languages are easy to learn? | BASIC, Pascal, LOGO, Scheme |
| Which languages are easy to express things, once fluent? | C, Common Lisp, APL, Algol-68, Perl |
| Which languages are easy to implement? | BASIC, Forth |
| Which languages are possible to compile to very good (small/fast) code? | Fortran |
| Why do we have programming languages? What is a language for? | Way of thinking, way of expressing algorithms |
| Which languages should you use for systems programming? | C, C++, C# |
| Which languages should you use for numerical computations? | Fortran, C |
| Which languages should you use for web-based applications? | PHP, Ruby |
| Which languages should you use for embedded systems? | Ada, C |
| Which languages should you use for symbolic data manipulation? | Common Lisp, Scheme, ML |
| Which languages should you use for networked PC programs | Java, .NET |
| Why study programming languages? | Makes it easier to learn new languages Some languages are similar, easy to walk down family tree |
| What’s another reason why we study programming languages? | Helps you make better use of whatever language you use |
| What are imperative languages? | Programming languages where you tell the computer exactly how to perform tasks step by step |
| What are the three major categories of imperative language? | Von Neumann, object-oriented, scripting languages |
| What are examples of Von Neumann languages? | C, Fortran, Pascal, Basic |
| What are examples of object-oriented languages? | Smalltalk, Eiffel, C++ |
| What are examples of scripting languages? | Perl, Python, JavaScript, PHP |
| What are declarative languages? | Programming languages where you specify what result you want, rather than how to compute it |
| What are the main types of declarative programming languages? | Functional languages, logic/constraint-based languages |
| What are examples of function programming languages? | Scheme, ML, pure Lisp, FP |
| What are examples of logic, constraint-based languages? | Prolog, VisiCalc, RPG |
| What are the advantages of interpretation? | Greater flexibility, better diagnostics (error messages) |
| What are the advantages of compilation? | Better performance |
| What does the preprocessor do in implementation strategies? | It removes comments/whitespace, groups characters into tokens, expands macros, and identifies higher-level structures like loops and subroutines. |
| What does the library of routines and linking do in implementation strategies? | The linker merges the needed library subroutines, like math functions (sin, cos, log), into the final program. |
| What is the purpose of post-compilation assembly in implementation strategies? | It makes debugging easier by producing readable assembly code and isolates the compiler from machine-language changes, so only the assembler needs updating. |
| What does conditional compilation in the C preprocessor do? | It deletes portions of code so multiple program versions can be built from the same source. |
| What is source-to-source translation in C++ implementation strategies? | It generates an intermediate C program instead of assembly code, as done by early AT&T C++ compilers. |
| What is source-to-source translation in C++ implementation strategies? | Early C++ compilers, like AT&T’s, translated C++ code into an intermediate C program instead of directly generating assembly. |
| What is bootstrapping in implementation strategies? | Bootstrapping is the process of writing a compiler in the language it is intended to compile. |
| How does compilation of interpreted languages work in implementation strategies? | The compiler generates code with runtime assumptions, running fast if valid, or reverting to the interpreter via dynamic checks if assumptions fail. |
| What is dynamic or just-in-time compilation? | It delays compilation until runtime, translating or optimizing code on the fly, such as Java bytecode or C# CIL, for faster or machine-independent execution. |
| What is microcode in implementation strategies? | Microcode runs the assembly-level instruction set via an interpreter stored in ROM, executed by hardware instead of being directly implemented in hardware. |
| Why are compilers for some interpreted languages not considered “pure”? | They selectively compile parts of the code, while interpretation of some code is still necessary. |
| Give examples of unconventional compilers. | Text formatters, silicon compilers, and query language processors. |
| What is scanning in compilation? | Scanning divides a program into tokens, simplifying parsing, saving time and complexity, and is typically implemented as recognition of a regular language using a DFA. |
| What is parsing in compilation? | Parsing recognizes a context-free language, discovering the program’s structure, often represented by syntax diagrams. |
| What is semantic analysis in compilation? | Semantic analysis discovers a program’s meaning, using static checks at compile time and leaving runtime checks (like array bounds) for dynamic semantics. |
| What is the purpose of an intermediate form (IF) in compilation? | Intermediate forms, created after semantic analysis, provide machine independence, enable optimization, and often resemble code for an idealized machine; some compilers use multiple IFs. |
| What does the optimization phase do in compilation? | It improves intermediate code for speed or space, though it is optional. |
| What happens during the code generation phase in compilation? | It produces assembly language or relocatable machine language from the intermediate code. |
| When are machine-specific optimizations performed in compilation? | They may be done during or after target code generation, using special instructions or addressing modes. |
| What is the role of the symbol table in compilation? | It tracks all program identifiers and compiler information about them, and may be retained for debugger use after compilation. |
| What do lexical and syntax analysis do in a C program like GCD? | Lexical analysis breaks the code into tokens, and syntax analysis checks the program’s structure against the grammar. |
| What happens during lexical and syntax analysis of a program? | Lexical analysis scans the code into tokens, and syntax analysis parses them to recognize the program’s structure. |
| How does parsing use context-free grammar in compilation? | Parsing organizes tokens into a parse tree using context-free grammar rules that define how constituents combine, including recursive structures. |
| What is logic programming based on? | Predicate calculus |
| What are predicates in logic programming? | Predicates are structural building blocks like P(a1,a2,...) that gain meaning only from explicitly stated relationships within the logical system. |
| What are the basic logical operators? | Conjunction, disjunction, negation, and implication. |
| What are universal and existential quantifiers? | They specify whether a statement applies to all elements (universal) or at least one element (existential). |
| What is the difference between axioms, theorems, and hypotheses? | Axioms are assumed true, theorems are provably true, and hypotheses (goals) are statements we aim to prove true. |
| Why do logic programming systems restrict the format of statements? | Restrictions make it possible to prove theorems mechanically, even though most mathematical theorems can’t be handled, while still retaining significant power. |
| What is the structure of a Horn clause in logic programming? | A Horn clause has a single-term head and a body of terms, where each term can be a constant, variable, or structure with a functor and arguments. |
| What are the types of terms in logic programming and how are they recognized? | Structures act as data or predicates, constants are atoms (lowercase or quoted) or numbers, variables start with uppercase letters, and all types are discovered implicitly without declarations. |
| What do facts, rules, and queries represent in a Horn clause? | A fact has an empty body, a query has an empty head, and a rule has both a head and a body; the body terms imply the head. |
| What role do facts play in a Prolog interpreter’s database? | Facts act as axioms, which the interpreter assumes to be true. |
| How can Prolog be viewed in terms of programming paradigms? | Prolog can be viewed declaratively, focusing on what is true, or imperatively, focusing on how to compute it; declarative semantics are emphasized for logic programming. |
| How does Prolog answer a query given a set of axioms? | It finds inference steps and variable assignments that prove the query starting from the axioms. |
| What determines the scope of a variable in a Horn clause? | The scope of a variable is the clause in which it appears. |
| How are variables implicitly quantified in a Horn clause? | Variables first appearing in the head are universally quantified; those first in the body are existentially quantified. |
| How does a Prolog interpreter respond when you ask a question (query)? | It tries to prove the predicate, responding “yes” if successful, “no” if not, and showing variable assignments needed to make it true. |
| How does backward chaining work in a Prolog interpreter? | It starts with the goal to prove and works backwards, looking for things that imply it until reaching facts. |
| What is forward chaining and why is it less commonly used alone? | Forward chaining works from facts to see if they imply the goal, but it can be very time-consuming without user hints or optimizations. |
| How does a Prolog interpreter use unification to satisfy a goal? | It merges compatible statements, instantiates variables with corresponding values, and links uninstantiated variables while keeping them unassigned. |
| How does a Prolog interpreter search its database to satisfy a goal? | It starts at the beginning of the database, unifies with facts to succeed, or with rules to recursively satisfy their bodies depth-first. |
| What principle motivates Prolog’s search strategy? | The Resolution Principle: if two Horn clauses can unify, the head of one can replace a term in the body of the other, preserving truth. |
| What happens in Prolog when a rule’s body cannot be satisfied? | The interpreter backtracks: it undoes unification, uninstantiates variables assigned during it, and continues searching the database for other unifications. |
| Why is Prolog not purely declarative? | The order of database statements and left-to-right goal pursuit gives deterministic, imperative behavior, affecting results, efficiency, and potentially causing infinite loops. |
| How does changing the order of rules or subgoals affect Prolog execution? | Changing rule order alters the sequence of answers, and reordering subgoals in a compound rule can cause infinite loops. |
| How does the = operator work in Prolog, and how are math operators treated? | = checks if operands can be unified; math operators are functors (structure names), not functions, so expressions like (2+3)=5 do not unify. |
| How does the is operator work in Prolog arithmetic? | is evaluates the right-hand side and assigns it to an uninstantiated left-hand variable; the LHS must be unassigned, and the RHS must already have values. |
| What does the Tic-Tac-Toe Prolog program do and what does it depend on? | It finds the next move given a board configuration but doesn’t play a full game, and its behavior depends on the ordering of rules. |
| How does the program decide a “good” move? | It prioritizes moves to win, block an opponent’s win, create a split, block a split, or build toward three in a row, following a fixed rule order. |