C Programming II with Linux - Practice MCQs
Module 3 PART 1 ( Pointers and Structures)
Prof. Manjunath
1
Which operator is used to access members of a structure using a pointer?
A) .
B) ->
C) &
D) *
Answer: B
2
What is the correct way to declare a pointer to a structure in C?
A) struct Student *ptr;
B) struct *Student ptr;
C) Student struct *ptr;
D) *struct Student ptr;
Answer: A
3
What does the following code print?
struct student { char s_name[20],
int s_id };
struct student s1 = {“Naveen”,25};
struct student *ptr = &s1;
printf("%d", ptr->s_id);
A) Error
B) 0
C) Address of x
D) 25
Answer: D
4
What does the -> operator actually do in the context of structures
and pointers?
A) Multiplies structure member
B) Dereferences a pointer and accesses the member
C) Allocates memory
D) Passes structure by value
Answer: B
5
Which statement correctly assigns a value to a structure member
using a pointer?
A) *ptr.x = 5;
B) ptr->x = 5;
C) ptr.x = 5;
D) (*ptr)->x = 5;
Answer: B
6
Given the following structure, how would you access the ID
member of Emp using a pointer?
struct Emp {
char name[25];
int ID;
};
struct Emp E1;
struct Emp *ptr;
A) ptr->ID;
B) ptr.ID;
C) E1->ID;
D) ptr[0].ID;
Answer: A) ptr->ID;
7
What is the primary advantage of passing large structures by
reference?
A) Better syntax
B) Less memory usage
C) More readable code
D) Makes no difference
Answer: B) Less memory usage
Prof. Pradeep
1 What does the following code print?
A. 0
B. Compilation Error
C. 5
D. Garbage value
Answer: C
2
Which of the following statements is true about structure pointers?
A. (*ptr).member is equivalent to ptr.member
B. ptr->member is invalid in C
C. ptr->member is equivalent to (*ptr).member
D. Pointers to structures cannot be passed to functions
Answer: C
3 What is the output of the following code?
A. 1
B. 2
C. Garbage Value
D. Compilation Error
Answer: B
4 What is wrong with the following code snippet?
A. Nothing
B. s is not initialized before use
C. id is not a member of the structure
D. Wrong use of pointer syntax
Answer: B
5 What will this program print?
A. 0
B. 42
C. Garbage
D. Error
Answer: B
Prof. Gourish
1 struct point { int x; int y; };
struct point p = {10, 20};
struct point *ptr = &p;
What is the correct way to access x using ptr?
A. ptr->x
B. (*ptr).x
C. *ptr.x
D. Both A and B
Solution:
Answer: D
Both ptr->x and (*ptr).x correctly access x.
ptr->x is shorthand for (*ptr).x.
Option C (*ptr.x) is invalid syntax.
2
What will be the output of the following code?
struct data { int value; };
void display(struct data d) {
printf("%d", d.value);
}
int main() {
struct data d1 = {50};
display(d1);
return 0;
}
A. 50
B. Garbage value
C. Compilation error
D. Address of value
Solution:
Answer: A
The structure d1 is passed by value to the function display().
So, it prints 50.
3
Consider this function:
struct point { int x; int y; };
void update(struct point *p) {
p->x = 5;
p->y = 15;
}
Which of the following correctly calls the function?
A. update(p);
B. update(&p);
C. update(&p1); where struct point p1; is declared
D. update(*p);
Solution:
Answer: C
The function expects a pointer to a structure, so update(&p1) is correct.
Option A and D are invalid (wrong type).
Option B assumes p is a structure, but we need explicit declaration of p1.
4
Which of the following modifies the actual structure values in the caller?
struct sample { int a; int b; };
void func1(struct sample s);
void func2(struct sample *s);
A. func1() — Call by value
B. func2() — Call by reference (pointer)
C. Both
D. Neither
Solution:
Answer: B
func2(struct sample *s) allows modifying actual structure values (call by
reference).
func1(struct sample s) passes by value, so changes won’t affect the caller.
5
What will be the output of this program?
struct sample { int a; };
void modify(struct sample *s) {
s->a = s->a + 10;
}
int main() {
struct sample s1 = {20};
modify(&s1);
printf("%d", s1.a);
return 0;
}
A. 20
B. 30
C. Compilation error
D. Garbage value
Solution:
Answer: B
modify(&s1) passes address of s1.
Inside modify(), s->a = 20 + 10 = 30.
Hence, output is 30.
6
Consider:
struct rect { int length; int breadth; };
void area(struct rect r) {
printf("%d", r.length * r.breadth);
}
int main() {
struct rect r1 = {4, 5};
area(r1);
return 0;
}
What will be the output?
A. 20
B. 9
C. Compilation error
D. Garbage value
Solution:
Answer: A
area(r1) passes structure by value.
r.length * r.breadth = 4 * 5 = 20.
Output is 20.
Prof. Pratik
1
What is the correct way to access members of structure ‘point’ using a pointer.
struct point {
int x;
int y;
}
struct point *pp;
A. *(pp->x)
B. (*pp).x
C. pp.*(x)
D. pp.x
Solution: B. Here pp is a pointer to a structure of type ‘struct point’, and ‘x’ and
‘y’ are members of it. To access members using pointers either use pp->x or
(*pp).x
2
Which set of statements can be used to initialize pp and print x coordinate of
points[1].
struct point points[2], *pp;
A. pp = points; and printf(“x: %d”,pp.points);
B. pp->points; and printf(“x: %d”,pp.x);
C. pp = &points[0]; and printf(“x: %d”,(pp+1)->x);
D. pp = points; and printf(“x: %d”,(*pp).x);
Solution: C. Initialize pp using pp=&points[0] or pp=points. Then to access x
coordinate of points[1], use printf(“x: %d”,(pp+1)->x)
3
Which is the correct way of initializing member ‘author’ of structure book.
struct book
{
char title[50];
char author[20];
char publisher[20];
int pubyr;
};
struct book book1, *bp = &book1;
A. struct book book1 = {“KN KING”};
B. book1.author = “KN KING”;
C. strcpy(book1-> author = “KN KING”);
D. strcpy(bp->author, “KN KING”);
Solution: D. bp is a pointer to book1 and author is a char array.
4
What will be the output of the following?
int main(){
struct book book1 = {.pubyr=1988};
modify_reference(book1);
printf(“%d”, book1.pubyr);
}
void modify_reference(struct book book1){
book1.pubyr = 1989;
}
A. 1988
B. 1989
C. Compilation error
D. Prints 1989 followed by 1988
Solution: A. book1 is passed by value. Inside modify_reference, a copy of pubyr
is being initialized to 1989, the original member value remains the same.
5
What should the prototype of function print_reference be?
intt main(){
struct book book1 = {“The C Prog Lang”, “Kernighan Ritchie”, “Prentice
Hall”, 1988};
print_reference(&book1);
}
A. print_reference(struct book);
B. print_reference(struct book1);
C. print_reference(struct book *);
D. print_reference(struct book->book1);
Solution: C. From the function call it is evident that print_reference expects a
pointer to structure of type struct book
Prof. Sowmya.B
1
What does the -> operator do in C?
a) Access value through pointer
b) Access structure member using pointer
c) Assign value to pointer
d) Dereference pointer
Answer: (b)
2 What is the output of the following code?
struct Point {
int x, y;
};
struct Point p = {10, 20};
struct Point *ptr = &p;
printf("%d", ptr->x);
a) Address of x
b) 10
c) 20
d) error
Answer: (b)
3
Which of the following is the correct way to initialize a pointer to a structure in C?
a) ptr = &s;
b) ptr = s;
c) ptr = *s;
d) ptr = &s.member;
Answer: (a)
4 Which of the following is the correct syntax to define a function that accepts a
structure by pointer in C?
a)void function(structName *ptr);
b) void function(structName ptr);
c) void function(&structName);
d) void function(structName &ptr);
Answer:(a)
5
What is the output of the following code?
struct point {
int x;
int y; };
struct point p1 = {2, 3};
struct point *ptr = &p1;
printf("%d %d", ptr->x, ptr->y);
a) 2 3
b) 3 2
c) 2 2
d) 3 3
Answer: (a)
6 What is the output of the following C code?
#include <stdio.h>
struct student {
char *name;
int age;
};
int main() {
struct student s = {"John", 20};
struct student *ptr = &s;
printf("%s is %d years old.", ptr->name, ptr->age);
return 0;
}
a) Compile-time error
b) John is 20 years old.
c) Garbage value
d) Segmentation fault
Answer:(b)
Prof. Padmaja
1 Question: What is the purpose of the & (address-of) operator in C?
● a): It returns the value stored at a memory address.
● b): It returns the memory address of a variable.
● c): It dereferences a pointer to access the value.
● d): It declares a pointer variable.
2 Which of the following is the correct way to access the age member of a structure using
a pointer?
A) (*ptr).age
B) ptr->age
C) Both A and B
D) ptr.age
Answer: C) Both A and B
3
Which operator is used to access structure members using a pointer to the
structure?
A) .
B) *
C) ->
D) &
Answer: C) ->
4
What will happen if you forget to allocate memory to a pointer before accessing
members of the structure it points to?
A) It will work fine
B) Compile-time error
C) Run-time error (Segmentation fault)
D) The program will print garbage values
Answer: C) Run-time error (Segmentation fault)
5
What is the correct way to declare a pointer to a structure?
A) struct Student *ptr;
B) Student ptr;
C) *struct Student ptr;
D) Student -> ptr;
Answer: A) struct Student *ptr;
6 What does this code do?
A) Shows error
B) Prints 0
C) Prints 100
D) Prints address
Answer: C) Prints 100
Prof. Chandrika
1 What is a structure pointer in C?
A. A pointer that points to a function returning a structure
B. A pointer that stores the address of a structure variable
C. A pointer that stores the size of a structure
D. A pointer that points to an array of structures
Answer:B) A pointer that stores the address of a structure variable
Explanation:A structure pointer is a pointer variable that holds the address of a structure
variable, allowing indirect access to its members.
2 What is the output of the following program?
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p = {10, 20};
struct Point *ptr = &p;
printf("%d %d", ptr->x, ptr->y);
return 0;
}
A) 10 20
B) 0 0
C) Compilation Error
D) Garbage Values
Answer: A) 10 20
Explanation: The pointer ptr points to the structure p, and using ptr->x and ptr->y accesses the
respective members, printing their values
2 Consider the below code snippet
struct Student {
int id;
char name[50];
};
struct Student s = {101, "Dennis"};
struct Student *ptr = &s;
Which of the following correctly accesses the id member in the above code?
A)ptr.id
B) *ptr.id
C)ptr->id
D) (*ptr)->id
Answer:C) ptr->id
Explanation: When using a pointer to a structure, the -> operator is used to access its members.
So, ptr->id correctly accesses the id member of the structure pointed to by ptr.
3 Which of the following accesses the id of the second element?
A) ptr[1].id
B) (*ptr[1]).id
C) (*(ptr + 1)).id
D) ptr->id + 1
Answer:C) (*(ptr + 1)).id
Explanation: ptr + 1 points to the second element of the array. Dereferencing it gives access to
the structure, and then using .id accesses the id member.
4 Which of the following is the correct way to declare a function that modifies a structure passed
by reference?
A) void modify(struct Point p);
B)void modify(struct Point *p);
C)void modify(struct Point &p);
D)void modify(Point p);
Answer: B)void modify(struct Point *p);
Explanation: Passing by reference is achieved by passing a pointer to the structure.
5 What is the output of this program?
#include <stdio.h>
struct Point
{
int x;
int y;
};
int main() {
struct Point pt = {1, 2};
modify(pt);
printf("x = %d, y = %d\n", pt.x, pt.y);
return 0;
}
void modify(struct Point p)
{
p.x = 10;
p.y = 20;
}
A) x = 10, y = 20
B) x = 1, y = 2
C)x = 0, y = 0
D) Compilation error
Answer:B) x = 1, y = 2
Explanation:When a structure is passed by value, a copy is made. Modifications inside the
function do not affect the original structure.
6 What will the below code print?
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point pt = {5, 15};
printf("Before update: x = %d, y = %d\n", pt.x, pt.y);
updatePoint(&pt); // Pass the address of the structure to the function
printf("After update: x = %d, y = %d\n", pt.x, pt.y);
return 0;
}
void updatePoint(struct Point *p) // Function to modify the coordinates of the point
{
p->x += 10;
p->y += 20;
}
A)Before update: x = 5, y = 15 After update: x = 15, y = 35
B)Before update: x = 5, y = 15 After update: x = 10, y = 20
C)Before update: x = 5, y = 15 After update: x = 5, y = 15
D)Before update: x = 5, y = 15 After update: x = 6, y = 16
Answer:A)Before update: x = 5, y = 15 After update: x = 15, y = 35
Explanation:Structure Definition: A struct Point is defined to represent a point in 2D space
with x and y coordinates.
Function updatePoint: This function accepts a pointer to a struct Point. By using the arrow
operator (->), it directly modifies the x and y members of the original structure.
Passing by Reference: In the main function, the address of pt is passed to updatePoint using the
address-of operator (&). This allows the function to modify the original pt structure.
“ All The Best!!”