Arrays, Strings, and PRNG in C Flashcards

1
Q

What is an array in C?

A

An array is a contiguous block of memory containing elements of the same type. The size of the array is determined during initialization.

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

Does C perform bounds checking on arrays?

A

No, C does not perform bounds checking, which can lead to undefined behavior if an index is out of range.

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

How do you declare and initialize a 1D array in C?

A

int array[5] = {1, 2, 3, 4, 5};

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

How do you declare and initialize a 2D array in C?

A

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

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

What is a string in C?

A

A string is an array of characters, terminated by the null character (\0).

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

How do you initialize a string?

A

char str[] = “Hello”;

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

What happens if a string is missing a null terminator?

A

Functions like printf may continue reading beyond the allocated memory, causing undefined behavior.

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

What does strlen do?

A

Returns the number of characters in a string, excluding the null terminator.

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

How does strcpy work?

A

Copies the content of one string into another, including the null terminator.

strcpy(dest, src);

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

What does strcat do?

A

Appends one string to another, overwriting the null terminator of the first string.

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

How does strcmp compare two strings?

A

Returns 0 if strings are equal, a positive value if the first is greater, or a negative value if the second is greater.

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

What is a pseudo-random number?

A

A number generated by an algorithm that appears random but follows a deterministic sequence

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

How do you generate a random number in C?

A

Use the rand() function, which generates an integer between 0 and RAND_MAX.

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

How do you seed the random number generator in C?

A

Use the srand() function, typically with the current time:

srand(time(0));

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

What are some common random number distributions?

A

Uniform: All values in a range are equally likely.
Normal (Gaussian): Forms a bell curve.
Poisson: Counts events in a fixed interval.
Bernoulli: Two outcomes (0 or 1).

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

Why is C considered weakly typed?

A

It allows implicit type conversions, which can lead to unintended behavior.

17
Q

What happens when you assign a float to an integer?

A

The decimal portion is truncated.

18
Q

Name some special characters in C and their meanings.

A

\n: New line
\t: Tab
\: Backslash
': Single quote
": Double quote

19
Q

What are the key topics covered in this slide deck?

A

Arrays and bounds checking.

Strings and their null-terminated nature.

Common string functions (strlen, strcpy, strcat, strcmp).

Pseudo-random numbers, seeding, and distributions.

Weak typing in C.