Quizes Flashcards
Review past quizzes
Which of the following tasks are performed by the C preprocessor? Select all answers that seem correct.
- Include the contents of other files within a C program
2. Modify the C program
What is assembly language?
A human-readable form of machine language
What is the purpose of the linker?
Combine object files into an executable file
What does a compiler do? Select all answers that seem right.
Translates a computer program into machine language
Checks a program for syntax errors
A C program, in a file called prog4.c has a main() function with heading
int main(int num, char *name[ ]);
The program is compiled and run, using the following commands:
gcc -Wall -o prog4 prog4.c
./prog4 tom dick harry
Within the main() function, what is the value of name[2]?
Answer: “dick”
name[0] = "./prog4" name[1] = "tom" name[2] = "dick" name[3] = "harry"
define DAYS 3
A C program contains the following line:
and, further on in the same file, the following statement:
if (days > DAYS) days = DAYS;
Which of the following is the C statement that is actually compiled?
Answer:
if (days > 3) days = 3;
all variable DAYS is replaced with 3
Given the C declaration:
char name[ ] = “egbert”;
what is the value of name[4]?
Answer:
‘r’
Given the C declaration:
char name[ ] = “egbert”;
what is the value of name[6]?
Note the 0 in the answers is a zero, not an upper-case O.
Answer:
0
What is the purpose of a header file in C programming?
It provides declarations of functions defined in a separate C module
If in a C program, x is declared as
char x = 12;
What pattern of bits is stored in the computer’s memory in the space allocated for x?
answer:
8 bits = char = 1 byte
bits pattern stored in the computer’s memory in the space
allocated for x = 0001100
Given the following C statements:
char text[20] = “CSCI_247 is fun!”;
int n;
for (n=0; text[n]; n++) ;
What is the value of n after these statements?
Answer:
16
n = 0 -- text[0] = 'C' . . n =15 --text[15] = '!' n = 16 -- text[16] = 0
If in a C program, x is declared as
char x = -12;
What pattern of bits is stored in the computer’s memory in the space allocated for x?
answer:
char = 8 bits
0000 1100
2s complement 1111 0011 \+ 1 ------------- 1111 0100
If in a C program, x and y are declared as
char x = -22;
unsigned char y = (unsigned char) x;
The use of (unsigned char) in the initialization of y is a type cast. It enables the bits representing x to be viewed as an unsigned char. No bits are changed, the value of y is simply a different interpretation of the same bits.
What is the decimal value of y?
answer: 234
char = 8 bits
0001 0110
2s complement 1110 1001 \+ 1 ------------- 11101010
128 + 64 +32+10 = 234
alternatively,
11101010
258 - 22 = 236
If in a C program, x is declared as
unsigned int x;
Assuming that int is a 4-byte number, what is the largest value that x may contain?
answer:
4-byte number = 32 bits
largest value for unsigned int 2^32 -1
What is the decimal value of the hexadecimal number 2E?
answer:
0x2E => 0010 1110 => 32+14 = 46
What is the hexadecimal representation of the binary number 0111010100101100?
answer:
0111 0101 0010 1100
0x752C
A short int in C is stored on a little-endian computer in 2 bytes as:
10110011 00111010
What is the hexadecimal value of this short int?
answer:
2 bytes = 16 bits
1011 0011 0011 1010
B 3 3 A
littlen endian => 0x3AB3
In class this week, we saw that the decimal number 12345678 is the same as the hexadecimal number 0x00BC614E.
For a 2’s-complement representation, what is the hexadecimal value of the decimal number -12345678?
answer: 0xFF439EB2
In class this week, we saw that the decimal number 12345678 is the same as the hexadecimal number 0x00BC614E.
For a 2’s-complement representation, what is the hexadecimal value of the decimal number -12345678?
solution:
0000 0000 1011 1100 0110 0001 0100 1110
2s complement
1111 1111 0100 0011 1001 1110 1011 0001 + 1
1111 1111 0100 0011 1001 1110 1011 0010
0xFF439EB2
Given the following C statements:
int a[6] = {1, 2, 3, 4, 5, 6};
for (int i = 1; i < 6; i++) a[i] = a[i] + a[i-1];
What is the value of a[4]?
answer:
15
A C program contains the statement:
if (number»_space; 31) count++;
If number is a 32-bit signed integer, what does it mean if the expression
number»_space; 31
is true?
answer: negative number
number»_space; 31
let number =7 = 0111
00000000 00000000 00000000 00000111 «_space;31
means shift arithmetically to the right 31 bits
11111111 11111111 11111111 11111110
If a C program has a variable declared as:
char number = somevalue;
which expression would determine the integer value of the least significant 5 bits of number?
answer:
number & 0x1F
If a C program has a variable declared as:
char number = somevalue;
which expression will always be false whenever number is even?
number & 0x01
example:
number = 2
0000 0010 & 0000 0001 ----------------- 0000 0000 => false
number = 3
0000 0011
How is the fraction 17/16 exactly represented as a binary number?
answer:
0. 010001
Suppose we have a 8-bit representation for fixed-point numbers, in which the first 4 bits specify the integer part of the number, in 2’s complement format and the second 4 bits specify the fractional part, as a binary fraction. The binary point is implicitly between the two half-bytes.
If the 8 bits for a value are:
01011010
what is the fixed-point number represented by those bits?
answer: 5 5/8
8-bit floating point
0101 1010
1/2 +1/8 = 5/8 for fraction
5 for exponent
therefore, 5 5/8
IEEE single-precision floating-point format uses 32 bits, with one bit for the sign, 8 bits for the exponent and 23 bits for the fraction. The smallest positive number that can be represented in this format is
00000000000000000000000000000001
What value does this bit pattern represent?
answer:
32-bit
8 bit for exp
23 bits for frac
0 00000000 00000000000000000000001
Denorm
Bias = 2^7 - 1= 127
E = 1-127 = -126
M = f = 2^-23
V = 1 x 2^-23 x 2^-126
V = 2^-149
IEEE single-precision floating-point format uses 32 bits. with one bit for the sign, 8 bits for the exponent and 23 bits for the fraction. What is the exact representation of the value 1 in this format?
answer:
32-bit
8 -exp bit
23 -frac bit
00111111100000000000000000000000
Suppose we have an 8-bit floating-point number that follows the same rules as the IEEE floating-point numbers but has one sign bit, 3 bits for the exponent and 4 bits for the fraction.
With this format, what is the numeric value of the following bit sequence?
01011101
answer:
8-bit
0 101 1101 => normalize
Bias = 3
E = 5-3 = 2
M =1+ (3/4+1/16) = 1+13/16 = 29/16
V = 1 x 29/16 x 2^2 = 29/4
For the same floating-point number format described in Question 8: one sign bit, 3 bits for the exponent and 4 bits for the fraction, what is the numeric value of the following bit sequence?
00001101
Answer:
0 000 1101 => Denorm
Bias = 3 E = 1 - 3 = -2 M = f = 13/16 V = 1 x 13/16 x 2^-2 = 13/64
For the same floating-point number format described in Question 8: one sign bit, 3 bits for the exponent and 4 bits for the fraction, what bit sequence exactly represents the numeric value LaTeX: \frac{1}{64} 1 64 ?
answer
00000001