250+ TOP MCQs on C++ vs C and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “C++ vs C”.

1. 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++
Answer: c
Clarification: 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  
int main(void) 
{ 
	const int j = 20; 
	int *ptr = &j;
	printf("*ptr: %dn", *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
Answer: d
Clarification: 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++?

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++
Answer: c
Clarification: 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++?

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
Answer: d
Clarification: 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  
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
Answer: d
Clarification: 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  
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
Answer: d
Clarification: 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  
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
Answer: a
Clarification: 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  
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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: References are introduced in C++. They are not present in C.

  250+ TOP MCQs on Locale and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top