C++ questions




C++ Frequently asked questions

Q1. What do you mean by Polymorphism?
  Ans: The concept of using operators or functions in diferent ways, depending on what they operate on, is called polymorphism. Overloaded functions and operator overloading are examples of polymorphism.
 Q2. Compare and contrast structures and classes?
  Ans: The member data and functions in a structure are public by default wheras they are private by default in a class.
 Q3. What do you mean by static polymorphism?
  Ans: Static polymorphism refers to an entity existing in different forms simultaneously. static polymorphism involves the binding or resolution of functions on the basis of the number, type and sequence of arguments. The various types of parameters are specified in the function declaration and therefore the function can be bound to the calls at the time of compilation.
   Q4. What are auto, static and extern data storage types?
  Ans: auto storage types are created each time the function is invoked. it can be accessed only in the function in which it is declared. static storage type is created the first time the function is invoked. It is initialized at the time of declaration so that the value is not reset. extern storage type is created at the time when declared global, but not when declared extern. it can be accessed by any function in the program file.
   Q5. What do you mean by encapsulation?
  Ans: The concept of hiding unwanted information from the user is called encapsulation.
    Q6. Differentiate between static binding and dynamic binding?
  Ans: Static binding is binding to the call during compile time while dynamic binding is binding to the call during run-time. Dynamic binding is more flexible than static binding.
    Q7. What is the difference between a base class and a derived class?
  Ans: The class (the superclass) from which the subclass is derived is referred to as the base class. The subclass that inherits the properties of the base class is referred to as the derived class.
    Q8. What is the difference between a File descriptor and a file pointer?
  Ans: A file descriptor is an integer used to access a file in a program . A file pointer is a pointer to the FILE structure, which contains a file descriptor. The file pointer is normally used by 'C++' standard library functions.
    Q9. What is the this pointer?
  Ans: Each class object maintains its own copy of the class data members. For example, for the class point if there are two objects named P1 and P2, both P1 and P2 maintain a separate copy of data. However, both P1 and P2 call the same copy of any particular member function. There exists only one instance of each class member function. This creates a problem, for example, if a function set_x of class point is to be invoked, how does it know which copy of x_co_ord should be manipulated, P1.x_co_ord or P2.x_co_ord? The this pointer is the answer to the above stated problem. Each class member function contains an implicit pointer of its class type, named this. The this pointer is created automatically by the compiler and contains the address of the class object through which the function is invoked. For example, for a class X, the implicit pointer has a definition: X *this; Any reference to a member of a class is, in essence, a reference to the this pointer.
    Q10. What is a virtual base class?
  Ans: When the same class is inherited more than once via multiple paths, multiple copies of the base class members are created in memory. By declaring the base class inheritance as virtual, only one copy of the base class is inherited. A base class inheritance can be declared virtual using the virtual qualifier.
    Q11. What are volatile storage types in C++? Where are they used?
  Ans: The volatile keyword is a type qualifier used to declare that an object can be modifed in the program by something other than statments, such as the operating system, the hardware. The volatile qualifier provides access to memory locations used by asynchronous processes such as interuppt handlers.
    Q12. How are constructors invoked?
  Ans: The constructor associated with a class is automaticaly invoked whenever you create a new object of that class. They need not be invoked separately.
    Q13. What is the difference between dynamic arrays & pointers?
  Ans: An array is a contiguous set of memory locations. For example, char str[10]; declares a set of 10 char variables in memory, and the name 'str' refers to this set of 10 variables. 'str' is not a pointer. A pointer is a kind of variable which can store the addresses of other variables. For example, if you declare a pointer char *ptr; and assign 'str' to it by ptr = str; , The starting memory location, that this the address of the first variable in the array, is picked up and stored in pointers.
    Q14. What are ternary operators ?
  Ans: A ternary operator is an operator that has three operands. For example:
ctr = num ==5 ? 1 : 0;
Here, if the value of 'num' is 5, then the value 1 would be assigned to 'ctr', else, the value 0 would be assigned to ctr.
    Q15. What are Linked Lists?
  Ans: A linked list is a chain of structures in which each structure consists of data as well as pointer which stores the address of the next logical structure in the list.
    Q16. What is the difference between C & C++ ?
  Ans: Object-orientation is the major difference between C and C++. C++ supports overloaded functions, operator overloading, etc.
    Q17. What is a pure virtual function?
  Ans: A pure virtual function is a virtual function with no body. A pure virtual function can be declared by equating it to zero.
    Q18. What is dynamic memory allocation?
  Ans: When you declare a variable, memory space is allocated to the variable statically. Most ofthe times the size of the memory variable is known at the time of creating the application. Dynamic memory allocation is used when you do not know the size to allocate to the variable when designing the program. For example, when accepting the user's name in your program .
    Q19. What are objects and classes?
  Ans: A class is a template or blueprint used to create objects. It defines the characteristics of an object and how it behaves. For example, a car is a class. It specifies that every car manufactured should have 4 wheels, doors, a brake, clutch, accelerator, etc An object is an instance of a class. For example, Zen is an object of the class car.
    Q20. What is meant by unsigned character or unsigned integer?
  Ans: A signed variable (whether int or char) has one bit kept aside to indicate whether the variable is positive or negative. This bit will hold the value 0 if the variable is positive, and 1 if the variable is negative. The bit kept aside is the left-most bit (called the Most Significant Bit or MSB). A char, for example, can hold values from 0 to 127 (since it is, by default, signed). If a char variable is declared unsigned, then the MSB is released and the variable has one more bit available, and therefore an unsigned char can hold a range of values from 0 to 255. To summarize: signed char has 7 bits available = maximum value of 127, minimum value of -127 unsgined char has 8 bits available = maximum value of 255, minimum value of 0 Thus, an unsigned variable can hold a larger value than a signed variable, but cannot hold negative values.
    Q21. Why do we need user defined datatypes?
  Ans: User defined datatypes are required to create a custom structure, such as employee, student, etc. For example, you can define a data type to represent a student. This data type will contain variables (elements) to store the registration number, name, address and other details of a student. Also, it allows you to define it once and then use it across all applications by including the header file in which it is defined.
    Q22. What is OOPS?
  Ans: OOPS (object-oriented programming systems) is a technique of application development that views all entities in the application (customers, suppliers, students, etc.) as objects. Thus, an object-oriented application consists of objects that are related to each other. All objects have attributes, termed as properties. Properties describe and identify a particular object. Objects also have methods, which are the functions associated with the object.
    Q23. Can the public member variables and functions of one class be used in another class or a function that has been defined in a different program file?
  Ans: Yes, but the class declaration must be included in the first class.
    Q24. What is endl and how is it different from '\n'?
  Ans: The endl manipulator directs the cout object to print a new line character and flushes the output buffer. Whereas the character, '\n', is the new line character, which places the cursor on the next line. The following two statements will both display the strings, "hello" and "bye", on two different lines on the screen. cout << "hello" << endl << "bye"; cout << "hello\nbye";
    Q25. What is the difference between " " and ' '?
  Ans: A string is represented as one or more characters enclosed within " " (double quotes), whereas a single character is enclosed within ' ' (single quotes).
    Q26. If a class has a constructor defined for it, is it necessary that it should also define a destructor?
  Ans: No, it is not necessary.
    Q27. Can I use functions from two different libraries in the same program?
  Ans: Yes, provided there are no duplicate symbols in the libraries.
    Q28. Can the protected access specifier be used without inheritance?
  Ans: Yes.
    Q29. How can a list be sorted on two fields?
  Ans: A list can be sorted on two fields, by having two pointers in a node structure, both pointers pointing to two different nodes. For example, struct Node
{
string Field1, Field2;
Node *nextField1, *nextField2;
Node(const string &f1, const string f2, Node
*n1 = NULL, Node *n2 = NULL) : Field1(f1),
Field2(f2), nextField1(n1), nextField2(n2))
{

}
};
    Q30. Can the details in the hundredth node be displayed without traversing the entire list?
  Ans: No, the entire list has to be traversed.
    Q31. What are the features of an object-oriented programming language?
  Ans: The features an object oriented programming language are:
a) Classes
b) Encapsulation
c) Inheritance
d) Polymorphism
    Q32. Who created the C++ programming language?

  Ans: Bjarne Stroustrup. 

Share This Post →

Powered By Blogger |   Designed By Blogger Templates
DMCA.com