Skip to content

Instantly share code, notes, and snippets.

@arvindpdmn
Last active June 26, 2026 15:13
Show Gist options
  • Select an option

  • Save arvindpdmn/81a597bada7ad152f2d7ebb2ec3e6223 to your computer and use it in GitHub Desktop.

Select an option

Save arvindpdmn/81a597bada7ad152f2d7ebb2ec3e6223 to your computer and use it in GitHub Desktop.
C Language Quiz for Beginners - Set 2

C Language Quiz for Beginners - Set 2

This quiz has 30 questions. Each question has four options (A, B, C, D). Choose the one most suitable option. The suggested time for the quiz is 30 minutes.

1. How many bytes does it take to store an unsigned long long type?

A. 4

B. 8

C. 12

D. 16

2. What is the value of a in hexadecimal notation?

int a = -7 / 2;

A. FFFC

B. 000000FD

C. FFFFFFFC

D. FFFFFFFD

3. What the result of the expression 10 / 3 << 1 + 2 * sizeof(int) >> 2?

A. 12

B. 24

C. 384

D. 3072

4. Which statement is true of the following code?

char city[] = "Mumbai";
void *ptr = city;
printf("%s", ptr + 3);

A. String bai is printed

B. ptr assignment will compile but with a warning

C. ptr + 3 will result in a compile-time error

D. ptr + 3 will result in a runtime error

5. What are the values of a, b and c at the end of this code?

int a = 2, b = 5, c = 0;
if (++a || c++) {
    if (b-- && ++c) {
    }
}

A. a:3 b:4 c:2

B. a:3 b:5 c:1

C. a:2 b:4 c:1

D. a:3 b:4 c:1

6. What will the following code output?

int i = 10;
{
    int i = 5;
    i++;
}
printf("%d", i);

A. 5

B. 6

C. 10

D. 11

7. What will the following code output?

char name[] = "Krishna\0murthy";
printf("%s", name+strlen(name)+1);

A. a

B. `` (empty string)

C. m

D. murthy

8. What will the following code output?

float xyz[][3] = {
    { 1.2, 4.5, 3.1 },
    { 1.4, 4.2, 2.3 },
    { 2.4, 2.7, 3.9 }
};
printf("%.1f %.1f", (float)sizeof(xyz)+1, (float)*(*xyz+1));

A. 13.0 8.0

B. 13.0 5.5

C. 37.0 4.5

D. 37.0 2.4

9. What will the following code output?

char city[] = "Mumbai";
printf("%d %d", sizeof(city), strlen(city));

A. 7 6

B. 6 7

C. 6 6

D. 7 7

10. What will the following code output?

char s1[50] = "hello";
char s2[] = "world";
char *s3 = strcat(strcat(s1, " "), s2);
s3[0] = 'H';
printf("%s; %s; %s", s1, s2, s3);

A. Hello world; world; Hello world

B. hello; world; Hello world

C. Hello; world; Hello world

D. hello; helloworld; Helloworld

11. Find the odd one out?

A. fopen, stdio.h

B. atoi, string.h

C. malloc, stdlib.h

D. log10, math.h

12. Given a string name with odd number of characters, how do we obtain the middle character?

A. name[strlen(name)/2]

B. name[strlen(name)/2+1]

C. name[strlen(name)+1>>1]

D. name[strlen(name)>>2]

13. What is the value of variable c?

int a = 2, b = 4;
int c = ++a+b---a*b;

A. -1

B. -2

C. 15

D. -3

14. Which is an invalid variable name?

A. firstName

B. first_name

C. 1st_name

D. _FIRSTNAME

15. Which of the following is an incorrect statement about fopen?

A. It returns data type FILE *

B. It takes two arguments of types const char* filename and const char mode

C. For read-only file operation mode has to be r

D. To create a new file, mode can be w, w+, a or a+

16. Given the following code, identify the correct statement?

char const name[] = "Krishna";
char const * q = name;
char * const r = name;
printf("%s %s", q++, r++);

A. Build error because q is constant

B. Build error because r is constant

C. Build warning in initialization of q

D. No errors or warnings

17. Suppose we have int main(int argc, char *argv[]) {}. Which of the following statements is correct?

A. argv[argc] is a NULL pointer

B. argv[argc] is a NUL character

C. argv[0] points to the first argument passed to the program

D. argc is zero if program is called without arguments

18. What can we say about the following code?

void main() {
    int a = 100;
    static int b = a / 2; 
}

A. b has half the value of a

B. Compile-time warning

C. Compile-time error

D. Runtime error

19. What can we say about the following code?

char *students[] = {
    "Akash", "M", "24",
    "Divya", "F", "22",
    "Praveen", "M", "23"
};

A. students is a 2-D array containing strings

B. It's better to write the numbers as integers

C. Variable should be defined as char *students[3] instead

D. Code is fine but could be improved using a struct for each student

20. What will the following code output?

int x = 5;
printf("%d %d", x, x++);    

A. 5 5

B. 6 5

C. 5 6

D. Undefined behaviour

21. Given the following function, what's the value of f1(2, 4) + f1(-2, -3)?

int f1(int a, int b) {
    return a > b ? a : -b;
}

A. -6

B. 5

C. -5

D. 6

22. We wish to swap two integers a and b. Which of the following function interface will do the job?

A. void swap(int a, int b);

B. void swap(int& x, int& y);

C. void swap(int& a, int& b);

D. void swap(int* x, int* y);

23. What will the following code output?

int a = 0x3E8; // hex for 1000
unsigned char *p = (unsigned char*)&a;
printf("%02x %02x", p[0], p[1]);

A. 00 00

B. E8 03

C. 03 E8

D. Either A or B

24. What will the following code output?

int x[] = { 1, 3, 5, 7, 9 };
int *p = x;
printf("%d %d", *p+1, *(p+3));

A. 3 10

B. 2 7

C. 3 7

D. 2 10

25. What can we say about the following code?

typedef struct {
    char first_name[15];
    char last_name[15];
    unsigned int age;
} Student;

int main() {
    Student cse_class[] = {
        { "Akash", "Kumar", 18 },
        { "Krishna", "S", 17 },
        { "Ramya", "Verma", 19 }
    };
    for (Student *p = cse_class; p != NULL; p++) {
        printf("Name: %s %s, Age: %d\n",
            p->first_name, p->last_name, p->age);
    }
    return 0;
}

A. Build error due to p->

B. Build error because printf call is not on a single line

C. Runtime error due to p++

D. Runtime error due to p != NULL

26. Assume that whole and part are non-NULL non-empty strings. What should TBD be to obtain correct behaviour?

/* This function returns the number of times sub-string part occurs in string whole.
   Search is done in a non-overlapping manner.
*/
int count_substr(const char *whole, const char *part) {
    int count = 0, offset = 0;
    while (1) {
        const char *found = strstr(whole+offset, part);
        if (found) {
            offset = TBD;
            count++;
        }
        else break;
    }
    return count;
}

A. found - whole + strlen(part)

B. whole - part + strlen(part) - 1

C. whole - part + strlen(part)

D. part - whole + strlen(part) - 1

27. Which statement is true of the following code?

int* list_add(int x[4], int y[4], size_t n) {
    int* sum = malloc(n * sizeof(int));
    
    for (int i = 0; i < n; i++) {
        sum[i] = x[i] + y[i];
    }

    return sum;
}

int main() {
    int a[] = {1, 2, 3, 4};
    int b[] = {10, 20, 30, 40};
    int *c = list_add(a, b, sizeof(a)/sizeof(int));
}

A. size_t n argument is unnecessary since size is available in x and y

B. list_add is constrained to arrays of exactly 4 integers

C. free(sum) should be called before returning from list_add

D. Arrays a and b are passed by reference but not the third argument

28. Which code counts the number of lines in a file?

A.

FILE *fp = fopen("input.txt", "r");
int lines = 0;
char ch;

while ((ch = fgetc(fp)) != EOF) {
    if (ch == '\n') lines++;
}

printf("%d\n", lines);

B.

FILE *fp = fopen("input.txt", "r");
int lines = 0;
int ch;

while ((ch = fgetc(fp)) != EOF) {
    if (ch == '\n') lines++;
}

printf("%d\n", lines);

C.

FILE *fp = fopen("input.txt", "r");
int lines = 0;

while (!feof(fp)) {
    if (fgetc(fp) == '\n') lines++;
}

printf("%d\n", lines);

D.

FILE *fp = fopen("input.txt", "r");
int lines = 0;

while (fgets(NULL, 0, fp)) lines++;

printf("%d\n", lines);

29. What should TBD be to obtain correct behaviour?

typedef enum Day {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY,
    DAY_COUNT
} Day;

const char *day_names[] = {
    "MONDAY",
    "TUESDAY",
    "WEDNESDAY",
    "THURSDAY",
    "FRIDAY",
    "SATURDAY",
    "SUNDAY"
};

int main() {
    Day today = SATURDAY;
    Day three_days_later = (today + 3) TBD;
    printf("%s %s", day_names[today], day_names[three_days_later]);
}

A. + DAY_COUNT -1

B. - 1

C. % DAY_COUNT

D. % SUNDAY

30. The following code is meant to count non-alphanumeric and non-blank characters. What should TBD be to obtain correct behaviour?

unsigned char address[] = "#12-B, 4th Main Road, 12th Cross, Domlur, Bangalore 560038.";
char *p = address;
unsigned int count = 0;
while (*p) TBD
printf("%d", count);

A. { if (!isalnum(*p) && !isblank(*p)) count++; p++; }

B. if (!isalnum(*p) && !isblank(*p++)) count++;

C. if (!isalnum(*p++) && !isblank(*p)) count++;

D. if (!isalnum(*p) && !isblank(*p++)) count++;







































Answers

  1. [B] See https://os.mbed.com/handbook/C-Data-Types or https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Primitive-Types
  2. [D] Division gives the result -3. Number 3 is 0011 in binary. Number -3 in Two's Complement is 1100 + 1 = 1101 = 0xD. Since int takes 4 bytes, we should left-fill the number with 1-bit values for a negative number.
  3. [C] Because of Operator Precedence, this is evaluated in this manner: 10 / 3 << 1 + 2 * sizeof(int) >> 2 = 3 << 1 + 2 * 4 >> 2 = 3 << 1 + 8 >> 2 = 3 << 9 >> 2 = 0011b << 9 >> 2 = 0011 0 0000 0000b >> 2 = 0011 0 0000 00b = 0001 1000 0000b = 0x180 = 384. Between, << and >>, the evaluation is left-to-right order. Hence, left shift happens first before right shift.
  4. [A] C language allows implicit conversion to and from a void*. So there's won't be any warning for ptr assignment. But for ptr + 3, some compilers will give a warning if the right compilation flags are enabled. Most commonly, compiler will move the pointer by three bytes.
  5. [D] Because of lazy evaluation, c++ is not executed since left-side expression of || is already true.
  6. [C] The second i has a limited scope only within the {} code block. Function printf deals with the i from the outer scope.
  7. [D] name+strlen(name) advances the pointer to the null character. +1 moves the pointer to the m character, from where the remaining string is printed.
  8. [C] Each row takes 12 bytes. xyz takes 36 bytes. *xyz dereferences the 2-D matrix to the first row. +1 moves the pointer to second item of the row. Then we dereference it to obtain the value 4.5. This is equivalent to xyz[0][1], which is more readable.
  9. [A] sizeof includes the NUL string-terminating character.
  10. [A] In fact, s3 is redundant since it points to s1. strcat modifies the first argument in-place but the first argument is also returned by the function. Hence, the return of strcat(s1, " ") can be passed directly as first argument of the outer strcat call.
  11. [B] atoi function is in header file stdlib.h.
  12. [A] For example, if name="Krishna" (7 characters), strlen(name)/2 = 7/2 = 3. Since indexing is from zero, name[3] is the middle character s. Another way to access the middle character is name[strlen(name)>>1].
  13. [B] Compiler parses the expression left to right as (++a) + (b--) - (a*b), which is 3 + 4 - 3*3 = -2. Note that due to postfix decrement, two different values of b are used.
  14. [C] Identifiers (variable, function or macro names) can't start with a digit (0-9) though digits are allowed in non-starting positions.
  15. [B] Second argument is const char* mode. It has to be a string, not a character. This is evident in w+, which has two characters. See https://en.cppreference.com/c/io/fopen
  16. [B] There are no warnings or errors due to q but r has both. In initialization of r, there's a warning since the constant nature of the value is discarded. Only the r pointer is a constant, not what it points to. Because r is a constant pointer, it can't be incremented.
  17. [A] char *argv[] is actually an array of pointers to characters. It's equivalent to char **argv. C standard requires that argv[argc] be initialized to NULL. argv[0] points to the program name, including the path if called that way. argc has a minimum value of 1 since argv[0] is always populated.
  18. [C] static requires compile-time initialization but a is evaluated only at runtime even though 100 is a constant.
  19. [D] students is 1-D array containing strings. Since we can't mix different types in a single array, everything is treated as strings. char *students[3] won't work since it expects only three strings but we're initializing with nine strings. A struct will give names for each data part (eg. name, sex, age) and make the code more readable.
  20. [D] C language doesn't impose any constraint on compiler on the order of evaluating the arguments to a function. If they're evaluation left-to-right, we would get 5 5; else we would get 6 5. Hence, it's better to avoid such code.
  21. [A] f1(2, 4) returns -4 and f1(-2, -3) returns -2.
  22. [D] The integers have to be passed by reference, not passed by value as shown in option A. The caller has to call the function as swap(&a, &b);. The argument names used in the function can be anything. Options B and C use invalid syntax, though such a syntax is allowed in C++ language.
  23. [D] Option B on Little-Endian machines. Option A on Big-Endian machines since the 03 is at p[2] and E8 is at p[3]. This difference due to Endianness means that such code is not portable across machines and therefore must be avoided.
  24. [B] With *p+1 value is incremented. With *(p+3) pointer is moved and then value is obtained.
  25. [D] p != NULL is useless to identify the end of the array of students. Instead use p - cse_class != sizeof(cse_class)/sizeof(cse_class[0]). Note that p - cse_class returns the offset in terms of Student, not bytes.
  26. [A] Once a match is found, we advance offset by the string length of part since searches are done in a non-overlapping manner. For overlapping searches, we can do offset = found - whole + 1;.
  27. [D] We can do sizeof(a) in main since the entire array is in the current scope. However, in list_add, only pointers to a and b are available. Hence, we can't do sizeof(x) in list_add. Therefore size_t n argument is needed. The syntax int x[4] is equivalent to int x[] and int *x. The value 4 has no importance to the compiler. free(sum) should be called in main.
  28. [B] fgetc returns an int. EOF is also an int. feof should be called after a call to another I/O function. See https://en.cppreference.com/c/io
  29. [C] enum values are in fact integers, by default starting from value 0. Hence, SUNDAY is 6 and DAY_COUNT is 7. The day_names array can be indexed in the range 0-6.
  30. [A] Pointer increment is placed outside the if so that it's not bypassed due to lazy evaluation. Since it's outside, {} is used to force it to be part of the while statement.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment