C++ Programming Questions and Answers – C++ Concepts – 1

Q 1. In which part of the for loop termination condition is checked?
for(I;II;III)
{IV}
A. I
B. II
C. III
D. IV

Show Answer Answer:-B. II
Explanation In II part the termination condition of the for loop is checked.

Q 2. What is dynamic binding?
A. The process of linking the actual code with a procedural call during compile -time
B. The process of linking the actual code with a procedural call during run -time
C. The process of linking the actual code with a procedural call at any-time
D. All of the mentioned

Show Answer Answer:-B. The process of linking the actual code with a procedural call during run -time
Explanation Binding of calls and variables with actual code at run-time is called dynamic binding. For example in the concept of polymorphism types are decided are defined during the execution of code which leads to the different function calls depending upon the types used this is called dynamic binding. As the function call is decided during the run-time therefore dynamic binding happens at run-time.

Q 3. What is static binding?
A. The process of linking the actual code with a procedural call during run-time
B. The process of linking the actual code with a procedural call during compile-time
C. The process of linking the actual code with a procedural call at any-time
D. All of the mentioned

Show Answer Answer:-B. The process of linking the actual code with a procedural call during compile-time
Explanation Binding of calls and variables with actual code at compile-time is called static binding. For example normally whenever we declare a variable we define its type hence compiler knows what type should be binded to that variable i.e. compiler can decide about that variable this is called static binding.

Q 4. What is name mangling in C++?
A. The process of adding more information to a function name so that it can be distinguished from other functions by the compiler
B. The process of making common names for all the function of C++ program for better use
C. The process of changing the names of variable
D. The process of declaring variables of different types

Show Answer Answer:-A. The process of adding more information to a function name so that it can be distinguished from other functions by the compiler
Explanation Name mangling is the process of adding some more information to a function name so that it can be distinguished from other functions by the compiler. This is used when a programmer uses the concept of function overloading in his/her program.

Q 5. What will be the output of the following program in both C and C++?

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("%d\n", (int)sizeof('a'));
	return 0;
}

A. Output in C is 1 and in C++ is 4
B. Output in C is 4 and in C++ is 1
C. Output in C is 1 and in C++ is 1
D. Output in C is 4 and in C++ is 4

Show Answer Answer:-B. Output in C is 4 and in C++ is 1
Explanation In C a character is stored as int therefore the size of ‘a’ is printed as 4 whereas in C++ it is stored as char only therefore in C++ it prints 1.

Q 6. What will be the output of the following C++ code?

#include<stdio.h>
int main(int argc, char const *argv[])
{
	char a = 'a';
	printf("%d\n", (int)sizeof(a));
	return 0;
}

A. Output in C is 1 and in C++ is 4
B. Output in C is 1 and in C++ is 1
C. Output in C is 4 and in C++ is 1
D. Output in C is 4 and in C++ is 4

Show Answer Answer:-B. Output in C is 1 and in C++ is 1
Explanation Both in C and C++ the type char has same size which is 1. But a character enclosed inside single quotes has difference sizes i.e. in case of char a; the size of a will be 1 in both C and C++ but in case of ‘a’ size will be 4 in case of C but 1 in case of C++.

Q 7. Which of the following syntax for declaring a variable of struct STRUCT can be used in both C and C++?
A. STRUCT S;
B. struct STRUCT S;
C. Both struct STRUCT S; and STRUCT S;
D. Both C and C++ have different syntax

Show Answer Answer:-B. struct STRUCT S;
Explanation C program requires struct keyword while defining a variable of any structure, therefore, we cannot use the second STRUCT S; definition to declare a variable.

Q 8. What if we define the below structure in C and C++?
A. Error in C but not in C++
B. Error in C++ but not in C
C. No error in both C and C++
D. Error in both C and C++

Show Answer Answer:-A. Error in C but not in C++
Explanation The above definition will give an error in C but not in C++ as C does not allows the programmer to give any default values to any member of structure but C++ does allow.

Q 9 . Which of the following is an entry-controlled loop?
A. for
B. while
C. do-while
D. both while and for

Show Answer Answer:-D. both while and for
Explanation Both while and for loops are called entry controlled loop because in both of them the termination condition is checked before we enter the body of the loop hence they are called entry controlled loop.

Q 10. Which of the following is an exit-controlled loop?
A. for
B. while
C. do-while
D. all of the mentioned

Show Answer Answer:-C. do-while
Explanation do-while is called exit controlled loop because in do-while termination condition is checked when we have executed the body of the loop i.e. we are exiting the body and then checking the condition, therefore, it is called exit controlled loop.

Q 11. Which of the following is C++ equivalent for printf()?
A. cin
B. cout
C. print
D. input

Show Answer Answer:-B. cout
Explanation C++ uses cout to print output to console. However C++ also uses printf().

Q 12. Which of the following is the correct difference between cin and scanf() ?
A. both are the same
B. cin is a stream object whereas scanf() is a function
C. scanf() is a stream object whereas cin is a function
D. cin is used for printing whereas scanf() is used for reading input

Show Answer Answer:-B. cin is a stream object whereas scanf() is a function
Explanation cin is a stream object available in C++ whereas scanf() is a function available in both C and C++. both are used for reading input from users.

Q 13. Which of the following is not a fundamental type is not present in C but present in C++?
A. int
B. float
C. void
D. bool

Show Answer Answer:-D. bool
Explanation Boolean type is not present as a fundamental type in C. int type is used as boolean in C whereas in C++ bool is defined as a fundamental type for handling boolean outputs.

Q 14. What is the size of a boolean variable in C++?
A. 1 byte
B. 1 bit
C. 4 bytes
D. 2 bytes

Show Answer Answer:-B. 1 bit
Explanation Boolean uses only 1 bit as it stores only truth values which can be true(1) or false(0).

Q 15. Which of the following is C++ equivalent for scanf()?
A. cin
B. cout
C. print
D. input

Show Answer Answer:-A. cin
Explanation C++ uses cin to read input form uses. However C++ also uses scanf().

Comments

Leave a Reply

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

You cannot copy content of this page