Introduction to Programming MCQs with Answers
Practice important Introduction to Programming MCQs with answers and explanations.
Chapter: 27
Q461:
How do constructors and destructors differ in their use in a class?
A. Constructors initialize objects, destructors clean up resources
B. Constructors and destructors both initialize data
C. Destructors are used to create objects
D. Constructors and destructors have similar functions
Correct Answer:
A
Chapter: 27
Q462:
Which function cannot be overloaded?
A. Destructor
B. Member function
C. Constructor
D. Setter function
Correct Answer:
A
Chapter: 27
Q463:
When is a destructor automatically called?
A. When an object is destroyed
B. When an object is created
C. When a member function is called
D. When a constructor is executed
Correct Answer:
A
Chapter: 27
Q464:
What is the role of utility functions in a class?
A. To assist member functions in performing tasks
B. To display output to the user
C. To initialize the class
D. To access private data members
Correct Answer:
A
Chapter: 27
Q465:
How are data members and functions stored in memory for multiple objects of the same class?
A. Data members have separate memory locations; functions share a single copy
B. Both data members and functions have separate memory locations
C. Both data members and functions share a single copy
D. Functions have separate memory locations; data members share a single copy
Correct Answer:
A
Chapter: 27
Q466:
What should be included in the constructor to ensure proper initialization of data members?
A. Initialization code for data members
B. Function to delete objects
C. Code to validate user input
D. Default values for data members
Correct Answer:
A
Chapter: 28
Q467:
What is the default name of the memory region used for dynamic allocation in C?
A. Heap
B. Stack
C. Data Segment
D. Code Segment
Correct Answer:
A
Chapter: 28
Q468:
Which function in C is used to dynamically allocate memory?
A. malloc()
B. free()
C. printf()
D. scanf()
Correct Answer:
A
Chapter: 28
Q469:
In C++, what is the operator used for dynamic memory allocation?
A. malloc
B. calloc
C. new
D. realloc
Correct Answer:
C
Chapter: 28
Q470:
What is the difference between malloc() and new in C++?
A. malloc() initializes memory, while new does not
B. new initializes memory, while malloc() does not
C. malloc() returns a typed pointer, while new returns a void pointer
D. new and malloc() are functionally identical
Correct Answer:
B
Chapter: 28
Q471:
How do you deallocate memory allocated with malloc() in C?
A. Using free()
B. Using delete
C. Using new
D. Using malloc()
Correct Answer:
A
Chapter: 28
Q472:
What is the primary advantage of using new over malloc() in C++?
A. new initializes the allocated memory
B. new returns a void pointer
C. new does not require a type cast
D. new requires the size of memory in bytes
Correct Answer:
C
Chapter: 28
Q473:
How do you allocate memory for an array of 10 int values using new in C++?
A. int *arr = new int[10];
B. int arr = new int(10);
C. int *arr = new int(10);
D. int arr[10] = new int;
Correct Answer:
A
Chapter: 28
Q474:
What will the following statement return: new int;?
A. A pointer to an int
B. A void pointer
C. An array of 10 ints
D. An integer value
Correct Answer:
A
Chapter: 28
Q475:
How do you properly deallocate memory allocated for an array of objects in C++?
A. delete ptr;
B. delete[] ptr;
C. free(ptr);
D. delete ptr[];
Correct Answer:
B
Chapter: 28
Q476:
What is the primary purpose of a constructor in C++?
A. To initialize an object
B. To deallocate memory
C. To allocate memory
D. To create a pointer
Correct Answer:
A
Chapter: 28
Q477:
When using new to allocate memory for a class object, what additional step does new perform compared to malloc()?
A. Calls the class constructor
B. Calls the class destructor
C. Initializes memory to zero
D. Returns a void pointer
Correct Answer:
A
Chapter: 28
Q478:
What happens if new fails to allocate memory in C++?
A. Returns NULL
B. Throws a bad_alloc exception
C. Returns 0
D. Returns an uninitialized pointer
Correct Answer:
B
Chapter: 28
Q479:
How should a destructor handle memory allocated with new?
A. It should call delete
B. It should call malloc
C. It should call free
D. It should call new
Correct Answer:
A
Chapter: 28
Q480:
What keyword in C++ ensures that a class member is only accessible from within the class itself?
A. public
B. private
C. protected
D. friend
Correct Answer:
B