Set - 7

Question 1 :

Will the following program execute?

void main(){
	void *vptr = (void *) malloc(sizeof(void));
	vptr++;
}

 

Answer :

Answer1
It will throw an error, as arithmetic operations cannot be performed on void pointers. 

Answer2
It will not build as sizeof cannot be applied to void* ( error "Unknown size" ) 

Answer3
How can it execute if it won't even compile? It needs to be int main, not void main. Also, cannot increment a void *. 

Answer4
According to gcc compiler it won't show any error, simply it executes. but in general we can't do arthematic operation on void, and gives size of void as 1 

Answer5
The program compiles in GNU C while giving a warning for "void main". The program runs without a crash. sizeof(void) is "1? hence when vptr++, the address is incremented by 1. 

Answer6
Regarding arguments about GCC, be aware that this is a C++ question, not C. So gcc will compile and execute, g++ cannot. g++ complains that the return type cannot be void and the argument of sizeof() cannot be void. It also reports that ISO C++ forbids incrementing a pointer of type 'void*'. 

Answer7
in C++
voidp.c: In function `int main()':
voidp.c:4: error: invalid application of `sizeof' to a void type
voidp.c:4: error: `malloc' undeclared (first use this function)
voidp.c:4: error: (Each undeclared identifier is reported only once for each function it appears in.)
voidp.c:6: error: ISO C++ forbids incrementing a pointer of type `void*'

But in c, it work without problems


Question 2 :

void main(){
	char *cptr = 0?2000;
	long *lptr = 0?2000;
	cptr++;
	lptr++;
	printf(" %x %x", cptr, lptr);
}

Will it execute or not?

Answer :

Answer1
For Q2: As above, won't compile because main must return int. Also, 0×2000 cannot be implicitly converted to a pointer (I assume you meant 0×2000 and not 0?2000.) 

Answer2
Not Excute.
Compile with VC7 results following errors:
error C2440: 'initializing' : cannot convert from 'int' to 'char *'
error C2440: 'initializing' : cannot convert from 'int' to 'long *'


Not Excute if it is C++, but Excute in C.
The printout:
2001 2004 

Answer3
In C++
[$]> g++ point.c
point.c: In function `int main()':
point.c:4: error: invalid conversion from `int' to `char*'
point.c:5: error: invalid conversion from `int' to `long int*' 

in C
[$] etc > gcc point.c
point.c: In function `main':
point.c:4: warning: initialization makes pointer from integer without a cast
point.c:5: warning: initialization makes pointer from integer without a cast
[$] etc > ./a.exe
2001 2004


Question 3 :

Write a program that ask for user input from 5 to 9 then calculate the average ?

Answer :

int main(){
	int MAX=4;
	int total =0;
	int average=0;
	int numb;
	cout<<"Please enter your input from 5 to 9";
	cin>>numb;
	if((numb <5)&&(numb>9))
		cout<<"please re type your input";
	else
		for(i=0;i<=MAX; i++){
			total = total + numb;
			average= total /MAX;
		}
		cout<<"The average number is"<<average<<endl;
	return 0;
}

 


Question 4 :

Assignment Operator - What is the diffrence between a "assignment operator" and a "copy constructor"?

Answer :

Answer1. 
In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object. For example: 
complex c1,c2;
c1=c2; //this is assignment
complex c3=c2; //copy constructor

Answer2. 
A copy constructor is used to initialize a newly declared variable from an existing variable. This makes a deep copy like assignment, but it is somewhat simpler: 
There is no need to test to see if it is being initialized from itself. 
There is no need to clean up (eg, delete) an existing value (there is none). 
A reference to itself is not returned.


Question 5 :

RTTI - What is RTTI?

Answer :

Answer1. 
RTTI stands for "Run Time Type Identification". In an inheritance hierarchy, we can find out the exact type of the objet of which it is member. It can be done by using: 
1) dynamic id operator 
2) typecast operator 

Answer2. 
RTTI is defined as follows: Run Time Type Information, a facility that allows an object to be queried at runtime to determine its type. One of the fundamental principles of object technology is polymorphism, which is the ability of an object to dynamically change at runtime.


Question 6 :

STL Containers - What are the types of STL containers?

Answer :

There are 3 types of STL containers: 
1. Adaptive containers like queue, stack 
2. Associative containers like set, map 
3. Sequence containers like vector, deque


Question 7 :

What is "mutable"?

Answer :

Answer1. 
"mutable" is a C++ keyword. When we declare const, none of its data members can change. When we want one of its members to change, we declare it as mutable. 

Answer2. 
A "mutable" keyword is useful when we want to force a "logical const" data member to have its value modified. A logical const can happen when we declare a data member as non-const, but we have a const member function attempting to modify that data member. For example: 

class Dummy {
    public:
        bool isValid() const;
    private:
        mutable int size_ = 0;
        mutable bool validStatus_ = FALSE; 
        // logical const issue resolved
};

bool Dummy::isValid() const 
// data members become bitwise const
{
    if (size > 10) {
    validStatus_ = TRUE; // fine to assign
    size = 0; // fine to assign
    }
}

Answer2. 
"mutable" keyword in C++ is used to specify that the member may be updated or modified even if it is member of constant object. Example: 

class Animal {
    private:
        string name;
        string food;
        mutable int age;
    public:
    void set_age(int a);
};

void main() {
    const Animal Tiger(’Fulffy’,'antelope’,1);
    Tiger.set_age(2); 
    // the age can be changed since its mutable
}

 


Question 8 :

What is a modifier? 

Answer :

A modifier, also called a modifying function is a member function that changes the value of  at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as 'mutators'. Example: The function mod is a modifier in the following code snippet:

class test{
	int x,y;
	public:
	test(){
		x=0; y=0;
	}
	void mod(){
		x=10;
		y=15;
	}
};

 


Question 9 :

What is a dangling pointer?

Answer :

A dangling pointer arises when you use the address of an object after its lifetime is over. This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed. The following code snippet shows this:

class Sample{
	public:
	int *ptr;
	Sample(int i){
		ptr = new int(i);
	}

	~Sample(){
		delete ptr;
	}
	void PrintVal(){
	cout << "The value is " << *ptr;
	}
};

void SomeFunc(Sample x){
	cout << "Say i am in someFunc " << endl;
}

int main(){
	Sample s1 = 10;
	SomeFunc(s1);
	s1.PrintVal();
}

In the above example when PrintVal() function is called it is called by the pointer that has been freed by the destructor in SomeFunc.


Question 10 :

What are proxy objects? 

Answer :

Objects that stand for other objects are called proxy objects or surrogates. 
template <class t="">

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:

Array2D<float>data(10,20);
cout<<data[3][6]; // fine

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.


Question 11 :

Name some pure object oriented languages?

Answer :

Smalltalk, Java, Eiffel, Sather.


Question 12 :

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

Answer :

Answer1:

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;
	}
}

Answer2:

node* reverselist(node* head){
	if(0==head || 0==head->next) 
		//if head->next ==0 should return head instead of 0;
		return 0;
	{
		node* prev = head;
		node* curr = head->next;
		node* next = curr->next;
		for(; next!=0; ){
			curr->next = prev;
			prev = curr;
			curr = next;
			next = next->next;
		}
		curr->next = prev;
		head->next = 0;
		head = curr;
	}
	return head;
}

 


Question 13 :

Detemine the code below, tell me exectly how many times is the operation sum++ performed ? 

Answer :

for ( i = 0; i < 100; i++ )
for ( j = 100; j > 100 - i; j–)
sum++;

(99 * 100)/2 = 4950
The sum++ is performed 4950 times.


Question 14 :

What is deadlock?

Answer :

Deadlock is a situation when two or more processes prevent each other from running. Example: if T1 is holding x and waiting for y to be free and T2 holding y and waiting for x to be free deadlock happens.


Question 15 :

What is semaphore?

Answer :

Semaphore is a special variable, it has two methods: up and down. Semaphore performs atomic operations, which means ones a semaphore is called it can not be interrupted. 

The internal counter (= #ups - #downs) can never be negative. If you execute the "down" method when the internal counter is zero, it will block until some other thread calls the "up" method. Semaphores are use for thread synchronization.


Question 16 :

Is C an object-oriented language? 

Answer :

C is not an object-oriented language, but limited object-oriented programming can be done in C.


Question 17 :

Name some major differences between C++ and Java?

Answer :

C++ has pointers; Java does not. Java is platform-independent; C++ is not. Java has garbage collection; C++ does not. Java does have pointers. In fact all variables in Java are pointers. The difference is that Java does not allow you to manipulate the addresses of the pointer


Question 18 :

What is the difference between Stack and Queue?

Answer :

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


Question 19 :

Write a fucntion that will reverse a string?

Answer :

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);
}

 


Question 20 :

What is the software Life-Cycle?

Answer :

The software Life-Cycle are
1) Analysis and specification of the task
2) Design of the algorithms and data structures
3) Implementation (coding)
4) Testing
5) Maintenance and evolution of the system
6) Obsolescence


Question 21 :

What is the difference between a Java application and a Java applet? 

Answer :

The difference between a Java application and a Java applet is that a Java application is a program that can be executed using the Java interpeter, and a JAVA applet can be transfered to different networks and executed by using a web browser (transferable to the WWW).


Question 22 :

Name 7 layers of the OSI Reference Model?

Answer :

-Application layer
-Presentation layer
-Session layer
-Transport layer
-Network layer
-Data Link layer
-Physical layer


Question 23 :

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

Answer :

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

Answer2
The major difference between B-tree and binary tres is that B-tree is a external data structure and binary tree is a main memory data structure. The computational complexity of binary tree is counted by the number of comparison operations at each node, while the computational complexity of B-tree is determined by the disk I/O, that is, the number of node that will be loaded from disk to main memory. The comparision of the different values in one node is not counted.


Question 24 :

Write the psuedo code for the Depth first Search?

Answer :

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


Question 25 :

Describe one simple rehashing policy?

Answer :

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.


Question 26 :

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

Answer :

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.


Question 27 :

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?

Answer :

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.