C Programming quiz

Data types, operators, control flow, arrays and pointers.

80 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 80 questions with answers
  1. Who developed the C programming language?

    • Bjarne Stroustrup
    • James Gosling
    • Dennis Ritchie (correct)
    • Guido van Rossum
  2. Which function is the entry point of a C program?

    • main() (correct)
    • start()
    • begin()
    • run()
  3. Which header file is needed for printf()?

    • stdlib.h
    • stdio.h (correct)
    • string.h
    • math.h
  4. Which format specifier prints an integer?

    • %f
    • %c
    • %s
    • %d (correct)
  5. Every statement in C ends with:

    • :
    • .
    • ; (correct)
    • ,
  6. What is the typical size of an int on most modern systems?

    • 2 bytes
    • 4 bytes (correct)
    • 8 bytes
    • 1 byte
  7. Which of these is NOT a loop in C?

    • for
    • while
    • foreach (correct)
    • do-while
  8. What does the & operator give in C?

    • Address of a variable (correct)
    • Value of a variable
    • Logical OR
    • Size of a variable
  9. Which is the correct way to declare an integer pointer?

    • int p*;
    • pointer int p;
    • int &p;
    • int *p; (correct)
  10. What is the output of printf("%d", 5/2);?

    • 2.5
    • 2 (correct)
    • 3
    • 2.0
  11. What does malloc() return?

    • int
    • char
    • void pointer (correct)
    • float
  12. At which index does a C array start?

    • 0 (correct)
    • 1
    • -1
    • Depends on the compiler
  13. Which function copies one string into another?

    • strcmp()
    • strlen()
    • strcat()
    • strcpy() (correct)
  14. How many bytes does char s[] = "hi"; occupy?

    • 2
    • 4
    • 3 (correct)
    • 1
  15. What does free() do?

    • Allocates memory
    • Releases dynamically allocated memory (correct)
    • Closes a file
    • Clears the screen
  16. What is a dangling pointer?

    • A pointer that is always NULL
    • A pointer to an integer array
    • A constant pointer
    • A pointer to memory that has been freed or is invalid (correct)
  17. What does the static keyword do for a local variable?

    • Makes it global
    • Retains its value between function calls (correct)
    • Makes it constant
    • Stores it in a register
  18. What usually causes a segmentation fault?

    • Missing semicolon
    • Using too many variables
    • Accessing invalid memory (correct)
    • Using printf()
  19. Memory allocated with malloc() comes from the:

    • Heap (correct)
    • Stack
    • Register
    • Code segment
  20. What is sizeof(char) in C?

    • 2
    • 4
    • 1 (correct)
    • Implementation defined
  21. Which format specifier prints a float with printf?

    • %d
    • %c
    • %f (correct)
    • %s
  22. Which format specifier prints a single character?

    • %ch
    • %d
    • %c (correct)
    • %s
  23. Which format specifier prints a string?

    • %d
    • %s (correct)
    • %str
    • %c
  24. Which function reads formatted input from the keyboard?

    • input()
    • printf()
    • scanf() (correct)
    • gets_int()
  25. Why do you write &x in scanf("%d", &x);?

    • It is optional decoration
    • To make x a pointer type
    • To print x
    • scanf needs the address of x to store the value (correct)
  26. Which keyword is used to make a decision in C?

    • case only
    • when
    • if (correct)
    • decide
  27. Which loop checks its condition after running the body?

    • while
    • do-while (correct)
    • for
    • foreach
  28. Which keyword exits a switch case or loop?

    • break (correct)
    • return only
    • stop
    • exit
  29. What is the return type of main in a standard C program?

    • int (correct)
    • void only
    • char
    • float
  30. What does return 0; at the end of main usually mean?

    • The program failed
    • The program restarts
    • Nothing is returned
    • The program finished successfully (correct)
  31. How do you write a multi-line comment in C?

    • <!-- comment -->
    • // comment //
    • # comment #
    • /* comment */ (correct)
  32. Which data type stores decimal numbers with double precision?

    • long
    • int
    • float only
    • double (correct)
  33. Which operator gets the value stored at a pointer's address?

    • * (correct)
    • &
    • ->
    • .
  34. How do you declare an array of 10 integers?

    • int a[10]; (correct)
    • int[10] a;
    • int a(10);
    • array int a = 10;
  35. What character marks the end of a C string?

    • '\n'
    • ' '
    • '\0' (the null character) (correct)
    • '.'
  36. Which header file is needed for strlen()?

    • <stdlib.h>
    • <string.h> (correct)
    • <stdio.h>
    • <math.h>
  37. Which header file is needed for malloc()?

    • <stdlib.h> (correct)
    • <memory.h> only
    • <string.h>
    • <stdio.h>
  38. What does the ++ operator do?

    • Increases a variable by 1 (correct)
    • Doubles a variable
    • Prints a variable
    • Adds two variables
  39. Which statement selects one of many branches based on an integer value?

    • goto
    • select
    • if-else only
    • switch (correct)
  40. What is the value of 7 % 3 in C?

    • 1 (correct)
    • 2.33
    • 0
    • 2
  41. What is the difference between i++ and ++i?

    • i++ uses the old value then increments; ++i increments first (correct)
    • i++ increments by 2
    • ++i is invalid in C
    • They are always different in value after the statement
  42. What does sizeof(int) typically return on a 64-bit Linux system?

    • 8
    • 16
    • 4 (correct)
    • 2
  43. What does the -> operator do?

    • Assigns a pointer
    • Accesses a struct member through a pointer (correct)
    • Compares pointers
    • Points to the next line
  44. What is a struct in C?

    • A function
    • A pointer type
    • A user-defined type grouping variables of different types (correct)
    • A loop
  45. What is the difference between a struct and a union?

    • All union members share the same memory; struct members each have their own (correct)
    • A union cannot hold integers
    • They are identical
    • A struct shares memory
  46. What does the typedef keyword do?

    • Converts types
    • Checks a type at runtime
    • Creates a new name for an existing type (correct)
    • Defines a new variable
  47. What does an enum define?

    • A pointer type
    • A dynamic array
    • A set of named integer constants (correct)
    • A list of strings
  48. What does #define PI 3.14 do?

    • Includes a file
    • Declares a variable PI
    • Defines a function
    • Defines a macro replaced by the preprocessor before compiling (correct)
  49. What does #include do?

    • Defines a macro
    • Links a library at runtime
    • Inserts the contents of another file before compilation (correct)
    • Starts the main function
  50. What does calloc() do differently from malloc()?

    • It is faster always
    • It allocates on the stack
    • It frees memory
    • It sets the allocated memory to zero (correct)
  51. What does realloc() do?

    • Frees memory
    • Allocates a string
    • Changes the size of previously allocated memory (correct)
    • Copies a file
  52. What is a memory leak?

    • Reading uninitialized memory
    • Allocated memory that is never freed (correct)
    • Memory that is too slow
    • A buffer that is too small
  53. What does strcmp(a, b) return when the strings are equal?

    • true
    • 1
    • -1
    • 0 (correct)
  54. Which function joins two strings?

    • strcpy()
    • strcat() (correct)
    • concat()
    • strjoin()
  55. What is a function prototype?

    • A function with no body ever
    • A macro
    • A declaration of a function's name, return type and parameters before its use (correct)
    • The first function in a file
  56. How are arrays passed to functions in C?

    • By value only
    • As a pointer to their first element (correct)
    • As a full copy
    • They cannot be passed
  57. What does a pointer to a pointer (int **p) store?

    • A function
    • Two integers
    • An array of strings only
    • The address of another pointer (correct)
  58. What is the result of 5 / 2.0 in C?

    • 2.0 as an int
    • 2
    • 2.5 (correct)
    • 3
  59. What does the const keyword do in const int x = 5;?

    • Prevents x from being modified (correct)
    • Makes x global
    • Makes x static
    • Makes x a macro
  60. What does the extern keyword mean?

    • The variable is on the heap
    • The variable is external memory
    • The variable is defined in another file (correct)
    • The variable is constant
  61. What is recursion in C?

    • A macro expanding itself
    • A function calling itself (correct)
    • A loop with goto
    • A struct inside a struct
  62. Which function opens a file in C?

    • fileopen()
    • open_file()
    • fopen() (correct)
    • readfile()
  63. Which function writes formatted output to a file?

    • fwrite_text()
    • fprintf() (correct)
    • printf()
    • fputs_format()
  64. Which mode string opens a file for reading in C?

    • "w"
    • "read"
    • "r" (correct)
    • "a"
  65. What does fclose() do?

    • Deletes a file
    • Reads a file
    • Opens a file
    • Closes a file and flushes its buffer (correct)
  66. What is a function pointer?

    • A variable that stores the address of a function (correct)
    • A pointer to the main function only
    • A macro
    • A pointer returned by a function
  67. What is pointer arithmetic for p + 1 when p is an int*?

    • It is not allowed
    • It advances by sizeof(int) bytes (correct)
    • It advances by 1 byte
    • It adds 1 to the value
  68. What does the volatile keyword tell the compiler?

    • The value may change unexpectedly, so do not optimize away reads (correct)
    • The variable is thread-safe
    • The value is constant
    • The variable is on the stack
  69. What is a buffer overflow?

    • Too many open files
    • Using too many functions
    • Writing past the end of an array's allocated memory (correct)
    • A full disk
  70. Why is gets() dangerous and removed from modern C?

    • It cannot limit input length and causes buffer overflows (correct)
    • It closes stdin
    • It only reads numbers
    • It is too slow
  71. What is the difference between an array name and a pointer?

    • A pointer cannot be indexed
    • An array name can be reassigned
    • An array name is not a modifiable variable and sizeof gives the full array size (correct)
    • They are exactly the same
  72. What does the restrict keyword promise?

    • The pointer is read-only
    • The pointer is aligned
    • The pointer cannot be null
    • The pointer is the only way to access that memory in its scope (correct)
  73. What are bit fields in a struct?

    • Boolean pointers
    • Bitwise operators
    • Members with a specified number of bits (correct)
    • Arrays of bits
  74. What is the output of printf("%d", 1 << 3);?

    • 4
    • 13
    • 8 (correct)
    • 3
  75. What does the expression x & 1 check?

    • Whether x is a power of two
    • Whether x is odd (correct)
    • Whether x is negative
    • Whether x is zero
  76. What does the static keyword do on a global function?

    • Makes it run once
    • Makes it inline
    • Limits its visibility to the current file (correct)
    • Keeps its value between calls
  77. What happens when you return a pointer to a local variable?

    • The pointer becomes dangling after the function returns (correct)
    • The variable is copied to the heap
    • It is safe
    • A compile error always occurs
  78. What is the purpose of the preprocessor guard #ifndef HEADER_H / #define HEADER_H?

    • To define a function
    • To speed up compilation always
    • To prevent a header from being included twice (correct)
    • To check for errors
  79. What is the value of sizeof(arr) / sizeof(arr[0]) for int arr[8];?

    • 8 (correct)
    • 1
    • 4
    • 32
  80. What does void* represent?

    • A null pointer
    • A function pointer
    • A generic pointer to data of any type (correct)
    • A pointer to nothing that cannot be used