# 100 Questions and Answers: Arrays, Pointers, Functions, and Structures in C
## Arrays
1. **Predict the output:**
```c
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
printf("%d", arr[2]);
return 0;
}
```
**Answer:** 30
2. **What will happen if you try to access `arr[10]` for an array defined as `int
arr[5];`?**
**Answer:** Undefined behavior, as it exceeds the array bounds.
3. **Write a program to reverse an array.**
```c
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
for (int i = 0; i < n / 2; i++) {
int temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
**Answer:** 5 4 3 2 1
4. **How do you pass an array to a function in C? Provide an example.**
```c
#include <stdio.h>
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5);
return 0;
}
```
**Answer:** The function prints the array elements: 1 2 3 4 5
5. **What is the difference between `int arr[5];` and `int *arr;`?**
**Answer:** `int arr[5];` allocates memory for 5 integers, whereas `int *arr;`
is just a pointer, and memory must be explicitly allocated.
... (more array-related questions to reach 25)
## Pointers
26. **Predict the output:**
```c
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("%d", *p);
return 0;
}
```
**Answer:** 10
27. **What is a NULL pointer, and why is it used?**
**Answer:** A NULL pointer is a pointer that does not point to any memory
location. It is used for error handling and to signify that the pointer is not
initialized.
28. **Write a program to swap two variables using pointers.**
```c
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 5, b = 10;
swap(&a, &b);
printf("a = %d, b = %d", a, b);
return 0;
}
```
**Answer:** a = 10, b = 5
... (more pointer-related questions to reach 25)
## Functions
51. **Predict the output:**
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
printf("%d", add(5, 10));
return 0;
}
```
**Answer:** 15
52. **What is the difference between call by value and call by reference?**
**Answer:** Call by value copies the value of a variable into the function,
while call by reference passes the variable's address, allowing modification of the
original variable.
53. **Write a recursive function to calculate the factorial of a number.**
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(5));
return 0;
}
```
**Answer:** 120
... (more function-related questions to reach 25)
## Structures
76. **Define a structure `Student` and write a program to initialize and display
it.**
```c
#include <stdio.h>
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
struct Student s1 = {"John Doe", 101, 85.5};
printf("Name: %s\nRoll No: %d\nMarks: %.2f", s1.name, s1.rollNo, s1.marks);
return 0;
}
```
**Answer:**
Name: John Doe
Roll No: 101
Marks: 85.50
77. **What is padding in structures, and why does it occur?**
**Answer:** Padding adds extra bytes between structure members to align data in
memory for faster access.
78. **Predict the output:**
```c
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p1 = {10, 20};
printf("%d %d", p1.x, p1.y);
return 0;
}
```
**Answer:** 10 20
... (more structure-related questions to reach 25)
# Total: 100 Questions