C Language quiz questions

C Language interview questions

  • 1.

    What value gets printed by the program below?

    struct Foo{
            int x:1;
            int y:2;
    };
    
    struct Foo obj;
    
    obj.x = 5;
    
    printf("%d\n", obj.x);
    1. 1

    2. 2

    3. 5

    4. not defined

    Answer
  • 2.

    what value should be printed by the program:

    typedef union ds_{
            short s;
            char c;
    } ds;
    
    ds object;
    object.s = 0x0503;
    printf("%d\n", object.c);
    1. 0

    2. 3

    3. 5

    4. 0x0503

    5. machine dependent

    Answer
  • 3.

    What number is output by the program below? (assuming 8 byte pointers)

    #include <stdio.h>
    
    int main(){
        const char   firstname[] = "bobby";
        const char*  lastname = "eraserhead";
        printf("%lu\n", sizeof(firstname) + 
                        sizeof(lastname));
        return 0;
    }
    1. 8

    2. 14

    3. 16

    4. 17

    5. 20

    Answer
  • 4.

    When running the program below, the malloc statement will always be executed?

    #include <stdlib.h>
    #include <stdio.h>
    
    int* ptrToData;
    
    int main(){
    
        if (!ptrToData){
            ptrToData = (int*)malloc(sizeof(int) * 10);
            printf("%p\n", ptrToData);
        }
    
        free(ptrToData);
        return 0;
    }
    
    1. True, the malloc statement will always be executed

    2. False, depending on how ptrToData is initialized in the machine the malloc statement might not get run.

    Answer
  • 5.

    For the code below which lines should be reported as errors by a compiler?

    int main(int argc, char** argv){
    
      const char* foo = "wow";           // line 1
      foo = "top";                       // line 2
      foo[0] = 1;                        // line 3
    
      return 0;
    }
    1. line 2

    2. line 3

    3. lines 2 and 3

    4. none of the lines

    Answer
  • 6.

    Which of the following is the correct way to declare a function pointer named pMyFunc that returns an int and has an int parameter?

    1. *(int pMyFunc(int));

    2. int ()(int)* pMyFunc;

    3. int (*pMyFunc)(int);

    4. int *pMyFunc (int);

    5. (int *pMyFunc int);

    Answer
  • 7.

    True or False?

    int32_t is a data type that is guaranteed to be available on all standard conforming C implementations and represents a 32 bit signed integer type?

    1. true

    2. false

    Answer
  • 8.

    In theory, which is faster, the call to strcpy or the call to memcpy?

    #include <string.h>
    #include <stdlib.h>
    int main(){
    
            char msg[12] = "Hello World";
            char buffer1[12];
            char buffer2[12];
    
            strcpy(buffer1, msg);
            memcpy(buffer2, msg, sizeof(msg));
            return 0;
    }
    1. strcpy

    2. memcpy

    3. they should have the same speed

    Answer
  • 9.

    What should the program below print?

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void myfunc(char** param){
        ++param;
    }
    int main(){
        char* string = (char*)malloc(64);
        strcpy(string, "hello_World");
        myfunc(&string);
        myfunc(&string);
        printf("%s\n", string);
        // ignore memory leak for sake of quiz
        return 0;
    }
    1. hello_World

    2. ello_World

    3. llo_World

    4. lo_World

    5. Illegal memory access, undefined behavior

    Answer
  • 10.

    How many bytes of memory are used to store a long long data type?

    1. 4 bytes

    2. 8 bytes

    3. 16 bytes

    4. 32 bytes

    5. It is implementation defined

    Answer
  • 11.

    In general which of the following functions should be faster for sending information to a file?

    1. int printf(const char *format, ...);

    2. int fprintf(FILE *stream, const char *format, ...);

    3. ssize_t write(int fd, const void *buf, size_t count);

    Answer
  • 12.

    Which are true about: void *realloc(void *ptr, size_t size);

    1) realloc changes the size of the allocated
        memory pointed to by the argument ptr
    
    2) newly allocated memory will be uninitialized
    
    3) you can pass a NULL ptr safely to realloc
    
    4) realloc will guarantee not to move the data 
        pointed to in ptr
    1. 1, 2, 3, 4

    2. 1, 2, 3

    3. 1, 2

    4. 1

    Answer
  • 13.

    True or false. Calling free on the same address twice is ok.

    1. true

    2. false

    Answer
  • 14.

    Which of the following differences between malloc and calloc are true?

    1) malloc allocates number of bytes passed as argument
    
    2) calloc allocates the product of number of elements 
        multiplied by the size of each element, 
        which are both passed as arguments.
    
    3) both malloc and calloc return void*
    
    4) both malloc and calloc initialize allocated
        memory to all 0
    1. 1, 2, 3, 4

    2. 1, 2, 3

    3. 1, 2

    4. 1

    5. none of the statements are true

    Answer
  • 15.

    What will the following code do?

    int main(int argc, char** argv){
       char* ptr = NULL;
       free(ptr);
       return 0;
    }
    1. core dump

    2. nothing, but the code is safe

    3. undefined behavior

    Answer
  • 16.

    What is the correct output from the following code?

    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
       int x = 3;
       printf("%d", x++  +  ++x);
       return 1;
    }
    1. 3

    2. 5

    3. 6

    4. 7

    5. undefined

    Answer

© 2017 QuizBucket.org