Wednesday, October 8, 2008

INTERVIEW QUESTIONS & ANSWERS FROM C, C ++ part II

Difference between C structure and C++ structure?

C++ places greater emphasis on type checking, compiler can diagnose every diff between C and C++

1. structures are a way of storing many different values in variables of potentially diff types under under the same name
2. classes and structures make program modular, easier to modify make things compact
3. useful when a lot of data needs to be grouped together
4. struct Tag {…}struct example {Int x;}example ex; ex.x = 33; //accessing variable of structure
5. members of a struct in C are by default public, in C++ private
6. unions like structs except they share memory – allocates largest data type in memory - like a giant storage: store one small OR one large but never both at the same time
7. pointers can point to struct:
8. C++ can use class instead of struct (almost the same thing) - difference: C++ classes can include functions as members
9. members can be declared as: private: members of a class are accessible only from other members of their same class; protected: members are accessible from members of their same class and friend classes and also members of their derived classes; public: members are accessible from anywhere the class is visible
10. structs usually used for data only structures, classes for classes that have procedures and member functions
11. use private because in large projects important that values not be modified in an unexpected way from the POV of the object
12. advantage of class declare several diff objects from it, each object of Rect has its own variable x, y AND its own functions
13. concept of OO programming: data and functions are properties of the object instead of the usual view of objects as function parameters in structured programming

Difference between calloc and malloc?

malloc: allocate n bytes
calloc: allocate m times n bytes initialized to 0


Difference between printf and sprintf?


sprintf: a function that puts together a string, output goes to an array of char instead of stdout

printf: prints to stdout

What is the difference between = and == in C?

“=” is an assignment operator while, “==” is comparative operator
a=b; //b’s value is stored in a

if( a==b) // a’s value is compared with b

What is the difference between superclass and subclass?

A super class is a class that is inherited whereas sub class is a class that does the inheriting.

What is difference between overloading and overriding?

a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.

b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.

c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.

d) Overloading must have different method signatures whereas overriding must have same signature.

How many ways can an argument be passed to a subroutine?

An argument can be passed in two ways. They are Pass by Value and Passing by Reference.

Passing by value: This method copies the value of an argument into the formal parameter of the subroutine.

Passing by reference: In this method, a reference to an argument (not the value of the argument) is passed to the parameter.

What do you mean by multiple inheritance in C++ ?

Multiple inheritance is a feature in C++ by which one class can be of different types. Say class teachingAssistant is inherited from two classes say teacher and Student.

What do you mean by virtual methods?

Virtual Methods are used to use the polymorphisms feature in C++. Say class A is inherited from class B. If we declare say function f() as virtual in class B and override the same function in class A then at runtime appropriate method of the class will be called depending upon the type of the object.

How many ways are there to initialize an int with a constant?

Two.
There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.
int foo = 123;
int bar (123);

Explain the scope resolution operator.

It 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.

In C, why is the void pointer useful? When would you use it?

The void pointer is useful becuase it is a generic pointer that any pointer can be cast into and back again without loss of information.

In C, what is the difference between a static variable and global variable?

A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).

What methods can be overridden in Java?

In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.

In C++, what is the difference between method overloading and method overriding?


Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

What is pure virtual function?

A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration

How can you tell what shell you are running on UNIX system?

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

How do you find out if a linked-list has an end? (i.e. the list is not a cycle)

You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will eventually meet the one that goes slower. If that is the case, then you will know the linked-list is a cycle.

What is polymorphism?

Polymorphism is the idea that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

How do you write a function that can reverse a linked-list?

void reverselist(void)
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur->next = head;

for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}

curnext->next = cur;
}
}

Write a fucntion that will reverse a string.

char *strrev(char *s)
{
int i = 0, len = strlen(s);
char *str;
if ((str = (char *)malloc(len+1)) == NULL) /*cannot allocate memory */
err_num = 2;
return (str);
}
while(len)
str[i++]=s[?len];
str[i] = NULL;
return (str);
}

What is the difference between Stack and Queue?

Stack is a Last In First Out (LIFO) data structure.
Queue is a First In First Out (FIFO) data structure

Suppose a 3-bit sequence number is used in the selective-reject ARQ, what is the maximum number of frames that could be transmitted at a time?

If a 3-bit sequence number is used, then it could distinguish 8 different frames. Since the number of frames that could be transmitted at a time is no greater half the numner of frames that could be distinguished by the sequence number, so at most 4 frames can be transmitted at a time.

Describe Stacks and name a couple of places where stacks are useful.

A Stack is a linear structure in which insertions and deletions are always made at one end, called the top. This updating policy is called last in, first out (LIFO). It is useful when we need to check some syntex errors, such as missing parentheses.

Describe one simple rehashing policy.

The simplest rehashing policy is linear probing. Suppose a key K hashes to location i. Suppose other key occupies H[i]. The following function is used to generate alternative locations:

rehash(j) = (j + 1) mod h

where j is the location most recently probed. Initially j = i, the hash code for K. Notice that this version of rehash does not depend on K.

Write the psuedo code for the Depth first Search.

dfs(G, v) //OUTLINE
Mark v as “discovered”
For each vertex w such that edge vw is in G:
If w is undiscovered:
dfs(G, w); that is, explore vw, visit w, explore from there
as much as possible, and backtrack from w to v.
Otherwise:
“Check” vw without visiting w.
Mark v as “finished”.

What are the advantages and disadvantages of B-star trees over Binary trees?

B-star trees have better data structure and are faster in search than Binary trees, but it’s harder to write codes for B-start trees.

What is an HTML tag?

An HTML tag is a syntactical construct in the HTML language that abbreviates specific instructions to be executed when the HTML script is loaded into a Web browser. It is like a method in Java, a function in C++, a procedure in Pascal, or a subroutine in FORTRAN.

Why are arrays usually processed with for loop?

The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All that is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a.length -1. That is exactly what a loop does.

What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.

An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be “attach” to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object

Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?

Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time.

Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].

quicksort ((data + 222), 100);

Tell how to check whether a linked list is circular.

Create two pointers, each set to the start of the list. Update each as follows:

while (pointer1)

{
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2)

{
print (\”circular\n\”);
}
}

Differentiate between functions read() and write().

Functions read() and write() are used for reading and writing blocks of binary data to a file. Their prototypes are

istream &read(unsigned char *buf, int num);
ostream &write(const unsigned char *buf, int num);

The read() function reads num bytes from the associated stream and puts them in the buffer pointed to by buf. The write() function writes num bytes to the associated stream from the buffer pointed to by buf.

What do you understand by visibility modes in class derivations? What are these modes?

The visibility mode determines the availability of the members of a base class to the deriving class (and other classes, functions and the main function) from the derived class. There are three visibility modes:
(i) Public (ii) Private (iii) Protected

What are proxy objects?

Objects that stand for other objects are called proxy objects or surrogates.
template
class Array2D
{
public:
class Array1D
{
public:
T& operator[] (int index);
const T& operator[] (int index)const;
};

Array1D operator[] (int index);
const Array1D operator[] (int index) const;
};

The following then becomes legal:

Array2Ddata(10,20);
cout<<>

Here data[3] yields an Array1D object and the operator [] invocation on that object yields the float in position(3,6) of the original two dimensional array. Clients of the Array2D class need not be aware of the presence of the Array1D class. Objects of this latter class stand for one-dimensional array objects that, conceptually, do not exist for clients of Array2D. Such clients program as if they were using real, live, two-dimensional arrays. Each Array1D object stands for a one-dimensional array that is absent from a conceptual model used by the clients of Array2D. In the above example, Array1D is a proxy class. Its instances stand for one-dimensional arrays that, conceptually, do not exist.

What are the conditions that have to be met for a condition to be an invariant of the class?

The condition should hold at the end of every constructor.
The condition should hold at the end of every mutator (non-const) operation.

What is a Null object?

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 cannot find such an object.

When does a name clash occur?

A name clash occurs when a name is defined in more than one place. For example., two different class libraries could give two different classes the same name. If you try to use many class libraries at the same time, there is a fair chance that you will be unable to compile or link the program because of name clashes.

Why does the function arguments are called as “signatures”?

The arguments distinguish functions with the same name (functional polymorphism). The name alone does not necessarily identify a unique function. However, the name and its arguments (signatures) will uniquely identify a function.
In real life we see suppose, in class there are two guys with same name, but they can be easily identified by their signatures. The same concept is applied here.
ex:
class person
{
public:
char getsex();
void setsex(char);
void setsex(int);
};
In the above example we see that there is a function setsex() with same name but with different signature.

Suppose a class acts an Actor in the problem domain, how to represent it in the static model?

In this scenario you can use “stereotype”. Since stereotype is just a string that gives extra semantic to the particular entity/model element. It is given with in the << >>.
class A
<>

attributes
methods.

What is guard condition?

Guard condition is one, which acts as a firewall. The access from a particular object can be made only when the particular condition is met.
For Example:- customer check customer number ATM.
Here the object on the customer accesses the ATM facility only when the guard condition is met.

What is the difference between an argument and a parameter?

While defining method, variables passed in the method are called parameters. While using those methods, values passed to those variables are called arguments.

What is an Object and how do you allocate memory to it?

Object is an instance of a class and it is a software unit that combines a structured set of data with a set of operations for inspecting and manipulating that data. When an object is created using new operator, memory is allocated to it.

what is the difference between Procedural and OOPs?

a) In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOPs program, unit of program is object, which is nothing but combination of data and code.
b) In procedural program,data is exposed to the whole program whereas in OOPs program,it is accessible within the object and which in turn assures the security of the code.

What are wrapper classes?

Wrapper classes are classes that allow primitive types to be accessed as objects.

Can the size of an array be declared at runtime?

No. In an array declaration, the size must be known at compile time. You can’t specify a size that’s known only at runtime. For example, if i is a variable, you can’t write code like this:

char array[i]; /* not valid C */

Some languages provide this latitude. C doesn’t. If it did, the stack would be more complicated, function calls would be more expensive, and programs would run a lot slower. If you know that you have an array but you won’t know until runtime how big it will be, declare a pointer to it and use malloc() or calloc() to allocate the array from the heap.