Frequently Asked Questions
about C++
Note: This section will be updated as and when more common questions get identified.
Que 1. What is C++ ?
Ans: It is a programming language.
Que 2. Who invented C++ and in which year ?
Ans: Bjarne Stroustrup in the year 1979.Que 3. Is C++ case sensitive?
Ans: YES
Que 4. What is an identifier?
Ans: The names of variables, functions, labels and various other user-defined objects are
called identifiers.
Que 5. What is a keyword?
Ans: Keywords are reserved words which are interpreted by the Compiler in a special way.
Examples: int, char,float, do, while, if, etc.
Que 6. Why is C++ called so?
Ans: Because C++ is an extension of programming language C and in both C as well as
C++, "++" is increment operator which signifies addition of something. So C got incremented
to C++, with some additions.
Que 7. What is the difference between Call by value & Call by reference?
Ans: In Call by Reference, the formal parameters ( the ones defined in the function declaration)
are references to the actual parameters ( the ones passed in function call). Any change made
to formal parameters inside the function definition is reflected in actual parameters. In Call
by Value, the formal parameters are just Copies of actual parameters. Any change made
to formal parameters inside the function definition does not affect actual parameters.
Que 8. What is Recursion?
Ans: Recursion is a programming technique in which a function calls itself. For example,
void max()
{
if ( i > 0 )
max()
i--;
}
Que 9. What is an array?
Ans: An array is a collection of elements of Similar Type, referenced by a Common Name and
an Index.
Que 10. What is a Structure?
Ans: A structure is a Collection of elements of Distinct Type, grouped together as a Single Unit.
Que 11. What is a Class?
Ans: A Class is similar to a Structure, but allows Member functions (methods) to be included.
Que 12. What is goto statement used for?
Ans: It is used to jump from one part of the program to another. It requires a label. For example,
LabelOne: i --;
cout<<"Label One";
if ( i > 0 )
goto LabelOne;
Que 13. What is a pointer?
Ans: A pointer is a variable which is used to store memory address of other variables.
Que 14. What is Type Casting?
Ans: It is the act of programming in which we intentionally convert a data of a specific type to
another type. For example,
float f=5.3;
f = (int) f * 7; // Type casting, converting the result to int type.
Que 15. What is a constructor?
Ans: A Constructor is a member function of a class which:
a. Has No return type.
b. Has the same name as that of the class.
c. Gets called automatically whenever an object of the class is instantiated.
d. Performs the function of memory allocation.
e. Can be overloaded.
Que 16. What is a destructor?
Ans: A Destructor is a member function of a class which:
a. Has No return type.
b. Has the same name as that of the class but preceded by a a tilde (~) symbol.
c. Gets called automatically when object goes out of scope.
d. Performs the function of memory de-allocation and clean-up.
e. Cannot be overloaded.
Que 17. What is the difference between C and C++?
Ans: 1. C is a subset of C++. C++ is an extension of C.
2. C++ supports Classes, Inheritance, etc and thus, is an Object Oriented Language.
C is Procedure Oriented Language.
3. C++ supports Function Overloading. C does not.
4. C++ has stream based input/ output. C uses printf() and scanf() functions.
5. C++ supports Reference Variables and passing parameters to functions by reference .
C does not.
6. Variable declarations must be at the beginning of a code block in C.
Variables may be declared anywhere in a C++ program.
7. C++ supports Exception handling. C does not.
Que 18. What is Function Overloading?
Ans: Function overloading is the process of using same name for two or more functions which
differ in either the type of parameters, or in the number of parameters or in both.
Que 19. What are the rules for naming an identifier in C++ ?
3. Identifier name cannot be same as any of the C++ Keywords.
4. Identifiers are CASE SENSITIVE. For example, Count, count and COUNT
are all different identifiers.
static int x = 10;
differ in either the type of parameters, or in the number of parameters or in both.
Que 19. What are the rules for naming an identifier in C++ ?
Ans: Rules for Naming an Identifier:
1. The First character must be a letter or an underscore ( _ ).
2. Subsequent characters must be either letters, digits or underscores. 3. Identifier name cannot be same as any of the C++ Keywords.
4. Identifiers are CASE SENSITIVE. For example, Count, count and COUNT
are all different identifiers.
Que 20. What is the difference between post-fix and pre-fix increment or decrement?
Ans: In post-fix increment, first the value is used and then the variable is incremented.
int x = 0, y = 10 ;
x = y++ ; // this assigns the value 10 to x and later increments y to 11.
In pre-fix increment, first the variable is incremented and then the incremented value
is used.
is used.
int x = 0, y = 10 ;
x = ++y; // this first increments y to 11 and then assigns the value 11 to x.
Same is applicable to post-fix and pre-fix decrement, with the only difference that
decrement operation is performed instead of increment.
Que 21. What is meant by operator precedence ?
Ans: In an expression involving more than one operators, a number of results can be
obtained based on the order in which the operators are used. Precedence of operators
defines the order in which the operators will be used in such situations, if explicit order
has not been specified using brackets.
obtained based on the order in which the operators are used. Precedence of operators
defines the order in which the operators will be used in such situations, if explicit order
has not been specified using brackets.
Que 22. What is meant by the statement : x + = 10 ; ?
Ans: It is another way of writing the statement : x = x + 10 ;
Que 23. What is the difference between while loop and do-while loop?
Ans: While loop is Entry controlled loop, it does not execute unless the specified condition
is true. The condition is checked at the beginning of the iteration (loop).
is true. The condition is checked at the beginning of the iteration (loop).
Do-while loop is Exit controlled loop, it executes at least once even if the specified
condition is not true. The condition is checked at the end of the iteration (loop).
condition is not true. The condition is checked at the end of the iteration (loop).
Que 24. What is the difference between continue and break statement ?
Ans: The break statement is used to exit from the inner-most loop. It terminates the inner-
most loop.
most loop.
The continue statement allows us to terminate the current iteration, and start afresh
with the next iteration of the loop. The entire loop does not terminate, but only the
particular iteration terminates in which the continue statement gets executed.
with the next iteration of the loop. The entire loop does not terminate, but only the
particular iteration terminates in which the continue statement gets executed.
Que 25. What is the relationship between array and pointers?
Ans: The name of an array is a pointer to the first element of the array.
Que 26. What are the advantages of using pointers?
Ans: 1. Pointers support Dynamic Allocation.
2. Pointers may improve efficiency of certain programs.
3. Pointers allow functions to modify their calling arguments.
Que 27. What are disadvantages of using pointers?
Ans: 1. Errors that arise due to pointers are difficult to track.
2. Unassigned pointers or incorrectly assigned pointers may lead to system crashes.
Although Exception Handling can be used to prevent such a situation.
Que 28. What are the uses of references?
Ans: 1. As a Function Parameter
2. As a Function Return Value
3. As Independent Reference
Que 29. What do you mean by function prototype?
Ans: Function Prototype serves as a declaration of the function. It enables the Compiler to
perform type checking i.e. checking whether the arguments provided by you during
function call are of the same type as those specified in the function parameter-list.
Prototype consists of the first line of function definition terminated by a semi-colon ( ; )
character. You must declare a Function prototype before you use that function i.e.
before the main function.
You can skip the variable name in the function prototype.
For example,
int multiply(int a, int b);
Que 30. What is Function Overloading?
Ans: Function overloading is the process of using same name for two or more functions which
differ in either the type of parameters, or in the number of parameters or in both. For
example, if you want a function that displays any variable passed to it, you can define
multiple versions of the function which take different type of parameters.
Que 31. What are storage class specifiers?
Ans: These specifiers affect the storage of variable. The specifiers precede the data-type in
variable declaration.
storage-specifier data-type variable-name ;
For example,static int x = 10;
Que 32. What are various storage class specifiers in C++?
Ans: There are five types of storage class specifiers :
1. extern2. static
3. register
4. auto
5. mutable
Que 33. What is Object Oriented Programming?
Ans: Increasing complexity of programs lead to the evolution of Object Oriented Approach.
In this approach, programs are organised around Data. It can be thought of as "Data
being acted upon by Code". The type of data defines which code/method can act on it.
All Object Oriented languages support three features : Encapsulation, Inheritance &
Polymorphism.
Example : C++ & Java
Que 34. What is Encapsulation?
Ans: Encapsulation is the mechanism of binding together code and data it manipulates,
keeping them safe from external interference and misuse. The concept of Classes
is used to support this behaviour. Inside a class, members may be private or public.
Public members can still be accessed from outside the class but private members
can be accessed only from within the class. The public members provide an interface
to the private members of the class.
keeping them safe from external interference and misuse. The concept of Classes
is used to support this behaviour. Inside a class, members may be private or public.
Public members can still be accessed from outside the class but private members
can be accessed only from within the class. The public members provide an interface
to the private members of the class.
Que 35. What is Inheritance?
Ans: Inheritance is the process which allows on object to acquire properties of another object.
This allows for the concept of classification. It becomes easier to think of information as
divided into various classes hierarchically. Without classification, all objects would need to
be described entirely. However, with classification we can define only the new
characteristics which make the object unique within its class.
Que 36. What is Polymorphism?
Ans: Polymorphism is the process in which one interface is used to perform different actions
depending upon the context of the situation. For example, a single function name 'area'
may be used to calculate the area of more than one kind of objects (figures). C++ supports
two kinds of Polymorphism :
Compile time (Static Binding) : Compile time Polymorphism is achieved through Function
& Operator Overloading. The Function calls (or bindings)
are resolved at compile time.
Run time (Dynamic Binding): Run time Polymorphism is achieved through Virtual Functions
& Inheritance. The Function calls are resolved at Run time.
Que 37. What is a Default Constructor?
Ans: Constructor with no arguments is called Default Constructor.
Que 38. What is the order of execution of Constructors and Destructors?
Ans: Constructor are always executed in the order in which objects are created.
Destructors are always executed in the reverse order in which the Constructors are called
or in which the objects are created.
Que 39. What is a Copy Constructor?
Ans: Copy constructor is an overloaded constructor which gets invoked automatically in the
following situations :
1. When an object of class is initialized (assigned value when created) with another object.
2. When an object is passed by value to a function as argument.
3. When an object is returned from a function.
Que 40. Can a static member function be declared volatile?
Ans: No.
Que 41. Can a static member function be declared const?
Ans: No.
Que 42. Can there be a static and non-static version of same member function?
Ans: No.
Que 43. const variables have global scope (external linkage) in C but local scope
(internal linkage) in C++. True or False?
Ans: True.
Que 44. Global anonymous unions must be specified as static. True or False?
Ans: True.
Que 40. Can a static member function be declared volatile?
Ans: No.
Que 41. Can a static member function be declared const?
Ans: No.
Que 42. Can there be a static and non-static version of same member function?
Ans: No.
Que 43. const variables have global scope (external linkage) in C but local scope
(internal linkage) in C++. True or False?
Ans: True.
Que 44. Global anonymous unions must be specified as static. True or False?
Ans: True.
No comments:
Post a Comment