C++ Interview Questions (to be modified...)

1.      Will the following program execute?void main()
{
void *vptr = (void *) malloc(sizeof(void));
vptr++;
}

Compile error. Cannot convert from int to char*.

 

2.      How about this one?
void main()
{
char *cptr = 0X2000;
long *lptr = 0X2000;
cptr++;
lptr++;
printf(” %x %x”, cptr, lptr);
}Will it execute or not?

Compile error as the sizeof operator cannot be used with void. And we cannot get the size of size of void*, so vptr++ also produce compile error.

 

3.      When the processor wakes up after power on, it goes to a particular memory location. What is that memory location called?

Wossit got to do with C++?

 

processor goes to MBR (master boot record) then i takes the address of boot loader form thr and execute this boot loader program

 

4.      What is the difference between Mutex and Binary semaphore?

From Multithreaded Applications in Win32, by Jim Beveridge ( page 93 ):
“…it turns out that a mutex is a degenerated form of a semaphore. If you create a semaphore and set the maximum count to one, you get mutual exclusion behavior. therefore a mutex is often refered to as a binary semaphore…In Win32, the ownership semantics of how the two operate are quite different, so they cannot be used interchangeably…”

 

Mutex is efficent than semaphore. Also unlocking of mutex can be done by the thread which owns the lock. Not the case with semaphore.

 

5.      Write a program to set 2nd bit in a 32 bit register with memory location 0×2000?

By “2nd bit” are you counting from most significant or least significant bit? Anyway, the answer is platform-dependent and compiler-dependent. Assuming you have a typedef “uint32″ that is exactly 32 bits long, then try something like *reinterpret_cast(0×2000) = 0×40000000 (for 2nd msb) or = 2 (for 2nd lsb)

 

6.

void main()

{

printf("%d/n");

printf("%d %d/n");

printf("%d %d %d/n");

}

Output:

0

0 309590428

0 309590428 2147348480

 

Why this?

 

7.

void main()

{

int *p = 0;//0 is NULL

delete p;

}

NO error and warning.

 

P.S. What’s the differences between critical section and mutex?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章