Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Multiple Choice Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

C MULTIPLE CHOICE QUESTIONS

C Programming Questions and Answers – Sizeof Keyword – 2

1. Which among the following is never possible in C when members are different in a structure and union?
//Let P be a structure
//Let Q be a union
a. sizeof(P) is greater than sizeof(Q)
b. sizeof(P) is less than sizeof(Q)
c. sizeof(P) is equal to sizeof(Q)
d. None of the mentioned
Answer:d
2. Which among the following is never possible in C when members in a structure are same as that in a
union?
//Let P be a structure
//Let Q be a union
a. sizeof(P) is greater than sizeof(Q)
b. sizeof(P) is equal to sizeof(Q)
c. sizeof(P) is less than to sizeof(Q)
d. None of the mentioned
Answer:c
3. What will be the size of the following structure?
1. #include <stdio.h>
2. struct temp
3. {
4. int a[10];
5. char p;
6. };
a. 5 b. 11 c. 41 d. 44
Answer:d
4. Comment on the output of following C program?
1. #include <stdio.h>
2. main()
3. {
4. int a = 1;
5. printf("size of a is %d, ", sizeof(++a));
6. printf("value of a is %d", a);
7. };
a. size of a is 4, value of a is 1
b. size of a is 4, value of a is 2
c. size of a is 2, value of a is 2
d. size of a is 2, value of a is 2
Answer:a
5. Which among the following is right?
a. sizeof(struct stemp*) > sizeof(union utemp*) > sizeof(char *)
b. sizeof(struct stemp*) < sizeof(union utemp*) < sizeof(char *)
c. sizeof(struct stemp*) = sizeof(union utemp*) = sizeof(char *)
d. The order Depends on the compiler
Answer:c
6. Comment on the following C code?
1. #include <stdio.h>
2. printf("%d", sizeof(strlen("HELLOWORLD")));
a. Output, 4
b. Output, 10
c. Output, 16
d. Error, sizeof cannot evaluate size of a function.
Answer:a
7. Which of the following cannot be used inside sizeof?
a. pointers
b. functions
c. macro definition
d. None of the mentioned
Answer:d
8. Comment on the following C code?
1. #include <stdio.h>
2. (sizeof double = 8, float = 4, void = 1)
3. #define PI 3.14
4. int main()
5. {
6. printf("%d", sizeof(PI));
7. }
a. Output is 8
b. Output is 4
c. Output is 1
d. Error, we can’t use sizeof on macro-definitions
Answer:a

C Programming Questions and Answers – Sizeof Keyword – 1

9. What is the sizeof(char) in a 32-bit C compiler?


a. 1 bit
b. 2 bits
c. 1 Byte
d. 2 Bytes
Answer:c
10. What is the output of this C code?
1. #include <stdio.h>
2. printf("%d", sizeof('a'));
a. 1
b. 2
c. 4
d. None of the mentioned
Answer:c
11. Size of an array can be evaluated by:
(Assuming array declaration int a[10];)
a. sizeof(a);
b. sizeof(*a);
c. sizeof(a[10]);
d. 10 * sizeof(a);
Answer:a
12. What is the output of this C code?
1. #include <stdio.h>
2. union temp
3. {
4. char a;
5. char b;
6. int c;
7. }t;
8. int main()
9. {
10. printf("%d", sizeof(t));
11. return 0;
12. }
a. 1
b. 2
c. 4
d. 6
Answer:c
13. Which of the following is not an operator in C?
a. ,
b. sizeof()
c. ~
d. None of the mentioned
Answer:d
14. Which among the following has the highest precedence?
a. &
b. <<
c. sizeof()
d. &&
Answer:c
15. The sizeof(void) in a 32-bit C is_____.
a. 0
b. 1
c. 2
d. 4
Answer:b
16. What type of value does sizeof return?
a. char
b. short
c. unsigned int
d. long
Answer:c

C Programming Questions and Answers – Float Datatype – 1

17. The number of digits present after decimal in float is________.


a. 1
b. 3
c. 6
d. 16
Answer:c
18. Which among the following is never possible as an output for float?
a. 3.666666
b. 3.666
c. 3
d. None of the mentioned
Answer:d
19. In a 32-bit compiler, which 2 types have same size?
a. char and short
b. short and int
c. int and float
d. float and double
Answer:c
20. What is the size of float in a 32-bit compiler?
a. 1
b. 2
c. 4
d. 8
Answer:c
21. Loss in precision occurs for typecasting from____________.
a. char to short
b. float to double
c. long to float
d. float to int
Answer:d
22. For union
1. union temp
2. {
3. char a;
4. int b;
5. float c;
6. };
The size is decided by:
a. char
b. int
c. float
d. Both (b) and (c)
Answer:d
23. %f access specifier is used for
a. Strings
b. Integral types
c. Floating type
d. All of the mentioned
Answer:c
24. Select the odd one out with respect to type?
a. char
b. int
c. long
d. float
Answer:d

C Programming Questions and Answers – Float Datatype – 2

25. What is the output of this C code?


1. #include <stdio.h>
2. printf("%.0f", 2.89);
a. 2.890000
b. 2.89
c. 2
d. 3
Answer:d
26. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. float a = 2.455555555555;
5. printf("%f", a);
6. }
a. 2.455555
b. 2.455556
c. 2.456
d. 2.46
Answer:a
27. Which of the following % operation is invalid?
a. 2 % 4;
b. 2 % 4l;
c. 2 % 4f;
d. Both (b) and (c)
Answer:c
28. Which data type is suitable for storing a number like?
10.0000000001
a. int
b. float
c. double
d. Both (b) and (c)
Answer:c
29. Modulus for float could be achieved by?
a. a % b
b. modulus(a, b);
c. fmod(a, b);
d. mod(a, b);
Answer:c
30. Predict the data type of the following mathematical operation?
2*9+3/2.0
a. int
b. long
c. float
d. double
Answer:d
31. %lf is used to display
a. float
b. long float
c. double
d. All of the mentioned
Answer:c

C Programming Questions and Answers – Random Number Generation – 1

32. The function srand(unsigned)


a) Sets the seed for rand
b) Doesn’t exist
c) Is an error
d) None of the mentioned
Answer:a
33. Which is the best way to generate numbers between 0 to 99?
a) rand()-100
b) rand()%100
c) rand(100)
d) srand(100)
Answer:b
34. The correct way to generate numbers between minimum and maximum(inclusive) is
_____________________.
a) minimum + (rand() % (maximum – minimum));
b) minimum + (rand() % (maximum – minimum + 1));
c) minimum * (rand() % (maximum – minimum))
d) minimum – (rand() % (maximum+minimum));
Answer:b
35. rand() and srand() functions are used
a) To find sqrt
b) For and operations
c) For or operations
d) To generate random numbers
Answer:d
36. What is the return type of rand() function?
a) short
b) int
c) long
d) double
Answer:b
37. Which of the following can be used for random number generation?
a) random()
b) rnd()
c) rndm()
d) None of the mentioned
Answer:a
38. Which of the following snippet will effectively generate random numbers?
a) rand();
b) rand(10);
c) rand(time(NULL));
d) All of the mentioned
Answer:a
39. Which among the following is correct function call for rand and random?
a) rand() and random();
b) rand() and random(1);
c) rand(1) and random(1);
d) rand(1) and random();
Answer:a
40. For the function call time(), what type of parameter is accepted?
a) int
b) int *
c) time_t
d) time_t *
Answer:d

C Programming Questions and Answers – Random Number Generation – 2

41. What is the output of this C code?


1. #include <stdio.h>
2. #include <stdlib.h>
3. int main()
4. {
5. printf("%d\n", rand() % 1000);
6. return 0;
7. }
a) Compile time error
b) An integer between 0-1000
c) An integer between 0-999 including 0 and 999.
d) An integer between 0-1000 including 1000
Answer:c
42. What is the output of this C code?
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main()
4. {
5. srand(9000);
6. printf("%d\n", rand());
7. return 0;
8. }
a) Compile time error
b) An integer in the range 0 to RAND_MAX.
c) A double in the range 0 to 1
d) A float in the range 0 to 1.
Answer:b
43. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. printf("%d\n", srand(9000));
5. return 0;
6. }
a) Compile time error
b) An integer in the range 0 to 9000
c) A float in the range 0 to 1
d) A double in the range 0 to 9000
Answer:a
44. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. srand(time(NULL));
5. printf("%d\n", rand());
6. return 0;
7. }
a) Compile time error
b) An integer in the range 0 to RAND_MAX.
c) A double in the range 0 to 1
d) A float in the range 0 to 1.
Answer:b
45. In the below program everytime program is run different numbers are generated.
1. #include <stdio.h>
2. #include <stdlib.h>
3. int main()
4. {
5. printf("%d\n", rand());
6. return 0;
7. }
a) true
b) false
c) Depends on the platform
d) Depends on the compiler
Answer:b
46. In the below program everytime program is run different numbers are generated.
1. #include <stdio.h>
2. int main()
3. {
4. srand(time(NULL));
5. printf("%d\n", rand());
6. return 0;
7. }
a) true
b) false
c) Depends on the platform
d) Depends on the compiler
Answer:a
47. Which of these is a correct way to generate numbers between 0 to 1(inclusive) randomly?
a) rand() / RAND_MAX
b) rand() % 2
c) rand(0, 1)
d) None of the mentioned
Answer:a

C Programming Questions and Answers – Mathematical Functions – 1

48. What is the output of this C code?


1. #include <stdio.h>
2. #include <math.h>
3. int main()
4. {
5. int i = 90;
6. printf("%f\n", sin(i));
7. return 0;
8. }
a) Compile time error
b) Undefined behaviour
c) 0.893997
d) 1.000000
Answer:a
49. What is the output of this C code?
1. #include <stdio.h>
2. #include <math.h>
3. int main()
4. {
5. unsigned int i = -1;
6. printf("%f\n", fabs(i));
7. return 0;
8. }
a) Compile time error
b) 1
c) -1
d) None of the mentioned
Answer:d
50. function fabs defined math.h header file takes argument of type integer.
a) true
b) false
c) Depends on the implementation
d) Depends on the standard
Answer:b
51. log(x) function defined in math.h header file is
a) Natural base logarithm
b) Logarithm to the base 2
c) Logarithm to the base 10
d) None of the mentioned
Answer:a
52. What is the output of this C code?
1. #include <stdio.h>
2. #include <math.h>
3. int main()
4. {
5. int i = 10;
6. printf("%f\n", log10(i));
7. return 0;
8. }
a) Compile time error
b) 1.000000
c) 2.302585
d) None of the mentioned
Answer:b
53. What type of inputs are accepted by mathematical functions?
a) short
b) int
c) float
d) double
Answer:d
54. In linux, apart from including math header file, the program is successfully executed by which of the
following?
a) cc filename.c
b) cc filename.c -lc
c) cc -math filename.c
d) cc -lm filename.c
Answer:d
55. Which of the following is not a valid mathematical function?
a) frexp(x);
b) atan2(x,y);
c) srand(x);
d) fmod(x);
Answer:d

C Programming Questions and Answers – Mathematical Functions – 2

56. Which of the following mathematical function requires 2 parameter for successful function call?
a) fmod();
b) div();
c) atan2();
d) All of the mentioned.
Answer:d
57. Which mathematical function among the following does NOT require int parameters?
a) div(x, y);
b) srand(x);
c) sqrt(x);
d) All of the mentioned.
Answer:c
58. sin(x) returns
a) sine of x where x is in radians
b) sine of x where x is in degree
c) cosine of x where x is in radians
d) cosine of x where x is in degree
Answer:a
59. cos(x) returns
a) sine of x where x is in radians
b) sine of x where x is in degree
c) cosine of x where x is in radians
d) cosine of x where x is in degree
Answer:c
60. What is the output of this C code?
1. #include <stdio.h>
2. #include <math.h>
3. void main()
4. {
5. int k = pow(2, 3);
6. printf("%d\n", k);
7. }
a) 9
b) 8
c) -1
d) 6
Answer:b
61. What is the output of this C code?
1. #include <stdio.h>
2. #include <math.h>
3. void main()
4. {
5. int k = fabs(-87);
6. printf("%d\n", k);
7. }
a) -87
b) 87
c) 78
d) error
Answer:b
62. What is the output of this C code?
1. #include <stdio.h>
2. #include <math.h>
3. void main()
4. {
5. int k = sqrt(-4);
6. printf("%d\n", k);
7. }
a) -2
b) 2
c) Compile time error
d) NaN
Answer:d
63. Which among the following mathematical function do not have a “double” return-type?
a) srand(x);
b) ceil(x);
c) floor(x);
d) Both (b) and (c);
Answer:a

C Programming Questions and Answers – Storage Management – 1

64. The function ____ obtains block of memory dynamically.


a) calloc
b) malloc
c) Both a & b
d) free
Answer:c
65. void * malloc(size_t n) returns
a) Pointer to n bytes of uninitialized storage
b) NULL if the request cannot be satisfied
c) Nothing
d) Both a & b are true
Answer:d
66. calloc() returns a storage that is initialized to.
a) Zero
b) Null
c) Nothing
d) One
Answer:a
67. In function free(p), p is a
a) int
b) Pointer returned by malloc()
c) Pointer returned by calloc()
d) Both b & c
Answer:d
68. What is the output of this C code?
1. #include <stdio.h>
2. void main()
3. {
4. char *p = calloc(100, 1);
5. p = "welcome";
6. printf("%s\n", p);
7. }
a) Segmentation fault
b) Garbage
c) Error
d) welcome
Answer:d
69. Memory allocation using malloc() is done in?
a) Static area
b) Stack area
c) Heap area
d) Both b & c
Answer:c
70. Why do we write (int *) before malloc?
int *ip = (int *)malloc(sizeof(int));
a) It is for the syntax correctness
b) It is for the type-casting
c) It is to inform malloc function about the data-type expected
d) None of the mentioned
Answer:b
71. Which one is used during memory deallocation in C?
a) remove(p);
b) delete(p);
c) free(p);
d) terminate(p);
Answer:c

C Programming Questions and Answers – Storage Management – 2

72. Which of the following will return a result most quickly for searching a given key?
a) Unsorted Array
b) Sorted Array
c) Sorted linked list
d) Binary Search Tree
Answer:d
73. On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to.
a) NULL
b) Other dynamically allocated memory
c) The same deallocated memory location
d) It points back to location it was initialized with
Answer:c
74. For the following program, Which of the following should be used for freeing the memory allocated?
1. #include <stdio.h>
2. struct p
3. {
4. struct p *next;
5. int x;
6. };
7. int main()
8. {
9. struct p *p1 = (struct p*)malloc(sizeof(struct p));
10. p1->x = 1;
11. p1->next = (struct p*)malloc(sizeof(struct p));
12. return 0;
13. }
a) free(p1);
free(p1->next)
b) free(p1->next);
free(p1);
c) free(p1);
d) All of the mentioned
Answer:b
75. What is the output of this C code?
1. #include <stdio.h>
2. struct p
3. {
4. struct p *next;
5. int x;
6. };
7. int main()
8. {
9. struct p *p1 = calloc(1, sizeof(struct p));
10. p1->x = 1;
11. p1->next = calloc(1, sizeof(struct p));
12. printf("%d\n", p1->next->x);
13. return 0;
14. }
a) Compile time error
b) 1
c) Somegarbage value
d) 0
Answer:d
76. What is the output of this C code?
1. #include <stdio.h>
2. struct p
3. {
4. struct p *next;
5. int x;
6. };
7. int main()
8. {
9. struct p* p1 = malloc(sizeof(struct p));
10. p1->x = 1;
11. p1->next = malloc(sizeof(struct p));
12. printf("%d\n", p1->next->x);
13. return 0;
14. }
a) Compile time error
b) 1
c) Somegarbage value
d) 0
Answer:c
77. calloc initialises memory with all bits set to zero.
a) true
b) false
c) Depends on the compiler
d) Depends on the standard
Answer:a
78. realloc(ptr, size), where size is zero means
a) Allocate a memory location with zero length
b) Free the memory pointed to by ptr
c) Undefined behaviour
d) Doesn’t do any reallocation of ptr i.e. no operation
Answer:b

You might also like