- (10%)
Convert
the following hexadecimal numbers into decimal (assuming
unsigned integers).
- (10%) Add
the following octal numbers. The answer should be written as an
octal number.
- (10%)
Answer the following questions.
- When we say the size of a variable is "4 bytes", that is
equivalent to how many bits?
- If the variable is used to store a signed integer, what is
the largest and smallest integer it can store, respectively? (You may
write a mathematical formula for calculating the numbers.)
- If the variable is used to store an unsigned integer, what
is the largest and smallest integer it can store, respectively? (You
may use a mathematical formula for calculating the numbers.)
- (10%) Determine
whether the syntax of the following code is correct or not. If it
is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Condition
#include
<iostream>
using std::cout;
int main()
{
int n = 100;
int sum = 0;
while (n > 0)
{
sum += n;
if (n = 0)
break;
--n;
}
cout << n << ' ' << sum <<
'\n';
return 0;
}
- (10%)
What will the following code display?
// Two's Complement
#include
<iostream>
using std::cout;
using std::endl;
int main()
{
signed short n = 1;
n = n << 15; cout << n << '\t';
n = n >> 1; cout << n << '\t';
n = n ^ -1; cout << n << '\t';
n = n >> 4; cout << n << '\t';
n = n + 32513; cout << n << '\n';
return 0;
}
- (10%)
What will the following code display?
// Bitwise Shift
#include
<iostream>
using std::cout;
int main()
{
short n = 10;
n <<= 1; cout << n << '\t';
n <<= 2; cout << n << '\t';
n <<= 4; cout << n << '\t';
n >>= 3; cout << n << '\t';
n /= 8; cout << n << '\n';
return 0;
}
- (10%)
Determine
whether the syntax of the following code is correct or not. If it
is correct, predict its output. If it is incorrect, point out the
mistake(s).
// Nested for-loop
#include
<iostream>
using std::cout;
using std::endl;
int main()
{
int i=1, j=1;
int sum=0;
for ( ; i<=9; i++)
for ( ; j<=9; j++)
sum
+= (i*10+j);
cout << sum << endl;
return 0;
}
- (10%)
What will the following code display?
// De Morgan's Law
// std::boolalpha
#include
<iostream>
using std::cout;
using std::boolalpha;
int main()
{
int p, q;
cout << "P\tQ\t~(P&&Q)\t~P||~Q\n";
cout << boolalpha;
for (p=0; p<=1; p++)
for (q=0; q<=1; q++)
cout
<< static_cast<bool>(p) << '\t'
<< static_cast<bool>(q) << '\t'
<< !(p&&q) << '\t'
<< (!p || !q) << '\n';
return 0;
}
- (10%)
What will the following code display?
// Euclidean algorithm for GCD
#include
<iostream>
using std::cout;
using std::endl;
int main()
{
int a=54, b=72;
int temp;
while (b!=0)
{
a %= b;
temp = a; a = b; b = temp;
}
cout << a << '\t' << b <<
endl;
return 0;
}
- (10%)
What will the following code display?
// while-loop
#include <iostream>
using std::cout;
int main()
{
bool first = true;
const int n = 7*25*27;
for (int m=n, i=3; m>1; i+=2)
while (m / i * i == m)
{
if (first)
first = false;
else
cout << '*';
cout << i;
m /= i;
}
cout << '\n';
return 0;
}
- (10%)
What will the following code display?
// Syracuse Sequence
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int n = 3;
while (n > 1)
{
cout << n << ' ';
if (n % 2)
n = 3 * n - 1;
else
n = n / 2;
}
cout << n << endl;
return 0;
}