click below
click below
Normal Size Small Size show me how
Exam II
Programming I
| Question | Answer |
|---|---|
| What are the three types of loops in C? | For, While, Do/While |
| What is a pre-test loop? Give an example. | A loop where the condition is tested before executing the body (e.g. while, for ) |
| What is a post-test loop? Give an example. | A loop where the condition is tested after executing the body (e.g do/while) |
| How do you create an infinite loop? | Using a loop with a condition that is always true e.g while(1) or for(;;) |
| what does break do inside a loop? | Exits loop immediately |
| what does continue do inside a loop? | skips the rest of the current iteration and continues to the next iteration |
| what does return do inside a function? | exits the function immediately and optionally returns a value. |
| Which libraries are needed to generate random numbers in C? | stlib.h and time.h |
| How do you seed the random number generator? | using srand(time(NULL)); |
| How do you generate a random integer in a range [min, max]? | rand() % (max - min +1) +min |
| What is the purpose of srand()? | To initialize the random number generator to produce different sequences each run |
| How do you declare a 1D array of 5 integers? | int arr[5]; |
| How do you declare a 2D array of 3x4 integers? | int arr[3][4]; |
| How can you initialize an array when declaring it? | int arr[5] = {1, 2, 3, 4, 5,}; |
| what happens if you exceed an arrays size? | accessing an out of bounds index causes undefined behavior |
| What is the difference between an element position and an element index? | Index: actual number used in code to access an element element position: logical placement in the array ( 1st , 2nd..) |
| How do you copy elements from one array to another? | Using a loop: for(int i=0; i <n; ++i) { arr2[i]=arr1[i];} |
| How do you fill a 2D array with random numbers? | Using a nested loop: for(int i=0; i < rows; i++) { for (int j=0; j < cols; j++) { arr[i][j] = rand() % range + offset; } } |
| Predict the output: for(int i=0;i<5;i++){ if(i==2) break; printf("%d ", i); } | 01 |
| Predict the output if break is replaced with continue in the previous example. | 0134 |
| Write a for loop to print numbers 1–10. | for( int i=1; i<=10; i++) { printf("%d ", i);} |
| write a while loop to sum numbers 1-5 | int sum= 0, i=1; while(i<=5) { sum +=i; i++} |
| write a do while loop that prints hello 3 times | int i=0; do{ printf("hello\n"); i++ } while(i<3); |
| Write code to generate a random number between 0–99. | rand() % 100; |
| Write code to generate a random number between 50–100. | rand() % 51 +50 |
| what is the purpose of srand()? | to initialize the random number generator so sequences differ each run |
| why is time(NULL) used with srand() | it provides a changing seed based on the current time |
| write a loop to print 5 random numbers 1-10 | for (int i=0; i <5; ++i) printf("%d", rand()%10 +1; |
| for(int i=4; i>=0; i--) printf("%d ", arr[i]); | |
| Fill a 1D array with random numbers 1–20. | for(int i=0;i<5;i++) arr[i]=rand()%20 + 1; |
| Calculate the sum of a 1D array using a loop. | int sum=0; for(int i=0;i<5;i++) sum+=arr[i]; |
| How do you initialize a 2D array at declaration? | int arr[2][3]={{1,2,3},{4,5,6}}; |
| Write nested loops to fill a 3x3 array with numbers 1–9. | int val=1; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ arr[i][j]=val++; } } |
| Write nested loops to print a 3x3 array in grid form | for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ printf("%d ", arr[i][j]); } printf("\n"); } |
| How do you fill a 2D array with random numbers 0–9? | for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ arr[i][j]=rand()%10; } } |
| How do you traverse a 2D array backward? | for(int i=rows-1;i>=0;i--){ for(int j=cols-1;j>=0;j--) printf("%d ",arr[i][j]); } |
| Fill a 1D array with random numbers 1–50, then print even numbers only. | for(int i=0;i<5;i++){ arr[i]=rand()%50 + 1; if(arr[i]%2==0) printf("%d ", arr[i]); } |
| Fill a 2D array with random numbers, then find the largest element. | int max=arr[0][0]; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ if(arr[i][j]>max) max=arr[i][j]; } } |
| Use a loop to sum all elements in a 2D array. | int sum=0; for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ sum+=arr[i][j]; } } |
| hat limitation does a C array have? | Fixed size; cannot resize once declared. |
| What is the difference between int arr[5] and int *arr? | arr[5] is a fixed-size array; int *arr is a pointer, can point to dynamic memory. |
| Fill an array with random numbers in range [10,30]. | arr[i] = rand() % 21 + 10; |
| Declare, initialize, and print a 2x2 array without loops. | int arr[2][2]={{1,2},{3,4}}; printf("%d %d\n%d %d", arr[0][0], arr[0][1], arr[1][0], arr[1][1]); |
| Use a loop to fill a 1D array with values equal to their index squared. | for(int i=0;i<5;i++) arr[i]=i*i; |
| difference between break and return | return exits the entire function, break exits only the current loop or switch and continues to next block of code |
| pre increment | ++i meaning increment first then use the value the variable is increased before its used in an expression |
| post increment | i++ meaning use the current value first then increment |
| we can store multiple types in an array and preserve data | false, only one data at a time |
| the index of an array can be any integral type | true |
| given an array of n values the last item is indexed n-1 | true |
| arrays cannot be resized after they are declared | false, dynamic arrays can using memory functions |
| any loop requires three items | initialization i=0;, update i++, conditioni<10 |
| if any of the 3 required items are missing a loop may easily become | infinite |
| declare a 4 x 4 2 dimensional array and use looping to fill it with characters as shown here without using scanf() | #include <stdio.h> int main(void) { char letters[4][4]; char ch = 'A'; // starting character for (int row = 0; row < 4; row++) { for (int col = 0; col < 4; col++) { letters[row][col] = ch; ch++; // mo |