Test Series - cpp

Test Number 5/102

Q: Which of the following is correct about new and malloc?
A. Both are available in C
B. Pointer object initialization of a class with both new and malloc calls the constructor of that class
C. Pointer object initialization of a class using new involves constructor call whereas using malloc does not involve constructor call
D. Pointer object initialization of a class using malloc involves constructor call whereas using new does not involve constructor call
Solution: Object initialization using new keyword involves constructor call whereas malloc does not involve constructor call. That’s why new is explicitly added in C++. Also, malloc is used to assign memory to any pointer hence it assigns memory equals to the size of the class however new keyword involves initialization also hence calls the constructor of that class.
Q: What is virtual inheritance?
A. C++ technique to avoid multiple copies of the base class into children/derived class
B. C++ technique to avoid multiple inheritances of classes
C. C++ technique to enhance multiple inheritance
D. C++ technique to ensure that a private member of the base class can be accessed somehow
Solution: Virtual inheritance is a C++ technique with which it ensures that a derived class contains only one copy of the base class’s variables. Refer Wikipedia for more info.
Q: What is the difference between delete and delete[] in C++?
A. delete is used to delete normal objects whereas delete[] is used to pointer objects
B. delete is a keyword whereas delete[] is an identifier
C. delete is used to delete single object whereas delete[] is used to multiple(array/pointer of) objects
D. delete is syntactically correct but delete[] is wrong and hence will give an error if used in any case
Solution: delete is used to delete a single object initiated using new keyword whereas delete[] is used to delete a group of objects initiated with the new operator.
Q: What is the correct syntax of declaring array of pointers of integers of size 10 in C++?
A. int arr = new int[10];
B. int **arr = new int*[10];
C. int *arr = new int[10];
D. int *arr = new int*[10];
Solution: As we have to declare an array of pointers of integers therefore we need double pointer array in which each element is collection pointers to integers. Therefore the correct syntax is int **arr = new int*[10];
Q: Which of the following is correct about new and malloc?
i) new is an operator whereas malloc is a function
ii) new calls constructor malloc does not
iii) new returns required pointer whereas malloc returns void pointer and needs to be typecast
A. i and ii
B. ii and iii
C. i and iii
D. i, ii and iii
Solution: All the statements about the new and malloc are correct. new is an operator whereas malloc() is a function. The constructor is called when new is used and new returns required type memory pointer.
Q: What happens if the following program is executed in C and C++?

#include 
int main() 
{ 
   foo();
}  
int foo() 
{ 
   printf("Hello"); 
   return 0;  
}
A. Error in both C and C++
B. Warning in both C and C++
C. Error in C++ but Warning in C
D. Error in C but Warning in C++
Solution: In C++ all the functions should be declared before it is called otherwise the C++ compiler will give an error but in case of C the compiler just gives a warning and the program can be executed.
Q: What happens if the following line is executed in C and C++?

int *p = malloc(10);
A. Error in both C and C++
B. Warning in both C and C++
C. Error in C++ and successful execution in C
D. Error in C and successful execution in C++
Solution: C++ is strict in type check but C is not and as malloc returns a void* which we are trying to assign to an int*, therefore, the C++ compiler gives error whereas C compiler executes the program successfully.
Q: What happens if the following line is executed in C and C++?

const int a;
A. Error in both C and C++
B. Warning in both C and C++
C. Error in C and successful execution in C++
D. Error in C++ and successful execution in C
Solution: C++ compiler does not allow the programmer to declare a constant variable without initializing it hence the C++ compiler gives an error whereas C allows such declaration, therefore, the program compiles and runs successfully.
Q: Which of the following type is provided by C++ but not C?
A. int
B. bool
C. float
D. double
Solution: C++ provides the boolean type to handle true and false values whereas no such type is provided in C.
Q: Which of the following feature is not provided by C?
A. Pointers
B. Structures
C. References
D. Functions
Solution: References are introduced in C++. They are not present in C.

You Have Score    /10