0% found this document useful (0 votes)
37 views14 pages

C++ Pointer and Memory Management MCQs

The document consists of multiple-choice questions (MCQs) focused on C++ programming concepts, particularly pointers, memory management, recursion, and structures. Each question is followed by four answer options, with the correct answer indicated. The content is designed to test knowledge and understanding of C++ programming fundamentals.

Uploaded by

kalapatirahul1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views14 pages

C++ Pointer and Memory Management MCQs

The document consists of multiple-choice questions (MCQs) focused on C++ programming concepts, particularly pointers, memory management, recursion, and structures. Each question is followed by four answer options, with the correct answer indicated. The content is designed to test knowledge and understanding of C++ programming fundamentals.

Uploaded by

kalapatirahul1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 5 C++ MCQs:

Q1. What is the main purpose of a pointer in C++?


a) Store a variable’s value
b) Store a variable’s address
c) Store the type of a variable
d) Store the size of a variable
Answer: b

Q2. Which operator is used to allocate memory dynamically in C++?


a) malloc
b) new
c) alloc
d) calloc
Answer: b

Q3. Which operator is used to free memory allocated by new?


a) delete
b) free
c) remove
d) release
Answer: a

Q4. Which is correct syntax to allocate a dynamic array of integers?


a) int* arr = new int[n];
b) int arr[n];
c) int arr = malloc(n);
d) int* arr = malloc(n*sizeof(int));
Answer: a

Q5. What happens if delete is used twice on the same pointer?


a) Safe, nothing happens
b) Undefined behavior
c) Pointer is automatically set to NULL
d) Compilation error
Answer: b

Q6. What is a dangling pointer?


a) Pointer to allocated memory
b) Pointer to freed memory
c) Pointer to NULL
d) Pointer to a global variable
Answer: b

Q7. What is the output of sizeof(int*) on a 64-bit system?


a) 2 bytes
b) 4 bytes
c) 8 bytes
d) 16 bytes
Answer: c

Q8. Which of the following is true about a function pointer?


a) Stores data
b) Stores a function address
c) Calls a variable
d) Deletes memory
Answer: b

Q9. What does the * operator do when used with a pointer?


a) Allocates memory
b) Returns the address
c) Dereferences the pointer
d) Multiplies the pointer
Answer: c

Q10. How do you pass an array to a function?


a) By value
b) By pointer
c) By reference
d) Both b and c
Answer: d

Q11. Which of the following is true about recursion?


a) Always uses less memory than iteration
b) Can cause stack overflow if base case is missing
c) Cannot solve factorial problem
d) Always slower than iteration
Answer: b

Q12. What is a base case in recursion?


a) Condition to stop recursion
b) Condition to call recursion
c) First call to recursion
d) Last line in function
Answer: a

Q13. Which of the following is a valid recursive factorial function?


a) int f(int n){ return n*f(n-1); }
b) int f(int n){ return n*f(n+1); }
c) int f(int n){ return n*f(n); }
d) int f(int n){ return 1; }
Answer: a

Q14. What is the default access specifier in a C++ struct?


a) private
b) public
c) protected
d) friend
Answer: b

Q15. What is the correct syntax to declare a struct?


a) struct Point { int x; int y; };
b) structure Point { int x; int y; };
c) class Point { int x; int y; };
d) struct Point();
Answer: a
Q16. Which is correct to declare a pointer to a struct?
a) Point p; Point* ptr = &p;
b) Point p; Point ptr = &p;
c) struct p* ptr;
d) Point ptr* = p;
Answer: a

Q17. How do you dynamically allocate memory for a struct?


a) Point* ptr = new Point;
b) Point* ptr = malloc(sizeof(Point));
c) Both a and b
d) Point ptr;
Answer: c

Q18. What happens when a pointer is NULL?


a) Points to some memory
b) Points to no valid memory
c) Automatically allocated memory
d) Compiler error
Answer: b

Q19. Which function cannot change the original argument if passed by value?
a) void f(int x)
b) void f(int* x)
c) void f(int &x)
d) void f(Point* p)
Answer: a

Q20. Which of these allows modification of original data?


a) Pass by value
b) Pass by reference
c) Pass by constant value
d) None of these
Answer: b

Q21. Which is correct to declare a pointer to a function returning int?


a) int (*ptr)();
b) int ptr();
c) void (*ptr)();
d) float (*ptr)();
Answer: a

Q22. Which of the following is invalid?


a) int* p;
b) int *p;
c) int p*;
d) int *p=NULL;
Answer: c

Q23. Which of these can be used to pass large structures efficiently?


a) By value
b) By pointer
c) By reference
d) Both b and c
Answer: d

Q24. Which is a valid way to declare a 2D dynamic array?


a) int **arr = new int*[m]; for(int i=0;i<m;i++) arr[i]=new int[n];
b) int arr[m][n];
c) int *arr = malloc(m*n*sizeof(int));
d) Both a and c
Answer: d

Q25. What does delete[] do?


a) Deletes single variable
b) Deletes dynamically allocated array
c) Deletes structure
d) Deletes function
Answer: b

Q26. What is the output of int* p = new int(10); cout<<*p;?


a) Garbage
b) 10
c) 0
d) Compilation error
Answer: b

Q27. Which statement is true about nullptr in C++11?


a) It is a macro
b) It is a literal representing null pointer
c) Replaces NULL
d) Both b and c
Answer: d

Q28. Which is correct syntax to define a function with pointer parameter?


a) void f(int* x)
b) void f(int &x)
c) void f(int x)
d) void f(*int x)
Answer: a

Q29. Which of these is correct to declare a union?


a) union Data { int x; float y; };
b) struct Data { int x; float y; };
c) class Data { int x; float y; };
d) union Data();
Answer: a

Q30. Size of union is determined by?


a) Sum of sizes of members
b) Size of largest member
c) Size of first member
d) Compiler choice
Answer: b

Q31. What is true about structure arrays?


a) Array can store multiple struct objects
b) Array cannot store structs
c) Array stores addresses only
d) Array size is dynamic only
Answer: a

Q32. Can structures contain arrays?


a) Yes
b) No
c) Only for C++
d) Only if array size is 1
Answer: a

Q33. Can structures be nested?


a) Yes
b) No
c) Only pointers
d) Only static arrays
Answer: a

Q34. Which is true about pointers to structure members?


a) ptr->member is valid
b) (*ptr).member is valid
c) Both a and b
d) None
Answer: c

Q35. What is the output? int a=5; int* p=&a; cout<<*p;


a) 0
b) 5
c) Address of a
d) Garbage
Answer: b

Q36. Which is valid to pass pointer to function?


a) void f(int* p)
b) void f(int p)
c) void f(int &p)
d) void f(int** p)
Answer: a

Q37. Which of these causes segmentation fault?


a) Dereferencing NULL
b) Using uninitialized pointer
c) Both a and b
d) None
Answer: c

Q38. Which is valid for dynamic memory deallocation?


a) delete ptr;
b) delete[] arr;
c) Both a and b
d) None
Answer: c
Q39. Which is true for recursive functions?
a) Must have base case
b) Can call itself
c) Can solve factorial, fibonacci, etc.
d) All of the above
Answer: d

Q40. What is the base case of Fibonacci recursion?


a) n==0 and n==1
b) n==1 only
c) n==2 only
d) None
Answer: a

Q41. Which is correct for passing pointer to function?


a) f(&var)
b) f(var)
c) f(*var)
d) f(**var)
Answer: a

Q42. Which is correct to allocate memory for int dynamically?


a) int* p = new int;
b) int* p = new int(10);
c) Both a and b
d) None
Answer: c

Q43. Which is correct for 2D array declaration in struct?


a) int arr[3][3];
b) int arr[3];
c) int** arr;
d) Both a and c
Answer: d

Q44. Size of struct with 1 int and 1 char?


a) 5 bytes
b) 8 bytes (due to padding)
c) 4 bytes
d) 2 bytes
Answer: b

Q45. Which is valid pointer arithmetic?


a) ptr++
b) ptr--
c) ptr + 2
d) All of the above
Answer: d

Q46. Which operator accesses struct members via pointer?


a) . (dot)
b) -> (arrow)
c) &
d) *
Answer: b

Q47. Which of these cannot be done using pointers?


a) Access array elements
b) Access dynamic memory
c) Access member functions
d) Access private variables directly without getter
Answer: d

Q48. Which is correct way to pass structure to function without copying?


a) Pass pointer
b) Pass by reference
c) Both a and b
d) None
Answer: c

Q49. What is the output? int a=2; int* p=&a; *p=5; cout<<a;
a) 2
b) 5
c) Garbage
d) Compilation error
Answer: b

Q50. Which memory segment stores dynamic memory?


a) Stack
b) Heap
c) Data segment
d) Code segment
Answer: b

Q51. Which is true about delete operator?


a) Frees heap memory
b) Frees stack memory
c) Frees code segment
d) Frees global variables
Answer: a

Q52. Which of these is valid pointer initialization?


a) int* p = 0;
b) int* p = NULL;
c) int* p = nullptr;
d) All of the above
Answer: d

Q53. Which of these correctly declares function pointer returning int?


a) int (*ptr)();
b) int ptr();
c) int *ptr();
d) void (*ptr)();
Answer: a

Q54. What is the output? int a=3; int* p=&a; cout<<*p+2;


a) 3
b) 5
c) 2
d) Garbage
Answer: b

Q55. Which of the following is true about unions?


a) Stores all members at same memory location
b) Stores members at different addresses
c) Members cannot be accessed
d) Unions cannot contain arrays
Answer: a

Q56. Maximum one member of a union can hold a value at a time.


a) True
b) False
c) Depends on compiler
d) Only for structs
Answer: a

Q57. Which of these correctly accesses a union member via pointer?


a) ptr->x
b) (*ptr).x
c) Both a and b
d) None
Answer: c

Q58. Which is correct syntax for passing array to function?


a) void f(int arr[], int n)
b) void f(int* arr, int n)
c) Both a and b
d) void f(arr[], int n)
Answer: c

Q59. What is the output? int arr[3]={1,2,3}; int* p=arr; cout<<*(p+1);


a) 1
b) 2
c) 3
d) Garbage
Answer: b

Q60. Which is correct to dynamically allocate 2D array?


a) int **arr=new int*[m]; for(int i=0;i<m;i++) arr[i]=new int[n];
b) int arr[m][n];
c) int *arr= new int[m*n];
d) Both a and c
Answer: d

Q61. Which of these is invalid pointer operation?


a) ptr++
b) ptr--
c) ptr+2
d) ptr*2
Answer: d
Q62. Which is true for recursion?
a) Must have base case
b) Can call itself
c) Can solve factorial, Fibonacci, etc.
d) All of the above
Answer: d

Q63. Which of the following is a base case in recursion?


a) if(n==0) return 1;
b) if(n>0) call recursion;
c) return n;
d) None
Answer: a

Q64. Which of these is valid recursive sum function?


a) int sum(int n){if(n==0)return 0; return n+sum(n-1);}
b) int sum(int n){return n+sum(n);}
c) int sum(int n){return sum(n-1);}
d) None
Answer: a

Q65. Which of the following cannot be done using pointer?


a) Access array elements
b) Access memory dynamically
c) Access private class members directly
d) Access structure members
Answer: c

Q66. Which is valid structure array declaration?


a) Point arr[5];
b) Point arr;
c) arr[5];
d) Point* arr;
Answer: a

Q67. Which is correct for nested structure access?


a) [Link]
b) outer->[Link]
c) Both valid depending on pointer
d) None
Answer: c

Q68. Which of these is true about pointer arithmetic?


a) ptr+1 moves by size of pointed type
b) ptr++ moves by 1 byte
c) ptr-- moves by 1 bit
d) None
Answer: a

Q69. Which is correct dynamic allocation of struct array?


a) Point* ptr=new Point[n];
b) Point ptr[n];
c) Point* ptr=malloc(n*sizeof(Point));
d) Both a and c
Answer: d

Q70. Size of union with int and char[10]?


a) 4 bytes
b) 10 bytes
c) 12 bytes
d) 14 bytes
Answer: b

Q71. Which statement about delete[] is correct?


a) Used for array allocated with new[]
b) Used for single variable
c) Frees stack memory
d) Frees global variable
Answer: a

Q72. Which is valid pointer to function with int return type?


a) int (*ptr)(int,int);
b) int ptr(int,int);
c) void (*ptr)(int,int);
d) int ptr*();
Answer: a

Q73. Which is correct syntax to declare pointer to 2D array?


a) int (*ptr)[n];
b) int* ptr[n];
c) int** ptr;
d) Both a and c
Answer: d

Q74. Which is correct syntax to access struct member via pointer?


a) ptr->member
b) (*ptr).member
c) Both a and b
d) None
Answer: c

Q75. Which of the following is invalid?


a) int* p=0;
b) int* p=NULL;
c) int* p=nullptr;
d) int* p=garbage;
Answer: d

Q76. Which is true about pointer dereferencing?


a) *ptr gives value
b) &ptr gives address
c) Both
d) None
Answer: c

Q77. Which of the following is true about recursion?


a) Must terminate
b) Uses stack memory
c) Can solve factorial
d) All of the above
Answer: d

Q78. Which is valid union declaration?


a) union Data{int x; float y;};
b) union Data{int x; float y;} d;
c) Both a and b
d) None
Answer: c

Q79. Which of these is invalid?


a) Point* ptr=new Point;
b) Point* ptr=new Point[5];
c) Point ptr=new Point;
d) Point ptr[5];
Answer: c

Q80. Which is correct way to access array element via pointer?


a) *(ptr+i)
b) ptr[i]
c) Both
d) None
Answer: c

Q81. Which is true about dynamic memory?


a) Stored in heap
b) Size known at runtime
c) Must be freed manually
d) All of the above
Answer: d

Q82. Which is correct to pass pointer to function?


a) f(&var)
b) f(var)
c) f(*var)
d) f(**var)
Answer: a

Q83. Which is valid way to declare array of structures?


a) Point arr[5];
b) Point arr;
c) Point arr();
d) Point* arr;
Answer: a

Q84. Which operator is used to access pointer to member?


a) .
b) ->
c) &
d) *
Answer: b
Q85. Which is true for function parameters?
a) Can pass by value
b) Can pass by reference
c) Can pass pointer
d) All of the above
Answer: d

Q86. Which is valid function pointer declaration?


a) int (*fp)(int,int);
b) int fp(int,int);
c) void (*fp)(int,int);
d) int fp*();
Answer: a

Q87. What is the output? int a=10; int* p=&a; *p=20; cout<<a;
a) 10
b) 20
c) Garbage
d) Error
Answer: b

Q88. Which statement about pointer arithmetic is correct?


a) ptr+1 moves by size of type
b) ptr++ increments by 1 byte
c) ptr-- moves by 1 bit
d) None
Answer: a

Q89. Which is correct to allocate memory for array dynamically?


a) int* arr=new int[5];
b) int arr[5];
c) int* arr=malloc(5*sizeof(int));
d) Both a and c
Answer: d

Q90. Which is correct way to pass struct by reference?


a) void f(Point &p)
b) void f(Point* p)
c) Both a and b
d) None
Answer: c

Q91. Which is valid union usage?


a) union Data d; d.x=10;
b) d.y=5;
c) Both
d) None
Answer: c

Q92. Which is true about stack vs heap?


a) Stack is automatic memory
b) Heap is dynamic memory
c) Heap memory must be freed manually
d) All of the above
Answer: d

Q93. Which is invalid pointer initialization?


a) int* p=0;
b) int* p=NULL;
c) int* p; (uninitialized)
d) int* p=garbage;
Answer: d

Q94. Which of the following is true about struct padding?


a) Compiler adds padding to align members
b) Padding increases memory usage
c) Padding ensures proper access
d) All of the above
Answer: d

Q95. Which of the following is true about recursion?


a) Must have base case
b) Uses stack
c) Can solve factorial, Fibonacci
d) All of the above
Answer: d

Q96. Which operator is used to access struct via pointer?


a) .
b) ->
c) *
d) &
Answer: b

Q97. Which is valid dynamic allocation of 2D array?


a) int** arr=new int*[m]; for(int i=0;i<m;i++) arr[i]=new int[n];
b) int arr[m][n];
c) int* arr=new int[m*n];
d) Both a and c
Answer: d

Q98. Which of these is a dangling pointer example?


a) Pointer after delete
b) Pointer pointing to valid memory
c) Pointer initialized to NULL
d) Pointer to global variable
Answer: a

Q99. Which is correct for accessing array element via pointer?


a) *(arr+i)
b) arr[i]
c) Both a and b
d) None
Answer: c
Q100. Which is true about pointer to struct?
a) Can access member via ->
b) Can access member via (*ptr).member
c) Can pass to function
d) All of the above
Answer: d

You might also like