Monday, March 24, 2008

C++ developer interview @ Tech Interviews.com

C++ developer interview @ Tech Interviews.com

  1. Will the following program execute?void main()
    {
    void *vptr = (void *) malloc(sizeof(void));
    vptr++;
    }
  2. How about this one?
    void main()
    {
    char *cptr = 0?2000;
    long *lptr = 0?2000;
    cptr++;
    lptr++;
    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?

18 Responses to “C++ developer interview”

  1. bhagya Says:

    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 Says:

    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 Says:

    Help…

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

  4. Mani Chauhan Says:

    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 Says:

    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 Says:

    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 Says:

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

    *reinterpret_cast <> (0×2000)

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

  8. Venkateswarlu Says:

    Q1)

    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. ROSHAN RAZDAN Says:

    GIVE SOME INFORMATION ON FILE MAINTAING IN C++.
    GIVE SOME SUGGETION TO MAKE THE SOLVE HARD PROGRAM BASED ON ‘INHERITENCE’ IN C++.

  10. Radha Says:

    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 Says:

    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 Says:

    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 Says:

    Q2:
    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 Says:

    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 Says:

    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 Says:

    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. Todd Says:

    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 Says:

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

No comments:

如何发掘出更多退休的钱?

如何发掘出更多退休的钱? http://bbs.wenxuecity.com/bbs/tzlc/1328415.html 按照常规的说法,退休的收入必须得有退休前的80%,或者是4% withdrawal rule,而且每年还得要加2-3%对付通胀,这是一个很大...