C - Pointers Practice Test.2

  1. What is the primary purpose of a const pointer in C?

    A) To declare a pointer that cannot be modified.

    B) To create a constant value that cannot be changed.

    C) To allocate memory for a variable.

    D) To define a pointer to a constant value.

    Answer: A) To declare a pointer that cannot be modified.

  2. Which of the following statements is true about a const pointer in C?

    A) A const pointer can change the value it points to.

    B) A const pointer cannot change the value it points to.

    C) A const pointer can change its own memory address.

    D) A const pointer can be used to create a non-constant variable.

    Answer: B) A const pointer cannot change the value it points to.

  3. What happens when you attempt to modify the value pointed to by a const pointer?

    A) It results in a compilation error.

    B) The modification is allowed without any issues.

    C) The program crashes.

    D) The value is automatically converted to a constant.

    Answer: A) It results in a compilation error.

  4. Consider the following declaration: `const int* ptr;` What does this declaration indicate?

    A) `ptr` is a constant integer.

    B) `ptr` is a pointer to a constant integer.

    C) `ptr` is a constant pointer.

    D) `ptr` is a pointer to a constant pointer.

    Answer: B) `ptr` is a pointer to a constant integer.

  5. Which of the following is a valid way to declare a constant pointer to an integer in C?

    A) `int* const ptr;`

    B) `const int* ptr;`

    C) `int const* ptr;`

    D) `const int* const ptr;`

    Answer: A) `int* const ptr;`

  6. What is the key difference between a constant pointer and a pointer to a constant value in C?

    A) A constant pointer cannot change its memory address, while a pointer to a constant value can change the value it points to.

    B) A constant pointer can change the value it points to, while a pointer to a constant value cannot change its memory address.

    C) There is no difference; the terms are used interchangeably.

    D) Both a constant pointer and a pointer to a constant value cannot be modified.

    Answer: A) A constant pointer cannot change its memory address, while a pointer to a constant value can change the value it points to.

  7. What does the following declaration mean: `int const* const ptr;`?

    A) `ptr` is a constant pointer to a constant integer.

    B) `ptr` is a constant integer.

    C) `ptr` is a pointer to a constant integer.

    D) `ptr` is a constant pointer.

    Answer: A) `ptr` is a constant pointer to a constant integer.

  8. When should you use a const pointer in C?

    A) When you want to declare a pointer to a constant value.

    B) When you want to prevent the pointer from changing its memory address.

    C) When you want to create a constant integer.

    D) When you want to allocate memory dynamically.

    Answer: B) When you want to prevent the pointer from changing its memory address.

  9. What is a void pointer (void*) in C primarily used for?

    A) Storing integer values

    B) Storing characters

    C) Storing pointers of unknown types

    D) Storing floating-point numbers

    Answer: C) Storing pointers of unknown types

  10. Which of the following statements about void pointers is true?

    A) They can be dereferenced directly without type casting.

    B) They can only point to integer data types.

    C) They are not allowed in C programming.

    D) They require explicit type casting when dereferencing.

    Answer: D) They require explicit type casting when dereferencing.

  11. What is the size of a void pointer (void*) on most systems?

    A) 1 byte

    B) 2 bytes

    C) 4 bytes

    D) It varies depending on the system.

    Answer: D) It varies depending on the system.

  12. When might you use a void pointer in C?

    A) To create arrays of integers

    B) To store characters

    C) To implement generic data structures

    D) To perform mathematical operations

    Answer: C) To implement generic data structures

  13. What is the purpose of type casting when using void pointers?

    A) To change the size of the pointer

    B) To convert the pointer to a specific data type

    C) To make the pointer constant

    D) To allocate memory for the pointer

    Answer: B) To convert the pointer to a specific data type

  14. In C, how do you assign a value to a void pointer?

    A) `void_ptr = 42;`

    B) `void_ptr = &variable;`

    C) `void_ptr = "Hello";`

    D) `void_ptr = (void*)&variable;`

    Answer: D) `void_ptr = (void*)&variable;`

  15. What is the main advantage of using void pointers in C?

    A) They are smaller in size compared to other pointer types.

    B) They allow direct access to memory addresses.

    C) They provide type flexibility for generic programming.

    D) They are automatically allocated by the compiler.

    Answer: C) They provide type flexibility for generic programming.

  16. What happens if you try to dereference a void pointer without proper type casting?

    A) It results in a compilation error.

    B) The program crashes.

    C) It automatically converts the pointer to the correct data type.

    D) It retrieves a random value from memory.

    Answer: A) It results in a compilation error.

  17. What is a null pointer in C?

    A) A pointer that points to a valid memory address.

    B) A pointer that points to a random memory address.

    C) A pointer that does not point to any memory location.

    D) A pointer that points to the end of an array.

    Answer: C) A pointer that does not point to any memory location.

  18. Which keyword is typically used in C to represent a null pointer?

    A) None, null pointers are represented without keywords.

    B) void

    C) NULL

    D) none of the above

    Answer: C) NULL

  19. What is the purpose of a null pointer in C?

    A) To access and modify memory locations.

    B) To indicate the end of an array.

    C) To represent an uninitialized or invalid pointer.

    D) To store numeric values.

    Answer: C) To represent an uninitialized or invalid pointer.

  20. What happens if you attempt to dereference a null pointer?

    A) It results in a compilation error.

    B) The program crashes.

    C) It returns a value of 0.

    D) It points to the next available memory location.

    Answer: B) The program crashes.

  21. How can you check if a pointer is a null pointer in C?

    A) By using the `is_null(ptr)` function.

    B) By comparing it to the integer value 0.

    C) By checking its type.

    D) By using the `void_ptr` keyword.

    Answer: B) By comparing it to the integer value 0.

  22. What is the primary use of null pointers in C?

    A) To allocate memory dynamically.

    B) To represent the beginning of an array.

    C) To indicate the absence of a valid memory address.

    D) To store integer values.

    Answer: C) To indicate the absence of a valid memory address.

  23. Which of the following is a valid way to initialize a pointer as a null pointer?

    A) `int* ptr = 0;`

    B) `int* ptr = NULL;`

    C) `int* ptr;`

    D) All of the above

    Answer: D) All of the above

  24. When should you use null pointers in C?

    A) When you want to access memory locations directly.

    B) When you need to indicate that a pointer is valid.

    C) When you want to check if a pointer is equal to 1.

    D) When you want to create an array.

    Answer: B) When you need to indicate that a pointer is valid.

  25. What is an "array of pointers" in C?

    A) A single pointer that can point to multiple arrays.

    B) An array in which each element is a pointer to another array.

    C) An array of integers.

    D) An array of characters.

    Answer: B) An array in which each element is a pointer to another array.

  26. What is the advantage of using an array of pointers in C?

    A) It allows you to store multiple arrays of different data types.

    B) It provides a way to dynamically allocate memory for arrays.

    C) It simplifies array indexing and access.

    D) It is more memory-efficient than regular arrays.

    Answer: B) It provides a way to dynamically allocate memory for arrays.

  27. How do you declare an array of pointers to integers in C?

    A) `int* ptrArray[10];`

    B) `int ptrArray[10]*;`

    C) `int*[] ptrArray = new int[10];`

    D) `int** ptrArray = (int*)malloc(10 * sizeof(int));`

    Answer: A) `int* ptrArray[10];`

  28. In an array of pointers, what does each element of the array typically point to?

    A) An integer value.

    B) A character value.

    C) Another array or data structure.

    D) A null pointer.

    Answer: C) Another array or data structure.

  29. How do you access an element of an array of pointers in C?

    A) Using array indexing like `ptrArray[3]`.

    B) By dereferencing the pointer like `*ptrArray[3]`.

    C) Using a loop.

    D) It's not possible to access elements of an array of pointers.

    Answer: B) By dereferencing the pointer like `*ptrArray[3]`.

  30. What is the advantage of using an array of pointers over a two-dimensional array?

    A) An array of pointers is more memory-efficient.

    B) An array of pointers can store elements of different data types.

    C) An array of pointers allows for more straightforward memory management.

    D) There is no advantage; they are used interchangeably.

    Answer: C) An array of pointers allows for more straightforward memory management.

  31. How do you allocate memory for an array of pointers in C?

    A) `ptrArray = new int*[10];`

    B) `ptrArray = (int**)malloc(10 * sizeof(int*));`

    C) `int* ptrArray[10];` does not require explicit allocation.

    D) `ptrArray = malloc(10 * sizeof(int*));`

    Answer: C) `int* ptrArray[10];` does not require explicit allocation.

  32. What does the following code do? `char* strArray[5] = {"apple", "banana", "cherry", "date", "fig"};`

    A) It declares an array of pointers to strings.

    B) It declares an array of characters.

    C) It declares an array of integers.

    D) It declares a null pointer.

    Answer: A) It declares an array of pointers to strings.

  33. What is a "pointer to pointer" (double pointer) in C?

    A) A pointer that points to another pointer.

    B) A pointer with a double-sized memory allocation.

    C) A pointer that stores two memory addresses.

    D) A pointer to an integer value.

    Answer: A) A pointer that points to another pointer.

  34. What is the primary use of a pointer to pointer in C?

    A) To store a double-precision floating-point number.

    B) To represent a two-dimensional array.

    C) To store a character string.

    D) To perform arithmetic operations.

    Answer: B) To represent a two-dimensional array.

  35. How do you declare a pointer to pointer in C?

    A) `int** ptr_to_ptr;`

    B) `int* ptr_to_ptr;`

    C) `double* ptr_to_ptr;`

    D) `ptr_to_ptr = &pointer;`

    Answer: A) `int** ptr_to_ptr;`

  36. What does a pointer to pointer point to?

    A) It points to an integer value.

    B) It points to another pointer.

    C) It points to a character.

    D) It points to a function.

    Answer: B) It points to another pointer.

  37. How many levels of indirection are there in a pointer to pointer?

    A) One level

    B) Two levels

    C) Three levels

    D) It varies depending on the type.

    Answer: B) Two levels.

  38. What is the purpose of using a pointer to pointer when dealing with functions?

    A) To simplify function declaration.

    B) To pass a function as an argument.

    C) To allow functions to modify the values of other functions.

    D) To pass an array to a function.

    Answer: D) To pass an array to a function.

  39. What is the syntax for dereferencing a pointer to pointer to access the value it points to?

    A) `**ptr_to_ptr`

    B) `*ptr_to_ptr`

    C) `ptr_to_ptr->value`

    D) `ptr_to_ptr[0]`

    Answer: A) `**ptr_to_ptr`

  40. How do you initialize a pointer to pointer with the address of another pointer in C?

    A) `ptr_to_ptr = pointer;`

    B) `ptr_to_ptr = &pointer;`

    C) `ptr_to_ptr = &&pointer;`

    D) `ptr_to_ptr = *pointer;`

    Answer: B) `ptr_to_ptr = &pointer;`

  41. What is pointer arithmetic in C?

    A) Performing mathematical operations on pointers.

    B) Creating new pointers from existing pointers.

    C) Comparing pointers for equality.

    D) Using pointers as function arguments.

    Answer: A) Performing mathematical operations on pointers.

  42. What operation can you perform on a pointer to increment its value?

    A) Addition (`+`)

    B) Subtraction (`-`)

    C) Multiplication (`*`)

    D) Division (`/`)

    Answer: A) Addition (`+`)

  43. If you have an integer pointer `intPtr` pointing to the address `0x1000`, what is the result of `intPtr + 2`?

    A) `0x1002`

    B) `0x1004`

    C) `0x2000`

    D) `0x2002`

    Answer: B) `0x1004`

  44. What is the size, in bytes, of a `char` data type in C?

    A) 2 bytes

    B) 4 bytes

    C) 1 byte

    D) 8 bytes

    Answer: C) 1 byte

  45. When you perform pointer arithmetic on an array, what does `arrPtr + 1` point to?

    A) The next element of the array.

    B) The first element of the array.

    C) The previous element of the array.

    D) The end of the array.

    Answer: A) The next element of the array.

  46. What happens if you subtract two pointers pointing to elements of the same array?

    A) It results in a compilation error.

    B) It gives the difference in bytes between the two elements.

    C) It gives the sum of the two elements.

    D) It points to the middle of the array.

    Answer: B) It gives the difference in bytes between the two elements.

  47. What is the result of adding an integer value to a pointer?

    A) The pointer is incremented by that many bytes.

    B) The pointer points to the next integer in memory.

    C) The pointer becomes a null pointer.

    D) The pointer points to the end of the array.

    Answer: A) The pointer is incremented by that many bytes.

  48. When subtracting a larger pointer address from a smaller one, what does it typically indicate?

    A) A program error or undefined behavior.

    B) The size of the data structure pointed to.

    C) A valid operation, as long as it's within the same array.

    D) The difference in memory allocated to the pointers.

    Answer: A) A program error or undefined behavior.