國立暨南國際大學 100 學年度第一學期小考試卷
(考試時間: 8:10-8:35)
科目名稱:資訊系統
與網路導論 |
開課系所:資訊工程
學系 |
任課教師
|
吳坤熹
|
系所別:
|
年級:
|
學號:
|
姓名:
|
考試日期
|
2012.1.6
|
- (10%) Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
#include <iostream>
using std::cout;
int main()
{
int i, n = 2012;
for (i=1; i<7; i++)
{
int n = 0;
for (int j=1; j<=i; j++)
n += j; // scope of variable n
cout << n << "\n";
}
cout << n << "\n";
return 0;
}
- (10%) Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
// modulus
#include
<iostream>
using std::cout;
using std::endl;
int mod(int a, int b)
{
do a -= b;
while
(a >= b);
return
a;
}
int main()
{
cout
<< mod(2012, 7) << endl;
return
0;
}
- (10%)
Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
// Caesar Cipher
#include <iostream>
using std::cout;
using std::endl;
void encrypt(char* a, int key)
{
while (*a)
{
if (*a >=
'A' && *a
<='Z')
*a = (*a - 'A' + key) % 26 +
'A';
a++;
}
}
int main()
{
char msg[] = "HELLO NCNU";
encrypt(msg, 3);
cout << msg <<
"\n";
return
0;
}
- (10%)
Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
#include <iostream>
using std::cout;
float average(float a, float b);
float average(float a, float b, float c); // overloading
int main()
{
cout << average(1.0, 5.2, 7.0) << "\n";
cout << average(4,0, 5.0) << "\n";
return 0;
}
float average(float a, float b)
{ return (a+b)/2.0; }
float average(float a, float b, float c)
{ return (a+b+c)/3.0; }
- (10%)
Determine
whether the following code has syntax errors or not. If
it is correct, predict its output. If it is incorrect, point out
the mistake(s).
// binary tree
#include <iostream>
using std::cout;
struct Node
{
char label;
Node* left;
Node* right;
};
void postorder(Node* p);
int main()
{
Node First = { 'A', NULL, NULL };
Node Second = { 'B', NULL, NULL };
Node Third = { 'C', NULL, NULL };
Node Fourth = { 'D', NULL, NULL };
Node Fifth = { 'E', &First, &Second };
Node Sixth = { 'F', &Third, &Fourth };
Node Seventh = { 'G', &Fifth, &Sixth };
postorder(&Seventh);
return 0;
}
void postorder(Node* p)
{
if (p)
{
postorder(p->left);
postorder(p->right);
cout << p->label;
}
}