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

Q 1. Which of the following feature is not provided by C?
A. Pointers
B. References
C. Structures
D. Functions

Show Answer Answer:-B. References
Explanation References are introduced in C++. They are not present in C.

Q 2. Which of the following type is provided by C++ but not C?
A. int
B. bool
C. float
D. double

Show Answer Answer:-B. bool
Explanation C++ provides the boolean type to handle true and false values whereas no such type is provided in C.

Q 3. 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++

Show Answer Answer:-C. Error in C++ and successful execution in C
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.

Q 4. 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

Show Answer Answer:-A. Error in both C and C++
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.

Q 5. 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

Show Answer Answer:-D. Error in C++ and Outputs Hello twice in C
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.

Q 6. 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

Show Answer Answer:-D. Error in C++ and successful execution in C
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.

Q 7. 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

Show Answer Answer:-D. Error in C++ and successful execution in C
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.

Q 8. 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++

Show Answer Answer:-C. Error in C++ and successful execution in 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.

Q 9. 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

Show Answer Answer:-D. Error in C++ but Warning in C
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.

Q 10. 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 C++ but Warning in C
B. Warning in both C and C++
C. Error in both C and C++
D. Error in C but Warning in C++

Show Answer Answer:-A. Error in C++ but Warning in 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.

Comments

Leave a Reply

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

You cannot copy content of this page