Date: December 3rd, 2010
Time: 8:10-10:00
Open book; turn off computer & mobile phone
#include <iostream>
int main() // Understanding bitwise operators.
{
unsigned s = 2010;
int i = (s >> 5) & ~(~0 << 4);
std::cout << i << std::endl;
}
10 INPUT A, BIf we want to get the minimum of two input numbers, what expressions should be filled into the blank in line 40?
20 WHILE NOT (A=0 AND B=0)
30 PRINT "MIN("; A; ","; B; ")=";
40 PRINT __________
50 INPUT A, B
60 WEND
70 END
10000000 00000001
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
// break vs. continue
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int sum=0, i = 1;
do {
if (i++ == 5) continue;
sum += i++;
} while (i<10);
cout << i << '\t' << sum << endl;
return 0;
}
(10%) What will be the output of the following bwBASIC program?
10 LET N = 5
20 LET A$ = STRING$(N-1, " ") + STRING$(2*N-1, "*")
30 FOR I=1 TO N
40 PRINT MID$(A$, I, I+N-1)
50 NEXT I
60 END
(10%) Determine whether the following code has syntax erros or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
#include <iostream>
int main() // break in a nested loop
{
for (int i = 1; i < 6; i++)
for (int j = 1; j < 6; j++)
{
if (j==3)
break;
std::cout << i << std::endl;
}
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int i, sum;
for (i=10, sum=0; i>5; sum+= i--)
cout << "i = " << i << endl; cout << "sum = " << sum << endl;
}
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char name[] = "tpgm";
int i = 0;
while (name[i] != '\0')
name[i++] = name[i++] ^ 0x20;
cout << i << name << endl;
return 0;
}