Arrays, Strings, Randomness and Pointers Flashcards

(40 cards)

1
Q

What is an example of signed int overflow in C?

A

When unsigned short int x = 65535; is assigned to short int y, y becomes -1.

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

What is the output of printf for variable c when c = x; with x = 65535?

A

Undefined behaviour.

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

What are the two types of arrays in C?

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

What does C not do with respect to arrays?

A

C does not perform bounds checking.

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

How are strings represented in C?

A

Strings are arrays of characters ending with a null terminator (0).

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

What does the function strlen(s) do in C?

A

Returns the length of the string (excluding the null terminator).

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

What is the purpose of srand(seed) in C?

A

Sets a new seed to change the sequence of random numbers.

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

What does the function rand() generate?

A

A pseudo-random integer between 0 and RAND_MAX.

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

What is the shape of the Normal (Gaussian) Distribution?

A

Follows a bell-curve shape.

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

What does the GNU Scientific Library (GSL) provide?

A

Better random number generators than rand().

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

What is the purpose of the function gsl_rng_set in GSL?

A

Sets the seed for the random number generator.

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

Fill in the blank: Strings in C are just arrays of characters ending with a ______.

A

null terminator.

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

True or False: C automatically checks for buffer overflows in arrays.

A

False.

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

What is the output of printf when using sprintf(buffer, “%s %d”, str, num)?

A

Formats and stores output into buffer.

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

What is a one-dimensional array in C?

A

An array that holds a fixed number of elements of the same type, initialized with specific values.

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

How can the size of an array be defined in C?

A

The size can be implicit and defined by its initialization.

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

What is an uninitialized array in C?

A

An array declared without an initial value, containing random data.

18
Q

How is a two-dimensional array initialized in C?

A

An array of arrays initialized with specific values.

19
Q

How do you access and set individual values in an array?

A

Using index notation, e.g., anArray[0] = 42; or array[3] = ‘Z’;

20
Q

What is a string in C?

A

A string is an array of characters terminated by a null character (0).

21
Q

What is the significance of the null terminator in strings?

A

It signals the end of the string to prevent the program from getting stuck.

22
Q

True or False: Strings in C are enclosed in single quotes.

23
Q

What are functions in C?

A

Uniquely named groups of program statements that may accept parameters and return values.

24
Q

What is the syntax for defining a function in C?

A

<return> functionName(<parameter>) { /* statements */ return <value>; }
</value></parameter></return>

25
What is the main function requirement in a C program?
Every program must include a main() function.
26
What is a procedure in C?
A function that does not return a value, defined with void.
27
What is the purpose of function prototypes?
To inform the compiler of the function’s signature before it is called.
28
What is the difference between global and local scope?
Global variables are declared outside functions, while local variables exist only within the function.
29
What are static variables in C?
Variables that are initialized only once and retain their value between function calls.
30
Fill in the blank: Functions must have a _______ name.
unique.
31
What should be avoided unless necessary in function design?
Using global variables.
32
What are the key points regarding functions in C?
* Define using a clear structure and unique names * Avoid overloading; separate by purpose and return type * Use prototypes to declare functions before use * Avoid global variables unless justified * Utilize static variables for data persistence
33
What fundamentals were covered in this lecture?
Defining and using functions, role of prototypes, scope, and static variables.
34
What is stored in memory?
Every variable.
35
What does each memory location have?
An address (a numerical label).
36
How many bytes are typically allocated for an integer variable?
4 bytes.
37
What is a pointer?
A variable that stores a memory address instead of a value.
38
How do you declare a pointer to a character?
char *p;
39
What does the & operator do?
Gives the address of a variable.
40
What does the * operator do?
Dereferences a pointer to access the value at the stored memory address.