Midterm Programming Languages
docx
keyboard_arrow_up
School
Florida International University *
*We aren’t endorsed by this school
Course
4210
Subject
Computer Science
Date
Feb 20, 2024
Type
docx
Pages
9
Uploaded by gonzvegas
1/28/2024
Programming Languages Midterm
1) All languages have specific design criteria, a goal in mind. (at least 1 page) A.
Give two languages that are in direct conflict with each and why.
Python and C++ are in direct conflict with each other because python was designed with the purpose of increasing simplicity to achieve ease of use by a programmer by abstracting low level details and as a consequence execution time is lost which means its performance is reduced. On the other hand, C++ is designed to increase performance by increasing control over memory and overall system resources that at the end of the day reduce simplicity for the programmer. B.
Provide examples of these conflicts as either programming examples (features allowed or not allowed) or program model, (environment).
Calculating the factorial of number 5:
This code snippet is concise and easy to read because of recursion, which is a common way to solve factorial in Python due to its simplicity.
This code snippet is more verbose but eliminates the overhead of recursive function calls which tends to be more efficient in terms of execution time. But at the same time is more complex and is just harder to read.
C.
Compare and contrast the terms readability and write ability regarding your selected two languages and explain why you believe this is so. Python: Readability: The syntax itself is designed to be clearer and more concise than any other language. Python avoids the use of curly braces and semicolons, using indentation to define code blocks instead which makes the code looks cleaner and just easier to understand (“More programmer friendly”).
In terms of writability python is also considered to be simpler mainly because of the large library that provides several built in functions and modules that help programmers achieve their tasks on a faster pace. Which ultimately translates into programmers having to write less code to do an specific task, which is not the same case for C++. Python dynamic typing and automatic memory management eliminates the need to specify memory usage which also contributes towards writability.
C++: Readability: It is less readable than python, its syntax is more complex because it does include the usage of curly braces , semicolons and explicit variable types which adds visual complexity.
When it comes to writability C++ is also considered less writable than python because of the need to declare the types of variables explicitly , which adds to the code. 2) As languages evolve, describe some of the restrictions that you believe are appropriate for changes in programming languages. Describe, in your opinion, the goal of program language evolution should be, why is this so and how should this be implemented? (at least ½ page) Some restrictions for changes in programming languages:
Consistency: Changes to a language should strive to maintain internal consistency. This means that the syntax and semantics of new features should align with the existing design of the language.
Performance: Any changes to the language should consider the impact on performance. New features should not degrade the performance of programs written in the language, on the other hand it should increase the performance. Security: Changes should aim to reduce the risk of security risks. This includes removing outdated features that may pose security risks and introducing new constructs that promote safer coding practices.
Dependency: Python, the number of external libraries and frameworks also increases. Each new version of Python must consider how changes affect these dependencies. Ensuring that core language changes do not unnecessarily disrupt the network of packages is important for maintaining a stable development environment.
The ultimate goal of programming language evolution should be focused on facilitation of creation of efficient and long-lasting maintainable code. I personally like what salesforce and Alteryx are doing with their declarative programming system. I believe everyone should have the possibility to code without knowing how to code by a drag and drop system where if you have the idea, you could implement it just using user friendly tools and packages which would increase productivity due to the increase of readability and writability.
This is a couple of ideas of how it should be implemented: 1) The system should have a graphical user interface (GUI) that is intuitive and easy to navigate.
2) A robust library of pre-built components or functions that users can select and configure is critical.
3) The system should allow for customization by allowing users to integrate custom code written in a traditional programming language.
4) Offering tutorials, examples, and comprehensive documentation can help users get started.
5) Providing simple-to-use testing frameworks and debugging tools that give clear, understandable feedback is important.
3) In chapter 3, there are discussions about syntax and semantics, in general there are two types of grammars for programming languages, regular and context-free, what is the difference, what is the scope
(where are they applied), and how can the use of these grammars produce a higher level of program reliability. Why does this higher level of reliability encourage complexity?. (at least ½ page) The main difference between regular and context-free grammars lies in their expressiveness. Regular grammars cannot handle nested structures, which are essential in programming languages for defining scopes, blocks of code, and matching pairs like parentheses. Context-free grammars, however, can handle such recursive nesting, making it suitable for expressing the syntax of most programming languages.
Using these grammars, especially CFGs, contributes to a higher level of program reliability in several ways:
Syntax Checking: Grammars enable the precise definition of a programming language's syntax, which allows for effective syntax checking. This helps catch syntactic errors early in the development process, reducing the number of runtime errors.
Error Reporting: With a well-defined grammar, compilers and interpreters can provide clear error messages, guiding programmers to quickly identify and correct issues within their code.
Automatic Code Analysis: Tools built upon these grammars can perform code analysis to enforce coding standards, detect potential bugs, and suggest improvements, leading to more robust and reliable code.
Why does this higher level of reliability encourage complexity? As languages become more reliable and errors become easier to detect and fix, language designers may be tempted to add more sophisticated features, believing that the robust tooling will manage the additional complexity.
Reliability can lead to the creation of higher levels of abstraction, which can hide the complexity from the end-user but add complexity to the language's implementation and specification.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
4) Describe the different types of Semantics, when and how are they applied, what are the advantages or disadvantages to each type. (at least ½ page) Operational semantics describe the meaning of a program by specifying the operations that should be performed to execute a program. This type of semantics is often specified by describing a state machine or an abstract machine, such as a Turing machine, and showing how the state changes as the program executes. Operational semantics are commonly used to define the execution model of lower-level languages or languages that are very close to actual machine instructions.
This approach is intuitive as it relates closely to how the program will be run on the computer. It is also useful for proving properties about specific program executions.
Denotational Semantics: Denotational semantics are used for theoretical analysis of programming languages and for proving general properties about languages rather than specific programs. It is especially useful in functional programming languages and for languages that support higher-order functions.
It provides a very high-level and abstract perspective of a programming language, allowing for mathematical reasoning about programs and language constructs.
The high level of abstraction can make denotational semantics difficult to understand and apply. It may also be challenging to define denotational semantics for languages with complex features like concurrency or exception handling.
Axiomatic semantics, use logical assertions to describe the behavior of a program. These assertions, known as preconditions and postconditions, specify what must be true before and after a piece of code is executed, respectively.
Axiomatic semantics are used in program verification to prove the correctness of a program. It is particularly applicable to imperative languages where the state of the program changes as instructions are executed.
Axiomatic semantics allow for the formal verification of program correctness and can be used to prove that programs meet their specifications.
Creating the necessary preconditions and postconditions can be difficult, and proving correctness can be complex and time-consuming. This method might not be practical for everyday programming tasks.
5) Explain why compilers use parsing algorithms that work on only a subset of all grammars. (at least ½ page)
Compilers use parsing algorithms that work on only a subset of grammars mainly for efficiency and practicality. Here's a concise explanation:
Computational Efficiency: Parsing algorithms for compiler use, like LL and LR parsers, are designed to work efficiently with specific subsets of context-free grammars (CFGs). These subsets are chosen because they can be parsed in linear time, making the compilation process faster.
Ambiguity Resolution: Compilers require unambiguous grammars to ensure each program has a unique, correct interpretation. Subsets of CFGs used in compilers are selected to avoid ambiguity in the parse trees, leading to predictable program behavior.
Error Detection and Reporting: Effective error reporting is crucial in compilers. Certain parsing techniques are better at detecting syntax errors and providing clear, meaningful feedback to programmers. These techniques are typically associated with specific grammar subsets.
Simplicity and Predictability: Using a restricted subset of grammars simplifies the parser's design and makes the language's behavior more predictable for both developers and users.
Practical Constraints: Compilers operate within practical limitations like memory and processing power. Focusing on certain grammar subsets helps ensure that compilers can handle large codebases efficiently.
6) Make an argument, which type of parser is more powerful, bottom-up or top down, use as may examples as possible to support your side and why. (at least ½ page) There are two primary types of parsers: top-down and bottom-up. Each has its strengths and weaknesses, but I will argue that bottom-up parsers are generally more powerful than top-down parsers,
especially in terms of the types of grammars they can handle and the robustness of the parsing process.
Bottom-up parsers work by constructing a parse tree for an input string starting from the leaves and working up to the root, which is why they are also called "shift-reduce" parsers. They handle the rightmost derivation of a string in reverse and are capable of recognizing virtually any grammar that can be defined by a context-free grammar (CFG), including left-recursive grammars, which are problematic for simple top-down parsers.
Bottom-up parsers are better at dealing with ambiguous grammars. They can be programmed with precedence and associativity rules that resolve ambiguities in expressions, which is a significant advantage for parsing programming languages where operator precedence matters.
Bottom-up parsers are known for their robustness in error detection and recovery. They often detect syntax errors as soon as it is mathematically possible to do so, which is not always the case with top-
down parsers.
Most modern programming languages use bottom-up parsers due to their ability to handle complex and irregular grammars.
7) What is a variable, and why don’t functional languages have them? Show example of the same code (perhaps a loop), in both languages. (at least ½ page) A variable in programming is a storage location paired with an associated name.
Functional programming languages, mostly emphasize in immutability and statelessness. In a purely functional language, variables, as known in imperative languages, do not exist. Instead, functional languages use the concept of bindings, which associate names with values, but once a name is bound to a value, it cannot be changed.
The binding method enforces a clear separation between data and state, which can lead to more predictable and less error-prone code.
Functional languages do this to avoid side effects, which are changes in state that do not depend on the function's input and can affect a program's behavior in many unpredictable ways.
In this loop I’m using a mutable variable sum to accumulate the total. Here I’m modifying the value of sum by each iteration of the loop.
Here I’m defining a class SumExample with a main method, which is the entry point of the program. I’m declaring a mutable variable sum and initializing it to 0. Then we use a for loop to iterate from 1 to 10, adding each number to the sum. Finally, we print the result to the console.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
8) What is implicit heap-dynamic variables, when how and why are they used, when should they not be used. How are they implemented? (at least ½ page) Implicit heap-dynamic variables are variables that are allocated on the heap at runtime without explicit requests from the programmer for allocation or deallocation.
Implicit heap-dynamic variables are commonly used in languages that aim to simplify memory management for the programmer. Languages like Python, JavaScript, and Java utilize implicit heap-
dynamic variables.
These languages manage memory allocation which helps preventing common errors such as dangling pointers references.
For example: In python when you create a list or an object, you don't need to specify how much memory
to allocate.
When they should not be used?
In systems where performance and memory efficiency are critical.
In environments with limited memory resources.
How should it be implemented? implicit heap-dynamic variables typically implement them using a garbage collector. The garbage collector automatically reclaims memory that is no longer in use by the program, which it determines through various algorithms.
In these languages, the compiler and runtime work together to manage the heap. When an object is no longer reachable through any references in the program, the garbage collector will eventually collect it and clean the memory.
9) What is dynamic scoping, when and where it is applied, what are the performance impacts of using dynamic scoping and how can these be minimized and why can this be to an advantage. (at least ½ page)
Dynamic scoping is a programming concept where the range of a variable is determined by the sequence
of function calls during program execution. Unlike static scoping, where the scope is determined by the program text, dynamic scoping allows for more flexibility in variable access.
The application of dynamic scoping can have performance impacts, as the determination of variable scope at runtime may incur additional computational overhead. However, research has shown that the overhead associated with the implementation of dynamic scoping schemes can be insignificant in terms of performance and power consumption (Dubach et al., 2010).
how can these be minimized? By limiting the use of dynamic scoping to specific cases where it provides clear benefits, also by using a scope marker can help quickly locate the variable in the call stack without having to traverse the entire stack.
why can this be to an advantage? In some cases, dynamic scoping can lead to simpler code because variables can be overridden without passing them as parameters through several layers of function calls.
10) What are the advantages and disadvantages of user-defined ordinal types as data types? (at least ½ page) Advantages:
User-defined ordinal types are data types defined by the programmer that establish a range of values with a built-in sequence.
User-defined ordinal types can make a program more readable and understandable. By creating a type with a meaningful enough name and specific values, you can communicate the intent of the code more clearly which can lead to a better maintainability.
Ordinal types often allow for more efficient use of memory, as the compiler can optimize storage for the limited range of values. They can be stored using the minimum amount of memory necessary to represent the range.
Disadvantages:
Not all programming languages support user-defined ordinal types, which can be a limitation of their use
to only certain environments or necessitate workarounds in languages without such support.
They might still lack the full range of operations available to built-in types and extending them with additional functionality can require extra work.
User-defined ordinal types may require custom serialization and deserialization logic, especially when interfacing with different systems or storing data.
11) What are the advantages and disadvantages to an associative array? Explain why a language would need an associative array, how does it work? (at least ½ page) Advantages:
Associative arrays offer efficient data retrieval, with accessing the value associated with a key typically being a fast operation, often close to constant time complexity. They provide the flexibility to use various types of keys, such as strings or integers, thereby enhancing how data is organized and accessed. The process of data storage and retrieval is simplified with associative arrays, as they allow for direct association of data, contributing to more readable and intuitive code. An added advantage is that there is no need to maintain an order of elements, which is beneficial when the order is not important or subject to frequent changes. Disadvantages:
However, there are disadvantages to using associative arrays. They generally require more memory than regular arrays, as they need to store both keys and values, in addition to the data structures necessary for efficient lookup. Performance can degrade, particularly in scenarios involving many hash collisions, where time complexity might worsen from O(1) to O(n), with 'n' being the number of elements in the array. Moreover, the lack of ordering in associative arrays, while advantageous in some cases, can be a
drawback when the order of elements is significant. Also, implementing an associative array from scratch
can be complex, as it involves designing an efficient hashing algorithm and handling collisions.
How does it work?
Associative arrays work by using a hash function to convert keys into array indices. When a key-value pair
is added to an associative array, the hash function takes the key and computes an index where the value will be stored. When retrieving a value, the same hash function is used to find the index where the value is stored.
12) Make an argument that narrowing or widening conversions are never safe or are safe, support your claim with examples, explain why this is so. (at least ½ page)
I believe narrowing conversions are never safe in an uncontrolled environment. The most obvious issue with narrowing conversions is the potential for data loss. When a larger data type is converted to a smaller one, there's a risk that the value won't fit into the smaller type.
Data loss in narrowing conversions is a significant issue in programming, particularly when dealing with data types of varying capacities. Understanding this concept is crucial for programmers, especially those working with languages that allow explicit or implicit type conversions.
All data types are designed to store some kinds of data. These types can vary in the size of data they can hold. For instance, common data types like int, long, float, and double differ in their storage capacity and
the nature of data they can contain. When a value is assigned from one data type to another, conversions occur.
To understand the concept of data loss, here is an example:
Imaging having containers of different sizes, each representing different data types. A large container (such as a long) can hold more content (larger numbers) than a smaller container (like an int). If you attempt to transfer the contents of the larger container into the smaller one, and the content exceeds the capacity of the smaller container, an overflow occurs. This overflow is similar to data loss in programming.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Documents
Recommended textbooks for you

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr

Fundamentals of Information Systems
Computer Science
ISBN:9781305082168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrFundamentals of Information SystemsComputer ScienceISBN:9781305082168Author:Ralph Stair, George ReynoldsPublisher:Cengage Learning
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Systems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage

C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning

C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr

Fundamentals of Information Systems
Computer Science
ISBN:9781305082168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,

Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage