C++ developer interview

  1. Will the following program execute?void main()
    void *vptr = (void *) malloc(sizeof(void));
  2. How about this one?
    void main()
    char *cptr = 0?2000;
    long *lptr = 0?2000;
    printf(” %x %x”, cptr, lptr);
    Will it execute or not?
  3. When the processor wakes up after power on, it goes to a particular memory location. What is that memory location called?
  4. What is the difference between Mutex and Binary semaphore?
  5. Write a program to set 2nd bit in a 32 bit register with memory location 0×2000?
This entry was posted in C++. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

22 Comments on C++ developer interview

  1. bhagya
    Posted 5/8/2006 at 7:54 am | Permalink

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

    4. semaphore is used to synchronize processes. where as mutex is used to provide synchronization between threads running in the same process.

  2. Diana
    Posted 5/22/2006 at 12:03 pm | Permalink

    1. it will not build as sizeof cannot be applied to void* ( error “Unknown size” )

    4. the statement in the comment above is not correct, a mutex can be used for inter-process syncronization ( for syncronization inside the same process CriticalSection can be used )

    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…”

  3. SK
    Posted 5/23/2006 at 7:59 am | Permalink


    Qs: Write a ‘grow’ function for a C++ vector class?

  4. Mani Chauhan
    Posted 5/28/2006 at 7:56 am | Permalink

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

  5. Suresh
    Posted 5/29/2006 at 5:06 am | Permalink

    for 4.

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

  6. klint
    Posted 6/12/2006 at 11:51 am | Permalink

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

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

    For Q3: Wossit got to do with C++?

    For Q5: 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)

  7. klint
    Posted 6/12/2006 at 11:55 am | Permalink

    My previous comment has been garbled by this web site. It should read

    *reinterpret_cast < uint32* > (0×2000)

    and 0×2000 should contain the letter x and not some funny symbol.

  8. Venkateswarlu
    Posted 6/19/2006 at 8:57 am | Permalink


    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

  9. Posted 7/27/2006 at 2:13 am | Permalink


  10. Radha
    Posted 8/2/2006 at 6:48 am | Permalink

    for Q 1
    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.

  11. Steve
    Posted 10/8/2006 at 7:19 pm | Permalink

    Q2: 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 *’

  12. Steve
    Posted 10/8/2006 at 7:30 pm | Permalink

    Further for Q2: Not Excute if it is C++, but Excute in C.

    The printout:
    2001 2004

    compiled with some warnings:
    warning C4047: ‘initializing’ : ‘char *’ differs in levels of indirection from ‘int’
    warning C4047: ‘initializing’ : ‘long *’ differs in levels of indirection from ‘int’

  13. required
    Posted 10/24/2006 at 10:00 pm | Permalink

    won’t compile since it’s an invalid asignment. Casting needs to be done. The format has to be:
    int *a = (int *)2000;

  14. Lei
    Posted 11/1/2006 at 3:39 pm | Permalink

    Q1: 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*’.

  15. Jisa
    Posted 11/2/2006 at 12:11 pm | Permalink

    Question 1.
    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

  16. Jisa
    Posted 11/2/2006 at 12:26 pm | Permalink

    Question 2:

    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

  17. Posted 11/15/2006 at 2:55 pm | Permalink

    Mani Chauhan is completely wrong for Q3.

    The correct answer is it starts executing code from address 0xFFFF`FFF0. That is the locaiton of the jump vector.

  18. Hari
    Posted 3/4/2007 at 6:23 pm | Permalink

    So power hits the system, the CPU wakes up and tells the chipset to look at the top of the memory stack. It gets redirected to the top of the first megabyte and executes the code that it finds there. That code activates enough of the CPU to enable it to decompress the BIOS code. Once that happens, the BIOS code identifies the system, setting values in certain chipset registers, such as the number of CPUs present, the CPU stepping and brand (among others).

  19. Posted 4/10/2008 at 7:41 am | Permalink

    About Q1

    In HP cc/aCC the program does not even compile:

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

    cc: “t.c”, line 2: error 1594: The sizeof operator cannot be applied to types with unknown size.
    cc: “t.c”, line 3: error 1539: Cannot do arithmetic with pointers to objects of unknown size.

  20. Posted 4/10/2008 at 7:45 am | Permalink

    After reboot the processor loads contents of High Registers.

  21. Cj
    Posted 6/28/2008 at 2:10 pm | Permalink

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

    Ans-when power is switched on processor goes to bootstrap loader with memory 0 to 80 units
    for loading of this loader

  22. Amit
    Posted 7/15/2008 at 12:09 pm | Permalink

    A mutex is owned by a thread/process. So once a thread locks it, then other threads/processes will either spin or block on the mutex. Whereas, semaphore allows one or more threads/processes to share the resource.


    here, you are locking global data so that only one thread/process can access it. Others will wait in a turnstile forming a Q. while in semaphore you can define that how many concurrent threads/processes can access that resource at a time. so if a psuedo code is like this

    sema(10) // ten threads/process have the concurrent access.


    then max 10 threads/processes will access it.
    when one process locks the semaphore the counts decrements by 1. So when the count=0, all the processes are using the data.

    So you can see that a binary semaphore is like a mutex.

Post a Comment

Your email is never published nor shared. Required fields are marked *