TPL Mids Assignment New
TPL Mids Assignment New
a. What was the first programming language you learned? If you chose it,
why did you do so? If it was chosen for you by others, why do you think
they chose it? What parts of the language did you find the most difficult
to learn?
Answer
I learned C++ language as my first programming language. It was chosen by the
college teachers because, if you learn to program in C++ from the ground up, you
must justify everything you do, which allows you to have a better understanding
of how every component function in programming. Simultaneously, C++ offers
a reasonably comprehensive implementation of object-oriented concepts, in my
view. Unlike certain other languages, you are not expected to write classes and
objects, but you should eventually apply yourself to more advanced aspects of
object orientation.
Because of their strength and complicated syntax, templates are the most difficult
feature of C++. It's difficult to use pre-written ones, and it's difficult to write a
simple templated class or method, but C++ templates are a Turing-complete
language that is implemented by a programmer.
b. For the language with which you are most familiar (this may or may not
be the first one you learned), list three things you wish had been
differently designed. Why do you think they were designed the way they
were? How would you fix them if you had the chance to do it over? Would
there be any negative consequences, for example in terms of compiler
complexity or program execution speed?
Answer:
I am most familiar with C++ language. Following are three things which I wish
they had been designed differently:
1. Confusing parameter passing value.
2. Three different memory spaces: stack/heap/static for difficult interactions
You can transfer pointers to local variables around some of the time, but not
all of the time.
3. For class inheritance, there are just too many laws.
They were designed the way they were because:
1. Speed: C++ is a faster programming language than most others, and it has
great concurrency support. This makes it useful in situations where
consistency is crucial and latency is expected to be kept to a minimum. Such
demands are common in high-load servers like web servers, application
servers, database servers, and so on. C++ is used widely in those servers.
2. Closer to hardware: C++ is more closely related to hardware than other
programming languages such as Python. This makes it useful in situations
where software is tightly coupled with hardware and low-level software
support is needed.
We can fix these simple but major issues in C++ language by learning how they
are fixed in other similar languages, for instance, Java, which has built a strong
base in handling classes and objects very well. The parameter passing issue in
C++ can also be fixed if we add function in C++ compiler to check for parameters
before execution.
There are a few challenges which may affect compiler complexity or program
execution speed:
• A C++ program cannot accept garbage collection, Dynamic Memory
Allocation, or security since it contains a pointer, buddy function, and global
variable, and it has no built-in thread support.
• The program's productivity has decreased.
• When C++ is used to develop web applications, it becomes complicated and
challenging to debug.
Question 2.
Write a program that commits a dynamic semantic error (e.g., division by
zero, access off the end of an array, dereference of a null pointer). What
happens when you run this program? Does the compiler give you options to
control what happens? Devise an experiment to evaluate the cost of runtime
semantic checks. If possible, try this exercise with more than one language
or compiler.
Answer:
Program in C++ language:
#include <iostream>
int main ()
float denominator = 0;
float result;
cout << "The quotient of 12.5/0 is " << result << endl;
Result:
The quotient of 12.5/0 is inf
It throws a runtime error exception. We can use exception management to
gracefully resolve operations like dividing a number by zero, which is a
mathematical error (not defined). If you write code without using exception
handling, the division by zero result will be infinity, and cannot be processed
anymore.
Although the program compiles successfully, it fails at runtime, resulting in an
exception. There is a semantic error, but it is a dynamic semantic error since there
is no error while compiling.
Program in C language:
// Online C compiler to run C program online
#include<stdio.h>
int main() {
int a, result;
a= 15;
result = a / 0;
return(0);
Result:
Floating point exception
The program executed with a Floating-point exception. The compiler didn’t gave
any warning before program execution. When the statements written in the
program are not meaningful to the compiler, this error happens. We can manage
those cases with exception handling and present a message informing the user
that they cannot divide a number by zero. One of the most critical aspects of
semantic analysis is type checking. We must ensure that each operator in the
source software has semantically compatible operands during type testing. This
type testing will happen at compile time or at run time.
Question 3.
a. Some languages (e.g., C) distinguish between upper- and lowercase
letters in identifiers. Others (e.g., Ada) do not. Which convention do you
prefer? Why?
Answer:
I prefer languages which distinguish between upper-case and lower-case letters
in identifiers because, it helps differentiate from identifiers for constant names
and allows more room for variable names. For example, languages such as the C
convention of using uppercase for constant names and lowercase for variable
names.
b. Give three examples of features that are not provided in some language
with which you are familiar, but that are common in other languages.
Why do you think these features are missing? Would they complicate the
implementation of the language? If so, would the complication (in your
judgment) be justified?
Answer:
In C, it is not object-oriented language but other some languages are object-
oriented. This feature makes a lot of difference in C with other languages like
python, C++, C#, Ruby, etc. C doesn’t have even constructors in it but other
languages have. It doesn’t have garbage collector in it.
These features are missing because, every language has its own specifications and
features which provides you to understand it with different perspectives. C is
simple and it has basic structure of a program. C is a procedural language that’s
why it is not an object oriented, it doesn’t support high- and low-level languages.
If we talk about complication, no, it’s not going to be complicated because C has
its own way of implementation, code of lines. There are some features which C
allows to perform. Every language implements its code according to given
instructions or permissions to that language from inventor of that language.
Question 4
Given the following program of a language called clang. What is the output
of the program if dynamic scoping is used? What is the output if static
scoping is used? Alpha is the entry point in the program:
X → Y | x | ZYW
Y→ Z | y
Z → x C’D’ | y C’D’ | z C’D’
C’ → YWC’ | ꜫ
D’ → C’ D’ | ꜫ
W→w