+91-6376366224     Send message / ask question on our official What's App Number .

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.