0% found this document useful (0 votes)
12 views23 pages

Quiz For C

The document contains a series of questions and answers related to the C programming language, covering topics such as variable declaration, data types, control structures, and functions. Each question is followed by multiple-choice options, with the correct answer indicated. The content serves as a quiz or review material for individuals learning or testing their knowledge of C programming.

Uploaded by

hemitrana2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Quiz For C

The document contains a series of questions and answers related to the C programming language, covering topics such as variable declaration, data types, control structures, and functions. Each question is followed by multiple-choice options, with the correct answer indicated. The content serves as a quiz or review material for individuals learning or testing their knowledge of C programming.

Uploaded by

hemitrana2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1. What is the correct syntax to declare a variable of type int in C?

● A) int a;
● B) int: a;
● C) int = a;
● D) a int;

Answer: A) int a;

2. What is the size of an int data type in C (assuming 32-bit architecture)?


● A) 2 bytes
● B) 4 bytes
● C) 8 bytes
● D) Depends on the compiler

Answer: B) 4 bytes

3. Which of the following is a valid comment in C?


● A) /* This is a comment */
● B) // This is a comment
● C) Both A and B
● D) None of the above

Answer: C) Both A and B

4. What is the output of the following C code?


int main() {
int x = 5;
printf("%d", x++);
return 0;
}

● A) 5
● B) 6
● C) Undefined
● D) Compilation error

Answer: A) 5

5. Which of the following is used to take input from the user in C?


● A) input()
● B) read()
● C) scanf()
● D) get()

Answer: C) scanf()

6. What is the value of x after the following code executes?


int x = 10;
x = x * 2 + 5;

● A) 15
● B) 20
● C) 25
● D) 30

Answer: C) 25

7. What is the purpose of the return 0; statement in the main function?


● A) It returns an integer value to the compiler
● B) It indicates that the program has ended successfully
● C) It returns the value 0 to the user
● D) It is used for debugging purposes

Answer: B) It indicates that the program has ended successfully

8. Which of the following is not a valid identifier in C?


● A) my_variable
● B) 1st_variable
● C) _var123
● D) varName

Answer: B) 1st_variable

9. What does the sizeof() operator do in C?


● A) It returns the number of elements in an array
● B) It returns the size of a data type or variable in bytes
● C) It checks the memory allocation of a variable
● D) It calculates the length of a string

Answer: B) It returns the size of a data type or variable in bytes

10. Which loop is guaranteed to execute at least once in C?


● A) for loop
● B) while loop
● C) do-while loop
● D) None of the above

Answer: C) do-while loop

11. What is the correct syntax to declare a constant in C?


● A) int const a = 10;
● B) constant int a = 10;
● C) const int a = 10;
● D) Both A and C

Answer: D) Both A and C

12. Which of the following functions is used to allocate memory dynamically in C?


● A) alloc()
● B) malloc()
● C) memalloc()
● D) new()

Answer: B) malloc()

13. What is the output of the following code?


int a = 10, b = 10;
if (b >= a)
printf("B is greater");
else
printf("A is greater");

● A) A is greater
● B) B is greater
● C) Compilation error
● D) No output

Answer: B) B is greater

14. Which of the following is used to terminate a loop prematurely in C?


● A) continue
● B) exit
● C) break
● D) return

Answer: C) break
15. Which of the following is the correct format specifier for printing a floating-point
number in C?
● A) %d
● B) %f
● C) %c
● D) %lf

Answer: B) %f

16. What is the output of the following code?


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

● A) 0
● B) 1
● C) 5
● D) Compilation error

Answer: B) 1

17. Which of the following is the correct syntax to use a pointer in C?


● A) int* ptr;
● B) int ptr*;
● C) int &ptr;
● D) int ptr@;

Answer: A) int* ptr;

18. What is the purpose of the #include directive in C?


● A) It is used to include standard libraries or other files
● B) It defines a constant value
● C) It comments out code
● D) It starts the main function

Answer: A) It is used to include standard libraries or other files

19. What is the output of the following code?


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

● A) 5
● B) 10
● C) 15
● D) 20

Answer: C) 15

20. Which of the following loops would execute indefinitely?


● A) for (int i = 0; i < 10; i++)
● B) while (1)
● C) do { } while (0)
● D) for (int i = 0; i > -1; i--)

Answer: B) while (1)

21. Which of the following is used to print a single character in C?


● A) printf("%s", ch);
● B) printf("%d", ch);
● C) printf("%c", ch);
● D) printf("%f", ch);

Answer: C) printf("%c", ch);

22. What is the correct syntax for a for loop in C?


● A) for (i = 0; i < 10; i++)
● B) for (i = 0; i < 10; i--)
● C) for i = 0; i < 10; i++
● D) for (int i = 0, i < 10, i++)

Answer: A) for (i = 0; i < 10; i++)

23. Which of the following is the correct way to declare a function in C?


● A) void func(int a);
● B) function void(int a);
● C) void function(int a);
● D) func(void int a);

Answer: A) void func(int a);


24. What is the output of the following code?
int a = 10;
if (a == 10)
printf("Equal");
else
printf("Not Equal");

● A) Equal
● B) Not Equal
● C) Compilation error
● D) No output

Answer: A) Equal

25. What is the function of return in a C program?


● A) It ends the function and returns control to the calling function
● B) It declares a function's return type
● C) It pauses the program execution
● D) It prints a value

Answer: A) It ends the function and returns control to the calling function

26. Which of the following statements is used to skip the current iteration of a loop?
● A) break
● B) continue
● C) goto
● D) return

Answer: B) continue

27. Which of the following is the correct way to declare an array in C?


● A) int arr[] = {1, 2, 3};
● B) int arr = {1, 2, 3};
● C) int arr[] = (1, 2, 3);
● D) int arr = (1, 2, 3);

Answer: A) int arr[] = {1, 2, 3};

28. What will be the value of b after executing the following code?
int a = 5;
int b = ++a;
● A) 5
● B) 6
● C) 7
● D) Compilation error

Answer: B) 6

29. Which of the following is the correct way to read a string in C?


● A) scanf("%s", str);
● B) scanf("%c", str);
● C) scanf("%f", str);
● D) scanf("%d", str);

Answer: A) scanf("%s", str);

30. What is the result of the following expression?


int result = 10 / 3;
printf("%d", result);

● A) 3.33
● B) 3
● C) 10
● D) Compilation error

Answer: B) 3

31. What is the default value of a local variable in C?


● A) 0
● B) NULL
● C) Garbage value
● D) -1

Answer: C) Garbage value

32. Which of the following is used to define a macro in C?


● A) #macro
● B) #define
● C) macro()
● D) defmacro

Answer: B) #define
33. What is the output of the following code?
int a = 5;
int b = a * 2;
printf("%d", b);

● A) 10
● B) 5
● C) 15
● D) Compilation error

Answer: A) 10

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


● A) int
● B) char
● C) float
● D) real

Answer: D) real

35. What does the fopen() function return if it fails to open a file?
● A) NULL
● B) 0
● C) 1
● D) The error code

Answer: A) NULL

36. Which function is used to close a file in C?


● A) close()
● B) fclose()
● C) fileclose()
● D) closefile()

Answer: B) fclose()

37. What is the result of the following expression?


int a = 5, b = 3;
printf("%d", a % b);
● A) 2
● B) 1
● C) 5
● D) Compilation error

Answer: A) 2

38. Which of the following correctly declares a pointer in C?


● A) int ptr;
● B) int *ptr;
● C) int &ptr;
● D) int ^ptr;

Answer: B) int *ptr;

39. What is the output of the following code?


char ch = 'A';
printf("%d", ch);

● A) 65
● B) 97
● C) A
● D) Compilation error

Answer: A) 65

40. Which of the following statements is true about arrays in C?


● A) Arrays are dynamically allocated
● B) Arrays can have varying data types
● C) Arrays can store only one type of data
● D) The size of an array can change during program execution

Answer: C) Arrays can store only one type of data

41. What is the output of the following code?


int arr[] = {1, 2, 3, 4};
printf("%d", arr[2]);

● A) 1
● B) 2
● C) 3
● D) 4
Answer: C) 3

42. Which of the following cannot be a pointer type in C?


● A) int*
● B) char*
● C) void*
● D) float[]

Answer: D) float[]

43. What is the output of the following code?


int a = 5;
printf("%d", a == 5);

● A) 0
● B) 1
● C) 5
● D) Compilation error

Answer: B) 1

44. Which keyword is used to prevent a variable from being modified in C?


● A) const
● B) static
● C) volatile
● D) readonly

Answer: A) const

45. What will the following code return?


int a = 10;
int b = 3;
printf("%f", (float)a / b);

● A) 3
● B) 3.33
● C) 3.000000
● D) Compilation error

Answer: C) 3.333333
46. What is the output of this code?
int a = 10;
int b = 5;
if (a < b) {
printf("A is smaller");
} else {
printf("B is smaller");
}

● A) A is smaller
● B) B is smaller
● C) Compilation error
● D) No output

Answer: B) B is smaller

47. What is the return type of the main() function in C?


● A) int
● B) void
● C) char
● D) float

Answer: A) int

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


int x = 1;
switch(x) {
case 1: printf("One ");
case 2: printf("Two ");
case 3: printf("Three ");
default: printf("Default ");
}

● A) One
● B) One Two
● C) One Two Three
● D) One Two Three Default

Answer: D) One Two Three Default


49. What is the correct way to declare a multidimensional array in C?
● A) int arr[][] = {{1,2},{3,4}};
● B) int arr[2][2] = {{1,2},{3,4}};
● C) int arr[2][] = {{1,2},{3,4}};
● D) int arr[2,2] = {{1,2},{3,4}};

Answer: B) int arr[2][2] = {{1,2},{3,4}};

50. Which of the following correctly initializes an integer pointer to null?


● A) int *ptr = NULL;
● B) int ptr = 0;
● C) int *ptr = null;
● D) int *ptr = 0;

Answer: A) int *ptr = NULL;

51. What is the output of the following code?


int a = 8;
int b = 4;
printf("%d", a / b);

● A) 2
● B) 4
● C) 0
● D) Compilation error

Answer: A) 2

52. Which of the following operators is used for bitwise AND in C?


● A) &&
● B) ||
● C) &
● D) and

Answer: C) &

53. What is the maximum value that can be stored in an unsigned char variable?
● A) 127
● B) 128
● C) 255
● D) 256
Answer: C) 255

54. What does the sizeof() operator do in C?


● A) It returns the size of a variable in bits
● B) It returns the size of a variable in bytes
● C) It returns the memory address of a variable
● D) It determines the type of a variable

Answer: B) It returns the size of a variable in bytes

55. Which of the following statements about recursion is correct?


● A) A recursive function must call another function
● B) A recursive function calls itself
● C) Recursion is not allowed in C
● D) Recursion always leads to infinite loops

Answer: B) A recursive function calls itself

56. What is the output of the following code?


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

● A) 0
● B) 1
● C) 3
● D) Compilation error

Answer: A) 0

57. Which of the following is not a loop structure in C?


● A) for
● B) while
● C) do-while
● D) repeat-until

Answer: D) repeat-until

58. What is the correct way to declare a function that returns a pointer in C?
● A) int* function();
● B) *int function();
● C) int function*();
● D) function int*();
Answer: A) int* function();

59. Which of the following is not a storage class in C?


● A) auto
● B) register
● C) static
● D) global

Answer: D) global

60. What will the output be for the following code?


int a = 1, b = 1;
printf("%d", a++ + ++b);

● A) 2
● B) 3
● C) 4
● D) 5

Answer: C) 4

61. Which keyword is used to declare a structure in C?


● A) union
● B) struct
● C) class
● D) typedef

Answer: B) struct

62. What is the correct syntax to declare a pointer to a function?


● A) int (*ptr)();
● B) int *ptr();
● C) int ptr*();
● D) ptr int*();

Answer: A) int (*ptr)();

63. Which function is used to write data to a file in C?


● A) fread()
● B) fwrite()
● C) fprintf()
● D) fput()

Answer: B) fwrite()

64. Which of the following is the correct syntax to pass an array as a parameter to a
function in C?
● A) func(int arr[]);
● B) func(int arr);
● C) func(int *arr[]);
● D) func(int &arr[]);

Answer: A) func(int arr[]);

65. What is the correct way to compare two strings in C?


● A) if (str1 == str2)
● B) if (strcmp(str1, str2) == 0)
● C) if (strcomp(str1, str2) == 0)
● D) if (str1.compare(str2) == 0)

Answer: B) if (strcmp(str1, str2) == 0)

66. Which of the following is a valid declaration of a 3-dimensional array?


● A) int arr[2][3][4];
● B) int arr[2][4];
● C) int arr[2][][4];
● D) int arr[][3][4];

Answer: A) int arr[2][3][4];

67. What is the output of the following code?


int arr[] = {10, 20, 30, 40, 50};
printf("%d", *(arr + 2));

● A) 10
● B) 20
● C) 30
● D) 50

Answer: C) 30
68. Which of the following correctly declares a variable to hold the address of a
pointer to an integer?
● A) int *ptr;
● B) int **ptr;
● C) int &ptr;
● D) int ptr;

Answer: B) int **ptr;

69. Which of the following is true about do-while loops in C?


● A) The loop will always execute at least once
● B) The loop will only execute if the condition is true
● C) The loop may never execute
● D) The loop does not require a condition

Answer: A) The loop will always execute at least once

70. Which of the following operators is used to access members of a structure using a
pointer?
● A) .
● B) ->
● C) *
● D) &

Answer: B) ->

71. What is the output of the following code?


int a = 10, b = 15;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);

● A) 10 15
● B) 15 10
● C) 0 0
● D) Compilation error

Answer: B) 15 10

72. Which function is used to dynamically allocate memory in C?


● A) alloc()
● B) malloc()
● C) calloc()
● D) realloc()

Answer: B) malloc()

73. Which of the following is true about sizeof operator in C?


● A) It can be used with data types only
● B) It can be used with both data types and variables
● C) It can be used with functions
● D) It always returns the value in bits

Answer: B) It can be used with both data types and variables

74. What is the output of the following code?


char str[] = "Hello";
printf("%c", *(str + 1));

● A) H
● B) e
● C) l
● D) Compilation error

Answer: B) e

75. Which of the following is a valid declaration of a pointer to an array of 10 integers?


● A) int *ptr[10];
● B) int (*ptr)[10];
● C) int **ptr[10];
● D) int ptr[10];

Answer: B) int (*ptr)[10];

76. What is the difference between malloc() and calloc() in C?


● A) malloc() allocates memory, calloc() does not
● B) calloc() initializes the allocated memory to zero, malloc() does not
● C) malloc() returns a void pointer, calloc() returns an integer pointer
● D) There is no difference

Answer: B) calloc() initializes the allocated memory to zero, malloc() does not
77. What will the following code output?
int x = 0;
if(x) {
printf("True");
} else {
printf("False");
}

● A) True
● B) False
● C) 0
● D) Compilation error

Answer: B) False

78. Which of the following is used to read a string input in C?


● A) scanf("%d", &str);
● B) scanf("%s", str);
● C) printf("%s", &str);
● D) gets(str);

Answer: B) scanf("%s", str);

79. What is the output of this code?


int a = 5, b = 10;
printf("%d", a + b++);

● A) 15
● B) 16
● C) 14
● D) Compilation error

Answer: A) 15

80. Which of the following is not a valid operator in C?


● A) ?:
● B) sizeof
● C) typeof
● D) &

Answer: C) typeof
81. Which keyword is used to declare a constant variable in C?
● A) constant
● B) const
● C) immutable
● D) static

Answer: B) const

82. Which of the following is used to terminate a for loop prematurely?


● A) continue
● B) break
● C) exit
● D) return

Answer: B) break

83. What is the return type of the strcmp() function in C?


● A) int
● B) char
● C) float
● D) void

Answer: A) int

84. What is the output of the following code?


int arr[] = {1, 2, 3, 4};
int *p = arr;
printf("%d", *p + 1);

● A) 1
● B) 2
● C) 3
● D) Compilation error

Answer: B) 2

85. Which of the following best describes a pointer?


● A) A keyword used to create variables
● B) A variable that stores the address of another variable
● C) A variable that stores data
● D) A data type in C
Answer: B) A variable that stores the address of another variable

86. Which of the following is the correct syntax for accessing a member of a structure
using a pointer to the structure?
● A) ptr.member
● B) ptr->member
● C) (*ptr).member
● D) Both B and C

Answer: D) Both B and C

87. What is the output of the following code?


int a = 3, b = 5;
if (a == 3 || b == 3)
printf("Yes");
else
printf("No");

● A) Yes
● B) No
● C) 3
● D) Compilation error

Answer: A) Yes

88. Which of the following correctly initializes a multi-dimensional array in C?


● A) int arr[2][2] = {1, 2, 3, 4};
● B) int arr[2][] = {1, 2, 3, 4};
● C) int arr[2][2] = {{1, 2}, {3, 4}};
● D) int arr[2][2] = (1, 2, 3, 4);

Answer: C) int arr[2][2] = {{1, 2}, {3, 4}};

89. What is the output of the following code?


char str[] = "Hello, World!";
printf("%c", str[7]);

● A) W
● B) o
● C) ,
● D) Compilation error

Answer: A) W
90. Which function is used to allocate a block of memory in C and initialize it to zero?
● A) malloc()
● B) calloc()
● C) realloc()
● D) free()

Answer: B) calloc()

91. Which of the following formats is used to print an octal number in C?


● A) %d
● B) %f
● C) %o
● D) %x

Answer: C) %o

92. What is the output of this code?


int a = 10;
int b = 3;
printf("%d", a % b);

● A) 3
● B) 10
● C) 1
● D) 0

Answer: C) 1

93. Which of the following is a correct way to declare a pointer to a function in C?


● A) int *func();
● B) int (*funcPtr)();
● C) int &funcPtr();
● D) int funcPtr*();

Answer: B) int (*funcPtr)();

94. What will be the result of the following code?


int a = 3;
a = a << 1;
printf("%d", a);
● A) 3
● B) 4
● C) 6
● D) 8

Answer: C) 6

95. Which of the following correctly assigns the memory address of variable x to
pointer p?
● A) int x = 5, p = x;
● B) int x = 5, *p = x;
● C) int x = 5, *p = &x;
● D) int x = 5, &p = x;

Answer: C) int x = 5, *p = &x;

96. What is the size of a pointer variable in a 64-bit system?


● A) 2 bytes
● B) 4 bytes
● C) 8 bytes
● D) 16 bytes

Answer: C) 8 bytes

97. Which operator is used to access the value at the address stored in a pointer?
● A) *
● B) &
● C) @
● D) %

Answer: A) *

98. What is the output of the following code?


int x = 5;
int *ptr = &x;
*ptr = 10;
printf("%d", x);

● A) 5
● B) 10
● C) Compilation error
● D) Garbage value
Answer: B) 10

99. What will be the output of this code?


int a = 5, b = 2;
float result = a / b;
printf("%.2f", result);

● A) 2.00
● B) 2.50
● C) 2.75
● D) 3.00

Answer: A) 2.00

100. Which of the following is not a storage class in C?

A) auto
B) register
C) extern
D) public

Answer: D) `public`

You might also like