Skip to content

Instantly share code, notes, and snippets.

@arvindpdmn
Last active June 9, 2026 05:53
Show Gist options
  • Select an option

  • Save arvindpdmn/50ee9e084806aa518f80efb9edf3f9fe to your computer and use it in GitHub Desktop.

Select an option

Save arvindpdmn/50ee9e084806aa518f80efb9edf3f9fe to your computer and use it in GitHub Desktop.
C Language Quiz for Beginners - Set 1

C Language Quiz for Beginners - Set 1

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. Given the following code, what is the character in greeting[4]?

char greeting[] = "Hello World!";

A. e

B. ll

C. o

D. Hell

2. Which statement is true of the following code?

unsigned int a = 100;
int b = -20;
if (b < a) printf("I expected this.\n");
else printf("I didn't expected this!\n");

A. Output is I expected this.

B. Output is I didn't expected this!

C. Build error due to syntax issues

D. Program will run but will crash

3. What will the following code output?

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

A. 5, 5

B. 5, 6

C. 6, 5

D. 6, 6

4. What are the data types of a and b?

int* a, b;

A. Both a and b are pointers to int

B. a is a pointer to int and b is an int

C. Both a and b are int

D. None of the above

5. Given the following code, what will strlen(greeting) give?

char greeting[] = "Hello\0World!";

A. 12

B. 11

C. 13

D. 5

6. What is the range of int data type? Assume ^ is exponential notation in the options given below.

A. [-2^32, 2^32]

B. [0, 2^32-1]

C. [-2^31, 2^31-1]

D. [-2^31+1, 2^31-1]

7. Find the odd pair?

A. fopen, fclose

B. malloc, free

C. printf, scanf

D. atoi, memset

8. What does num contain after the following code is executed?

int num[5] = {10, 20, 30};
int *ptr = num;
*(ptr+1) += 5;

A. {15, 20, 30}

B. {10, 25, 30}

C. {10, 25, 30, 0, 0}

D. {10, 20, 30, 5, 0}

9. What will the following code output?

float value = 5 / 2;
printf("%.1f", value);

A. 2.5

B. 2.50

C. 2.0

D. .5

10. What will the following code output?

int a = 0x1122DE31;
int b = a & 0x1100;
printf("%x, %x", a>>8, b);

A. 1122de, 1000

B. 0, 1100

C. 1122DE31, 1000

D. 1122de31, 1100

11. What will the following code output?

int x = 0;
int y = 10;
if ((x & 0x10) && ++y) {}
printf("%d, %d", x, y);

A. 0, 11

B. 0, 10

C. 2, 11

D. 2, 10

12. What will the following code output?

#include <stdio.h>

int x = 3;

void update(int a) {
    int x = 1;
    static int y = 5;
    x += a;
    y += a;
    printf("x:%d, y:%d; ", x, y);
}

int main() {
    update(x);
    
    x += 4;
    printf("x:%d; ", x);
    
    update(2);
    
    return 0;
}

A. x:8, y:8; x:12; x:14, y:10;

B. x:4, y:8; x:7; x:3, y:10;

C. x:8, y:8; x:7; x:3, y:7;

D. x:4, y:8; x:12; x:14, y:7;

13. What will the following code output?

char *str = "Hello";
str[0] = 'Y';
printf("%s", str);

A. Yello

B. Hello

C. Build error

D. Runtime error

14. What will the following code output?

char greeting[] = "Hello";
char *ptr = "Hello";
printf("%zu, %zu", sizeof(greeting), sizeof(ptr));

A. 5, 5

B. 6, 8

C. 6, 4

D. 5, 8

15. What happens after this code execution?

char src[] = "Data";
char dst[3];
strcpy(dst, src);

A. dst has the value Data

B. dst has the value Dat but there's also undefined or unsafe behaviour

C. dst has the value Dat

D. dst has the value Da plus a NUL-terminating character

16. What will the following code output?

double *ptr = 0x1000;
printf("%p", ptr + 1);

A. 0x1001

B. 0x1004

C. 0x1008

D. 0x1000

17. What will the following code output?

int sum = 0;
for (int i = 0; i < 10; i++) {
    sum += (i % 3) ? 0 : i;
}
printf("%d", sum);

A. 27

B. 24

C. 20

D. 18

18. How can we access the value 5 in this 2-D array of integers?

int matrix[2][2] = {
    {1, 3},
    {5, 7}
};

A. **(matrix+1)+1

B. matrix[0][1]

C. **(matrix+1)

D. *(*matrix+1)

19. What's the value of the expression 3+2^6+9*3>>2?

A. 13

B. 5

C. 3

D. 7

20. What will the following code output?

int x = 0;
if (x > 1)
    if (x < 10)
        printf("In range");
else
    printf("Out of range");

A. In range

B. Out of range

C. Build error

D. Program will run but no output

21. Which of the following is invalid code?

char name[] = "Krishna";
const char *p = name;
char const *q = name;
char * const r = name;

A. p++;

B. q++;

C. r++;

D. *(r+1) = 'x';

22. What will the following code output?

char *str = "456";
printf("%s %c %d %d", str, str[0], str[0], atoi(str)+1);

A. 4 4 4 5

B. 456 4 52 5

C. 456 4 52 457

D. 456 4 25 457

23. What will the following code output?

int arr[] = {10, 20, 30, 40};
int *ptr = arr;
int w = *ptr++;
int x = (*ptr)++;
int y = ++*ptr;
int z = *++ptr;
printf("%d, %d, %d, %d", w, x, y, z);

A. 11 21 31 41

B. 10 20 31 41

C. 10 22 30 40

D. 10 20 22 30

24. What will the following code output?

#include <stdio.h>

#define SQUARE(x) (x * x)

void main()
{
    int a = 3, b = 2;
    printf("%d", SQUARE(a+b));
}

A. 25

B. 13

C. 11

D. 16

25. Which code is equivalent to strlen function?

A.

size_t mystrlen( const char* str ) {
    size_t len;
    for (len = 0; str[len] != '\0'; ++len);
    return len;
}

B.

size_t mystrlen( const char* str ) {
    size_t len = 0;
    while (*str++) len++;
    return len;
}

C.

size_t mystrlen( const char* str ) {
    char *p = str;
    while (*p++);
    return p - str - 1;
}

D. All of the above

26. Given that reg is an unsigned int, which code flips bit 3 (4th bit) and keeps the other bits unchanged?

A.

reg |= 1 << 4;

B.

reg &= 1 << 4;

C.

reg ^= 1 << 3;

D.

reg |= 0xF7 >> 3;

27. What will the following code output?

char buffer[] = "Good versus Bad";
memset(buffer, 'X', 4);
strncpy(buffer + 3, "Not", 2);
printf("%s", buffer);

A. XXXNoversus Bad

B. Good versus Bad

C. XXXXNotversusBa

D. XXXX Not versus

28. Which of the following code counts the number of vowels in a string?

A.

int c = 0;
while (*str)
    if (strchr("aeiou", tolower(*str))) c++;

B.

int c = 0;
while (*str++)
    if (strchr("aeiou", tolower(*str))) c++;

C.

int c = 0;
while (*str) 
    if (strchr("AEIOUaeiou", *str)) c++;

D.

int c = 0;
while (*str) 
    if (strchr("AEIOUaeiou", *str++)) c++;

29. What will the following code output?

char s1[] = "abc";
char *s2 = "abc";
if (s1 == s2) printf("Strings are equal");
else printf("Strings are not equal");

A. Strings are equal

B. Strings are not equal

C. Build error

D. None of the above

30. Which of the following statements is true?

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

int main() {
    Student cse_class[32] = {
        { "Akash", "Kumar", 18 },
        { "Krishna", "S", 17},
        { "Ramya", "Verma" }
    };
}

A. Compilation error because Ramya's age is not initialised

B. 34 bytes are allocated for each student

C. Student data is initialised only for three students

D. 36 bytes are allocated for each student







































Answers

  1. [C] Arrays start from zero index.
  2. [B] Since int is compared to unsigned int, it is implicitly converted to unsigned int. Thus, -20 becomes a big positive number. For implicit conversion rules, see https://www.scaler.com/topics/c/implicit-type-conversion-in-c/
  3. [C] Value x is assigned to y and then it is incremented in-place.
  4. [B] The * is associated with the variable name and not the type. It's better to write this as int *a, b; if single-line declaration is preferred.
  5. [D] The string is terminated by '\0'. Hence it's only 5 characters though more memory is allocated to greeting.
  6. [C] See https://os.mbed.com/handbook/C-Data-Types
  7. [D] In options A, B and C, one function does the opposite of the other: open/close file, allocate/free memory and output/input message. Functions atoi and memset are not related in this way.
  8. [C] The last two items of the array are default initialized to zero.
  9. [C] The division is performed on integers and therefore the result gets truncated to an integer even though it's subsequently assigned to a float type. Correct division can be done with (float)5 / 2 or simply 5. / 2 to force decimal division.
  10. [A] The two least significant bytes of a is 0xDE31 (1101 1110 0011 0001 in binary). If we do bitwise AND with 0x1100 (0001 0001 0000 0000 in binary) we get 0001 0000 0000 0000, which is 0x1000 in hexadecimal. Bit shifting by 8 bits to the right means that leftmost 8 bits are lost and more significant bits are shifted lower. Formatting %x prints in lowercase.
  11. [B] Left-side expression of && evaluates to zero, which implies false. With AND, there's no point in evaluating the right-side expression. Hence, y is not incremented. This is called lazy evaluation. Lazy evaluation can happen for || as well if the left-side expression evaluates to true.
  12. [B] This tests understanding of variable scope. Top x is global variable. x and y inside update are local to that function but since y is static it's initialized only once and the variable persists across multiple calls to the function. With x += 4; in main, the global variable is updated.
  13. [D] "Hello" is a string literal. It's allocated in a read-only memory section. Hence it can't be modified. However, a smart modern compiler when used with the correct compilation flags can catch this problem at build time. The way to initialise a string on the stack is by doing char str[] = "Hello"; instead.
  14. [B] Note that sizeof counts the NUL character at the end unlike strlen. Most modern systems are 64-bit systems. Hence the pointer takes up 8 bytes. Older 32-bit system allocate 4 bytes for a pointer. For printf formatting options, see https://en.cppreference.com/c/io/fprintf
  15. [B] Array dst is smaller than src. Function strcpy copies the data beyond the allowable boundary of dst. See https://en.cppreference.com/c/string/byte/strcpy
  16. [C] The type double takes up 8 bytes. When we do ptr + 1, the pointer moves to the next double, hence 8 bytes further.
  17. [D] Integers 0-9 are processed. (i % 3) is zero for integers divisible by 3, that is, [3, 6, 9]. In these cases, they are added to sum, else only 0 is added.
  18. [C] The expressions **(matrix+1), matrix[1][0] and *matrix[1] are equivalent.
  19. [A] This relates to operator precedence. Multiplication happens before addition. Bit shifting happens next. Finally, bitwise XOR happens. The expression is equivalent to (3 + 2) ^ ( (6 + (9*3))>>2 ) = 5 ^ ((6+27)>>2) = 5 ^ (33>>2) = 5 ^ (00100001b>>2) = 0101b ^ 1000b = 1101b = 13. See https://en.cppreference.com/c/language/operator_precedence
  20. [D] Code indentation in C language is for code readability. Compiler doesn't care about code indentation. It associates the else with the second if. This sort of problem can be avoided by always using {} with if-else statement.
  21. [C] p and q are equivalent. They are pointers to a constant string. Though name is not constant and can be changed directly, it can't be changed via p or q. Pointers p and q can be changed to point to another location. r is a constant pointer to a variable string. String itself can be changed via r but r can't be changed to point to another location. The keyword const must be associated with what precedes it. Going by this rule, the syntax of q is preferred over that of p.
  22. [C] Character 4 has ASCII value 52. It's worth remembering the following ASCII values: 0:0x30, A:0x41, a:0x61. See https://www.ascii-code.com/
  23. [D] For w, value is read, assigned to w and postfix increment on the pointer is done. In fact, even if we were to write w = *(ptr++); postfix increment happens last after w is assigned the value 10. For x, value is read, assigned to x and postfix increment on the value is done (20 becomes 21). For y, value is accessed, incremented in-place (21 becomes 22) and then assigned to y. For z, pointer is prefix incremented, value is read and assigned to z. Note that pointer is increment only in w and z statements. In x and y statements, value is incremented.
  24. [C] Preprocessor replaces SQUARE(a+b) with (a+b * a+b), which is effectively a + b*a + b. The proper way to write the macro is #define SQUARE(x) ((x) * (x)). Alternatively, call with parentheses, SQUARE((a+b)), which will become (a + b)*(a + b).
  25. [D] In option C, pointer moves beyond '\0' character because of postfix increment. Hence -1 is used to account for this. Because of pointer arithmetic, option C doesn't a separate variable len to keep track of the count.
  26. [C] 1 << 3 = 0000 0001b << 3 = 0000 1000b. Thus, bit 3 is 1 and others are 0. When we do XOR (^) with reg only bit 4 is flipped and keeps others the same.
  27. [A] memset replaces Good with XXXX. buffer+3 pointers to the last X. strncpy replaces X and space with No.
  28. [D] In A and C, str is never incremented resulting in an infinite loop. In B, str is incremented in while, thus skipping the check on the first character. It's interesting to note that if '\0' character is passed to strchr as second argument, it will get matched since the first argument ends with the same character. See https://en.cppreference.com/c/string/byte/strchr
  29. [B] The operator == when used on strings only compares the pointer values. To compare string values, use strcmp, strncmp or even memcmp instead.
  30. [B] Although Student appears to take 15 + 15 + 4 = 34 bytes, there's something called memory alignment. Since age is 4 bytes, computer architecture expects its location to be a multiple of 4. But 15 + 15 = 30 is not a multiple of 4. Hence, 2 bytes are padded (and never used) after last_name so that age starts at a 4-byte boundary. This can be verified by printing the locations of the members of Student, printf("%p %p %p", cse_class[0].first_name, cse_class[0].last_name, &cse_class[0].age). You can also check using sizeof(Student).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment