C++ Programming Questions and Answers – C++ vs C

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

Answer: c
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.
advertisement
advertisement

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

Answer: d
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What happens if the following line is executed in C and C++?

advertisement
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

Answer: c
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.
advertisement

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

Answer: d
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

Answer: d
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

Answer: d
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

Answer: a
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

Answer: d
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

Answer: b
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

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.