Comp2 midterm Flashcards

1
Q

What escape character is used to print a backslash?

A

\

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

What escape character prints a double quote marks?

A

"

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

How do you convert an integer to a float in a calculation?

A

float average = (float)total/count;

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

Syntax for enum.

A

enum Name {VALUE, VALUE, VALUE};
//defines enum class Name with three
//values. Values are constant.
enum Name VariableName;
//creates an enum variablename of type name.

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

Syntax for passing a function to a function where the inputted functions takes two arguments a and b which are also arguments to the original function. (prototype).
How would the function be called?

A

void functionName( int , int , void (*otherFunction)(int, int) );

functionName(a , b, function being inputted )

inside functionname: (*otherFunction)(a, b) calls otherFunction

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

How can an array of pointers be created?

A

char *a[4] = {“”, “”,””,””}
//Can store strings of any size.

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

Define an array of three functions to pointers with functions named 1 2 and 3. All these functions return type int and take in two ints.
How would said function be called given inputs a and b and function choice = a variable called number?

A

int (*f[3])(int , int ) = {function1, function2, function3}

(*f[number])(a, b);

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

What is the end of file character on windows?

A

ctrl + Z

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

What function reads from a sequential file (give syntax)? What function writes to a sequential file (give syntax)?

A

fscanf(file, type, variable);
fprintf(file, type, variable);

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

What function checks for end of file symbol?

A

feof(stdin) or feof(file pointer)

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

How is a pointer to a file created and opened?

A

FILE *cfPtr;
cfPtr = fopen(“name.txt”, “type”);

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

Different access types for files.

A

w = open for writing (replaces files or creates new)
r = open for reading (for existing files)
a = open for appending (existing files and creates)
r+,a+,w+ = open for all three

b added for binary modes.

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

How is the file position pointer reset?

A

rewind(file pointer name)

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

What functions read and write to files of random access type?(with syntax)

A

fread(&nameofvariabletoberead, sizeof(type of variable being read), 1, file pointer name)
fwrite(&nameofvariabletobewritten, sizeof(type of variable being written), 1, file pointer name)

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

Syntax for fseek function?

A

fseek(file pointer name, index * sizeof(struct), SEEK_SET);

where index starts at 0.

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

For the third argument of fseek, what is it? Explain the three possible values.

A

Is where the offset calculated by fseek starts from.
SEEK_SET = start of file
SEEK_CUR = current position in file
SEEK_END = end of file

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

What does the # mean in code?

A

Pre-processor command, processed before complimation

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

What is the \ in \n called?

A

the escape character

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

What are the four stages of processing code in order?

A

Pre-processing
Compiling
Linking
Loading

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

What is the file called after linking?

A

An executable

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

What is the coercion of arguments?

A

Forcing of arguments to be the appropriate type.

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

Arrange the order from lowest to highest in terms of C arithmetic conversion rules.
(a) char (b) float (c) double (d) int

A

Highest: Double
Float
Int
Lowest: Char

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

What does LIFO stand for?

A

Last In First Out

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

What is a stack frame?

A

An individual entry on the stack that holds a function’s address and its variables.

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

How are random numbers generated? What is the 6 called? How is the random seeded?

A

rand() % 6; The scaling factor

srand(time(NULL));

25
Q

What is an array?

A

A group of contiguous memory locations that all have the same type.

26
Q

What is size_t?

A

Unsigned integral type

27
Q

What is a symbolic constant?

A

Is an identifier that’s replaced with replacement text by the C pre-processor before the program is compiled.

28
Q

What does VLA stand for?

A

Variable Length Array

29
Q

What is the advantage of a random access file over a sequential access file?

A

Records in a sequential access file are not necessarily the same length.

Random access files are fixed in length and may be accessed directly without searching through other records. So we can read, write or edit data without reading or re-writing the whole file as individual records can be easily accessed.

30
Q

What is a derived data type?

A

A data type constructed using other data types.

31
Q

What are the four valid operations on structs?

A

Assigning structs to structs of the same type
Getting the address
Accessing the members
Sizeof operator to determine size of struct

32
Q

Are structs passed by value or passed by reference?

A

Passed by value

33
Q

What type is the pointer returned by malloc?

A

void *

34
Q

What is the purpose of the malloc function in C?

A

To allocate memory on the heap.

35
Q

What is the size of a union?

A

The size of a union is the size of its largest member.

36
Q

What is a function prototype in C programming?

A

A declaration of the function that describes its interface.

37
Q

What is a pointer in C programming?

A

A variable that holds the memory address of another variable.

38
Q

What is the difference between a while loop and a do-while loop in C
programming?

A

A do-while loop executes at least once, while a while loop may not
execute at all.

39
Q

What is a structure in C programming?

A

A data type that can hold multiple variables of different data types.

40
Q

T or F. A structure can contain instances of itself.

A

False

41
Q

What is used to terminate a switch case in C?

A

break;

42
Q

What is the purpose of the sizeof operator in C?

A

To determine the size of a data type or variable.

43
Q

What is the purpose of the break statement in C?

A

To exit a loop or switch statement immediately.

44
Q

What is the purpose of the typedef keyword in C?

A

To define a new data type.

45
Q

Which is true (a) Pointers cannot be used in arithmetic operation or (b) Pointers can be used to allocate memory dynamically.

A

(b)

46
Q

Explain the concept of a self referential structure.

A

A self-referential contains a pointer to a struct of the same type as the struct it is inside.

47
Q

Explain dynamic memory allocation.

A

The ability for a program to obtain
more memory space at execution time to hold
new nodes, and to release space no longer
needed.

48
Q

What are the advantages of linked lists over arrays? (4)

A

Linked lists allows the number of elements to be more unpredictable where as arrays require size defined at initialisation.
Dynamic memory allocation means a linked list only takes up as much space as it needs but array may have many empty spaces.
Arrays can become full but linked lists are only full when there is no more memory available. Also not storing nodes consecutively means space can be allocated even when consecutive space is not available.
Can be maintained in sorted order which is time consuming in array.

49
Q

What is the one disadvantage of linked lists over arrays?

A

Pointers take up space and dynamic memory allocation incurrs the overhead of function calls.

50
Q

What does FIFO stand for?

A

First In First Out

51
Q

Explain the following big O representations. (a) O(1) (b) O(n) (c) O(n^2) (d) O(2^n) (e) O(log n). For the last one an example will suffice.

A

(a) A constant amount of comparisons that doesn’t grow as the size increases. E.g. Check if any of the first three elements of an array are equal.
(b) A linear amount of comparisons that grows linearly with the size. E.g. Compare the first element of an array to every other element in the array.
(c) A quadratic amount of comparison that grows quadratically with the size. E.g. See if any of the elements in the array are equal.
(d) An exponential amount of comparison that grows exponentially with the size. E.g. Recursive functions.
(e) E.g. Binary Search

52
Q

Describe a linked list and explain how a linked list works in C.

A

A linked list is a data structure in which nodes are linked together to form a list, and the list is accessed from a pointer to the first node in the list. In C, each node consists of a self-referential struct (a struct which contains a pointer to a struct of the same type). So each node contains a pointer to the next, the final node has NULL as the value it points to. The linked list is accessed from the front and each node is created using dynamic memory allocation so only the necessary amount of memory is used.

53
Q

Explain what is meant by the scope of an identifier in C.

A
  • The scope of an identifier is the portion of the program
    in which the identifier can be referenced.
  • The four types of identifier scope are function scope, file
    scope, block scope, and function-prototype scope.
  • Block scope variables can be referenced within their blocks denoted by {}
54
Q

How do you create the following?
(a) A const pointer to const data
A const pointer to data
A pointer to const data
A pointer to data

A

(a) const int* const intPtr
(b) int* const intPtr
(c) const int* intPtr
(d) int* intPtr

55
Q

Explain fully the coercion of arguments.

A

Argument values that do not correspond precisely to the parameter types in the function prototype are converted to the proper type before the function is called. There exists a hierarchy in terms of C types. Lower types can be forced to higher types but it won’t work well the opposite way. For example if an int is converted to the higher ranked double no information will be lost, but if the reverse occurs the double will be truncated into an integer value and information will be lost. In terms of the main C data types the ranking from lowest to highest is as follows: char, int, float, double.

56
Q

Explain the difference between pass by reference and pass by value.

A

Pass by value is when a parameter is passed into function without a ptr, a copy of the variable is passed into the function and any changes made inside the function will not apply to the value outside the function unless a return is used. It is less efficient as an extra copy of the variable is stored in memory.

Pass by reference passes a ptr to the value we want to pass to the function. This ptr is then dereferenced when we want to alter the value. The altered value then changes for the original variable outside the function.

57
Q

Briefly explain what a recursive function is and what the key components of a recursive function are.

A

A recursive function is a function that calls itself to solve a problem, breaking it down into smaller instances until reaching a base case. Key components include a base case to stop recursion and a recursive case that calls the function with modified parameters. This approach efficiently solves complex problems by dividing them into simpler subproblems.

58
Q

Explain the benefits of the union datatype.

A

The union is only as large as its largest member so takes up much less space than a struct and is far more efficient if we only want to access one member at a time.

59
Q

What is meant by the duration of a variable in C?

A

In C programming, the duration of a variable refers to how long it exists in memory and retains its value. Automatic variables are created when a function is called and destroyed when it exits. Static variables persist throughout the program’s execution. Dynamic variables are explicitly created and destroyed by the programmer. Understanding variable duration helps manage memory efficiently.

60
Q

Briefly explain how data in a random access file is accessed?

A

The function fseek can be used with an offset to directly access specific data in a random access file. Whole file doesn’t need to be rewritten to edit a single entry.

61
Q

Explain VLA.

A

Variable Length Arrays (VLAs) are arrays whose size is determined at runtime.
More flexibility in memory allocation
Memory allocated on the stack.
Not as efficient as fixed-size arrays.