Questions???

Questions and their Answers

 

Q. Why is #include<time.h>, #include<graphics.h>, #include<dos.h> header files are included ?

Sol.   #include<time.h> implements date and time manipulation operations.They provide support for time acquisition.
#include<graphics.h> can be used to draw different shapes, display text in different fonts, change colors and many more.
#include<dos.h> contains functions for handling interrupts, producing sound, date and time functions etc. It is borland specific and works in turbo c compiler.

 

Q. What is the use of rand() function ?

Sol  .C++ provides random number generation function rand() that is found in <stdlib.h> header.
The rand function generates an integer between 0 and RAND_MAX.

Q. What is the concept behind declaring function first and defining it at the end ?

Sol.  Function Declaration gives us a lot of information about the function.
First, it tells the return type of the function
Two, it tells how many parameters are there in the function and their types.
Function Definition tells what the function does and the statements performing some action are contained in function definition only.

Q. What is the use of goto and gotoxy() ?

 The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally. In its general form, the goto statement is written as
goto label;
where the label is an identifier that is used to label the target statement to which the control is transferred.

gotoxy is a function or procedure that positions the cursor at (X,Y) where X is in horizontal and Y is in vertical relative to the origin of the current window. The origin is located at (1,1), the upper left corner of the window.


Q. Why do we write comments ?

Sol.  Comments are explanatory statements that you can include in the C++ code that you write and helps anyone reading it's source code.

Q. What is the difference between pre increment(++a) and post increment(a++) ?

Sol.  ++a is pre-increment and a++ is post-increment that is in the first a is incremented before being used and in the second a is incremented after being used.

Q. What is the use of files in C++?

Sol.  All the programs input only from the keyboard, and output only to the screen. If we were restricted to use only the keyboard and screen as input and output devices, it would be difficult to handle large amounts of input data, and output data would always be lost as soon as we turned the computer off. To avoid these problems, we can store data in some secondary storage device, usually magnetic tapes or discs. Data can be created by one program, stored on these devices, and then accessed or modified by other programs when necessary. To achieve this, the data is packaged up on the storage devices as data structures called files.


Q. What is the difference between a class and a structure ?

Sol.  The only difference between a class and a struct in c++ is that the struct have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and can have member functions.

Important!! 

Q. What is the concept used here and why ?

Sol. The concept used here is object oriented programming (i.e the use of classes and objects).
The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods.

Object:
This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.

Class:
When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
Abstraction:
Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
Encapsulation:
Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.
Inheritance:
One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.
This is a very important concept of object-oriented programming since this feature helps to reduce the code size.
Polymorphism:
The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.
Overloading:
The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type, it is said to be overloaded.



Q. What is the difference between return 0 and return 1 ?

Sol. Usually zero is returned to the calling function or operating system when a program was executed successfully. Other numbers may be returned if an error occurred during the execution and the specific number would indicate the type of error. For Example, say, if an error occurred while opening a file the value returned will be 1. this also helps in debugging the programs. 

Q. What is the use of void ?

Sol. Void means that the function does not return a value. If the function is just used to perform some action but does not need to return a status it is declared as void.

Q.What is implicit conversion/coercion in c++?

 Sol.Implicit convertsions are performed when a type (say T) is used in a context where a compatible type (Say F) is expected so that the type T will be promoted to type F. 
  

short a = 2000 + 20;

 In the above example, variable a will get automatically promoted from short to int. This is called implicit conversion/coercion in c++.

Q. What are C++ inline functions? 

Sol. C++ inline functions are special functions, for which the compiler replaces the function call with body/definition of function. Inline functions makes the program execute faster than the normal functions, since the overhead involved in saving current state to stack on the function call is avoided. 
By giving developer the control of making a function as inline, he can further optimize the code based on application logic. But actually, it's the compiler that decides whether to make a function inline or not regardless of it's declaration. 
Compiler may choose to make a non inline function inline and vice versa. Declaring a function as inline is in effect a request to the compiler to make it inline, which compiler may ignore. So, please note this point for the interview that, it is upto the compiler to make a function inline or not. 

Q.What do you mean by internal linking and external linking in c++?

[This interview question is related to questions on "translation unit" and "storage classes"]

Sol. A symbol is said to be linked internally when it can be accessed only from with-in the scope of a single translation unit. By external linking a symbol can be accessed from other translation units as well. This linkage can be controlled by using static and extern keywords.

 Q.What do you mean my storage classes?

Sol. Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.

Q.How many storage classes are available in C++?

Sol. Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.
Following storage classes are available in C++

  • auto
It's the default storage class for local variables. They can be accessed only from with in the declaration scope. auto variables are allocated at the beginning of enclosing block and deallocated at the end of enclosing block. 
  • register
It's similar to auto variables. Difference is that register variables might be stored on the processor register instead of RAM, that means the maximum size of register variable should be the size of CPU register ( like 16bit, 32bit or 64bit). This is normally used for frequenly accessed variables like counters, to improve performance. But note that, declaring a variable as register does not mean that they will be stored in the register. It depends on the hardware and implementation. 
  • static
A static variable will be kept in existence till the end of the program unlike creating and destroying each time they move into and out of the scope. This helps to maintain their value even if control goes out of the scope. When static is used with global variables, they will have internal linkage, that means it cannot be accessed by other source files. When static is used in case of a class member, it will be shared by all the objects of a class instead of creating separate copies for each object. 
  • extern
extern is used to tell compiler that the symbol is defined in another translation unit (or in a way, source files) and not in the current one. Which means the symbol is linked externally. extern symbols have static storage duration, that is accessible throughout the life of program. Since no storage is allocated for extern variable as part of declaration, they cannot be initialized while declaring. 
  • mutable
mutable storage class can be used only on non static non const data a member of a class. Mutable data member of a class can be modified even is it's part of an object which is declared as const. 

C++ Storage SpecifierStorage LocationScope Of VariableLife Time
auto Memory (RAM) Local With in function
static Memory (RAM) Local Life time is from when the flow reaches the first declaration to the termination of program.
register CPU register Local With in function
extern Memory (RAM) Global Till the end of main program

Q.What is realloc() and free()? What is difference between them?

Sol. 

  • void* realloc (void* ptr, size_t size)

This function is used to change the size of memory object pointed by address ptr to the size given by size. If ptr is a null pointer, then realloc will behave like malloc(). If the ptr is an invalid pointer, then defined behavior may occur depending the implementation. Undefined behavior may occur if the ptr has previously been deallocated by free(), or dealloc() or ptr do not match a pointer returned by an malloc(), calloc() or realloc().

  • void free (void* ptr)

This function is used to deallocate a block of memory that was allocated using malloc(), calloc() or realloc(). If ptr is null, this function does not doe anything.

Q.What is difference between shallow copy and deep copy? Which is default?

[This question can be expected in any interviews, not just c++ interviews. This is a usual question in most of the java interviews.]

Sol. When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want.
In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.

Q.What do you mean by persistent and non persistent objects?

[This question may be asked in many ways during c++ interviews, like how to send an object to a remote computer or how to save the your program state across application restarts. All these are related to serialization.]
Sol. Persistent objects are the ones which we can be serialized and written to disk, or any other stream. So before stopping your application, you can serialize the object and on restart you can deserialize it. (Drawing applications usually use serializations.)
Objects that can not be serialized are called non persistent objects. (Usually database objects are not serialized because connection and session will not be existing when you restart the application.)

 Q.Is it possible to get the source code back from binary file?

Sol.Technically it is possible to generate the source code from binary. It is called reverse engineering. There are lot of reverse engineering tools available. But, in actual case most of them will not re generate the exact source code back because many information will be lost due to compiler optimization and other interpretations.

Q.What are virtual functions and what is its use?

[This is a sure question in not just C++ interviews, but any other OOP language interviews. Virtual functions are the important concepts in any object oriented language, not just from interview perspective. Virtual functions are used to implement run time polymorphism in c++.] 
Sol. Virtual functions are member functions of class which is declared using keyword 'virtual'. When a base class type reference is initialized using object of sub class type and an overridden method which is declared as virtual is invoked using the base reference, the method in child class object will get invoked.

 Q.What is meant by reference variable in C++? 

Sol.In C++, reference variable allows you create an alias (second name) for an already existing variable. A reference variable can be used to access (read/write) the original data. That means, both the variable and reference variable are attached to same memory location. In effect, if you change the value of a variable using reference variable, both will get changed (because both are attached to same memory location).

Q.What are the difference between reference variables and pointers in C++?

[This question is usually asked in a twisted way during C++ interviews. Sometimes the interviewer might use examples and ask you to find the error.]



PointersReference Variables
Pointers can be assigned to NULL References cannot be assigned NULL. It should always be associated with actual memory, not NULL.
Pointers can be (re)pointed to any object, at any time, any number of times during the execution. Reference variables should be initialized with an object when they are created and they cannot be reinitialized to refer to another object
Pointer has own memory address and location on stack Reference variables has location on stack, but shares the same memory location with the object it refer to.








 

Q.What do you mean by pure virtual functions in C++? Give an example? 

Sol. Pure virtual function is a function which doesn't have an implementation and the same needs to be implemented by the the next immediate non-abstract class. (A class will become an abstract class if there is at-least a single pure virtual function and thus pure virtual functions are used to create interfaces in C++).

 How to create a pure virtual function?
A function is made as pure virtual function by the using a specific signature, " = 0" appended to the function declaration as given below,

  1. class SymmetricShape {
  2. public:
  3. // draw() is a pure virtual function.
  4. virtual void draw() = 0;
  5. };

Q.Why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?

Sol. Pure virtual functions are used when it doesn't make sense to provide definition of a virtual function in the base class or a proper definition does not exists in the context of base class. Consider the above example, class SymmetricShape is used as base class for shapes with symmetric structure(Circle, square, equilateral triangle etc). In this case, there exists no proper definition for function draw() in the base class SymmetricShape instead the child classes of SymmetricShape (Circle, Square etc) can implement this method and draw proper shape.

Q.What is virtual destructors? Why they are used?

Sol.  [This c++ interview question is in a way related to polymorphism.]

Virtual destructors are used for the same purpose as virtual functions. When you remove an object of subclass, which is referenced by a parent class pointer, only destructor of base class will get executed. But if the destructor is defined using virtual keyword, both the destructors [ of parent and sub class ] will get invoked.

Q.What you mean by early binding and late binding? How it is related to dynamic binding?

Sol.  [This c++ interview question is related to question about virtual functions ]

Binding is the process of linking actual address of functions or identifiers to their reference. This happens mainly two times.
  • During compilation : This is called early binding
For all the direct function references compiler will replace the reference with actual address of the method.
  • At runtime : This is called late binding.
In case of virtual function calls using a Base reference, as in shown in the example of question no: 2, compiler does not know which method will get called at run time. In this case compiler will replace the reference with code to get the address of function at runtime.

Dynamic binding is another name for late binding.

  Q.What do you mean by translation unit in c++? 

Sol. We organize our C++ programs into different source files (.cpp, .cxx etc). When you consider a source file, at the preprocessing stage, some extra content may get added to the source code ( for example, the contents of header files included) and some content may get removed ( for example, the part of the code in the #ifdef of #ifndef block which resolve to false/0 based on the symbols defined). This effective content is called a translation unit. In other words, a translation unit consists of
  • Contents of source file
  • Plus contents of files included directly or indirectly
  • Minus source code lines ignored by any conditional pre processing directives ( the lines ignored by #ifdef,#ifndef etc)

No comments:

Post a Comment