This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “C++ vs C”.
1. What happens if the following program is executed in C and C++?
#include<stdio.h> 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++
View Answer
Explanation: 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.
2. What happens if the following program is executed in C and C++?
#include <stdio.h> int main(void) { const int j = 20; int *ptr = &j; printf("*ptr: %d\n", *ptr); 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
View Answer
Explanation: C++ is strict on the use of types of variables hence when the programmer tries to assign const int to a normal pointer the program gives error whereas C is not strict on types therefore it gives warning only.
3. 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++
View Answer
Explanation: 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.
4. 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
View Answer
Explanation: 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.
5. What happens if the following program is executed in C and C++?
#include <stdio.h> int main(void) { int new = 5; printf("%d", new); }
a) Error in both C and C++
b) A successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with name new but as there is no such keyword new in C, therefore, the program is compiled and executed successfully in C.
6. What happens if the following program is executed in C and C++?
#include <stdio.h> void main() { printf("Hello World"); }
a) Error in both C and C++
b) Successful run in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Explanation: main() function in C++ must return int otherwise the C++ compiler gives the error whereas C does not forces such things on main() function. Thereas when we aremaking void main(){} function in this program the C++ compiler gives error whereas C compiler runs successfully.
7. What happens if the following program is executed in C and C++?
#include <stdio.h> void func(void) { printf("Hello"); } void main() { func(); func(2); }
a) Error in both C and C++
b) Outputs Hello twice in both C and C++
c) Error in C and successful execution in C++
d) Error in C++ and successful execution in C
View Answer
Explanation: As the func(void) needs no argument during its call, hence when we are calling func(2) with 2 as passed as a parameter then this statement gives the error in both C++ and C compiler.
8. What happens if the following program is executed in C and C++?
#include <stdio.h> void func() { printf("Hello"); } void main() { func(); func(2); }
a) Error in both C and C++
b) Outputs Hello twice in both C and C++
c) Error in C and Outputs Hello twice in C++
d) Error in C++ and Outputs Hello twice in C
View Answer
Explanation: In C++ whenever a function without argument is declared it is equivalent to function with void arguments i.e. func() == func(void) whereas in C a function without argument is equivalent to func(…) i.e. it can take any number of arguments so func(2) call is also valid in C but not valid in C++. Hence it gives error in C++ whereas no error in C.
9. Which of the following type is provided by C++ but not C?
a) int
b) bool
c) float
d) double
View Answer
Explanation: C++ provides the boolean type to handle true and false values whereas no such type is provided in C.
10. Which of the following feature is not provided by C?
a) Pointers
b) Structures
c) References
d) Functions
View Answer
Explanation: References are introduced in C++. They are not present in C.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Check Computer Science Books
- Practice Programming MCQs
- Apply for Computer Science Internship
- Apply for C++ Internship