Showing 71–80 of 100 questions
in Pointers
A function needs to modify a variable in the calling function. Which parameter passing method is appropriate?
A
Pass by value
B
Pass by reference using pointers
C
Pass by global variable
D
None of the above
Correct Answer:
B. Pass by reference using pointers
EXPLANATION
To modify a variable in the calling function, you must pass its address (pointer). Pass by value creates a copy and cannot modify the original.
Consider: int arr[5] = {1, 2, 3, 4, 5}; int *p = arr; What is the value of *(p+2)?
EXPLANATION
p points to arr[0]. p+2 points to arr[2], and *(p+2) dereferences to the value at arr[2], which is 3.
Which memory allocation function does NOT initialize allocated memory to zero?
A
calloc()
B
malloc()
C
realloc()
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
malloc() and realloc() return uninitialized memory with garbage values. calloc() initializes all bytes to zero.
Consider: int x = 5; int *p = &x; int **q = &p; What is the value of **q?
A
5
B
Address of p
C
Address of x
D
Error
EXPLANATION
q is a pointer to pointer p. **q dereferences twice: first to get p, then to get the value x, which is 5.
Which of the following best describes a dangling pointer situation?
A
A pointer that points to a valid memory location
B
A pointer that points to memory that has been freed or deallocated
C
A pointer that has not been initialized
D
A pointer used in arithmetic operations
Correct Answer:
B. A pointer that points to memory that has been freed or deallocated
EXPLANATION
A dangling pointer occurs when it points to memory that no longer exists, typically after free() or when a local variable goes out of scope.
Consider the code: int arr[] = {5, 10, 15, 20}; int *ptr = arr; ptr++; What does ptr now point to?
A
The value 5
B
The value 10
C
The address of arr[1]
D
The address increased by 1 byte
Correct Answer:
C. The address of arr[1]
EXPLANATION
Pointer arithmetic increments by the size of the data type. For int (4 bytes), ptr++ moves to the next integer, which is arr[1].
A void pointer can be used to store addresses of variables of any data type. However, it must be cast before dereferencing. Which statement about void pointers is FALSE?
A
void *p can store address of int, float, or char
B
void *p can be directly dereferenced without casting
C
void pointers are useful in generic functions like malloc()
D
A void pointer must be cast to appropriate type for arithmetic operations
Correct Answer:
B. void *p can be directly dereferenced without casting
EXPLANATION
A void pointer cannot be directly dereferenced. It must be cast to the appropriate pointer type first.
What will be printed by the code: char *ptr = "Hello"; printf("%c", *ptr);
EXPLANATION
ptr points to the first character of the string, so *ptr dereferences to 'H'. The %c format specifier prints a single character.
Which operator is used to get the address of a variable?
A
* (asterisk)
B
& (ampersand)
C
-> (arrow)
D
. (dot)
Correct Answer:
B. & (ampersand)
EXPLANATION
The & (address-of) operator returns the memory address of a variable.
Consider: int x = 10; int *p = &x; What does the expression *p represent?
A
The address of x
B
The pointer variable p
C
The value stored in x
D
The size of x
Correct Answer:
C. The value stored in x
EXPLANATION
The dereference operator (*) accesses the value at the address pointed to by p, which is the value 10.