Answer Booklet. May 2021
Answer Booklet. May 2021
ASTON UNIVERSITY
College of Engineering and Physical Sciences
MAY/JUNE 2021
Question Number:
1
(a) Languages which run in a virtual machine like Java is generally safer in this
respect: Program errors are normally intercepted and handled in a defined way. At
the next level, errors detected at compile time instead of at run time make a
language safer.
For Example: the Java Virtual Machine (VM), which runs the bytecode verifier to
type-check code, perfoms some runtime checks, and periodically
invokes the garbage collector
(b) We need a way of dealing with code to make it easier to manage. A common
technique is to reuse blocks of code, so that you don’t have to keep writing the same
code over and over again.
1
MODULE CODE:
For Example:
DEFINE SayHello age
PRINT "Hello, you are " + age + " years old"
END
The first line of the code says we are defining a re-useable block of code that does a
particular task. This task has a name: SayHello and a variable called age. In this
example the code displays the text “Hello, you are AGE years old”, where the word
“AGE” is replaced with whatever the variable age contains. The last line tells us that
this is the end of the code block.
IF age=10 THEN
SayHello age
ELSE
PRINT "You are not 10"
END
If the variable age contains 10, the code within SayHello will be run and the program
will display the text “Hello, you are 10 years old”. If age is anything else, it will display
the text “You are not 10”.
When we defined SayHello, we added a new instruction to our programming
language. As well as using the loops, logic and variables that are built into the
language, we now have another instruction we can use in programs: SayHello.
Defining SayHello has accomplished two things. First we can use it as many times
as we like in our program without having to repeat the code it contains. SayHello
only has one line of code in it, so that’s not a big of an advantage; but more complex
code blocks could be hundreds of lines long. That would save you a lot of time and
avoid potential errors.
Question Number:
2
The first difference is in java we can assign the value of one variable to other using
assignment operator while in ada we have to use implicit type casting before
assigning the value of on variable to other. Secondly, we define the user defined
integer data type with specific numbers of which is 0 to 150, on the other hand in
java we cannot limit the numbers of words while creating a variable type.
2
MODULE CODE:
Question Number:
3
(a)
C program into Java
((ArithExpsizeof(ArithExp);
e.isLeaf = true;
return e;
(b)
In the first line “*” is used to defined the return type of a function where the “*” is refered as
returning the address from that function. The “*” in the fourth line is used because we are applying
operation on the value, if we remove the “*” then it will apply operation on the address
Question Number:
4
>>> value = [45,301,25,66,102,2]
>>> temp = [i if i <100 else 0 for i in value]
>>> temp
Output:
[45,0,25,66,0,2]
Question Number:
5
(a)
The Auto type. For this library, an Auto semantically representsdenotes a a
relationship/ between an input and an output that is preserved over multiple steps,
where that relationship is (optionally) maintained within the context of a monad.
A Retryis a function that takes an Retry and possibly returns a delay in
microseconds. Iteration numbers start at zero and increase by one on each retry. A
*Nothing* return value from the function implies we have reached the retry limit.
(b)
In java we have multiple approaches to iterate this program instead of using do
while. Second difference is we can use exceptional error handling using try and
catch.
(c)
3
MODULE CODE:
for () {
(internal< (readTVar * -1));
internalTV;
(external < (readTVar * -1));
externalTVif;
external;
retry;
return;
}
Question Number:
6
On line number 5 it is checking and calling the function using variable a,b and pos
where pos is the given value by user. From line 6 to 8 it checked whether the pos is
greater then equal to the given value or not. In line 9 to 16 it checked whether the
pos is less than equal to the given value or not and then it is assigning the result in
the end.
Question Number:
8
(a)
The scope of the i in line 11 is local because it is declared inside the function which
means no other function can use this variable.
(b)
i will be declared once because in main function we are calling the init func. once
and i is declared outside the for loop. So i will be declared once in a program
(c)
The four scope of java are
Public( it is accessible by packages, subclasses and world)
Private ( it is not accessible by packages, subclasses and world)
Protected( it is accessbile by packages and subclasses)
Default( it is accessbile by packages only)
(d)
4
MODULE CODE:
Question Number:
9
(a)
In line 2, a and b are act as formal parameters
In line 13, a, b and c act as formal parameters
In line 15, a and b act as actual parameters
In line 16, b and c act as actual parameters
In line 17, a and b act as actual parameters 1 2 1 a = 1,b=2,c=1
In line 25, a, b and c act as actual parameters
(b)
When program is executed, it will initialize the three-variable a, b and c than it will
call the function sort3() and passed these three variable as actual parameters. Sort 3
function receive and store the address of these three actual parameters into formal
parameters a,b and c then sort() function will be called with two actual parameters a
and b. sort 2 function receive and store the address of these two actual parameters
into formal parameters a and b. then the value will be sort by using if statement and
then function will be ended without and return because of return type void. Compiler
will go back to sort 3 and again run the second function call and then the third
function call. After executing the third function call function will move out of the
function sort3 and then return i will be executed and function doSomeSorting() will be
ended by returning the smallest value which is number 2.
(c)
(i) when sort2() will be called it will pass the two values 2 and 1 and sort2 wil swap
the value of a and b, so when sort2 will be called 2 and 1 will be sent to the formal
parameters and similarly when sort 2 will be called with and b again 1 and 1 will be
passed and no swaping will be happening this time, in the end a =1,b=1,c=2
5
MODULE CODE:
(ii)
When sort2() will be called it will pass two value 1 and 2 but no swapping will be
done because if condition is false, second time when sort2() will be run 2 and 1 bill
be passed to a and b, it will swap the value of b and c, last time it will call the sort2()
the value 1 and 1 will be passed and no swapping will be done this time.
(d)
Yes, java you can use the sort(), or max() predefined function which allow us to code
less than current code.
(e)
In pure functions, the best was to pass the parameter is by using value. Because
Pure functions always return the same result for the same argument values. They
only return the result and there are no extra side effects like argument modification,
I/O stream, output generation etc
6
MODULE CODE:
7
MODULE CODE:
Question Number: