=========================preview======================
(COMP102)final4_sol.pdf
Back to COMP102 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
CyberU, Summer 2002 Final Examination
Date: 22 August 2002 Time: 5:30-7:30 pm
Venue: LTB
This exam contains 10 questions in 14 pages. Please count the pages.
You have 2 hours to complete this exam.
Problem Your points Max points Problem Your points Max points
1 10 7 11
2 8 8 15
3 4 9 21
4 10 10 7
5 8
6 6
Subtotal 46 Subtotal 54
Your total points 100
Please identify yourself: Lab section Name
School Name
WebCT ID
Signature Question 1. (10 points) 1 a) (5 points) Given the following program:
struct record{
char name[15];
char course[15];
int stud_no;
};
void main() {
record stu1;
}
Mark the following statement as correct or incorrect. Mark it with CORRECT if it is correct and WRONG if it is NOT correct. You should consider both syntax and logical correctness.
// 1 point each
__WRONG__ i) stu1 = HKUST GUEST;
__WRONG__ ii) stu1.name = HKUST GUEST;
_CORRECT_ iii) stu1.stud_no = 123;
_CORRECT_ iv) record stu2={HKUST GUEST,COMP102,123};
__WRONG___ v) strcpy(stu1,stu2);
1 b) (5 points) What is the value of each of the following expressions?
i) false || 0 || false && true
Ans : _________0/false___________
ii) 5==1>2
Ans : _________0/false___________
iii) double(-13%7/4)
Ans : _________1_________________
iv) true!=!false
Ans : _________0/false___________
v) (h-a)/(e-b)
Ans : ________2__________________
Question 2. (8 points) Read the following programs and write the output of each program. If there is/are compile error(s) or fatal runtime error(s), indicate the error(s) and correct it/them.
2 a) (2 points)
#include <iostream.h>
int func1(int a[])
{ cout << a[0] << a[1];a[0] = 5;return 0;
}
int main(){ int a[4] = {1,2,3,4};
func1(a);
cout << a[0] << a[1];
return 0;
}
Ans: ________1252_____________ // 0.5 point for each digit
2 b) (3 points)
#include <iostream.h>
int main(){ char a[12] = "Hello World!";
for(int i = 0; i < 6; i++)
cout << a[i*2];
return 0;
}
Ans: __compile error, 13th char is out of bound __ // 1 pt for error, 2 pt for explanation
Question 2(contd) 2 c) (3 points)
#include <iostream.h>
int func1(int a[][4]){
for(int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 4; j++)
{
a[i][j] = i+j;
cout << a[i][j];
}
cout << endl;
}
return 0;}
int main(){ int a[2][4];
func1(a);return 0;}
Ans: __ runtime error, out of bound.___ // 1 pt for error, 2 pt for explanation Question 3. (4 points)What is the output of this program?
#include <iostream.h> #include <string.h>
int main(){ char str1[20]; char str2[] = "summer session";char str3[] = "summer";char str4[5] = {'c','o','m','p','\0'};
int x = strlen(str4);
int i;
for (i=0; i<x; i++)
str1[i] = str2[i+2];
str1[--i] = '\0';
if (strcmp(str2,str3)<0)
co