Welcome to graduate2professional.blogspot.com

Saturday, March 31, 2012

Repeatedly asking C++ Interview Questions


1.What is public, protected, private?
Ans:
 
public, protected, private are access specifiers that is used to implement encapsulation of data at various level.
Private:

* Can be data or method members
* Are private to the class where they are declared
* Accessible ONLY by the member methods of the class where they are declared
* Only exception to the above rule is Friend (explanation of friends is beyond the scope of this topic
* In a C++ class, private is default for member declaration. That is, if you do not specify any access specifier (private, public, protected), the member is considered private

Public:

* Can be data or method members
* Are accessible by any function/method/application globally, so long as an instance of the class where the public members are declared is created.
* These members are accessible only through an instance of the class where they are declared

Protected

* Can be data or method members
* Act exactly as private members for all practical purposes, so long as they are referenced from within the class (and/or instances of the class)where they are declared
* Specifically used to define how certain data/method members of a class would behave in a child class (used to define their behaviour in inheritance)
* The protected members become private of a child class in case of private inheritance, public in case of public inheritance, and stay protected in case of protected inheritance.


2. What is a class?
Ans:
Class is a user defined data type,in which data members and member functions are defined. A class can also be defined as a classification/category of objects that have similar attributes and behaviour.

For example, Automobile is a category of objects that have similar attributes, such as wheels, engine, doors, shape, color, cylinders etc., and behaviours, such as start, run, move, turn etc.
Car is an instance of automobile which has different values for the attributes (4 wheels, one engine, 2 or 4 doors, 4/6/8 cylinders, etc),

3.What is an object?
Ans:
In C++, Object is an instance of a Class that has a runtime state, and is associated with certain specific methods that can change its state.
 
4.Difference between realloc() and free()?
Ans:
Realloc() is used to reallocate the memory for variable. Realloc()used to resize the memory held by the pointer to the number of bytes specified. If the new
size is larger than current size, new memory is allocated. If it is less, the remaining (additional) bytes are released to general OS/application consumption.

Free() is used to free the allocated memory of a variable.


5. What is function overloading and operator overloading?
Ans:
Function overloading: C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types
are concerned). This capability is called function overloading.
When an overloaded function is called, the C++ compiler selects the proper function by examining the number,types and order of the arguments in the call.
Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types
For example, void Function_Test();
void Function_Test(int); // Overloaded
void Function_Test(int, int); // Overloaded

Operator overloading: Allows existing C++ operators to be redefined so that they work on objects of user-defined classes. They can improve understandability and reduce maintenance costs.


6. What is virtual class and friend class?
Ans:
Virtual Base Class: Used in context of multiple inheritance in C++. If you plan to derive two classes from a class, and further derive one class from the two classes in the second level, you need to declare the uppermost base class as 'virtual' in the inherited classes. This prevents multiple copies of the uppermost base class data members when an object of the class at the third level of hierarchy is created.

Friend class: When a class declares another class as its friend, it is giving complete access to all its data and methods including private and protected data and methods to the friend class member methods.
Friendship is not bi-directional. If A declares B as its friend it does NOT mean that A can access private data of B. It only means that B can access all data of A.

7. What is abstraction?
Ans:
Abstraction refers to the act of representing essential features without including the background details. That means abstraction is separating the logical properties from implementation details.
For example driving the car is a logical property and design of the engine is the implementation detail.


8. What do you mean by inline function?
Ans:
The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange for increased compile time and possibly (but not always) an increase in the size of the generated binary executables. When an inline Function is invoked the code of function is inserted instead of jump to code of function.

9. What do you mean by pure virtual functions?
Ans:
A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape {

public:

virtual void draw() = 0;
};


10. What is virtual constructors/destructors?
Ans:
Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object,the base-class destructor function (matching the pointer type) is called on the object.



11. What is a scope resolution operator?
Ans:
A scope resolution operator (::) can be used to define the member functions of a class outside the class. Most generally a scope resolution operator is required when a data member is redefined by a derived class,or an overriden method of the derived class wants to call the base class version of the same method.

12. what is difference between constructor and destructor?
Ans:
Constructor is the member function of the class which has the same name as that of class and it is invoked whenever the class object is instantiated. Using constructor we can allocate memory.

Destructor is also the member function of same class name and has ~ operator when ever declared in the function and it is used to destruct the object which has been constructed ,whenever we want to destroy it.

Thursday, December 24, 2009

What is Condition,Decision and How they influence the Coverage?

Condition - A Boolean expression containing no Boolean operators.

Decision - A Boolean expression composed of conditions and zero or more Boolean operators. A decision without a Boolean operator is a condition. If a condition appears more than once in a decision, each occurrence is a distinct condition.

Decision Coverage - Every point of entry and exit in the program has been invoked at least once and every decision in the program has taken on all possible outcomes at least once.

Modified Condition/Decision Coverage - Every point of entry and exit in the program has been invoked at least once, every condition in a decision in the program has taken all possible outcomes at least once, every decision in the program has taken all possible outcomes at least once, and each condition in a decision has been shown to independently affect.


1. Structural coverage guidelines are:
a) Every statement in the program has been invoked at least once;
b) Every point of entry and exit in the program has been invoked at least once;
c) Every control statement (i.e., branchpoint) in the program has taken all possible outcomes (i.e., branches) at least once;
d) Every non-constant Boolean expression in the program has evaluated to both a True and a False result;
e) Every non-constant condition in a Boolean expression in the program has evaluated to both a True and a False result;
f) Every non-constant condition in a Boolean expression in the program has been shown to independently affect that expression's outcome.

2. Based upon these definitions:
• Statement Coverage requires (a) only
• DC requires (b, c, d)
• MC/DC requires (b, c, d, e, f)

Tuesday, December 22, 2009

What is a Thread?

•Technically, a thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system.

•To the software developer, the concept of a “procedure” that runs independently from its main program may best describes a thread.

•To go one step further, imagine a main program (a.out) that contains a number of procedures. Then imagine all of these procedures being able to be scheduled to run simultaneously and/or independently by the operating system. That would describe a "multi-threaded" program.

•Before understanding a thread, one first needs to understand a UNIX process. A process is created by the operating system, and requires a fair amount of "overhead".

Processes contain information about program resources and program execution state, including:
1.Process ID, process group ID, user ID, and group ID
2.Environment
3.Working directory.
4.Program instructions
5.Registers
6.Stack
7.Heap
8.File descriptors
9.Signal actions
10.Shared libraries
11.Inter-process communication tools (such as message queues, pipes, semaphores, or shared memory).

•Threads use and exist within these process resources, yet are able to be scheduled by the operating system and run as independent entities largely because they duplicate only the bare essential resources that enable them to exist as executable code.

This independent flow of control is accomplished because a thread maintains its own:
1.Stack pointer
2.Registers
3.Scheduling properties (such as policy or priority)
4.Set of pending and blocked signals
5.Thread specific data.

So, in summary, in the UNIX environment a thread:
1.Exists within a process and uses the process resources
2.Has its own independent flow of control as long as its parent process exists and the OS supports it
3.Duplicates only the essential resources it needs to be independently schedulable
4.May share the process resources with other threads that act equally independently (and dependently)
5.Dies if the parent process dies - or something similar
6.Is "lightweight" because most of the overhead has already been accomplished through the creation of its process.

Because threads within the same process share resources:
1.Changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads.
2.Two pointers having the same value point to the same data.
3.Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer.

Wednesday, November 11, 2009

HR Questions

1. Do you ever outperform others? Tell me a time that demonstrates this?
2. Do you prefer a slow, medium, fast paced environment and why?
3. Do you ever take on to many tasks?
4. Do people come to you for answers and why?
5. How important is it to you to be the best?
6. Do you prefer to be part of a group or to lead a group and why?
7. What do you look for in a company that you work for?
8. On a scale of 1 to 10, how competitive are you and why?
9. How important is it for you to be 'liked' by your peers?
10.Did anyone approach you if they face any obstacles? When?
11. Explain me about the character of any of your manager?
12.Explain me the best reputation or the best compliments about your work?
13. Have you felt very proud of yourself when you accomplished
anything?when?
14. Rate yourself from 1 - 5 about your honesty.
15. Did anyone feel you are friendly in nature? Explain
16. Is your character the same with everyone or changes from person to
person.
17. What type of characters do you feel to work out with.
18. Are you liked by your colleagues?
19. Do you get your things done because they like you?
20. Whom do you share with if you complete your work?
21. How did you deal with the conflicts?
22. DO you to be busy always are free?
23. How do you manage things on a typical day?
24. What will you do if there are many projects due at a time?
25. Do your colleagues approach you for any help? Example.
26. How do you thank your colleague for their help?
27. Do you feel that you need to be recognized for your work?
28. If there are many distractions beside you while you are working what
will you do?
29. Are you focused? Example.
30. If your voice is ugly in the group what will you do in the discussions?
31. Will you according to the plan or according to the flow.
32. Will you take decisions according to the facts or indigenous?
33. If all the things are equal will you like to be in the group or take the
lead position?
34. Will you like to instruct someone?