lOMoARcPSD|4828471
FINAL December 2016, questions and answers
Programming Methodology I (Concordia University)
StuDocu is not sponsored or endorsed by any college or university
Downloaded by The Merc (
[email protected])
lOMoARcPSD|4828471
Concordia University
Department of Electrical and Computer Engineering
FINAL EXAMINATION QUESTIONS
COEN 243 2016 Sections D,U – Programming Methodologies
Instructors: Yan Liu, Chadi Assi
Saturday Dec 17, 2016
9:00am – 12:00 noon (3 Hours)
Instructions:
- Closed book and no calculators allowed.
- Clearly state all reasonable assumptions.
- Handwriting must be legible.
- Questions sheet must be signed and handed in
- Answers must be written in the answer sheets provided.
I am aware of the university regulations concerning cheating and plagiarism: [ ] yes, [ ] no.
Consistent with the code of ethics of my (future) profession, I will not cheat during this
examination:
_____________ ____________________ ___________________
_
Student ID Printed Name Signature
lOMoARcPSD|4828471
Part 1 Multiple Choice Answer (55 marks, 3 marks each Q1-Q20, 5 marks each Q21-Q23)
Q1. The data type bool:
a. Can take on values true and false.
b. Can take on any expression as a value.
c. Can take on values -1, 0 or 1.
d. Can only be used in a selection statement.
Q2. Which of the following statements initializes the unsigned int variable counter to 10?
a. unsigned int counter = 10;
b. unsigned int counter = {10};
c. unsigned int counter{10};
d. All of the above.
Q3. If a variable is declared in the initialization expression of a for statement, then:
a. It is automatically reinitialized to zero once the loop is finished.
b. The scope of the variable is restricted to that for loop.
c. It retains its final value after the loop is finished.
d. It can not be used in any structures that are nested in that for structure.
Q4. Which of the following is a parameterized stream manipulator used to format output?
a. setw
b. right
c. left
d. fixed
Q5. The expression if (num != 65) cannot be replaced by:
a. if (num > 65 || num < 65)
b. if (!(num == 65))
c. if (num – 65)
d. if (!(num – 65))
Q6. Consider the following code, assuming that x is an int with an initial value of 12
if(x = 6) {
cout << x;
}
What is the output?
lOMoARcPSD|4828471
a. 6
b. 12
c. Nothing.
d. A syntax error is produced.
Q7. Converting from type ________ to type ________ will result in the loss of data.
a. bool, char.
b. float, double.
c. int, char.
d. short, long.
Q8. Call-by-reference can achieve the security of call-by-value when:
a. The value being passed is small.
b. A large argument is passed in order to improve performance.
c. A pointer to the argument is used.
d. The const qualifier is used.
Q9. Overloaded functions must have:
a. Different parameter lists.
b. Different return types.
c. The same number of parameters.
d. The same number of default arguments.
Q10. Which of the following is not a correct way to initialize the array named n?
a. array<int, 5> n{0, 7, 0, 3, 8};
b. array<int, 5> n{0, 7, 0, 3, 8, 2};
c. array<int, 5> n{7};
d. array<int, 5> n{9, 1, 9};
Q11. (*max)(num1, num2, num3);
a. Is the header for function max.
b. Is a call to the function pointed to by max.
c. Is the prototype for function max.
d. Is a declaration of a pointer to a function called max.
Q12. When a function argument is called by reference:
a. The program will make a copy of the value of the argument and use it in the function and
delete it when the function is finished.
b. The program will only use global variables to access any reference arguments.
c. The program will use the address of the argument to read and write data.
d. The program will make a new address for the argument to read and write the data for the
function and will let the main program refer to the old address after the function is finished.
lOMoARcPSD|4828471
Q13. Assuming that t is an array and tPtr is a pointer to that array, which expression refers to
the address of element 3 of the array?
a. *(tPtr + 3)
b. tPtr[3]
c. &t[3]
d. *(t + 3)
Q14. The first step performed by the binary search algorithm at each iteration is to:
a. Compare the search key with the lowest element in the current set of data.
b. Compare the search key with the middle element in the current set of data.
c. Compare the search key with the highest element in the current set of data.
d. Count the number of elements in the current set of data.
Q15. The output produced by the following program:
#include <iostream>
using namespace std;
int main()
{
int i = 5;
int j = 6;
int k = 7;
int l;
l = (++i + j--) / (--k);
cout << l << " " << " " << i << " " << j << " " << k << endl;
return 0;
}
is:
(a) 2 5 5 6
(b) 2 6 5 6
(c) 2 6 5 5
(d) 0 0 0 0
(e) 2 6 6 7
Q16. The output produced by the following program is:
#include <iostream>
using namespace std;
int main()
{
lOMoARcPSD|4828471
int keith = 5;
int* const keef = &keith;
*keef = 70;
cout << keith << endl;
return 0;
}
(a) 5
(b) 70
(c) No output is produced as the program does not compile
(d) 0
(e) 5 70
Q17. The output produced by the following program is:
#include <iostream>
using namespace std;
int main()
{
int keith = 5;
int mick = -5;
int* const keef = &keith;
*keef = 70;
cout << keith << endl;
keef = &mick;
*keef = 5;
cout << mick << endl;
return 0;
}
(a) 70
5
(b) 70 5
(c) No output is produced as the program does not even compile.
(d) 5
70
(e) 5 70
lOMoARcPSD|4828471
Q18. What is the output of the following code segment?
n = 1;
while (n <= 5)
cout << n << ' ';
n++;
a) 1 2 3 4 5
b) 1 1 1... and on forever
c) 2 3 4 5 6
d) 1 2 3 4
e) 2 3 4 5
Q19. What will the following code display?
int numbers[] = {99, 87, 66, 55, 101 };
cout << numbers[3] << endl;
a) 55
b) 66
c) 101
d) 87
Q20. What is the value stored at x, given the statements:
int x;
x = 3 / static_cast<int>(4.5 + 6.4);
A) .3
B) 0
C) .275229
D) 3.3
E) None of these
lOMoARcPSD|4828471
The next three questions are based on the program below (5 marks each)
#include <iostream>
#include <array>
const size_t ARRAY_SIZE{7};
void print_array(std::array<int,ARRAY_SIZE>& arr);
bool compare(int first, int second);
void insertion_sort
(std::array<int, ARRAY_SIZE>&, size_t size,bool (*compare)(int first, int second));
int main(){
int array[ARRAY_SIZE];
std::array<int, ARRAY_SIZE> arrTemplate{10, 130, 23,45,33, 89, 55};
insertion_sort(arrTemplate, ARRAY_SIZE, compare);
print_array(arrTemplate);
return 0;
}
void print_array(std::array<int,ARRAY_SIZE>& arr){
for(int& item:arr){
std::cout << item << "\t";
}
std::cout<< std::endl;
}
bool compare(int first, int second){
if(first<second)
return true;
else
return false;
}
void insertion_sort(std::array<int, ARRAY_SIZE>& arr, size_t length, bool (*compare)(int
first, int second)){
int i,j,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && compare(arr[j - 1], arr[j])) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}
lOMoARcPSD|4828471
Q 21. The output of the program is:
a) 130 89 55 45 33 23 10
b) 10 130 23 45 33 89 55
c) 10 23 33 45 55 89 130
d) None of the above
Q22. The function insertion_sort() has one argument as std::array<int, ARRAY_SIZE>& arr.
What is description about it is true given the program in the context.
a) It is pass-by-reference, and the original array was copied, therefore elements of the
original array passed are not changed.
b) It is pass-by-value, and the original array was copied, and therefore elements of the
original array passed are not changed.
c) It is pass-by-reference, and elements of the original array are changed.
d) It is pass-by-value, and elements of the original array are changed.
Q23. Suppose we output the intermediate state of the array by changing one line of the
insertion_sort() as
void insertion_sort(std::array<int, ARRAY_SIZE>& arr, size_t length, bool (*compare)(int
first, int second)){
int i,j,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && compare(arr[j - 1], arr[j])) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
print_array(arr); //only this line is added;
}
The program runs on the main() function and insertion_sort() is called. What’s the output when i
equals to 2?
a) 130 10 23 45 33 89 55
b) 130 23 10 45 33 89 55
c) 130 45 23 10 33 89 55
d) 130 45 33 23 10 89 55
e) None of the above
lOMoARcPSD|4828471
Part II Answer all three questions in this section (20 marks, 10 marks each)
Q24. What is the output of this code: (show your work step by step)
int mystery(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return mystery (n-1) + mystery (n-2);
}
int main()
{
cout<<mystery(6);
}
Q25. What is the output of this code:
char * fun(char s[], char c1, char c2)
{
char *start;
for(start = s; *start != '\0'; start++)
{
if (*start == c1)
{
*start = c2;
}
}
return s;
}
int main()
{
char word[]="mississippi";
char* wptr;
char s ='p';
char t ='t';
wptr = fun(word, s, t);
cout<<wptr;
return 0;
}
mississitti
lOMoARcPSD|4828471
Part III Write code (25 Marks)
Q 26. Complete the heading for function F so that, on return from the call to F in main, the
variable n1 contains the value 3 and the variable n2 contains the value 7. (5 Marks)
void F ( _________________________________ ) {
k1++;
k2 = k2+2;
}
int main () {
int n1=3, n2=5;
F(n2,n1);
cout<<n1<<endl;
cout<<n2<<endl;
}
void F (int &k2, int k1)
Q27. Write a program that asks the user to type 10 integers of an array and an integer value V.
The program must search if the value V exists in the array and must remove the first occurrence
of V, shifting each following element left and adding a zero at the end of the array. The program
must then write the final array. (Marks 20)
int main()
{
int array[10];
int input, ctr=0, move;
for (int x=0; x<10; x++)
{
cout<<"Enter a number on index ["<<x<<"]: "; cin>>array[x];
}
int v;
cout<<"Enter the value of 'v': "; cin>>v;
for (int y=0; y<10; y++)
{
if (array[y]==v)
{
if (ctr==0)
{
ctr=y;
}
}
}
if (ctr==0)
{
cout<<endl<<"***Cannot find Value of v is not in the array***"<<endl;
}
else if (ctr!=0)
{
move=array[ctr];
for (long int z=ctr; z<10; z++)
10
lOMoARcPSD|4828471
{
array[z]=array[move];
move++;
}
array[9]=0;
for (int d=0; d<10; d++)
{
cout<<array[d]<<", ";
}
}
return 0;
}
11