C PROGRAMMING EXERCISE
C PROGRAMMING EXERCISE
1. Of the following, what is the correct syntax to declare an array of integers called
myNumbers with the values 25, 50, 75, and 100?
a) int myNumbers[25, 50, 75, 100];
b) int myNumbers = {25, 50, 75, 100};
c) int myNumbers[] = {25, 50, 75, 100};
d) int myNumbers[] = 25, 50, 75, 100;
2. True or False: The sizeof operator returns the size of a type in bytes.
a) True
b) False
3. What will be printed by this code?
int myNumbers[] = {25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf("%d", length);
a) 4
b) 25
c) 100
d) 1
4. What does the following code output?
double myDouble;
printf("%lu", sizeof(myDouble));
a) 1
b) 2
c) 4
d) 8
5. Print the value of the second element in the myNumbers array.
int myNumbers[] = {25, 50, 75, 100};
printf("%d",.......................);
6. What formula can you use to find the number of elements in an array called myNumbers?
a) sizeof(myNumbers) * sizeof(myNumbers[0]);
b) sizeof(myNumbers) / sizeof(int);
c) sizeof(myNumbers) / sizeof(myNumbers[0]);
d) sizeof(myNumbers[0]) / sizeof(myNumbers);
7. from the following statement, write a statement to change the value of the first element to
33:
int myNumbers[] = {25, 50, 75, 100};
8. What will the following code output?
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 12;
printf("%d", myNumbers[0]);
a) 25
b) 50
c) 12
d) 100
9. Which index should be used to access the third element in an array?
a) 0
b) 1
c) 2
d) 3
10. What is the output of the following section of code?
int myNumbers[] = {25, 50, 75, 100};
for (int i = 0; i < 4; i++)
{
printf("%d ", myNumbers[i]);
}
a) 25 50 75 100
b) 25 75 50 100
c) 100 75 50 25
d) 0 1 2 3
a) 25 50 75 100
b) 25
50
75
100
c) 25 75 50 100
d) 25
75
50
100
15. Which operator is used to get the value stored at the memory address a pointer is pointing to?
a) *
b) &
c) %
d) #
16. Which of the following statements is correct about pointers in C?
a) Pointers increase the size of variables.
b) Pointers allow direct manipulation of data in memory.
c) Pointers can only point to int variables.
d) Pointers are not allowed in C programming.
17. What does the keyword void indicate when used with a function in C?
a) The function returns an integer
b) The function can only be called once
c) The function does not return a value
d) The function can accept any type of parameter