3.2 Pointers, structure and data files in C programming

3.2.POINTER,STRUCTURE AND DATA FILE IN C PROGRAMMING

 

POINTER ARITHMETIC

 

Question 1:

What is the result of adding an integer value n to a pointer ptr in C?

 

A) Increases the value of ptr by n bytes.

B) Increases the value of ptr by n times the size of the data type it points to.

C) Increases the value of ptr by n times the size of the pointer itself.

D) Raises a compilation error as pointer arithmetic is not allowed in C.

 

Answer: B

 

Question 2:

In C a pointer variable to an integer can be created by the declaration

a) int p*;
b) int *p;
c) int +p;
d) int $p;


 

Answer: a

 

Question 3:

Given an integer pointer int *ptr, how can you access the value it points to?

 

A) *ptr

B) ptr

C) &ptr

D) **ptr

 

Answer: A

 

Question 4:

Which of the following statements is true about pointer arithmetic in C?

 

A) Pointer arithmetic is limited to addition only.

B) Pointer arithmetic automatically scales the integer value based on the size of the data type.

C) Pointer arithmetic can be performed on any type of pointer except function pointers.

D) Pointer arithmetic can only be performed on pointers to arrays.

 

Answer: B

 

Question 5:

Which of the following is a valid expression for incrementing a pointer ptr by one element?

 

A) ptr++

B) ptr += 1

C) ptr = ptr + 1

D) All of the above

 

Answer: D

 

Question 6:

What is the value of *(ptr + 3) if ptr is a pointer to an integer array and ptr initially points to the first element of the array?

 

A) The value of the fourth element in the array.

B) The address of the fourth element in the array.

C) The sum of the first four elements in the array.

D) A compilation error, as pointer arithmetic cannot be applied to arrays.

 

Answer: A

 

Question 7:

Prior to using a pointer variable it should be

a) Declared
b) Initialized
c) Both declared and initalized
d) None of these


 

Answer: c

 

Question 8:

Which of the following statements correct about k used in the below statement?

char ****k;

a) k is a pointer to a pointer to a pointer to a char
b) k is a pointer to a pointer to a pointer to a pointer to a char
c) k is a pointer to a char pointer
d) k is a pointer to a pointer to a char


 

Answer: C

 

Question 9:

In C, which of the following statements is true regarding pointer comparisons?

 

A) Two pointers can be compared using the relational operators (<, >, <=, >=).

B) Pointer comparisons are not allowed in C.

C) Two pointers can be compared only if they point to elements of the same array.

D) Pointer comparisons are only allowed for pointers to function types.

 

Answer: C

 

Question 10:

What is the result of dereferencing a NULL pointer in C?

 

A) It raises a runtime error.

B) It returns the value 0.

C) It retrieves the data at the memory location pointed to by the NULL pointer.

D) It causes undefined behavior.

 

Answer: D

 

POINTER AND ARRAY

 

Question 1:

In C, an array name is a constant pointer that points to the first element of the array.

 

A) True

B) False

 

Answer: A

 

Question 2:

What is the correct way to initialize an array in C?

 

A) int arr[5] = (1, 2, 3, 4, 5);

B) int arr[] = {1, 2, 3, 4, 5};

C) int arr[5]; arr = {1, 2, 3, 4, 5};

D) int arr[5]; arr[5] = {1, 2, 3, 4, 5};

 

Answer: B

 

Question 3:

Which of the following statements is true regarding arrays and pointers in C?

 

A) Arrays and pointers are the same and can be used interchangeably.

B) Arrays and pointers are different, but the array name behaves like a pointer.

C) Arrays and pointers have similar syntax, but they are completely unrelated.

D) Arrays are a type of pointer, but pointers cannot be used as arrays.

 

Answer: B

 

Question 4:

What is the size of the array arr declared as int arr[10]?

 

A) 10

B) 40

C) 4

D) It depends on the compiler and system.

 

Answer: B

 

Question 5:

What does the expression arr[2] represent in C, assuming arr is an integer array?

 

A) The value stored in the second element of the array.

B) The memory address of the second element of the array.

C) The sum of all elements up to the second element of the array.

D) The size of the array.

 

Answer: A

 

Question 6:

Which of the following expressions correctly accesses the third element of a two-dimensional array arr?

 

A) arr[2]

B) arr[0][2]

C) arr[2][0]

D) arr[3]

 

Answer: B

 

Question 7:

What is the relationship between pointers and arrays in C?

 

A) Pointers and arrays are completely unrelated concepts.

B) Pointers can be used to iterate over the elements of an array.

C) Arrays are used to store pointers to other variables.

D) Pointers are used to dynamically allocate memory for arrays.

 

Answer: B

 

Question 8:

Which of the following statements is true regarding passing arrays to functions in C?

 

A) Arrays cannot be passed to functions.

B) When passing an array to a function, the entire array is copied to the function.

C) Only a specific element of an array can be passed to a function.

D) When passing an array to a function, a pointer to the first element is passed.

 

Answer: D

 

Question 9:

What is the result of the expression sizeof(arr) where arr is an array of 100 integers?

 

A) 100

B) 400

C) 4

D) It depends on the compiler and system.

 

Answer: B

 

Question 10:

Which of the following statements is true regarding array indexing in C?

 

A) Array indexing starts from 0 and goes up to the size of the array minus 1.

B) Array indexing starts from 1 and goes up to the size of the array.

C) Array indexing can start from any arbitrary value.

D) Array indexing is not supported in C.

 

Answer: A

 

PASSING POINTER TO FUNCTION

 

Question 1:

In C, what is the purpose of passing a pointer to a function?

 

A) To allow the function to modify the original variable.

B) To save memory by avoiding unnecessary copying of large data.

C) To enable the function to access global variables.

D) To improve the performance of the function.

 

Answer: A

 

Question 2:

Which of the following is the correct syntax for declaring a function that takes a pointer as an argument in C?

 

A) void func(int* ptr);

B) void func(*ptr);

C) void func(ptr int);

D) void func(&ptr);

 

Answer: A

 

Question 3:

What is the significance of using the const keyword when passing a pointer to a function?

 

A) It indicates that the pointer cannot be modified within the function.

B) It specifies that the function should return a constant pointer.

C) It allows the function to modify the original variable through the pointer.

D) It indicates that the pointer is a constant value and cannot be changed.

 

Answer: A

 

Question 4:

When passing an array to a function, does the array decay into a pointer?

 

A) Yes, the array decays into a pointer to its first element.

B) No, the array retains its array type within the function.

C) It depends on the size of the array being passed.

D) Only static arrays decay into pointers.

 

Answer: A

 

Question 5:

Which of the following is the correct way to pass a pointer to a function by reference in C?

 

A) void func(int* &ptr);

B) void func(&ptr);

C) void func(int** ptr);

D) void func(int* *ptr);

 

Answer: C

 

Question 6:

What happens if a null pointer is passed to a function in C?

 

A) It will result in a compilation error.

B) The function will receive the null pointer and can handle it accordingly.

C) It will result in a runtime error.

D) Null pointers cannot be passed to functions in C.

 

Answer: B

 

Question 7:

Which of the following is true regarding the scope of a pointer parameter in a function?

 

A) The scope of a pointer parameter is limited to the function it is defined in.

B) The scope of a pointer parameter extends beyond the function it is defined in.

C) The scope of a pointer parameter depends on the storage class specifier used.

D) Pointers do not have a specific scope in C.

 

Answer: A

 

Question 8:

Which of the following statements is true about passing a pointer to a function in C?

 

A) Only stack-allocated variables can be passed as pointers to functions.

B) Pointers can only be passed by value to functions.

C) Pointers can be passed by value or by reference to functions.

D) Pointers can only be passed by reference to functions.

 

Answer: C

 

Question 9:

What is the purpose of using the void pointer when passing a generic pointer to a function in C?

 

A) It allows the function to modify the original variable through the pointer.

B) It specifies that the function accepts any type of pointer.

C) It indicates that the pointer cannot be modified within the function.

D) It enables the function to access global variables.

 

Answer: B

 

STRUCTURE VS UNION

 

Question 1:

In C, what is the main difference between a structure and a union?

 

A) Structures allow storing multiple types of data, while unions can only store a single type.

B) Structures provide better memory utilization compared to unions.

C) Structures allocate memory for each member independently, while unions share the same memory space for all members.

D) Structures are used for static data, while unions are used for dynamic data.

 

Answer: C

 

Question 2:

Which of the following statements is true regarding structures in C?

 

A) Structures can have members of different sizes.

B) Structures can only contain members of the same data type.

C) Structures automatically allocate memory for their members.

D) Structures cannot have functions as members.

 

Answer: A

 

Question 3:

What is the memory requirement for a structure or union in C?

 

A) The memory requirement is equal to the sum of the sizes of its members.

B) The memory requirement is equal to the size of its largest member.

C) The memory requirement is equal to the size of its smallest member.

D) The memory requirement is fixed and does not depend on the size of its members.

 

Answer: B

 

Question 4:

Which of the following statements is true regarding the initialization of structures and unions in C?

 

A) Structures and unions cannot be initialized during declaration.

B) Structures and unions can only be initialized with literal values.

C) Structures and unions can be initialized using designated initializers.

D) Structures and unions can only be partially initialized.

 

Answer: C

 

Question 5:

What is the keyword used to define a structure in C?

 

A) struct

B) union

C) type

D) typedef

 

Answer: A

 

Question 6:

Which of the following statements is true about the memory layout of structures and unions in C?

 

A) Structures and unions have the same memory layout.

B) Structures have a fixed memory layout, while unions have a variable memory layout.

C) Unions have a fixed memory layout, while structures have a variable memory layout.

D) The memory layout of structures and unions depends on the compiler and system.

 

Answer: D

 

Question 7:

What is the purpose of using the typedef keyword with structures and unions in C?

 

A) It is used to define new types based on existing structures and unions.

B) It is used to allocate memory for structures and unions.

C) It is used to access individual members of structures and unions.

D) It is used to declare arrays of structures and unions.

 

Answer: A

 

Question 8:

Which of the following statements is true regarding the storage size of a union in C?

 

A) The storage size of a union is equal to the sum of the sizes of its members.

B) The storage size of a union is equal to the size of its largest member.

C) The storage size of a union is equal to the size of its smallest member.

D) The storage size of a union is fixed and does not depend on the size of its members.

 

Answer: B

 

Question 9:

What happens if a structure or union is assigned to another structure or union of a different type in C?

 

A) It results in a compilation error.

B) The assignment is allowed, and the data is converted to the target type.

C) The assignment is allowed, and the data is truncated to fit the target type.

D) The assignment is allowed, but the behavior is undefined.

 

Answer: D




 

ARRAY OF STRUCTURE


 

Question 1:

In C, what is an array of structures?

 

A) It is an array where each element is a structure.

B) It is a structure that contains an array.

C) It is a data type that combines arrays and structures.

D) It is a data structure used to store arrays.

 

Answer: A

 

Question 2:

What is the correct way to declare an array of structures in C?

 

A) struct array[10];

B) struct array[10]{};

C) struct array[10] = {};

D) struct array[10] = {0};

 

Answer: C

 

Question 3:

What is the size of an array of structures in C?

 

A) It is the size of a single structure multiplied by the number of elements in the array.

B) It is the size of a single structure divided by the number of elements in the array.

C) It is the size of a single structure plus the number of elements in the array.

D) It is the size of a single structure minus the number of elements in the array.

 

Answer: A

 

Question 4:

Which of the following statements is true regarding accessing members of an array of structures in C?

 

A) Members of an array of structures cannot be accessed individually.

B) Members of an array of structures can only be accessed using a loop.

C) Members of an array of structures can be accessed using the dot operator.

D) Members of an array of structures can be accessed using the arrow operator.

 

Answer: D

 

Question 5:

What is the purpose of using an array of structures in C?

 

A) It allows storing multiple types of data in a single array.

B) It provides better memory utilization compared to a structure with multiple members.

C) It enables efficient access and manipulation of related data elements.

D) It simplifies the process of allocating memory dynamically.

 

Answer: C

 

Question 6:

What is the relationship between the size of a structure and the size of an array of structures in C?

 

A) The size of a structure is always equal to the size of an array of structures.

B) The size of a structure is smaller than the size of an array of structures.

C) The size of a structure is larger than the size of an array of structures.

D) The size of a structure and the size of an array of structures are unrelated.

 

Answer: A

 

Question 7:

How do you access a specific element in an array of structures in C?

 

A) By specifying the index of the desired element within square brackets.

B) By using the sizeof operator with the desired element's index.

C) By using the malloc function to allocate memory for the desired element.

D) By using the memcpy function to copy the desired element to a new variable.

 

Answer: A

 

Question 8:

Which of the following statements is true regarding initializing an array of structures in C?

 

A) An array of structures cannot be initialized during declaration.

B) An array of structures can only be partially initialized.

C) An array of structures can be initialized using designated initializers.

D) An array of structures can only be initialized with literal values.

 

Answer: C

 

Question 9:

Can an array of structures have different sizes for each element?

 

A) Yes, each element in an array of structures can have a different size.

B) No, all elements in an array of structures must have the same size.

C) It depends on the specific implementation of the C compiler.

D) It depends on the data types used in the structure.

 

Answer: B

 

PASSING STRUCTURE TO FUNCTION 

 

Question 1:

In C, what is the purpose of passing a structure to a function?

 

A) To allow the function to modify the original structure.

B) To save memory by avoiding unnecessary copying of large structures.

C) To enable the function to access global structures.

D) To improve the performance of the function.

 

Answer: A

 

Question 2:

Which of the following is the correct way to pass a structure to a function in C?

 

A) Pass the structure by value.

B) Pass the structure by reference using a pointer.

C) Pass the individual members of the structure to the function.

D) Structures cannot be passed to functions in C.

 

Answer: B

 

Question 3:

What is the syntax for declaring a function that takes a structure as an argument in C?

 

A) void func(structure s);

B) void func(struct s);

C) void func(structure *s);

D) void func(struct *s);

 

Answer: C

 

Question 4:

Which of the following statements is true regarding passing structures to functions in C?

 

A) Structures cannot be passed to functions in C.

B) When passing a structure to a function, the entire structure is copied to the function.

C) Only a specific member of a structure can be passed to a function.

D) When passing a structure to a function, a pointer to the structure is passed.

 

Answer: B

 

Question 5:

What is the result of passing a structure to a function without using a pointer in C?

 

A) The function will receive a copy of the structure.

B) The function will receive the original structure and can modify it directly.

C) It will result in a compilation error.

D) Passing a structure without using a pointer is not allowed in C.

 

Answer: A

 

Question 6:

Which of the following statements is true regarding the scope of a structure parameter in a function?

 

A) The scope of a structure parameter is limited to the function it is defined in.

B) The scope of a structure parameter extends beyond the function it is defined in.

C) The scope of a structure parameter depends on the storage class specifier used.

D) Structures do not have a specific scope in C.

 

Answer: A

 

Question 7:

What happens if a structure is passed to a function by value in C?

 

A) The function receives a copy of the structure, and modifications do not affect the original structure.

B) The function receives a reference to the structure and can modify the original structure directly.

C) It will result in a runtime error.

D) Structures cannot be passed by value to functions in C.

 

Answer: A

 

Question 8:

Which of the following statements is true regarding passing a pointer to a structure to a function in C?

 

A) Pointers to structures cannot be passed to functions.

B) When passing a pointer to a structure to a function, the function can modify the original structure.

C) Pointers to structures can only be passed by value to functions.

D) Pointers to structures can only be passed by reference to functions.

 

Answer: B

 

Question 9:

What is the purpose of using the const keyword when passing a structure to a function in C?

 

A) It indicates that the structure cannot be modified within the function.

B) It specifies that the function should return a constant structure.

C) It allows the function to modify the original structure.

D) It indicates that the structure is a constant value and cannot be changed.

 

Answer: A


 

STRUCTURE AND POINTER

 

Question 1:

In C, what is a structure?

 

A) It is a pointer to a memory location.

B) It is a data type that allows storing multiple related variables.

C) It is a keyword used to declare functions.

D) It is a mathematical operation in C.

 

Answer: B

 

Question 2:

What is the correct syntax for declaring a structure in C?

 

A) struct MyStructure {};

B) structure MyStructure {};

C) struct MyStructure;

D) MyStructure {};

 

Answer: A

 

Question 3:

Which of the following is true about accessing members of a structure in C?

 

A) Members of a structure cannot be accessed directly.

B) Members of a structure are accessed using the -> operator.

C) Members of a structure are accessed using the dot (.) operator.

D) Members of a structure can only be accessed through a pointer.

 

Answer: C

 

Question 4:

What is the purpose of using pointers with structures in C?

 

A) Pointers allow dynamic memory allocation for structures.

B) Pointers are used to access individual members of a structure.

C) Pointers provide a way to pass structures to functions efficiently.

D) Pointers are required to declare a structure in C.

 

Answer: C

 

Question 5:

Which of the following is true about passing a pointer to a structure to a function in C?

 

A) Passing a pointer to a structure allows the function to modify the original structure.

B) Passing a pointer to a structure is not allowed in C.

C) Passing a pointer to a structure results in a compilation error.

D) Passing a pointer to a structure is slower compared to passing the structure itself.

 

Answer: A

 

Question 6:

What is the correct way to access a member of a structure through a pointer in C?

 

A) ptr->member

B) *ptr.member

C) ptr[member]

D) ptr.member

 

Answer: A

 

Question 7:

Which of the following statements is true about the size of a structure in C?

 

A) The size of a structure is determined by the sum of the sizes of its members.

B) The size of a structure is always equal to the size of its largest member.

C) The size of a structure is fixed and does not depend on the sizes of its members.

D) The size of a structure is determined by the difference between the sizes of its members.

 

Answer: A

 

Question 8:

What is the purpose of using the sizeof operator with structures in C?

 

A) It returns the size of the structure in bytes.

B) It returns the number of members in the structure.

C) It calculates the sum of the sizes of the members of the structure.

D) The sizeof operator cannot be used with structures.

 

Answer: A

 

Question 9:

Which of the following statements is true regarding initializing a structure in C?

 

A) Structures cannot be initialized during declaration.

B) Structures can only be partially initialized.

C) Structures can be initialized using designated initializers.

D) Structures can only be initialized with literal values.

 

Answer: C

 

Question 10:

What happens when a structure is assigned to another structure in C?

 

A) The values of all members of the source structure are copied to the destination structure.

B) The addresses of the source and destination structures are swapped.

C) The structure assignment is not allowed in C.

D) Only the first member of the source structure is copied to the destination structure.

 

Answer: A

 

INPUT/OUTPUT OPERATION ON FILE

 

Question 1:

Which library should be included to perform file input/output operations in C?

 

A) stdio.h

B) fileio.h

C) iofile.h

D) iostream.h

 

Answer: A

 

Question 2:

What is the function used to open a file for reading in C?

 

A) fopen()

B) open()

C) read()

D) file_open()

 

Answer: A

 

Question 3:

Which mode is used to open a file for writing in C, creating a new file if it doesn't exist or truncating the file if it exists?

 

A) "r"

B) "w"

C) "a"

D) "x"

 

Answer: B

 

Question 4:

What is the purpose of the fclose() function in C?

 

A) It closes the file after reading its contents.

B) It deletes the file from the file system.

C) It opens a file for writing.

D) It closes the file after writing its contents.

 

Answer: D

 

Question 5:

Which function is used to read a single character from a file in C?

 

A) fread()

B) fgetc()

C) fgets()

D) scanf()

 

Answer: B

 

Question 6:

Which function is used to write a string to a file in C?

 

A) fwrite()

B) fputc()

C) fputs()

D) printf()

 

Answer: C

 

Question 7:

Which function is used to read a line from a file in C?

 

A) fread()

B) fgetc()

C) fgets()

D) scanf()

 

Answer: C

 

Question 8:

What is the purpose of the feof() function in C?

 

A) It checks if a file exists.

B) It checks if a file is empty.

C) It checks if the end-of-file indicator is set for a file.

D) It checks if the file permissions are valid.

 

Answer: C

 

Question 9:

Which function is used to position the file pointer at a specific location in a file in C?

 

A) fseek()

B) ftell()

C) rewind()

D) fmove()

 

Answer: A

 

Question 10:

What is the purpose of the fprintf() function in C?

 

A) It reads formatted input from a file.

B) It writes formatted output to a file.

C) It reads binary data from a file.

D) It writes binary data to a file.

 

Answer: B


 

SEQUENTIAL AND RANDOM ACCESS TO FILE 




 

Question 1:

What is sequential access to a file in C?

 

A) Accessing file data in a random order.

B) Reading or writing data from or to a file in a sequential order from start to end.

C) Accessing file data using a pointer.

D) Performing simultaneous read and write operations on a file.

 

Answer: B

 

Question 2:

Which function is used to perform sequential reading from a file in C?

 

A) fread()

B) fgetc()

C) fgets()

D) fscanf()

 

Answer: C

 

Question 3:

What is random access to a file in C?

 

A) Accessing file data using a pointer.

B) Reading or writing data from or to a file in any order, not necessarily sequentially.

C) Accessing file data using an index.

D) Performing simultaneous read and write operations on a file.

 

Answer: B

 

Question 4:

Which function is used to perform random reading from a file in C?

 

A) fread()

B) fgetc()

C) fgets()

D) fscanf()

 

Answer: A

 

Question 5:

Which function is used to perform random writing to a file in C?

 

A) fwrite()

B) fputc()

C) fputs()

D) fprintf()

 

Answer: A

 

Question 6:

What is the purpose of the fseek() function in C?

 

A) To close a file after reading its contents.

B) To move the file pointer to a specific position in a file.

C) To check if the end-of-file indicator is set for a file.

D) To determine the size of a file.

 

Answer: B

 

Question 7:

Which function is used to obtain the current position of the file pointer in C?

 

A) fseek()

B) ftell()

C) rewind()

D) fgetpos()

 

Answer: B

 

Question 8:

What is the purpose of the rewind() function in C?

 

A) To close a file after reading its contents.

B) To move the file pointer to the beginning of a file.

C) To check if the end-of-file indicator is set for a file.

D) To determine the size of a file.

 

Answer: B

 

Question 9:

Which of the following statements is true about sequential access to a file in C?

 

A) It allows for direct access to any location in the file.

B) It is slower compared to random access.

C) It is performed using the fseek() function.

D) It is used for binary data processing.

 

Answer: B

 

Question 10:

Which of the following statements is true about random access to a file in C?

 

A) It allows for reading and writing of file data in any order.

B) It is faster compared to sequential access.

C) It is performed using the fgets() function.

D) It is used for text data processing.

 

Answer: A