Thursday, February 2, 2017

C++ Interview Questions

In this post i am sharing C++ Interview Questions and answers for freshers and experienced candidates. These are frequently asked interview questions in technical round and written test also.

1) What is C++?

Ans: C++ is an Object Oriented Programming Language created by Bjarne Stroustrup in 1985. C++ contains almost all aspects of the C language. C++ maintains the features of C which allowed for low-level memory access but also gives the programmer new tools to simply memory management.

2) What is use of C++?

Ans: C++ is a powerful general-purpose programming language. It can be used to create small programs or large applications.It can be used to make CGI scripts or DOS programs. C++ allows you to create programs to do almost anything you need to do. 

3) What is namespace?

Ans: Namespace allows us to group a set of global classes,objects and functions under a name. The form to use namespace is:
namespace identifier
{
namespace-body
}
Where identifier is any valid identifier and namespace-body is the set of classes,objects and functions that are included within the namespace.

4) What is difference between C++ and Java?

Ans: C++ has pointers but java does not. Java is the platform independent as it works any type of Operating System. Java has Garbage collection but C++ does not. C++ has global variables but java does not

5) What is difference between C and C++?

Ans: 
  1. C is a Procedural Oriented Programming Language whereas C++ is an Object Oriented Programming Language.
  2. C is super set of C++
  3. C can't support inheritance,function overloading,method overloading etc..but C++ can do this
  4. In C the main function could not return a value whereas C++ should return a value.
  5. C does not have a built in exception handling framework whereas C++  has.
  6. C follows top-down approach but C++ follows a bottom-up approach.
  7. In C data security is less,but in C++ you can use modifiers for your class members to make it inaccessible from outside.

 6) What are token in C++?

Ans: The smallest individual units of a program is known as tokens. C++ has the following tokens:
Keywords
identifiers
constants
Strings
Operators

7) How variable declaration in C++ differs than in C?

Ans: C requires all the variables to be declared at the beginning of a scope but in C++ we can declare variables anywhere in the scope.This makes the programmer easier to understand because the variables are declared in the context of their use.





8)What are storage qualifiers in C++?

Ans: There are 3 storage qualifiers in C++. They are
1) Const keyword
2) Volatile Keyword
3) Mutable Keyword

1)Const Keyword: This indicates that memory once initialized,should not be altered by a program
2) Volatile Keyword: This indicates that the value in the memory location can be altered even though nothing in the program.

3)Mutable keyword: This indicates that particular member of a structure or class can be altered even if a particular structure variable,class or class member function is constant.







9) What are the access specifier in C++?

Ans: There are three types of access specifier in C++. They are
public 
protected
private

These are used to define how the members(functions and variables) can be accessed outside the class.

Public: Members declared as public are accessible from any where.
protected: Members declared as protected can not be accessed from outside the class except a child class.
Private: Members declared as private are accessible only with in the same class and they can not accessed outside the class they are declared.

10) Is it possible to have virtual constructor? If yes,how? if not, why not possible?

Ans: There is nothing like virtual constructor. The constructor can't be virtual as the constructor is a code which is responsible for creating an instance of a class and it can't be delegated to any other object by virtual keyword means.

11) What is Constructor?

Ans: Constructor creates an object and initializes it. It also creates vtable for virtual functions. It is different from other methods in a class.

12) What is default Constructor?

Ans: A constructor with no arguments or all the arguments has default values.

13) What is Conversion Constructor?

Ans: Constructor with a single argument makes that constructor as conversion ctor and it can be used for type version.



14) What is Copy Constructor?

Ans: Constructor which initializes the it's object member variables with another object of the same class.If you don't implement one in your class then compiler implements one for you.

Example:

sum obj1(10);//calling sum constructor
sum obj2(obj1);//calling sum copy constructor
sum obj2=obj1;//calling sum copy constructor



15) What is difference between a copy constructor and overloaded assignment operator?

Ans: A copy constructor constructs a new object by using the content of the argument object. Wheres as overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

16)  What is an Explicit Constructor?

Ans: A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction.

17) Can a copy constructor accept an object of the same class as parameter,instead of reference of the object?

Ans: NO. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

18) When a copy constructor called?

Ans: Copy Constructor are called in the fallowing cases:


  1.  When a function returns an object of that class by value
  2.  When the object of that class is passed by value as an argument to a function
  3. When you construct an object based on another object of the same class
  4. When compiler generates a temporary object.
19) What is virtual class and friend class?

Ans: Friend classes are used when two or more classes and virtual base class aids in multiple inheritance.  Virtual class is used for run time polymorphism when object is linked to procedure call at run time.

20) What is virtual function?

Ans: When derived class overrides the base class method by redefining the same function,then if client wants to access redefined the method from derived class through a pointer from a base class object then you must define this function in base class as virtual function.

Example:
class Employee
{
void show()
{
cout<<" i am employee"<<endl;
}
};
class student:public Employee
{
void show()
{
cout<<" i am student"<<endl;
}
};

21) What is class?

Ans: A class can hold both data and function. It is the combination of both data and members.

22) What is an Object?

Ans: It is an entity which may correspond to real-world entities such as students,emp,bank account,table,pan etc.. Every object will have data structures called attributes and behavior called operations.

23) What is difference between class and object?

Ans: All objects possessing similar properties are grouped into class.

Example: person is a class, sreenu and ramu are objects of person class. All have similar attributes like name,age,sex and similar operations like speak,walk.

class person
{
private:
char name[20];
int age;
char sex;
public: speak();
walk();
};

24) What is difference between class and structure?

Ans:  In class the data members by default are private but in structure they are by default public.

25) What the meaning of Object based programming Language?

Ans: Object based programming language support encapsulation and object identity without supporting some important features of OOPS language. Object based language=Encapsulation+object identity.
                      Object oriented language incorporates all the features of object based programming languages along with inheritance and polymorphism.
Examples: C++,java

26) What is scope resolution operator?

Ans: The scope resolution operator permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

27) What is template?

Ans: Template allows us to create generic functions that admit any data type as parameters and return a value without having to overload the function with all the possible data types.Its prototype is  following :

template function_declaration;

28) What is Null object?

Ans: It is an object of some class whose purpose is to indicate that a real object of that class does not exist. One common use for a null object is a return value from a member function that is supposed to return an object with some specified properties but can not find such an object.

29) How can we access protected and private members of a class?

Ans: In the case of members protected and private,these could not be accessed from outside the same class at which they are declared. This rule can be transgressed with the use of the friend keyword in a class,so we can allow an external function to gain access to the protected and private members of a class.
  
30) Can you handle Exception in C++?

Ans: Yes. We can handle exception in C++ using keywords such as try,catch and throw

31) What is cin and cout?

Ans: These are predefined  objects corresponding to a program's default input and output files. The cout object is said to be "connected to" standard output device,which usually is the display screen. The cin is used to to get the input data from the keyboard.

32) What is Inheritance?

Ans:  Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called parent or super class. And,the class which inherits properties of other class is called child or sub class.

33) What is this pointer?

Ans: It is a pointer that points to the current object. This can be used to access the members of the current object with the help of the arrow operator.

34) What is static method?

Ans: By using static method there is no need creating an object of that class to use that method. We can directly call that method on that class.

For example, A has static function f(), then we can call f() function as A.f(). There is no need of creating an object of class A.

35) What are the main defining traits of Object Oriented language?

Ans:
encapsulation
inheritance
polymorphism

encapsulation: It is used to hide the information
inheritance: It represents "IS-A" relationship between different objects(classes).
polymorphism: It is one of the best feature that at run time depending upon the type of object the appropriate method is called.

36)  What is polymorphism?

Ans:polymorphism means having many forms. It allows that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. For example, a client component asking for net revenue from a business object need not know the data's origin.

37) What is encapsulation?

Ans:  It is one of the key feature of Object Orient Language that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Containing and hiding information about an object,such as internal data structures and code.

38) What is friend function?

Ans: The function that are declared with the keyword friend as friend function. A friend function of a class is defined outside that class scope but it has rights to access private and protected members of a class.

39) What is difference between template and macro?

Ans: A template can be used to create a family of classes or functions.It describes a set of related classes or set of realted functions in which a list of parameters in the declaration describe how the members of the set vary.
Identifiers that represent statements or expressions are called macros.

40) What is dynamic binding?

Ans: Dynamic Binding is also known as late binding means that the code associated with a given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance.

41) What is memory Leak?

Ans: Memory leak occurs when programmers create a heap memory and forget to delete it.  Many programmers failure to correctly deallocate memory that was previously allocated. Thus memory leak occurs in that time.

42) What is the difference between delete and delete[]?

Ans: Whenever you allocate memory with new[],you have to free the memory using delete[]. when you allocate memory with 'new' then use 'delete' without brackets. You use new[] to allocate an array of values.

43) What is abstraction?

Ans: It simplified view of an object in user's language is called abstraction. It provides all the information that the user requires. It separates specifications from implementation & keep the code simpler and more stable.

44) What is stack? 

Ans:  A stack is a liner structure in which insertions and deletions are always made at one end. It follow LastInFIrstOut(LIFO) formula. Stacks are useful when we need to check some syntax errors like missing parentheses.

45)  What is extern?

Ans:  when declaring function,extern tells the compiler about the existence of a variable or a function,even though the compiler hasn't yet seen it in the file currently being compiled. This variable or function may be defined in another file or further down in the current file.

46) What is difference between pointer and reference?

Ans:  A reference must always refer to some object and,therefore,must always be initialized;
Pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. 

47) How do you link a c++ program to C functions?

Ans:  By using extern "c" linkage specification around the c function declarations. Programmers should know about mangled function names and type-safe linkages.

48) What are operators can not be overloaded?

Ans: sizeof,.,*,->,::,? :

49) What is overloading?

Ans: You can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope. Any two functions must have overloaded functions must have different argument lists.

50) What are the benefits of Operator Overloading?

Ans: Using overloading standard operator on a class,you can exploit the intuition of the users of that class. This lets users program in the language of the problem domain rather than in the language of the machine. The main goal is to reduce both learning curve and the defect rate.
 

No comments:

Post a Comment