C Language quiz 1 quiz

C quiz with data types. control flow, method and programming skills

This quiz is in C Language quiz collection.

Start quiz
  • 1.

    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;
    }
  • 2.

    What will the following code do?

    int main(int argc, char** argv){
       char* ptr = NULL;
       free(ptr);
       return 0;
    }
  • 3.

    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
  • 4.

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

  • 5.

    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
  • 6.

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

  • 7.

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

  • 8.

    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;
    }
  • 9.

    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;
    }
  • 10.

    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?

  • 11.

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

  • 12.

    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;
    }
  • 13.

    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;
    }
    
  • 14.

    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;
    }
  • 15.

    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);
  • 16.

    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);
  • 17.

    What value gets printed by the program below?

    int w = 3;
    int x = 31;
    int y = 10;
    double z =  x / y % w;
    printf("%f\n", z);
  • 18.

    What gets printed by the code below? (Assume 1 byte characters)

    char array[] = "foo";
    printf("%lu\n", sizeof(array[1]));
    
  • 19.

    What gets printed?

    int array[2][2] = {0, 1, 2, 3};
    int i;
    int sum = 0;
    for (i =0; i < 4; ++i){
        int x, y;
        x = i % 2;
        if (x){
            y = 0;
        }
        else{
            y = 1;
        }
        sum += array[x][y];
    }
    printf("%d\n", sum);
  • 20.

    What gets printed?

    int i = 3;
    if (!i)
        i++;
        i++;
    if (i==3)
        i+=2;
        i+=2;
    printf("%d\n", i);

© 2017 QuizBucket.org