You are on page 1of 6

SIT102 – Introduction to Programming

Answers for 8.1P Reading Other Languages


Student Name:
Student ID:

Program 1:

1. What output data type will this function return? How do you reveal the data type
from the given function’s declaration and definition? Elaborate your answer.

*Solution

The datatype of the output will be boolean. If we declare the output will be always in
Bolean. We are simply returning True and False from the definition, which is either 1
or 0. The return type is therefore boolean(1 or 0).

2. What data types have been respectively declared for variables, data and val? In terms
of the programming scope, are variables, data and val used locally or globally?

*Solution

The data types and val of variables are both of int type.
The data and the val are locally declared in terms of programming scope.
They are limited to use to this function exclusively.

3. Variables, data and val are both used to store data but they store different numbers of
data value. How many data value(s) have variables data and val stored respectively?
Which of them is an array? Why? Elaborate your answer.

*Solution

data has five values.


val stores 1 value.
Data is an array because it contains several identical values and was explicitly defined
as such in the function.
data as Integer()

4. What is the purpose of the variable, i in the function? Is it a local or global variable?
What is the data type declared for it?

*Solution

i has two purposes: iterating the for loop and setting up a condition to end the loop
based on i.
i is a local variable. This is declare inside the function.
i is integer data type.
5. What would be returned value if the function is called with the data in sample data 1?

*Solution

If the function is used with sample data 1, False would be returned because val is not
present sample data 1
5 is not present in sample data 1.

6. What would be returned value if the function is called with the data in sample data 2?

*Solution

true would be returned if the function is invoked with that data.


Because val is present in sample data 1
2 is not present in sample data 2.

7. Deduce the purpose of this function after two sample datasets are executed through it.
Provide an appropriate name (meaningful name aligned with the purpose) for this
function. Hint: Recall the output data type in your provided answer for the question 1
in this section – Program 1, think about how it could provide the expected outcome.

*Solution

The ideal name might be NumberCheck because we are determining


Whether or not a numeric value is present in the array.
The purpose of this function is to count the number of '8' that are present in dataset
provided.

For example in dataset 1, result will be 1 as single '8' is present there.

In second dataset 2, result will be 3 as there are total three '8' present.

An appropriate name can be "countTheEights".

Returned result will be the count of '8' present in array datasets.

The expected output will be

Function (data As Integer(),

val As Integer) As Boolean Dim i As Integer

For i = 0 To data.Length - 1

If data(i) = val Then Return True

End If
End For

Return False

End Function

Program 2:

What will be output to the terminal when the program is run? Hint: make sure you have
traced all print statements along the program flow. The first two print-out lines are given as
below.

*Solution

Starting…
ADDING 32 + 6
SUBTRACT 78-5
total= 38 difference: 73
I like bananas
I like orange
I like figs
I like lemons
My favourite fruit is oranges
1. What are the data types respectively of the following variables? Since data types are not
declared in Python source code, how did you reveal their data types in a Python
program?

*Solution

Identifier Data type How did you reveal their data type in a Python program?
total int Total=add(32,6) gives 38, and when printing, it uses %d,
which stands for integer.
differenc int Difference=Subtract(78,5) returns an integer number, and
e when printing, it uses the integer symbol %d.

fruit list A list of strings called "fruit" is surrounded with the


symbols "[" and "]".

Program 3:

1. What will be output to the terminal when the program is run? Hint: make sure you have
traced all print statements along the program flow. The first two print-out lines are given
as below.

*Solution

Checking scores: [55, 26, 103, 92, 14]


Checking score 55
2. How many iterations will be run by the for loop of the program? How did you figure
this out? How does this for loop in a Swift program work in the same mechanism of
providing repetition dynamics compared to the one you learnt in a C++ program?

*Solution

The for loop iteration will be run 6 time.


We figure put this when program run this print checking score 55, 26, 103, 92 and 14
when all is run successfully than program terminate. If any iteration stop than program
will exit automatically
for the first time, the TeamScore = TeamScore+bonusScore (0+5)

the second time, TeamScore+baseScore (5+2) (because of the cost of 50)

the third half - TeamScore+bonusScore (7+5)

for the fourth time - TeamScore+bonusScore (11+5)

for the fifth time, TeamScore+bonusScore (17+2)

final teamscore = 19 in the display.

3. Regarding individualScores, bonusScore, baseScore, and teamScore, are they used as a


variable or constant in the given Swift program? Give your answer in the following
table.

*Solution

Identifier Is it used as a variable or constant?


individualScores It is the constant array type which stores integer type of values
bonusScore It is the constant which cannot be change
baseScore It is the constant which cannot be change
teamScore It is the var data type which can change at run time

4. What do you think the difference is between the keywords let and var? Hint: Deduce
from how they are used in the program. Think about whose value will ever be adjusted
throughout the program?

*Solution

Inability to assign to property The 'let' constant 'variable Name'


Variable can be change from 'let' to 'var'.
This adjusted Individual Scores, Bonus Scores, Base Score, Team Score, and
Description which fixed-type constant matrix to stores a signed integer type; it cannot
be altered. This type of data can vary throughout run time even though it is a constant
that cannot be altered.
- End of questions (for 3 programs) -

You might also like