3 Lecture
CS410
Midterm & Final Term Short Notes
Arrays and Pointers
Arrays and Pointers are fundamental concepts in programming. Arrays store a collection of elements of the same data type, accessed using indices. Pointers store memory addresses, enabling direct memory manipulation and efficient passing of data
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
1. Question: In C, how do you declare an integer array named "numbers" with 5 elements?
a) array numbers[5];
b) int numbers[5];
c) int[] numbers = {5};
d) int numbers(5);
Solution: b) int numbers[5];
2. Question: What is the value of the expression "sizeof(numbers)" in C, where "numbers" is an integer array with 10 elements?
a) 10
b) 40
c) 4
d) 14
Solution: b) 40
3. Question: In C, how do you access the third element of an array named "data"?
a) data(3);
b) data[3];
c) data{3};
d) data.3;
Solution: b) data[3];
4. Question: What is the correct way to pass an array "arr" to a function in C?
a) function(arr);
b) function(arr[]);
c) function(&arr);
d) function(*arr);
Solution: b) function(arr[]);
5. Question: What is a pointer in C?
a) A variable that stores multiple values
b) A variable that stores the address of another variable
c) An array that points to another array
d) A function that points to another function
Solution: b) A variable that stores the address of another variable
6. Question: What does the "*" symbol represent when used with a pointer variable in C?
a) Multiplication
b) Exponentiation
c) Address of a variable
d) Dereferencing the pointer
Solution: d) Dereferencing the pointer
7. Question: How do you declare a pointer variable named "ptr" that points to an integer in C?
a) int* ptr;
b) ptr* int;
c) pointer ptr = int;
d) ptr = int*;
Solution: a) int* ptr;
8. Question: What is the value of "ptr" after the following code: "int num = 10; int* ptr = #"?
a) 10
b) The address of "num"
c) The address of "ptr"
d) Garbage value
Solution: b) The address of "num"
9. Question: What happens when you increment a pointer in C using "ptr++"?
a) The pointer points to the previous element.
b) The pointer points to the next element.
c) The pointer becomes NULL.
d) The pointer points to the first element.
Solution: b) The pointer points to the next element.
10. Question: How do you dynamically allocate memory for an integer array "arr" of size 5 in C?
a) int arr[5];
b) int arr = (int*)malloc(5);
c) int* arr = new int[5];
d) int* arr = (int*)malloc(5 * sizeof(int));
Solution: d) int* arr = (int*)malloc(5 * sizeof(int));
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
1. Question: What is an array in C?
Answer: An array in C is a collection of elements of the same data type, stored in contiguous memory locations and accessed using an index.
2. Question: How do you declare an array in C?
Answer: You declare an array in C using the syntax: "data_type array_name[size];"
3. Question: Explain the concept of pointers in C.
Answer: Pointers are variables that store memory addresses. They are used to manipulate memory directly and are useful for dynamic memory allocation.
4. Question: How do you declare a pointer in C?
Answer: To declare a pointer in C, you use the syntax: "data_type *pointer_name;"
5. Question: What does the "sizeof" operator do with arrays and pointers?
Answer: The "sizeof" operator returns the size of an array in bytes and the size of a pointer in the specific platform's memory address length.
6. Question: How do you access elements of an array using pointers in C?
Answer: You can access array elements using pointers by dereferencing the pointer and using the array index notation, like "*(ptr + index)".
7. Question: What happens when you pass an array to a function in C?
Answer: When an array is passed to a function, it decays into a pointer, and the function receives a pointer to the first element.
8. Question: How do you dynamically allocate memory for an array using pointers in C?
Answer: You can use functions like "malloc" or "calloc" to dynamically allocate memory for an array and assign the memory address to a pointer.
9. Question: How do you deallocate memory for a dynamically allocated array in C?
Answer: You use the "free" function to deallocate memory for a dynamically allocated array.
10. Question: Explain the concept of pointer arithmetic in C.
Answer: Pointer arithmetic involves performing arithmetic operations (like addition or subtraction) on pointers. This allows moving to different memory locations based on the pointer's data type, size, and the operation performed.