MIDTERM TEST EESC 2031 Software Tools - York · PDF file... what is the output of the printf()...
-
Upload
phungduong -
Category
Documents
-
view
231 -
download
5
Transcript of MIDTERM TEST EESC 2031 Software Tools - York · PDF file... what is the output of the printf()...
MIDTERM TEST
EESC 2031 – Software Tools
June 13, 2017
Last Name: _____________________ First Name: __________________________
Student ID: __ __ __ __ __ __ __ __ __ EECS user name:_______________________
TIME LIMIT: 110 minutes
• This is a closed-book test. No books and notes are allowed.
• Extra space for answers can be found at the end of the test booklet.
• Assume that o size of char is 1 (byte); o size of short integer is 2 (bytes); o size of integer is 4 (bytes); o size of long integer is 8 (bytes); o size of float is 4 (bytes); o size of double is 8 (bytes);
• Also assume that the size of a pointer is 8 (bytes).
Question Value Mark
1 14.5
2 12 3 23.5+1.5 bonus
TOTAL 50 +1.5 bonus
Good Luck
Jun 13, 2017 (Tuesday) Name: Student ID: Page 3 of 15
Question 1 (14.5 pts)
1.1 (0.25 pt) For the following code fragment, what is the output of the printf() statement?
int x = 9, y = 13, z = 3;
printf("z: %d\n", z *= y + x + 2 ); z:
1.2 (1 pt) What is the output of the printf() statement?
int x = 2, y = x << 2;
printf("%d %d %d %d\n", x, y, x|y, x&y);
1.3 (1 pt) What is the output of the printf() statement?
int x = 13, y = z = 10;
y = x++; z += y++;
printf("x:%d y:%d z:%d\n", x,y,z);
1.4 (1 pt) What is the output of the printf() statement?
int x = 3; int y = 4;
printf("%d %d %d %d\n", x | y, x & y, x || y, x && y);
1.5 (0.5 pt) What is the output of the printf() statement?
int a = 6; int c = 4; float d = 1.1; short s =1;
printf("x:%.3f y:%.3f\n", s*a/c*d, d/c*a*s);
1.6 (0.5 pt) In 1.5, when d/c*a*s is evaluated, how many type conversions are performed?
What is the type of the expression?
1.7 (0.5 pt) Recall that in ANSI-C, implicit type conversions are preformed on the following
occasions
1) When the operands in an arithmetic or logical expression don’t have the same type.
2) When the type of the expression on the right side of an assignment doesn’t match the
type of the variable on the left side.
72
2 8 10 0
14 14 23
7 0 1 1
1.100 1.650
three float
3) When the type of an argument in a function call doesn’t match the type of the
corresponding parameter.
4) When the type of the expression in a return statement doesn’t match the function’s
return type.
On which of the above occasion(s), is “type promotion” performed, in which the type of the
operand with the narrower type is converted to the type of the other operand?
a. On 1)
b. On 2)
c. On 3) and 4)
d. All of the above.
1.8 (1.5 pt) Consider the following ANSI-C program, where the programmer made typos on
line 6 and 8, and also missed a else on line 8
#include <stdio.h>
int main (){
int in;
printf("Enter an integer: ");
scanf ("%d", &in);
if (in = 1) /* this is line 6 */
printf("Entered one\n");
if (in = 2) /* this is line 8 */
printf("Entered two\n");
else printf("Entered others\n");
return 0;
}
What happens when you try to compile and run the program with input 2? (chose a. or b.)
a. The program does not compile, because
b. The program compiles, output is
What happens when you try to compile and run the program with input 36? (chose a. or b.)
a. The program does not compile
b. The program compiles, output is
What happens when you try to compile and run the program with input 1? (chose a. or b.)
a. The program does not compile
b. The program compiles, output is
Entered one
Entered two
Entered one
Entered two
Entered one
Entered two
Jun 13, 2017 (Tuesday) Name: Student ID: Page 5 of 15
1.9 (0.25pt) Recall that a gcc compilation process contains three stages. The correct order is
a. Linking → Pre-processing → Compiling
b. Pre-processing → Compiling → Linking
c. Compiling → Pre-processing → Linking
d. None of the above
1.10 (1.5 pt) Consider the following statements. For each of the statements, if the right hand
side of = is a valid integer literal in ANSI-C, then specify the decimal value of x. If the right
hand side of = is not a valid integer literal, specify “invalid”.
int x = b00101101; int x = 014;
int x = 0X3f; int x = 032L;
int x = 0xfFG; int x = 0392;
1.11 (3.75 pt) Suppose flags is an integer variable with some value.
What happens to flags in each of the following 3 statements, in terms of turning on/off or
keeping the bits of flags? Denote the right-most bit as bit 0, the 2nd right-most bit as bit 1…
1) flags = flags | (1 << 6); turn on bit 6 (right most is bit 0), keep other bits
2) flags = flags | ~(1 << 4); keep bit 4, turn on all other bits
3) flags = flags & 0177; keep right-most 7 bits, turn off all other bits
In statement 1) and 2) above, can any of the parentheses be removed without affecting the
meaning and value of the expression? If yes, specify which one(s) and briefly explain why.
Now suppose you need an int mask whose binary representation is 000....000101001.
How do you declare it in ANSI-C code? List 3 ways of doing it.
int mask = 41 int mask = 051 int mask = 0x29
What happens to flags in the following two statements, in terms of turning on/off or
keeping bits of flags? Denote the right-most bit as bit 0, the 2nd right-most as bit 1 ...
• flags = flags | mask turn on bits 0, 3, 5, keep others
• flags = flags & mask keep bit 0, 3, 5, turn off all other bits
invalid
63
invalid
26
12
invalid
parenthesis in 1) can be removed. Because << has higher precedence than |, so 1 << 6 is evaluated before | anyway
Other slu: int mask = 1 <<5 | 1 << 3 | 1 int mask = 9 | 1 << 3 … anything evaluated to 41 decimal
1.12 (1.25 pt) For each of the following array declaration and initializations, determine if the
statement is legal in ANSI C. If it is illegal, write ‘illegal’. If it is legal, then list (the value of) all
the elements in the array, separated by comma or space.
• int k[3] ={1,5,3,2,25}; illegal
• int k[5] = {1}; 1 0 0 0 0
• int k[6] = {1,4,5}; 1 4 5 0 0 0
• int k[] ={1,5,3,2,25}; 1 5 3 2 25
• int k2[] = k; illegal
1.13 (1.5 pt) Given the statement
char messages[3][20] = {"Hello", "Hi", "There"};
• What is the value of sizeof(messages)? 3 * 20 * 1 = 60 (bytes)
• write a single C statement to change "Hi" to "How are you" strcpy (messages[1], “How are you”) or sprintf(messages[1], “%s”,”How are you”) or sprintf(messages[1],”How are you”)
• write a single C statement to change 'h' in "There" to 'H'
messages [2][1] = ‘H’ or messages[2][1] =toupper(messages[2][1]);
Question 2 (12 pt)
2.1 (1 pt) Consider the following ANSI C program.
#include<stdio.h>
void aFunction (float input){
input = input * 2.0;
}
int main(){
float input;
scanf("%f", &input);
aFunction(input);
printf("%.3f\n", input);
}
Now suppose the program compiles and the user enters 7.4 What is the output of the
program? 7.400 Call by value. The formal argument of function happens to have the same name
as the actual argument but they are two variables. The former is a copy of the latter.
Jun 13, 2017 (Tuesday) Name: Student ID: Page 7 of 15
2.2 (1 pt) Consider the following function definition in ANSI C.
/* this function initializes argument array arr’s first n
elements to 0 */
void clearArr (int arr[], int n){
int index;
while (index < n){
arr[index++] = 0;
}
}
Do you think that this function will work as expected?
• Yes
• No. The reason is:
2.3 (1pt) Consider the following ANSI C program.
What are the outputs of the program?
2.4 (1 pt) Describe the usage(s) of key word static for global variable in ANSI-C.
Limit the scope to the file in which it is defined. That is, make it not accessible to the other files.
#include<stdio.h>
#define N 4
void aFun();
int main(int argc, char *argv[])
{
int k;
for(k=0; k<N; k++)
aFun();
return 0;
}
void aFun(){
int static counter;
int y = ++counter;
printf("%d ", y);}
}counter +=10;
}
index is a local variable, which is initialized by the compiler to a garbage value, not 0 need to initialize to 0 explicitly.
1
2
3
4
2.5 (2pt) Consider the following ANSI C program.
When you invoke the pre-processor, using gcc -E, what is the pre-processed code of main,
i.e., the output generated by the preprocessor (and sent as input to the compiler)?
int main(){
int i = 4 + 6;
int j = i / 2;
int k = j + 10 * j + 10 * j + 10
}
Now we compile and run the program. At the end of main, what is the value of i, j and k?
i: 10 j: 5 k: 115
2.6 (1.5pt) What are the outputs of the 2 printf() statements in the following code fragment?
char msg [] = "Hello";
printf("%d %d", sizeof(msg), strlen(msg));
char msg2[10] = "Hello";
printf("%d %d", sizeof(msg2), strlen(msg2));
2.7 (1.5pt) What are the outputs of the 2 printf() statements in the following code fragment?
char msg [] = "Hello the world";
printf("%s %d %d", msg, sizeof(msg), strlen(msg));
strcpy(msg, "Hi"); Hello the world 16 15
printf("%s %d %d", msg, sizeof(msg), strlen(msg)); Hi 16 2
#define N 4
#define INCRE(x) x * x * x
#define DECRE(x) x / 2
int main(){
/* use marcos */
int i = N + 6;
int j = DECRE(i);
int k = INCRE(j + 10);
}
6 5
10 5
Jun 13, 2017 (Tuesday) Name: Student ID: Page 9 of 15
2.8 (3 pt) Consider the following ANSI C program.
Explain briefly:
Briefly explain:
• Why does function scanf() require the use of & for age and wage (i.e., &age and &wage)?
(FYI: if you use age and wage without &,you get “segmentation fault” and the program
crashes.)
Scanf needs to change the value of its parameter.
Since c is call/pass by value, to change/set the value of age and wage, scanf needs the
address/pointer of the variables, and thus &age, &wage.
• Why don’t we need to use & for name in scanf()?
(FYI: the program won’t compile if you use &name.)
In C, array name contains the address of the first elements arr=&arr[0]. So we just pass it to the
function without & (actually it is wrong to pass & for arrays)
• Why don’t we need & for age and wage in printf()?
To output the values of age and wage (rather than changing them), pass-by-value is sufficient.
Just get a copy of the values of age and wage and output them.
Question 3 Pointers and arrays (23.5 + 1.5 pt)
3.1 (4 pt) Compete the following program so that the output of main is a: 600 b:18.
That is, values of a and b are swapped and then the swapped values are doubled.
int main(){
char name[60];
int age; float wage;
printf("Enter name, age and wage: ");
scanf("%s %d %f", name, &age, &wage);
printf("%s %d %f", name, age, wage);
}
/* This function double the pointee of arguments, and then call
another function to swap the (doubled) values */
void increSwap(int *px, int *py)
{ /* double the values of pointees of px and py */
* px *=2;
* py *=2;
/* now call function swap() to swap the pointees.
swap( px , py );
}
/* this function swap the pointees of the two pointers */
void swap ( int *p1, int *p2 )
{
int tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
void main( )
{
int a=9, b=300;
increSwap( &a , &b );
printf("a:%d b:%d", a, b); /* should get a:600 b:18 */
}
3.2 (2 pt) What is the output of the following ANSI C program?
What are the two types of testing method and what are the main differences?
#include<stdio.h>
main(){
int x = 199, y = 22;
int *ip, *ip2,**pip;
ip = &x;
pip = &ip;
ip2 = ip;
y = **pip;
(*ip2) *= 10;
ip = &y;
(** pip)++;
(*ip2) -= 10;
printf("x:%d y:%d\n", x,y);
}
x: 1980 y: 200
Jun 13, 2017 (Tuesday) Name: Student ID: Page 11 of 15
3.3 (1.5 pt) Consider the following ANSI C code fragment.
int arr [] = {1,2,3,4,5,6};
int * p = arr;
int i = arr[3];
In addition to arr[3], what are the other ways to access the same element of arr? List 3 ways.
int i = *(p + 3) int i = * (arr + 3) int i = p[3]
3.4 (2.25 pt) Consider the following ANSI C program.
main(){
int i; int * pInt = &i;
char c; char * pChar = &c;
double d; double * pDouble= &d;
printf("%p %p %p\n", pChar, pInt, pDouble); /* line 4 */
printf("%p %p %p\n", pChar+1, pInt+1, pDouble+1); /* line 5 */
pInt++; pChar++; pDouble++;
printf("%p %p %p\n", pChar, pInt, pDouble); /* line 7 */
pInt += 4; pChar += 4; pDouble +=4;
printf("%p %p %p\n", pChar, pInt, pDouble); /* line 9 */
}
Suppose the output of the first printf (line 4) is 100 4000 20000
(Recall that printing a pointer directly with %p outputs the content/value of the pointer, which is the address of its pointee, in Hex. To simplify, here we assume it prints in decimal)
• What is the output of the second printf (line 5)?
101 4004 20008
• What is the output of the third printf (line 7)? 101 4004 20008
• What is the output of the fourth printf (line 9)? 105 4020 20040
3.5 (2.5 pt) Given the following declaration statements in ANSI C,
int arr[]={1,2,3,4,5,6}; int * ptr = arr; int i;
Specify if each line of (independent) statement below is valid or invalid
int * ptr = arr + 3; valid invalid
int * ptr = &arr[0] valid invalid
i = ptr – arr; valid invalid
i = ptr + arr; valid invalid
i = * (ptr + 2); valid invalid
arr++; valid invalid
*(arr + 4)=2; valid invalid
ptr ++; valid invalid
ptr --; valid invalid
i = ptr > arr; valid invalid
3.6 (6 pt) Given the following ANSI C program, and assume that the first element of arr is stored in memory address 2000; What is the output of each printf( ) statement? (Recall that %p is used to print the content/value of the pointer, which is the address of its pointee. Here we assume it prints in decimal)
main() {
int arr[] = {17, 3, 58, 10, 126, 22, 35, 140, 165, 190};
int *p = arr; int *q;
printf("%p %p %d\n", arr, p, *p); 2000 2000 17
printf("%p %d\n", p+4,*(p+4) ); 2016 126
++p;
printf("%p %d\n", p, *p); 2004 3
p = arr + 5;
printf("%p %d\n", p, *(p + 2)); 2020 140
q = p;
p = q +3;
printf ("%p %p %d %d %d\n", p, q, p-q, *p, *q); 2032 2020 3 165 22
(*p)++;
printf("%d %d\n", *(arr+4), *(arr + 8) ); } 126 166
Jun 13, 2017 (Tuesday) Name: Student ID: Page 13 of 15
3.7 (1.5 pt) For a function that expects a char array as its argument, its prototype can be either void processArr (char []) or void processArr (char *). True False
Assume the following ANSI C code fragment
int main(){
char msg[] = "Hello the World";
char * p = msg;
processArr (…); /* line 4 */
}
void processArr (char []){….}
Which of the following could be a valid function call to processArr in line 4
a. proceeeArr(&msg[2]);
b. processArr(p);
c. processArr(&p[0]);
d. processArr(msg+3);
e. All of the above
3.8 (2.5+1 bonus) Consider the following ANSI C code, assume that the first element of msg
has starting address 1000; what are the outputs of the three printf statements? Recall that
%p prints the content of a pointer variable, and here we assume it prints in decimal.
main(){
char msg[] = "HelloWorld";
printf("%s %p %d %d\n", msg, msg, sizeof(msg), strlen(msg));
processArray(msg); HelloWorld 1000 11 10
processArray2(msg);}
void processArray (char c []){
printf("%s %p %d %d\n", c, c, sizeof(c), strlen(c));
} HelloWorld 1000 8 10
void processArray2 (char * c){
printf("%s %p %d %d\n", c, c, sizeof(c), strlen(c)); }
HelloWorld 1000 8 10
3.9 (1.25pt) Consider the following ANSI C program.
int main(){
int a[]={2,4,6, 8,10};
int resu = doSthRecursively(a, 5);
printf("%d\n", resu);
return 0;
}
int doSthRecursively(int* c, int n){
if (n==1)
return *c;
else return *c + doSthRecursively(c+1, n-1);
}
What is the output of the first printf() in main(), or, explain what does function
doSthRecursively intend to do?
Sum up first n elements in the array c. 2 +4 +6+8+10=30
3.10 (0.5 pt Bonus) As mentioned in the textbook and class, the declaration of a pointer
related variable is intended as a mnemonic (means ‘help you memorize’). For example,
declaration int * ptr; can be interpreted as "expression * ptr is an int (and thus
ptr is an integer pointer).
Based on this rule, guess what the following statements declare. That is, what is the type that
ptr is declared to be? You can draw pictures if you like. (We will talk about this next week in
class.)
int * ptr [];
ptr is an array. Each element of the array is a pointer to integer. That is, ptr is an array of (int)
pointers
ptr
int int * ptr [3]
(END of TEST)
Jun 13, 2017 (Tuesday) Name: Student ID: Page 15 of 15
Operator precedence (from high to low) and associativity.
(You can detach this page, and use it for scratch work )