Vous êtes sur la page 1sur 14

UNIVERSITY OF MAURITIUS

FACULTY OF ENGINEERING

SECOND SEMESTER/YEARLY EXAMINATIONS

MAY 2012
PROGRAMME

BSc (Hons) Electronics with Computer Science


BEng (Hons) Electronic and Communication Engineering
BEng (Hons) Electrical and Electronic Engineering
BSc (Hons) Information and Communication Technologies

MODULE NAME
DATE

Computer Programming
Friday

MODULE CODE

CSE 1018Y(1)

04 May 2012

TIME
NO. OF
QUESTIONS SET

09:30 12:30 Hrs


5

DURATION
NO. OF QUESTIONS
TO BE ATTEMPTED

INSTRUCTIONS TO CANDIDATES
There are 2 Sections in this paper: Section A and Section B.
Section A consists of 3 questions. Answer ALL questions.
Use the MCQ Sheet to answer Question 1.
Section B consists of 2 questions. Answer ALL questions.
Use separate answer books for each Section.

3 hours
5

COMPUTER PROGRAMMING CSE 1018Y(1)


SECTION A
Answer ALL questions
Use separate answer books for each Section.
Use the MCQ Sheet to answer Question 1
Question 1 (Multiple Choice questions)

[20 x 1 Marks]

1. Select the correct value of variable b after executing the following code:
int a=5;
int b=5*(7/2);

A.
B.
C.
D.
E.

5
17.5
15
35.5
NONE of the above

2. Which of the following is the correct function prototype for a function,f, which
accepts 2 integers and return a float?
A.
void float f(int a, int b)
B.
void f(int a, int b)
C.
void f(float a, int b, int c)
D.
float f(int a, int b)
E.
NONE of the above
3. Trace the following code:
int a=7;
int b=5;
int c=7%5;
if (c==0)
printf( Hello!);
else
if(c==1)
printf( Hi!);
else
if (c==2)
printf( Bye!);
else
printf(Dear!);
printf(How are you?);

Which of the following would be the correct output?


A.
Hello!
B.
Hi! How are you?
C.
Bye! How are you?
D.
How are you?
E.
NONE of the above
Page 1 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


4. What is the correct value of I to be used for the following code to print Hello this is
fun:
if (I%2==0)
if(I%3==0)
printf(Hello this is fun);
else
printf(Cool Guys);
else
printf(It is 3.143)

A.
B.
C.
D.
E.

2
7
6
3
NONE of the above

5. Select the correct syntax for a loop which runs exactly 5 times
A.

for (i=1;i<5;i++)
{
//code to be executed
}

B.

int i=8;
while (i<13):
{
//code to be executed
}
int i=9;
while (i>4)
{
//code to be executed
i--;
}
for (i=5;i>1;i--)
{
//code to be executed
}

C.

D.

E.

NONE of the above

6. Which of the following contains all the possible value which a%b can yield, given
that both are ints?
A.
0,1,2.....a
B.
a.....b
C.
1,2,3,....a
D.
0,1,2,.....b-1
E.
NONE of the above

Page 2 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


7. Out of the types of loop which follow, which one always executes at least once?
A.
for loop
B.
while
C.
do-while
D.
pre-test loop
E.
NONE of the above

8.

Which of the following are true with reference to functions.


I. Functions allow for Code Re-use by reducing repetition
II. Functions ease Debugging.
III. Function make programs more complex to understand
IV. Functions make programs modular and facilitate abstraction

A.
B.
C.
D.
E.

All of the above


I and III only
I and IV only
I, II and IV
NONE of the above

9. Which of the following keywords is used to import header files so as to use the
functions they contain?
A.
import
B.
include
C.
define
D.
Return
E.
NONE of the above
10. Select the correct option for the output of the following code.
int I=6
for (I=8;I<10;I++)
printf(%d,I);
printf(-)
while (I>5)
{
printf(%d,I);
I--;
}
printf()

A.
B.
C.
D.
E.

678910-9876
6789-98765
678910-9876
89-109876
NONE of the above
Page 3 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


11. Refer to the C program given below. What will be the output of the program?
#include<stdio.h>
int main(){
int x=5;
do{
printf("%d,",x);
x=x+1;
}
while(x<10);
return 0;
}

A.
B.
C.
D.
E.

5,6,7,8,9,
5,5,5,5,5,
5,6,7,10,11
f,f,f,f,f,
NONE of the above

12. What will be output of following program?


int call();
void main(){
int *ptr;
*ptr=call();
printf("%d",*ptr);
}
int call(){
int a=25;
a++;
return a;
}

A.
B.
C.
D.
E.

25
26
Any address
Garbage value
Compiler value

13. What will be output if you will compile and execute the following C code?
#include <stdio.h>
void main(){
int array[]={10,20,30,40};
printf("%d",-2*array[2]);
}

A.
B.
C.
D.
E.

-60
-30
60
Garbage value
Compiler error
Page 4 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


14. An Array passed as an argument to a function in C is interpreted as
A.
B.
C.
D.
E.

Address of the array


Value of the first element of the array
Address of the first element of the array
Number of elements of the array
None of the above

15. Predict the output of the program below.


void e(int );
main()
{
int a;
a=3;
e(a);
}
void e(int n)
{
if(n>0)
{
e(--n);
printf("%d
e(--n);
}
}

A.
B.
C.
D.
E.

" , n);

0 1 2 0
0 1 2 1
1 2 0 1
1 2 0 1
NONE of the above

The structures to maintain details about employees can be defined as given below.
Questions 16,17 & 18 refer to this struct.
struct date
{
int dd;
int mm;
int yy;
};
struct employee
{
int eid;
char ename[20];
struct date joined;
};

Page 5 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


16. Which of the following is the most appropriate statement for creating and initializing
a variable E of type employee?
A. struct employee E={010,"Deepak",22/1/2010};
B. struct employee E={010,"Deepak",{22,1,2010}};
C. struct employee E={010,"Deepak,22-1-2010};
D. struct employee E={010,"Deepak,22,1,2010};
E. NONE of the above

17. The prorotype of a function to input an employee struct is


A. void input(struct employee E)
B. void input(char E[])
C. int input()
D. void input(struct employee *E))
E. NONE of the above

18. Predict the output of the following program.


#include <stdio.h>
struct date
{
int dd;
int mm;
int yy;
};
struct employee
{
int eid;
char ename[20];
struct date joined;
};
void main()
{
/*code to create and employee E with eid=10, ename=Deepak and
date joined is 22/1/2010*/
show(E);
}
show(struct employee e)
{
printf("%d %s
%d/%d/%d",e.eid,e.ename,e.joined.dd,e.joined.mm,e.joined.yy);
}

A.
B.
C.
D.
E.

10 Deepak 1/22/2010
010 Deepak 22/1/2010
10 Deepak 22/1/2010
Deepak 22/1/2010
NONE of the above

Page 6 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


The program below can be used to read a number of values (for age) from a file
myfile.txt and display the values on screen. Questions 19 and 20 refer to this
program.
#include <stdio.h>
int main()
{
/* define MAXLEN at start */
FILE *inFile;
int age;
inFile = fopen( "myfile.txt", "r" );
if (????) {
printf ("Error opening myfile.txt\n");
return 0;
}
while (*****){
fscanf(inFile, "%d", &age );
printf( "%d\n", age );
}
fclose( inFile );
return 1;
}

19. Which one of the following can replace the ???? in the code above to determine if
the file (myfile.txt) does not exist?
A. inFile==NULL
B. empty(inFile)
C. null(inFile)
D. inFile == NULL
E. NONE of the above
20. Which one of the following can replace the ***** in the code above to determine if
end-of-file is reached?
A. feof(inFile )
B. !eof(inFile)
C. eof(inFile )
D. inFile == NULL
E. NONE of the above

Page 7 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


Question 2 [Total 15 Marks]
In Bouboun University's Information Systems 3 arrays are used to store student details,

Stud_Names (array of strings) holds the name of 100 Students


Stud_Map (array of strings) hold the ids of the 100 students (whose details are stored
Stud_Name)
Stud_Marks_Mod1 (array of floats) holds the grades of grades of each of the 100 students
Module1
Stud_Marks_Mod2 (array of floats) holds the grades of grades of each of the 100 students
Module2
Stud_Marks_Mod3 (array of floats)holds the grades of grades of each of the 100 students
Module3

in
in
in
in

For example, if John Smith is the 30th Student on the attendance sheet and has scored 50
in module 1, 70 in module 2 and 20 in module 3 - this implies that his name is in position
29 in Stud_Names, his marks for module 1 will be at position 29 in Stud_Marks_Mod1,
marks for module 2 will be at position 29 in Stud_Marks_Mod2 and so on.
Write a program in C which computes the average marks for each student and displays
the Name and Id of the student with the highest average.
[15 marks]
Question 3 [Total 15 marks]
A bookshop wishes to maintain a computerized list of books and for that purpose a
struct Book is defined as follows:
struct Book
{
char title[25];
char author[25];
float cost;
int pub_year;
int ssn;
};
(a)
(b)
(c)

Write a function input() in C that can be used to input details for a struct Book.
[6 marks]
Write a function output() in C that can be used to output details of a struct Book.
[3 marks]
Write a function main() in C that:

Declares an array myBook of five(5) Books


Inputs details of the five(5) books using the function input() defined in (a)
Finds the oldest book from the five(5) books
Displays the details of the five(5) books using the function output() defined
in (b)
[1+ 1.5+ 2.5 + 1 marks]
Page 8 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


SECTION B
Answer ALL questions
Use separate answer books for each Section.
Question 4
Mauritius Revenue Authority (MRA) is a public organization that collects taxes from
employees and profit-making companies. An employee will pay tax amounting to 15%
of their taxable income, given by their total annual salary minus some tax-exemption
threshold. The tax-exemption threshold is Rs 250000 for single persons, and Rs 300000,
Rs 350000 and Rs 400000 for parents with one child, two children and three (or more)
children, respectively. For example, if a particular employee has two children and earns
an annual salary of Rs 500000, his taxable income will be 500000 350000 = 150000, on
which he will pay a tax of 15%. If the same employee earns Rs 270000, his taxable income
is nil and he does not get to pay any tax.
A company, on the other hand, will pay to MRA a straight-forward value-added tax
(VAT) of 17% on total annual revenue obtained from sales of items or services.
The following diagram shows a class diagram.
Customer
int TAN
char address[]
Customer(int, char[])
float CalculateTax()=0

Employee
int num_child
float total_annual_salary
float tax_rate = 15%
Employee(int, char[], int,
float)
float CalculateTax()

Company
float total_annual_sales
float vat_rate = 17%

Company(int, char[], float)


float CalculateTax()

The full implementation of the class Customer is given below:


// Customer.h
#include <cstring>
#ifndef CUSTOMER_H
#define CUSTOMER_H
class Customer{
private:
int TAN;
//Tax Account Number of customer
char address[];
//address of customer
public:
Customer(int , char [] );
virtual float CalculateTax()=0;
};
#endif

/Contd next page


Page 9 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


Question 4 [Contd]
//Customer.cpp
#include customer.h
#include <cstring>

//using strcpy()

Customer::Customer(int tan_num, char addr[]){


TAN = tan_num;
strcpy(address, addr);
}

(a)

(i)

(ii)

In the above class diagram, CalculateTax() is a pure virtual method. State


what is the implication of having a pure virtual method in the class
Customer.
In the implementation of class Customer, the following preprocessor
directives were used.
#ifndef CUSTOMER_H
#define CUSTOMER_H
// header file information
#endif

State the purpose of the above preprocessor directives.


(iii)

State, with justifications, whether the following piece of code will compile:
Customer *acust = new Customer(569969, Route du Club, Vacoas);

[2 +2+2 Marks]
(b)

(c)

Write both the Interface and Implementation of the class Employee shown in the
above class diagram.
[12 Marks]
Read the following main program:

#include customer.h
#include employee.h
#include company.h
void main(){
Customer *cust_list[15];
cust_list[0] = new Employee (788877, Royal Road, Beau Bassin, 5, 788900.45);
cust_list[1] = new Company (988477, Plaine Lauzun , 7325900.45);
cust_list[2] = new Employee (611177, Remy Olier, Rose Hill, 0, 348900.88);
. . . . .
cust_list[13] = new Employee (788877, Royal Road, Beau Bassin, 2, 986900.45);
cust_list[14] = new Company (918827, Phoenix Industrial Zone, 50000000.00);
.
.
.
}

(i)

Include in the above main program a section of code that displays the total
tax paid to MRA by all the customers.
[4 Marks]

(ii)

Describe the modifications you would make to the system so the code of
part(c)(i) displays the details of all customers who pay taxes.
[3 Marks]
Page 10 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


Question 5
(a)

The Employee Details and Company details as described in Question 4 are stored
in the files employee.txt and company.txt with each field stored on a separate
line. Assume that a displayDetails() function has been added to the different classes
to display the relevant details and that relevant accessor function have been
added to the different classes . The MRA requires that ALL the customer details
be displayed after sorting in order of TAN. Also, the total amount to be received
from all the customers is to be retrieved. The following codes are written:

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#include "customer.h"
#include "employee.h"
#include "company.h"
//i) Missing codes to define functor to do sorting according to TAN
//ii) Missing codes to define function calculateTotalRevenue, which takes a vector as
parameter, to compute the total to be received by MRA
int main()
{
//iii) Missing codes to initialise vector of customers declared as customers;
int TAN;
char address[100];
int num_child;
float total_salary;
char dummy[10];
//Reading employees data
ifstream employeesfile("data/employees.txt");
if(employeesfile.is_open())
{
while(!employeesfile.eof())
{
employeesfile>>TAN;
employeesfile.ignore();
employeesfile.getline(address,100,'\n');
employeesfile>>num_child;
employeesfile>>total_salary;
employeesfile.ignore();
employeesfile.getline(dummy,10,'\n');
//iv) Missing codes to add the Employee to customers vector
}//end while
employeesfile.close();
}//end if
//Reading company data
ifstream companyfile("data/company.txt");
float total_sales;
if(companyfile.is_open())
{
while(!companyfile.eof())
{

Page 11 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


companyfile>>TAN;
companyfile.ignore();
companyfile.getline(address,100,'\n');
companyfile>>total_sales;
companyfile.ignore();
companyfile.getline(dummy,10,'\n');
//v) Missing codes to add the Company to customers vector
}//end while
companyfile.close();
}//end if

//vi) Missing codes to sort the customers vector according to the functor defined in
part i)
//vii) Missing codes to display the customers in the customers vector

//viii) Missing codes to display the total revenue received by MRA by calling function
calculateTotalRevenue defined in ii) above
return 0;
}//end main

Complete the different parts of missing codes to perform the desired behavior.
Note: You may find the following functions for the vector class useful:
push_back(), iterator, begin(), end(), size()
[17 Marks]
(b)

The MRA has stored the TANs of late tax payers for 2010 and 2011 in two
different files. The TANs are not ordered. It may also happen that a file may
contain a TAN more than once. You are required to write codes that give the
TANs of all the customers who were late payers both in 2010 and 2011. The
following codes have been written:

#include
#include
#include
#include

<iostream>
<fstream>
<algorithm>
<set>

using namespace std;


int main()
{
set<int> tans2010;
set<int> tans2011;
set<int> commonretards;
int TAN;
ifstream tans2010file("data/TAN2010.txt");
if(tans2010file.is_open())
{
while(!tans2010file.eof())
{
tans2010file>>TAN;
if(!tans2010file.eof())
tans2010.insert(TAN);
}//end while
tans2010file.close();
}//end if

Page 12 of 13

COMPUTER PROGRAMMING CSE 1018Y(1)


ifstream tans2011file("data/TAN2011.txt");
if(tans2011file.is_open())
{
while(!tans2011file.eof())
{
tans2011file>>TAN;
if(!tans2011file.eof())
tans2011.insert(TAN);
}//end while
tans2011file.close();
}//end if
cout<<"Common Retards "<<endl;
//i) Missing codes to compute the common retards for 2010 and 2011
set<int>::iterator it;
for(it = commonretards.begin(); it!=commonretards.end(); it++)
{
cout<<(*it)<<endl;
}
return 0;
}//end main

I. Explain why the set container is an ideal candidate to store the details of late
payers of the two years.
[3 Marks]
II. Complete the missing codes as indicated above.
[5 Marks]
Note:
You may find the set_intersection function defined in STL useful. The prototype of the
set_intersection is as follows:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );

Set_intersection constructs a sorted range that is the intersection of the sorted ranges
[first1, last1) and [first2, last2). The return value is the end of the output range.
You may also find the inserter function useful.
template <class Container, class Inserter>
insert_iterator<Container> inserter (Container& x, Inserter i);

Inserter generates an insert iterator for a container.

END OF QUESTION PAPER


/ph

Page 13 of 13

Vous aimerez peut-être aussi