You are on page 1of 24

ENGN 1310 ̶ Computer Programming with Engineering Applications

Lecture 12

Revision

Dr. Amr El Mougy


Associate Dean for the Faculty of Science and Innovation
Program Director for Mathematical and Computational Sciences
amr.elmougy@uofcanada.edu.eg
Synopsis

What is Java, how to compile and run “Hello World!”


 Primitive datatypes and their properties
Literals and special values
Type compatibilities (explicit and implicit cast)

 Simple expressions (+, -, *, /, %, &, |, ˜, >>, . . . ), their properties, etc.


 Operand and result types, precedence, etc.

 Assignments (expression with essential side effect)


 Conditional operations (if – else, switch-case)
 Iterative operations using while, for
 Vectors, Matrices
 Functions
Conceptual Questions

1. What is the value of x after x = uint8(600)? ___________________________


A) 600 C) 0
B) Nothing. An error is produced D) 255

2. What is the type and result of this expression? (9 / uint64(5 - 3.0) + 13 / 2 + mod(15,4)) * 2

uint64
Type: _____________________________ 30
Result: ___________________________________

3. What (exactly) is displayed by the following statement?


disp(“1 + “ + 2 + 3 + “(4 + 5)” + 6 + (7 + 8));
1_+_23(4_+_5)615 where ‘ _’ represents a space
______________________________________________________________________________
Conceptual Questions
4. Suppose the next version of Matlab were to include a primitive data type called tribit that uses 3 bits to store
integers. What range of integers could be stored in a variable of type tribit?
0
__________________________________ 7
to __________________________________________

5. How many dollar signs are displayed by the following?


a = 0;
while (a < 4)
disp(“$”);
b = 0;
while (b <= 5)
disp(“$”);
b = b + 1;
end
a = a + 1;
end

28 dollar signs
______________________________________________________________________________
Conceptual Questions

6. Write a C, R, or L to indicate whether each of the following situations is a compile-time, runtime or logic error:
C Forgot a closing parenthesis
____
R Divided by a variable whose value is zero
____
C Used a variable outside its scope
____
L An improperly indented “else” caused the wrong code to be executed
____

7. Assume the character array phrase contains ‘Much mess many monkeys make’. Write a statement that copies the
substring “monkey” from phrase, converts it to uppercase, and then assigns it to a character array str.

str = upper(phrase(16:21));
______________________________________________________________________________

8. If word1 and word2 are character arrays, write a boolean expression to test if the first n characters of word1 are
identical to word2

Strcmp(word1, word2, n)
______________________________________________________________________________
Conceptual Questions

9. Complete the Truth table below (A and B are any Boolean expressions).

A B A && !B
F F F
F T F
T F T
T T F
Conceptual Questions

10. Consider the following code that assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on student's test score.
Which statement below most accurately describes the outcome?
if (score >= 90) grade = 'A';
if (score >= 80) grade = 'B';
if (score >= 70) grade = 'C';
if (score >= 60) grade = 'D';
else grade = 'F';
A) This code will work correctly only if grade < 60
B) This code will not work correctly under any circumstances
C) This code will work correctly only if grade >= 60
D) This code will work correctly in all cases
E) This code will work correctly only if grade < 70

______________________________________________________________________________
Conceptual Questions

11. What is the equivalency for the following codes


If (condition) If (~condition)
Statement 1 Statement 2
else else
Statement 2 Statement 1
end end
A. Equivalent C. Equivalent under certain conditions
B. Not equivalent D. Opposite to each other

12. Which one of the following is a correct representation of the given mathematical expression in Matlab?

•a - b / 2
•a – b / 2 % 2
•a - (b / 2) / 2
•(a – b / 2) / 2
Conceptual Questions

13. What is the output of the following code snippet?

var1 = 10;
var2 = 2;
var3 = 20;
var3 = var3 / (mod(var1, var2));
disp(var3);

A. 4
B. Inf
C. 20
D. 0
Conceptual Questions

14. In an array List that contains 5 elements, which of these produces an out of bounds error
A. List(3)
B. List(4)
C. List(5)
D. List(6)

15. For an array List of 5 elements, which of the following statements is correct
( )
List(i) = List(i) + 1;
end
A. for i = 0:length(List)
B. for i = 0:length(List) - 1
C. for i = 1:length(List)
D. For i = 1:length(List) - 1
Conceptual Questions

16. In a switch statement, if the input does not match any of the cases, _______________.
A. An error will be produced
B. The statements under “otherwise” will be executed, if present.
C. The first case is taken as the default
D. All the statements under all cases are executed

17. Complete the following Arduino code to implement an alarm system using an ultrasonic sensor and a buzzer.
The schematic of the system is shown in the figure
int trigger_pin = ; int echo_pin = ; int buzzer_pin = ; int time; int distance;

void setup ( ) {
Serial.begin (9600);
pinMode (trigger_pin, OUTPUT); pinMode (echo_pin, INPUT); pinMode (buzzer_pin, OUTPUT);
}

void loop ( ) {
digitalWrite ( , HIGH);
delayMicroseconds (10);
digitalWrite ( , LOW);
time = pulseIn ( , HIGH);
distance = (time * 0.034) / 2;
if (distance <= 10)
{
Serial.println (" Door Open "); Serial.print (" Distance= "); Serial.println (distance);
digitalWrite ( , HIGH);
delay (500);
}
else {
Serial.println (" Door closed "); Serial.print (" Distance= "); Serial.println (distance);
digitalWrite ( , LOW);
delay (500);
}}
int trigger_pin = 2; int echo_pin = 3; int buzzer_pin = 10; int time; int distance;

void setup ( ) {
Serial.begin (9600);
pinMode (trigger_pin, OUTPUT); pinMode (echo_pin, INPUT); pinMode (buzzer_pin, OUTPUT);
}

void loop ( ) {
digitalWrite ( trigger_pin, HIGH);
delayMicroseconds (10);
digitalWrite (trigger_pin, LOW);
time = pulseIn ( echo_pin, HIGH);
distance = (time * 0.034) / 2;
if (distance <= 10)
{
Serial.println (" Door Open "); Serial.print (" Distance= "); Serial.println (distance);
digitalWrite ( buzzer_pin, HIGH);
delay (500);
}
else {
Serial.println (" Door closed "); Serial.print (" Distance= "); Serial.println (distance);
digitalWrite ( buzzer_pin, LOW);
delay (500);
}}
Conceptual Questions
18. The function isstrprop(str, ‘lower’) returns a Boolean value of (true) for each character in str that is
lowercase. What is the output of this code snippet?

str = ‘ABCabc’;
i = 1;
while (i < length(str))
ch = str(i);
if (isstrprop(ch, ‘lower’))
disp(i + " ");
else
i = i + 1;
end
end

A. 4 4 4 4 4 ... (infinite loop)


B. 4
C. 456
D. 123
Conceptual Questions
19. Which of the following is correct about a local variable?
A. It is declared exclusively in the main program.
B. It is declared before all the functions in a program.
C. It is declared within the scope of any function/program.
D. It is visible to all the functions declared after it.

20. Which of the following is true about functions?


A. Functions can have one argument and can return multiple return values.
B. Functions can have multiple arguments and can return multiple return values.
C. Functions can have only one argument and can return only one return value.
D. Functions can have multiple arguments and can return one return value.
Conceptual Questions

21. Fill in the blank to find the total for the specific row of a two-dimensional matrix data2D with
dimensions ROWS and COLUMNS?

total = 0;
for i = 1:COLUMNS
total = total + ______________;
end

A. data2D(i,ROWS)
B. data2D(i,row)
C. data2D(ROWS,i)
D. data2D(row,i)
Conceptual Questions

22. In Arduino programming, what is the result of this statement


int var = 4.3;
A. the value 4.3 is assigned to the variable named var
B. The rounded value 4 is assigned to the variable named var
C. An error is produced due to wrong variable types
D. The variable var will contain null

23. Fill in the blank for this algorithm for removing an element at a position in an ordered array,
assuming currentSize indicated the actual number of elements in the data array?

for (i = position + 1:currentSize)


____________________
end
currentSize = currentSize - 1;

A. data(i) = data(i + 1);


B. data(i + 1) = data(i);
C. data(i – 1) = data(i);
D. data(i – 1) = data(i + 1);
Conceptual Questions

24. In Arduino programming, what is the result of the following code


int x = 4;
if x < 5
Serial.print(x);
Serial.print(“ is the value”);

A. 4 is the value
B. 4
C. is the value
D. Nothing

25. What is the result if the value of x is changed to 6


A. 4 is the value
B. Nothing
C. 4
D. is the value
Programming Questions

The letters and their corresponding point values in the game Scrabble can be represented by the following
declarations:

letters = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
values = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10];

So, ‘A’ is worth 1 point, ‘B’ is worth 3 points, and so on. Given these declarations, write a fragment of code
to compute and display the total point value for a word stored in the variable word. You can assume the
word contains only uppercase alphabetic letters. (NOTE: You do not have to consider blanks as in the real
game, nor do you have to take into account that some positions on the Scrabble board give double and
triple score values).
letters = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’;
values = [1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10];
word = ‘SAMPLE’;

% ---- only required the following code fragment


score = 0;
for i = 1:length(word)

score = score + values(find(letters == word(i)));

end
disp("q2: Word score is = " + score);
% ------------
Programming Questions

Write a function, countChars, that accepts an array of names as a parameter and returns a total count of the number
of characters in all names EXCEPT the longest name. Use only a single loop.
function total = countChars(names)

longest = 0;
total = 0;
for (i = 1:length(names))

len = length(convertStringsToChars(names(i)));
if (len > longest)
longest = len;
end
total = total + len;
end
total = total - longest;
end
Programming Questions

At the end of the semester, your professor needs a software to help him/her prepare the final grades of the course. This
software has the following features:
• It allows the professor to add students to the course
• It allows the professor to delete students from the course
• Each added student has six entries: name, ID number, coursework (out of 100), midterm (out of 100), final (out of 100),
overall grade (out of 100). The coursework has weight 30%, midterm has weight 30%, while the final has weight 40%
• The list of students must not at any time have two entries with the same ID (duplicate names are allowed)
• Calculate the average overall grade of all students

Write a program that implements these features. Your program should start by displaying a welcome message and the
list of options to the user. Once the user chooses an option, follow up actions are taken accordingly. These options and
actions are:

1. Adding a student and his/her grades: if the user chooses this option, the program will ask the user to enter a name
and an ID number. The program should then check that this ID number does not exist in the database. If it does not
exist, then the program should also prompt the user to enter three grades for coursework, midterm, and final. The
program then automatically calculates, stores, and displays the overall grade according to the aforementioned weights.
Programming Questions

2. Deleting a student: if the user chooses this option, the program will ask the user to enter a name and an ID number. If
this ID number does not exist, or belongs to a different name, an error message is displayed.
3. Modifying grades of a student: if the user chooses this option, the program will ask the user to enter a name and an ID
number. The program then checks that this ID number exists and belongs to this name. If it doesn’t, an error message is
displayed. If it does, the program asks the user to enter three grades for coursework, midterm, and final. The program then
automatically calculates, stores, and displays the overall grade according to the aforementioned weights.
4. Searching for a student: here the user is asked only to enter a name. The program then checks if the name is found. If
not, an error message is displayed. If one or more entries are found matching this name, they are all displayed with their
corresponding grades.
5. Calculate the average overall grade: if the user chooses this option, the program calculates and displays the average
of overall grades of all entered students.
6. Quit

Once the user chooses an option and the follow up actions are executed, the program should go back to display all
options again and allows the user to make another choice. This should continue until the user chooses the 6th option. Your
program should implement at least three functions.
Hint: you may find Matlab functions such as find, isempty, and ismember helpful.

You might also like