Chapter 7: Pointers
Introduction to Pointers
- What is a pointer in C?
- a) A variable that holds the address of another variable
- b) A variable that holds the value of another variable
- c) A data type
- d) None of the above
- Which of the following operator is used to access the value of a variable via a pointer?
- What does the ‘address-of’ operator (&) do in C?
- a) Returns the value of a variable
- b) Returns the address of a variable
- c) Dereferences a pointer
- d) Initializes a pointer
- How do you declare a pointer to an integer in C?
- a) int *ptr;
- b) int &ptr;
- c) pointer int;
- d) ptr int;
- What is the output of the following code?cCopy code
int num = 10; int *ptr = # printf("%d", *ptr);
- a) 10
- b) Address of num
- c) Garbage value
- d) Error
Pointer Declaration, Initialization, and Dereferencing
- What does dereferencing a pointer mean?
- a) Changing the pointer’s address
- b) Accessing the value stored at the memory address the pointer points to
- c) Initializing the pointer
- d) Declaring the pointer
- How can a pointer be initialized?
- a) By using the address-of operator
- b) By assigning a constant value
- c) By assigning the value of a variable
- d) Both a and c
- Which of the following is a valid pointer declaration?
- a) int* ptr;
- b) int ptr*;
- c) *int ptr;
- d) ptr int*;
- In C, what will the following code output?cCopy code
int x = 5; int *ptr = &x; *ptr = 10; printf("%d", x);
- What is the role of a pointer in dynamic memory allocation?
- a) Points to an already allocated memory block
- b) Helps in allocating memory at runtime
- c) Deallocates memory
- d) Both a and b
Pointer Arithmetic
- What does pointer arithmetic allow you to do in C?
- a) Add or subtract integers to/from pointers
- b) Add or subtract pointers
- c) Assign values to a pointer
- d) Initialize pointers
- What happens when you increment a pointer (e.g.,
ptr++)?
- a) It points to the next variable of the same type
- b) It increments the value stored at the memory address
- c) It points to a random address
- d) It points to the previous variable
- If
ptr is a pointer to an integer, what does ptr + 1 do?
- a) Increments the value of ptr
- b) Moves the pointer to the next integer location
- c) Subtracts one from the pointer
- d) Dereferences the pointer
- Which of the following is the correct way to perform pointer arithmetic on a pointer to an array?
- a)
ptr = ptr + 3;
- b)
ptr = ptr - 3;
- c)
ptr++;
- d) All of the above
- How do you calculate the distance between two pointers in C?
- a) By subtracting the address of the first pointer from the second pointer
- b) By adding the two pointer values
- c) By finding the length of the array
- d) By dereferencing both pointers
Pointers and Arrays
- How are pointers and arrays related in C?
- a) An array is a pointer to its first element
- b) A pointer is an array of values
- c) Pointers cannot be used with arrays
- d) An array stores pointer values
- What is the result of this code snippet?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 2));
- What is the output of the following code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *ptr);
- a) 10
- b) 20
- c) 30
- d) Garbage value
- When using pointers with arrays, which operation can be used to access an element of the array?
- a)
*ptr + index
- b)
ptr[index]
- c)
*ptr[index]
- d) Both b and c
- Which of the following is an example of pointer access to an array element?
- a)
*arr[0]
- b)
ptr[0]
- c)
&arr[0]
- d)
arr[ptr]
Pointers and Strings
- How are strings represented in C?
- a) As arrays of characters
- b) As arrays of integers
- c) As arrays of pointers
- d) As integers
- Which function is used to find the length of a string in C?
- a)
strlen()
- b)
strsize()
- c)
strlen()
- d)
stringlen()
- Which of the following will dereference a pointer pointing to a string?
- a)
*ptr
- b)
ptr[]
- c)
ptr()
- d)
ptr + 1
- In the statement
char *str = "Hello";, what type of variable is str?
- a) Integer pointer
- b) Character array
- c) Character pointer
- d) String literal
- Which of the following functions copies a string in C?
- a)
strcpy()
- b)
strcat()
- c)
strlen()
- d)
strdup()
Pointers to Functions
- What does a function pointer store?
- a) The address of a function
- b) The return type of the function
- c) The parameters of the function
- d) The function’s name
- Which syntax is used to declare a pointer to a function?
- a)
return_type (*pointer_name)(parameter_types);
- b)
pointer_type *function_name(parameter_types);
- c)
function_pointer return_type(parameter_types);
- d)
pointer_type function_name(parameter_types);
- How do you call a function through a function pointer?
- a)
pointer_name()
- b)
*pointer_name()
- c)
pointer_name;
- d)
(*pointer_name)();
- Which of the following correctly declares a pointer to a function that takes two integers and returns an integer?
- a)
int (*ptr)(int, int);
- b)
int ptr(int, int);
- c)
int *ptr(int, int);
- d)
ptr(int, int) int;
- How would you use a function pointer in C?
- a) To change the logic of a function dynamically
- b) To point to the data members of a structure
- c) To call a function in place of a loop
- d) None of the above
Dynamic Memory Allocation
- What function is used to allocate memory dynamically in C?
- a)
malloc()
- b)
calloc()
- c)
free()
- d)
realloc()
- How do you allocate memory for an array of 10 integers dynamically?
- a)
int *arr = malloc(10 * sizeof(int));
- b)
int arr = malloc(10 * sizeof(int));
- c)
int arr[10] = malloc();
- d)
int *arr = calloc(10, sizeof(int));
- What does
calloc() do in dynamic memory allocation?
- a) Allocates memory and initializes it to 0
- b) Allocates memory without initialization
- c) Frees allocated memory
- d) Reallocates the size of memory block
- Which function is used to free dynamically allocated memory in C?
- a)
free()
- b)
realloc()
- c)
malloc()
- d)
calloc()
- What happens if you don’t free dynamically allocated memory in C?
- a) Memory leak occurs
- b) Memory is automatically freed
- c) Program will crash
- d) Memory will get corrupted
- How do you resize a previously allocated memory block in C?
- a)
realloc()
- b)
malloc()
- c)
calloc()
- d)
resize()
- Which of the following is correct for reallocating memory?
- a)
ptr = realloc(ptr, new_size);
- b)
ptr = malloc(ptr, new_size);
- c)
ptr = realloc(new_size, ptr);
- d)
ptr = resize(ptr, new_size);
- How do you check if memory allocation was successful using
malloc()?
- a) By checking if the pointer is
NULL
- b) By checking if the pointer is not
NULL
- c) By checking the memory address
- d) By checking the array size
- What value is returned by
malloc() if memory allocation fails?
- a)
NULL
- b)
0
- c)
-1
- d)
Error
- What is the purpose of
sizeof() in dynamic memory allocation?
- a) It calculates the amount of memory to allocate
- b) It frees allocated memory
- c) It checks if memory allocation is successful
- d) It allocates the required memory
41. Which of the following code snippet is correct to pass an array to a function using pointers?
- a)
void function(int *arr)
- b)
void function(int arr[])
- c)
void function(int arr[10])
- d) All of the above
42. What does the following code do?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 3));
- a) Prints the address of the fourth element of the array
- b) Prints the value of the fourth element of the array
- c) Prints the value of the first element of the array
- d) Prints a garbage value
43. What is the result of the following code?
int arr[3] = {5, 10, 15};
int *ptr = arr;
ptr++;
printf("%d", *ptr);
- a) 10
- b) 5
- c) 15
- d) Garbage value
44. In the statement int arr[] = {5, 10, 15};, what type is arr?
- a) Integer
- b) Integer pointer
- c) Array of integers
- d) Array of pointers
Pointers and Strings
45. What is the correct way to assign a string to a pointer?
- a)
char *ptr = "Hello";
- b)
ptr = "Hello";
- c)
char ptr = "Hello";
- d)
ptr = char "Hello";
46. Which function is used to compare two strings in C?
- a)
strcmp()
- b)
strcompare()
- c)
strcmpi()
- d)
strcomp()
47. What will this code output?
char str[] = "Hello";
char *ptr = str;
ptr++;
printf("%c", *ptr);
48. Which of the following functions concatenates two strings?
- a)
strcat()
- b)
strconcat()
- c)
strcopy()
- d)
strjoin()
49. How would you initialize a pointer to the first character of a string?
- a)
char *ptr = "Hello";
- b)
char *ptr = &"Hello";
- c)
char *ptr = "H";
- d)
char *ptr = &'H';
50. What is the result of this code?
char *str = "Programming";
printf("%s", str);
- a) “Programming”
- b) P
- c) Error
- d) g
| Qno | Answer |
|---|
| 1 | a) A variable that holds the address of another variable |
| 2 | b) * |
| 3 | b) Returns the address of a variable |
| 4 | a) int *ptr; |
| 5 | a) 10 |
| 6 | b) Accessing the value stored at the memory address the pointer points to |
| 7 | d) Both a and c |
| 8 | a) int* ptr; |
| 9 | b) 10 |
| 10 | b) Helps in allocating memory at runtime |
| 11 | a) Add or subtract integers to/from pointers |
| 12 | a) It points to the next variable of the same type |
| 13 | b) Moves the pointer to the next integer location |
| 14 | d) All of the above |
| 15 | a) By subtracting the address of the first pointer from the second pointer |
| 16 | a) An array is a pointer to its first element |
| 17 | b) 3 |
| 18 | a) 10 |
| 19 | d) Both b and c |
| 20 | b) ptr[index] |
| 21 | a) As arrays of characters |
| 22 | a) strlen() |
| 23 | a) *ptr |
| 24 | c) Character pointer |
| 25 | a) strcpy() |
| 26 | a) The address of a function |
| 27 | a) return_type (*pointer_name)(parameter_types); |
| 28 | d) (*pointer_name)(); |
| 29 | a) int (*ptr)(int, int); |
| 30 | a) To change the logic of a function dynamically |
| 31 | a) malloc() |
| 32 | a) int *arr = malloc(10 * sizeof(int)); |
| 33 | a) Allocates memory and initializes it to 0 |
| 34 | a) free() |
| 35 | a) Memory leak occurs |
| 36 | a) realloc() |
| 37 | a) ptr = realloc(ptr, new_size); |
| 38 | a) By checking if the pointer is NULL |
| 39 | a) NULL |
| 40 | a) It calculates the amount of memory to allocate |
| 41 | d) All of the above |
| 42 | b) Prints the value of the fourth element of the array |
| 43 | a) 10 |
| 44 | c) Array of integers |
| 45 | a) char *ptr = “Hello”; |
| 46 | a) strcmp() |
| 47 | b) e |
| 48 | a) strcat() |
| 49 | a) char *ptr = “Hello”; |
| 50 | a) “Programming” |
Post Views: 65