Vous êtes sur la page 1sur 5

System Programming sample exam

1. Suppose that x and y have byte values 0x66 and 0x39, respectively. Fill in the following
table indicating the byte values of the different C expressions:

2. what is the output of the following program, assuming that the address of x is
00367034, the address of a is 00367038 and address of p is 00367058 and address of p2
is 00367060?

#include <stdio.h>
int *p, x;
int a[5] = {100,200,300,400,500};
int *p2;
int main()
{
p=NULL;
x=500;
p=&x;
printf("1) %d %d %p %p %p \n",x,*p,p,&x,&p);
p2=a;
*(p2+1) =*p;
*p= *p2 + *(p2+2);
printf("2) %d %d %d \n",x,*p,*p2);
return 0;
}

3. Find and correct the errors in the following program, if there are any.

int sum(float x){


int a, *b=&a;
int array[] = {10, 20, 30, 40, 50};
a += x + array[0];
*b = *b + array[1];
return b;
}

4. What is the output of the following code fragment?


int a = 54;
int b = a & (a >> 2);
printf(“%d \n”,b);

5. Fill out the following two box-and-arrow diagrams to describe the state of the program
at Point A and Point B labeled in the code below. Be sure to include the values of any
integers (for a pointer the arrow is sufficient).
int x[3] = {3, 5, 7};
int *p = x;
int *p2p = &p;
/* Point A*/
p++;
(*p2p)++;
(*p)++;
/*Point B*/
Point A
p2p
p
x[3]
x[2]
x[1]
x[0]

Point B
p2p
p
x[3]
x[2]
x[1]
x[0]
6. What will be printed by this code?

void foo()
{
int a=5, b=7, c=-4, d[3] = {4,6,12};
int *p1, *p2;
int x,y,z;

p1 = &d[0];
*(p1++);
(*p1)++;
p2 = &c;

x = a + d[2];
y = *p1 + x;
z = *p2 + b;
*p2 += d[0];

printf("%d %d %d %d \n", y, z, c, d[1]);

}
7. Given the following code snippet, choose the statement that is valid

struct MyType{
int myInt;
double myDouble;
char myChar;
} hello;

a. hello myHello; D. myInt = 23;


b. struct hello MyHello; E. hello.myInt = 23;
c. MyType.myInt = 23;
8. Assuming we have the following declarations, which of the following statements display ‘A’?
char * char_ptr;
char a = ‘A’;
char_ptr = &a;
a. printf(“%p”, char_ptr);
b. printf(“%c”, &a);
c. printf(“%c”, *char_ptr);
d. printf(“%c”, char_ptr);
e. printf(“%c”, *a);

9. What is the output from this program?

#include <stdio.h>
#include <stdlib.h>
struct car {
char *make;
int row;
int col;
};
typedef struct car Car;

void printCar(Car veh){



printf ("C : %2d %2d %s\n", veh.row, veh.col,
veh.make) ;
}
void printCarPtr(Car *veh)
{
static count = 1;

printf ("P%d: %-2d %-2d %s\n", count, veh->row,
veh->col, veh->make);

count++;

}

int main ()
{
Car Toyo = {"Toyota", 12};
Car Ford = {"Ford" , 7, 6};
Car Hond = {"Honda" , 8, 5};
Car *cPtr1, *cPtr2, *cPtr3;
cPtr1 = malloc(sizeof(Car));
cPtr2 = &Toyo;
cPtr3 = &Hond;
cPtr1->row = 4;
cPtr1->col = cPtr2->col;
cPtr1->make = Ford.make;
cPtr2 = cPtr1;
printCarPtr (cPtr2);
cPtr3->col = Toyo.row;
printCar (Hond);
cPtr1->make = "Prius";
cPtr1->row = cPtr3->col+2;
cPtr1->col++;
printCarPtr (cPtr1);
free (cPtr1);
printCarPtr (cPtr2);
return 0;

10. Given the following screen output from a Linux shell and assuming you are user:

$ pwd

/home/user/CS2303/q2

$ls -l
total 77
-r-------- 1 user cs2303a14 43 Sep 14 12:42 q1.c

-r-x------ 1 user cs2303a14 188 Sep 14 12:45 q2.c

-rwx------ 1 user user 209 Sep 15 08:49 q.out

1. Provide the command to create a new directory midterm.


2. Provide the command that copies the file q1.c into midterm.
3. Provide the command(s) that changes the access permissions on the file q2.c
to:

• read, write and execute for the owner


• read and execute for the group
• read and execute for other

Vous aimerez peut-être aussi