4. C Programming Flashcards

1
Q

Sort the following features according to if they are a feature of Java or a feature of C:

  • object-oriented
  • compiled into byte code
  • compiled into an executable
  • interpreted
  • run-time errors are caught
  • garbage collection
  • choice between static and dynamic allocation
  • solely dynamic allocation
  • pointers
  • explicit boolean and string type
A
  • object-oriented: Java
  • compiled into byte code: Java
  • compiled into an executable: C
  • interpreted: Java
  • run-time errors are caught: Java
  • garbage collection: Java
  • choice between static and dynamic allocation: C
  • solely dynamic allocation: Java
  • pointers: C
  • explicit boolean and string type: Java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In
struct point { int x; int y; } p1;
what does p1 do?

A

Creates an instance of p1 (of structure type point)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What operator accesses an attribute of a structure?

A

.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What predefined function returns the total size of an instance of a structure?

A

sizeof()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Take
struct point { int x; int y; } p1;

Then, p1 is set to (4, 5).
What does p1.y now translate to in MIPS?

A

Assume the base address of p1 is stored in $s0. Then, lw $t0, 4($s0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a structure and a type definition?

A

A structure is a defined collection of attributes about a thing. A type definition is a association between the name of the user-defined type and the structure itself:

typedef struct point point or typedef unsigned char byte

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you declare the array [3, 4, 5]?

A

int arr[] = {3, 4, 5};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you declare an array of size 4 by 5?

A

int arr[4][5];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are strings ‘defined’ in C?

A

They are arrays of chars via ASCII. They end with \0, the null character.

Technically, you could officially define them by:

typedef char string[];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What string functions do the following:
- assignment
- get length
- comparison

A
  • strcpy (strcpy(s, "string"); )
  • strlen (strlen(s))
  • strcmp (strcmp(s1, s2))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a pointer?

A

A variable that holds the address of a piece of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What do each of these lines of code do?

int *p;
p = &i;
*p = 5;
A
  1. Creates an pointer that can point to an integer.
  2. p is set to the address of variable i
  3. The value of i is set to 5.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are arguments passed to a function?

A

By value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a function that takes in two integer addresses and swaps their contents.

A
void swap(int* a, int* b) {
     int temp = *a;
     *a  = *b;
     *b = temp;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Adding one to a pointer adds ____ to the address.

A

The size of the data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What do the following commands do?

*p++;
*++p;
(*p)++;
A
  1. Uses the value, and then increment the pointer.
  2. Increment the pointer, then use the value;
  3. Increase the value.
17
Q

What will the following code produce:

int* p = NULL;
*p;
A

A crash.

18
Q

What line will allocate dynamic space for n integers?
What frees the space?

A

int *p = malloc(n*sizeof(int));
free(p);

19
Q

What are the three areas of memory used for program variables?

A

Heap (dynamic allocation), Static (static/global variables) and Stack (local variables)

20
Q

Order the stack, static, instructions and heap by proximity to top of memory.

A

Stack ->, <- heap, static, instructions.