C - Basics Practice Test.1

  1. What is the primary purpose of the #include statement in C?

    A) Define a new function

    B) Declare a variable

    C) Include a header file

    D) Initialize a loop

    Answer: C) Include a header file

  2. Which preprocessor directive is used to include a standard C library header file?

    A) #import

    B) #include

    C) #define

    D) #ifdef

    Answer: B) #include

  3. In C programming, which symbol is used to enclose the header file name in a #include statement?

    A) < >

    B) " "

    C) { }

    D) ( )

    Answer: A) < >

  4. What does the #include statement do when it includes a header file in a C program?

    A) Inserts the content of the header file into the source code

    B) Declares a new function

    C) Generates machine code

    D) Comments out a block of code

    Answer: A) Inserts the content of the header file into the source code

  5. Which of the following is a valid #include statement to include a user-defined header file named "myheader.h"?

    A) #include <myheader.h>

    B) #include 'myheader.h'

    C) #include "myheader.h"

    D) #include <myheader.c>

    Answer: C) #include "myheader.h"

  6. What is the role of the C preprocessor in processing #include statements?

    A) It compiles the included file separately

    B) It resolves macros within the included file

    C) It checks for syntax errors in the included file

    D) It excludes the included file from compilation

    Answer: B) It resolves macros within the included file

  7. When including a header file, which option is typically used to specify the location of the file?

    A) -L

    B) -I

    C) -H

    D) -X

    Answer: B) -I

  8. Which of the following statements is true regarding the order of #include statements in a C program?

    A) The order doesn't matter

    B) They must be included after the main function

    C) They must be included before any variable declarations

    D) They must be included before any other preprocessor directives

    Answer: A) The order doesn't matter

  9. What happens if you include the same header file multiple times in a C program?

    A) It leads to a compilation error

    B) Only the first inclusion is considered, and subsequent inclusions are ignored

    C) Each inclusion creates a separate copy of the file

    D) The compiler automatically reorders the inclusions for optimization.

    Answer: B) Only the first inclusion is considered, and subsequent inclusions are ignored.

  10. Which of the following is an example of a standard C library header file?

    A) math.h

    B) program.c

    C) myfunctions.h

    D) main.c

    Answer: A) math.h

  11. How do you display text on the screen in C?

    A) print()

    B) display()

    C) printf()

    D) show()

    Answer: C) printf()

  12. What is the escape sequence used to print a newline character in C?

    A) \n

    B) \t

    C) \r

    D) \b

    Answer: A) \n

  13. Which function is used to display a single character in C?

    A) putc()

    B) putchar()

    C) printc()

    D) displaychar()

    Answer: B) putchar()

  14. To display the value of an integer variable `num`, which format specifier should be used in printf?

    A) %s

    B) %c

    C) %d

    D) %f

    Answer: C) %d

  15. What is the purpose of the escape sequence `\t` in printf?

    A) Prints a tab character

    B) Prints a backspace character

    C) Prints a carriage return

    D) Prints a double quote character

    Answer: A) Prints a tab character

  16. Which library should be included to use the printf function in C?

    A) stdlib.h

    B) math.h

    C) conio.h

    D) stdio.h

    Answer: D) stdio.h

  17. How do you print a floating-point number with two decimal places in C?

    A) %f

    B) %lf

    C) %.2f

    D) %.2lf

    Answer: C) %.2f

  18. What is the purpose of the `%c` format specifier in printf?

    A) Used for printing integers

    B) Used for printing floating-point numbers

    C) Used for printing characters

    D) Used for printing strings

    Answer: C) Used for printing characters

  19. To display the address of a variable `var`, which format specifier should be used in printf?

    A) %d

    B) %p

    C) %a

    D) %x

    Answer: B) %p

  20. What will be the output of the following code?

    
    int x = 10;
    printf("The value of x is %d\n", x);
                

    A) The value of x is 10

    B) The value of x is %d

    C) The value of x is 0

    D) There will be a compilation error

    Answer: A) The value of x is 10

  21. How do you read a character from the terminal in C?

    A) `getch()`

    B) `read_char()`

    C) `scanf("%c", &ch)`

    D) `getchar()`

    Answer: D) `getchar()`

  22. Which library should be included to use the `scanf` function for reading input in C?

    A) `stdlib.h`

    B) `stdio.h`

    C) `conio.h`

    D) `math.h`

    Answer: B) `stdio.h`

  23. To read an integer from the terminal in C, which format specifier should be used with `scanf`?

    A) `%s`

    B) `%c`

    C) `%d`

    D) `%f`

    Answer: C) `%d`

  24. What is the purpose of the `fgets` function in C for reading input?

    A) Reads a single character

    B) Reads a line of text

    C) Reads a floating-point number

    D) Reads an integer

    Answer: B) Reads a line of text

  25. How do you read a string with spaces in C using `scanf`?

    A) You cannot read a string with spaces using `scanf`

    B) Use `%s` format specifier

    C) Use `%c` format specifier

    D) Use `%S` format specifier

    Answer: A) You cannot read a string with spaces using `scanf`

  26. What function is commonly used to read an entire line of text from the terminal in C?

    A) `getline()`

    B) `readline()`

    C) `getstring()`

    D) `gets()`

    Answer: A) `getline()`

  27. How do you read a floating-point number from the terminal in C using `scanf`?

    A) `%s`

    B) `%c`

    C) `%d`

    D) `%f`

    Answer: D) `%f`

  28. Which function is used to read a single character without waiting for the user to press Enter in C?

    A) `getchar()`

    B) `getch()`

    C) `read_char()`

    D) `scanf("%c", &ch)`

    Answer: B) `getch()`

  29. What is the purpose of the `getchar` function in C?

    A) Reads a string

    B) Reads a line of text

    C) Reads a single character

    D) Reads an integer

    Answer: C) Reads a single character

  30. How do you read a formatted line of text from the terminal in C?

    A) `scanf("%s", str)`

    B) `gets(str)`

    C) `fgets(str, size, stdin)`

    D) `readline(str)`

    Answer: C) `fgets(str, size, stdin)`

  31. What is the primary purpose of data types in C?

    A) To specify the memory size of variables

    B) To define the order of execution of statements

    C) To control the flow of program execution

    D) To determine the number of loops in a program

    Answer: A) To specify the memory size of variables

  32. Which of the following is not a valid C data type?

    A) int

    B) char

    C) real

    D) float

    Answer: C) real

  33. What is the size (in bytes) of the `char` data type in C?

    A) 1

    B) 2

    C) 4

    D) 8

    Answer: A) 1

  34. Which data type is used to represent whole numbers in C?

    A) int

    B) float

    C) double

    D) char

    Answer: A) int

  35. Which data type is used to store characters in C?

    A) char

    B) int

    C) double

    D) float

    Answer: A) char

  36. What is the size (in bytes) of the `float` data type in C?

    A) 2

    B) 4

    C) 8

    D) 16

    Answer: B) 4

  37. Which data type is typically used for storing single-precision floating-point numbers in C?

    A) float

    B) double

    C) long double

    D) real

    Answer: A) float

  38. What is the size (in bytes) of the `double` data type in C?

    A) 2

    B) 4

    C) 8

    D) 16

    Answer: C) 8

  39. Which data type is used for storing boolean values in C?

    A) bool

    B) int

    C) char

    D) boolean

    Answer: C) char

  40. What is the size (in bytes) of the `long long` data type in C?

    A) 2

    B) 4

    C) 8

    D) 16

    Answer: C) 8

  41. What is a variable in C?

    A) A constant value

    B) A reserved keyword

    C) A storage location with a name in which data can be stored and manipulated

    D) A data type

    Answer: C) A storage location with a name in which data can be stored and manipulated

  42. Which of the following is a valid variable name in C?

    A) 123variable

    B) _myVariable

    C) float

    D) #myVariable

    Answer: B) _myVariable

  43. What is the purpose of declaring a variable in C?

    A) To initialize it

    B) To assign a value to it

    C) To allocate memory for it

    D) To create a constant

    Answer: C) To allocate memory for it

  44. Which keyword is used to declare a variable in C?

    A) var

    B) int

    C) variable

    D) declare

    Answer: B) int

  45. What does it mean to "initialize" a variable in C?

    A) To give it a name

    B) To declare it

    C) To assign an initial value to it

    D) To allocate memory for it

    Answer: C) To assign an initial value to it

  46. What is the data type of a variable that can store whole numbers in C?

    A) integer

    B) float

    C) double

    D) char

    Answer: A) integer

  47. Which of the following is a correct way to declare and initialize an integer variable with the value 42 in C?

    A) int x = 42

    B) int x = "42"

    C) integer x = 42

    D) x = 42

    Answer: A) int x = 42

  48. What is the scope of a local variable in C?

    A) Limited to the function in which it is declared

    B) Accessible from any function in the program

    C) Limited to the block of code in which it is declared

    D) Accessible from any file in the program

    Answer: C) Limited to the block of code in which it is declared

  49. Which of the following data types is used for storing characters in C?

    A) character

    B) str

    C) string

    D) char

    Answer: D) char

  50. How do you declare a constant variable in C?

    A) Using the `var` keyword

    B) Using the `constant` keyword

    C) Using the `const` keyword

    D) Constants cannot be declared in C

    Answer: C) Using the `const` keyword

  51. Which data type is used for storing decimal numbers with single precision in C?

    A) float

    B) double

    C) long double

    D) real

    Answer: A) float

  52. What is the size (in bytes) of the `double` data type in C?

    A) 2

    B) 4

    C) 8

    D) 16

    Answer: C) 8

  53. What is the maximum number of characters that a variable name can have in C?

    A) 32

    B) 64

    C) 128

    D) There is no fixed limit

    Answer: D) There is no fixed limit

  54. Which of the following is not a valid data type in C?

    A) long long

    B) real

    C) short

    D) char

    Answer: B) real

  55. How do you declare a global variable in C?

    A) Prefix it with `static`

    B) Prefix it with `global`

    C) Declare it outside of any function

    D) Use the `global` keyword

    Answer: C) Declare it outside of any function

  56. What happens if you declare a variable without initializing it in C?

    A) The program will not compile

    B) The variable will be initialized to 0 by default

    C) The variable will contain a random value

    D) It is not possible to declare a variable without initializing it

    Answer: C) The variable will contain a random value

  57. Which of the following is not a valid way to comment a line in C?

    A) // This is a comment

    B) /* This is a comment */

    C) -- This is a comment

    D) # This is a comment

    Answer: C) -- This is a comment

  58. What is the purpose of a data type in C?

    A) To specify the number of decimal places in a number

    B) To specify the size and type of data that can be stored in a variable

    C) To specify the number of significant digits in a number

    D) To specify the precision of a floating-point number

    Answer: B) To specify the size and type of data that can be stored in a variable

  59. In C, which data type is used to represent true or false values?

    A) boolean

    B) bool

    C) char

    D) int

    Answer: C) char

  60. What is the size (in bytes) of the `long long` data type in C?

    A) 2

    B) 4

    C) 8

    D) 16

    Answer: C) 8