100% found this document useful (1 vote)
162 views46 pages

Sastra College C Programming MCQ Answer & Solution

The document is a multiple-choice question (MCQ) test focused on the C programming language, covering topics such as data types, variable names, operators, and memory management. Each question includes options, the correct answer, and an explanation for the answer. The test aims to assess knowledge of C programming concepts and syntax.

Uploaded by

viray93169
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
100% found this document useful (1 vote)
162 views46 pages

Sastra College C Programming MCQ Answer & Solution

The document is a multiple-choice question (MCQ) test focused on the C programming language, covering topics such as data types, variable names, operators, and memory management. Each question includes options, the correct answer, and an explanation for the answer. The test aims to assess knowledge of C programming concepts and syntax.

Uploaded by

viray93169
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/ 46

C LANGUAGE MCQ TEST

1. What is the 16-bit compiler allowable range for integer constants?


(a)-3.4e38 to 3.4e38 (b) -32767 to 32768
(c) -32668 to 32667 (d) -32768 to 32767
Ans: [d] -32768 to 32767
Explanation:- In a 16-bit C compiler, we have 2 bytes to store the value.
The range for signed integers is -32768 to 32767.
The range for unsigned integers is 0 to 65535.
The range for unsigned character is 0 to 255.

2. #include "stdio.h"
int main()
{
int x, y = 5, z = 5;
x = y == z;
printf("%d", x);
getchar();
return 0;
}
(a) 0 (b) 1
(c) 5 (d) Compiler error
Ans: [b]
Explanation:- The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of
comparison operators (<=, >= and ==) is higher than assignment operator =.
The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z
becomes 1 and the value is assigned to x via the assignment operator.

3. Who is the father of C language?


(a) Steve Jobs (b) James Gosling
(c) Dennis Ritchie (d) Rasmus Lerdorf
Ans: [c]
Explanation:- Dennis Ritchie is the father of C Programming Language. C programming language was developed in 1972 at American
Telephone & Telegraph Bell Laboratories of USA.

4. What is the range of values that can be stored by int datatype in C?


(a) -(2^31) to (2^31) – 1 (b)-256 to 255
(c) -(2^63) to (2^63) – 1 (d) 0 to (2^31) – 1
Ans: [a]

5. Study the following program:


main()
{printf("javatpoint");
main();}
What will be the output of this program?
(a) Wrong statement
(b) It will keep on printing javatpoint
(c) It will Print javatpoint once
(d) None of the these
Ans: [b]
Explanation: In this program, the main function will call itself again and again. Therefore, it will continue to print javatpoint.

6. #include <stdio.h>

Page 1 of 46
C LANGUAGE MCQ TEST

// Assume base address of "GeeksQuiz" to be 1000


int main()
{
printf(5 + "GeeksQuiz");
return 0;
}
What is the output of the above program?
(a) GeeksQuiz (b)Quiz
(c) Compile- time error (d) 1005
Ans: [c]
Explanation:- printf is a library function defined under stdio.h header file. The compiler adds 5 to the base address of the string through the
expression 5 + "GeeksQuiz" . Then the string "Quiz" gets passed to the standard library function as an argument.

7. Which of the following is not a valid C variable name?


(a) int number; (b) float rate;
(c) int variable_count; (d) int $main;
Ans: [d]
Explanation:- Since only underscore and no other special character is allowed in a variable name, it results in an error.

8. What will be the output of the following code snippet?


#include <stdio.h>
int main() {
int a = 3, b = 5;
int t = a;
a = b;
b = t;
printf("%d %d", a, b);
return 0;
}
(a) 3 5 (b) 3 3
(c) 5 5 (d) 5 3
Ans: [d]

9. What is required in each C program?


(a) The program must have at least one function.
(b) The program does not require any function.
(c) Input data
(d) Outputdata
Ans: [a]
Explanation: Any C program has at least one function, and even the most trivial programs can specify additional functions. A function is
a piece of code. In other words, it works like a sub-program.

10. #include <stdio.h>


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

What is the output of the above program?

Page 2 of 46
C LANGUAGE MCQ TEST

(a) 3 (b) 4
(c) 5 (d) Compile- time error
Ans: [d]
Explanation:- In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when
executed increments the value of variable i(i is a l-value) and returns r-value. The compiler generates the error(l-value required) when it
tries to post-incremeny the value of a r-value.

11. All keywords in C are in ____________


(a) Lower Case letters (b) Upper Case letters
(c) CamelCase letters (d) None of the mentioned
Ans: [a]

12. How is an array initialized in C language?


(a) int a[3] = {1, 2, 3); (b) int a = {1, 2, 3);
(c) int a[] = new int[3] (d) int a(3) = [1, 2, 3];
Ans: [a]

13. What will this program print?


main()
{
int i = 2;
{
int i = 4, j = 5;
printf("%d %d", i, j);
}
printf("%d %d", i, j);
}
(a) 4525 (b) 2525
(c) 4545 (d) None of the these
Ans: [a]
Explanation:- In this program, it will first print the inner value of the function and then print the outer value of the function.

14. #include <stdio.h>


#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
(a) 3
(b) 5
(c) 3 or 5 ddepending on value of x
(d) Compile time error
Ans: [b]
Explanation:- In the first look, the output seems to be compile-time error because macro X has not been defined. In C, if a macro is not
defined, the pre-processor assigns 0 to it by default. Hence, the control goes to the conditional else part and 5 is printed.

15. What is the output of the following code snippet?

Page 3 of 46
C LANGUAGE MCQ TEST

#include <stdio.h>
int main() {
int a[] = {1, 2, 3, 4};
int sum = 0;
for(int i = 0; i < 4; i++) {
sum += a[i];
}
printf("%d", sum);
return 0;
}
(a) 1 (b) 4
(c) 20 (d) 10
Ans: [d]

16. Which of the following is true for variable names in C?


(a) They can contain alphanumeric characters as well as special characters
(b) It is not an error to declare a variable to be one of the keywords(like goto, static)
(c) Variable names cannot start with a digit
(d) Variable can be of any length
Ans: [c]
Explanation:- According to the syntax for C variable name, it cannot start with a digit.

17. Which of the following comment is correct when a macro definition includes arguments?
(a) The opening parenthesis should immediately follow the macro name.
(b) There should be at least one blank between the macro name and the opening parenthesis.
(c) There should be only one blank between the macro name and the opening parenthesis.
(d) All the above comments are correct.
Ans: [a]

18. Consider the following C declaration


struct {
short s[5];
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for
variable t, ignoring alignment considerations, is (GATE CS 2000)
(a) 22 bytes (b) 18 bytes
(c) 18 bytes (d) 10 bytes
Ans: [b]
Explanation:- Short array s[5] will take 10 bytes as size of short is 2 bytes.
When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share
this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18
bytes (10 + 8).

19. What is the output of the following code snippet?


int main() {
int sum = 2 + 4 / 2 + 6 * 2;
printf("%d", sum);

Page 4 of 46
C LANGUAGE MCQ TEST

return 0;
}
(a) 2 (a) 15
(c) 16 (d) 18
Ans: [c]

20. Which is valid C expression?


(a) int my_num = 100,000; (b) int my_num = 100000;
(c) int my num = 1000; (d) int $my_num = 10000;
Ans: [b]
Explanation:- Space, comma and $ cannot be used in a variable name.

21. What is a lint?


(a) C compiler (b) Interactive debugger
(c) Analyzing tool (d) C interpreter
Ans: [c]
Explanation:- Lint is an analyzing tool that analyzes the source code by suspicious constructions, stylistic errors, bugs, and flag
programming errors. Lint is a compiler-like tool in which it parses the source files of C programming. It checks the syntactic accuracy of
these files.

22. In C, parameters are always


(a) Passed by value
(b) Passed by reference
(c) Non-pointer variables are passed by value and pointers are passed by reference
(d) Passed by value result
Ans: [a]
Explanation:- In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.

23. How is the 3rd element in an array accessed based on pointer notation?
(a) *a + 3 (b) *(a + 3)
(c) *(*a + 3) (d) &(a + 3)
Ans: [b]

24. Which of the following cannot be a variable name in C?


(a) volatile (b) true
(c) friend (d) export
Ans: [a]

25. What is the output of this statement "printf("%d", (a++))"?


(a) The value of (a + 1) (b) The current value of a
(c) Error message (d) Garbage
Ans: [b]

26. Which of the following is true about return type of functions in C?


(a) Functions can return any type
(b) Functions can return any type except array and functions
(c) Functions can return any type except array, functions and union
(d) Functions can return any type except array, functions, function pointer and union
Ans: [b]
Explanation:- In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to
array or pointer to function

27. How are String represented in memory in C?


(a) An array of characters.

Page 5 of 46
C LANGUAGE MCQ TEST

(b) The object of some class.


(c) Same as other primitive data types.
(d) LinkedList of characters.
Ans: [a]

28. What is short int in C programming?


(a) The basic data type of C
(b) Qualifier
(c) Short is the qualifier and int is the basic data type
(d) All of the mentioned
Ans: [c]

29. Study the following program:


} main()
{
char x [10], *ptr = x;
scanf ("%s", x);
change(&x[4]);
}
change(char a[])
{
puts(a);
If abcdefg is the input, the output will be
(a) abcd (b) abc
(c) efg (d) Garbage
Ans: [c]

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


(a) auto (b) register
(c)static (d) extern
(e) volatile
Ans: [e]
Explanation:- volatile is not a storage class specifier. volatile and const are type qualifiers.

31. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
int a[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++) {
if(i % 2 == 0) {
sum += *(a + i);
}
else {
sum -= *(a + i);
}
}
printf("%d", sum);
}
int main() {
solve();

Page 6 of 46
C LANGUAGE MCQ TEST

return 0;
}
(a) 2 (b) 15
(c) Syntax Error (d) 3
Ans: [d]
Explanation:- This is an example of accessing an array element through pointer notation. So *(a + i) is equivalent to a[i], and we are
adding all the even indices elements, and subtracting all the elements at odd indices, which gives a result of 3.

32. Which of the following declaration is not supported by C language?


(a) String str;
(b) char *str;
(c) float str = 3e2;
(d) Both “String str;” and “float str = 3e2;”
Ans: [a]

33. Study the following program:


main()
{
int a = 1, b = 2, c = 3:
printf("%d", a + = (a + = 3, 5, a))
}
What will be the output of this program?
(a) 6 (b) 9
(c) 12 (d) 8
Ans: [d]
Explanation: It is an effect of the comma operator.
a + = (a + = 3, 5, a)
It first evaluates to "a + = 3" i.e. a = a + 3 then evaluate 5 and then evaluate "a".
Therefore, we will get the output is 4.
Then,
a+=4
It gives 8 as the output.

34. Which of the following is not a logical operator?


(a) && (b) !
(c) || (d) |
Ans: [d]

35. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
int first = 10, second = 20;
int third = first + second;
{
int third = second - first;
printf("%d ", third);
}
printf("%d", third);
}
int main() {
solve();

Page 7 of 46
C LANGUAGE MCQ TEST

return 0;
}
(a) 10 30 (b) 30 10
(c) 10 20 (d) 20 10
Ans: [a]
Explanation:- This problem uses the concept of scopes. In the pair of {}, the variable third which is declared inside it is local to the block
and takes preference over the global third. So it is printed first, and then the global third.

36. Which keyword is used to prevent any changes in the variable within a C program?
(a) immutable (b) mutable
(c) const (d) volatile
Ans: [c]

37. What does this declaration mean?


int x : 4;
(a) X is a four-digit integer.
(b) X cannot be greater than a four-digit integer.
(c) X is a four-bit integer.
(d) None of the these
Ans: [c]
Explanation:- This means, "X" is a four bit integer.

38. Which file is generated after pre-processing of a C program?


(a) .p (b) .i
(c) .o (d) .m
Ans: [b]
Explanation:- After the pre-processing of a C program, a .i file is generated which is passed to the compiler for compilation.

39. What is the disadvantage of arrays in C?


(a) The amount of memory to be allocated should be known beforehand.
(b) Elements of an array can be accessed in constant time.
(c) Elements are stored in contiguous memory blocks.
(d) Multiple other data structures can be implemented using arrays.
Ans: [a]

40. What is the result of logical or relational expression in C?


(a) True or False
(b) 0 or 1
(c) 0 if an expression is false and any positive number if an expression is true
(d) None of the mentioned
Ans: [b]

41. Why is a macro used in place of a function?


(a) It reduces execution time.
(b) It reduces code size.
(c) It increases execution time.
(d) It increases code size.
Ans: [d]

42. In C, static storage class cannot be used with:


(a) Global variable (b) Function parameter
(c) Function name (d) Local variable
Ans: [b]

Page 8 of 46
C LANGUAGE MCQ TEST

Explanation:- Declaring a global variable as static limits its scope to the same file in which it is defined.
A static function is only accessible to the same file in which it is defined.
A local variable declared as static preserves the value of the variable between the function calls.

43. Which of the following typecasting is accepted by C language?


(a) Widening conversions
(b) Narrowing conversions
(c) Widening & Narrowing conversions
(d) None of the mentioned
Ans: [c]

44. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
int a = 3;
int res = a++ + ++a + a++ + ++a;
printf("%d", res);
}
int main() {
solve();
return 0;
}
(a) 12 (b) 24
(c) 20 (d) 18
Ans: [c]
Explanation:- The ++a operator follows the “change then use” rule. The a++ operator follows the “use then change” rule. Using these
rules along with proper associativity we get the result of 20.

43. In the C language, the constant is defined _______.


(a) Before main
(b) After main
(c) Anywhere, but starting on a new line.
(d) None of the these.
Ans: [c]
Explanation:- In the C language, the constant is defined anywhere, but starting on a new line.

44. In below program, what would you put in place of “?” to print “Quiz”?
#include <stdio.h>
int main()
{
char arr[] = "GeeksQuiz";
printf("%s", ?);
return 0;
}
(a) arr (b) (arr+5)
(c) (arr+4) (d) Not possible
Ans: [b]
Explanation:- Since %s is used, the printf statement will print everything starting from arr+5 until it finds ‘\0’

45. Where in C the order of precedence of operators do not exist?


(a) Within conditional statements, if, else

Page 9 of 46
C LANGUAGE MCQ TEST

(b) Within while, do-while


(c) Within a macro definition
(d) None of the mentioned
Ans: [d]

46. What will be the value of x in the following code snippet?


#include <stdio.h>
void solve() {
int x = printf("Hello");
printf(" %d", x);
}
int main() {
solve();
return 0;
}
(a) 10 (b) 5
(c) 1 (d) 0
Ans: [b]

47. How many times will the following loop execute?


for(j = 1; j <= 10; j = j-1)
(a) Forever (b) Never
(c) 0 (d) 1
Ans: [a]

48. Which of the following is true about arrays in C.


(a) For every type T, there can be an array of T.
(b) For every type T except void and function type, there can be an array of T.
(c) When an array is passed to a function, C compiler creates a copy of array.
(d) 2D arrays are stored in column major form
Ans: [b]
Explanation:- In C, we cannot have an array of void type and function types.
For example, below program throws compiler error
int main( )
{
void arr[100];
}
But we can have array of void pointers and function pointers. The below program works fine.
int main( )
{
void *arr[100];
}

49. Which of the following is NOT possible with any 2 operators in C?


(a) Different precedence, same associativity
(b) Different precedence, different associativity
(c) Same precedence, different associativity
(d) All of the mentioned
Ans: [c]

50. What does the following declaration indicate?

Page 10 of 46
C LANGUAGE MCQ TEST

int x: 8;
(a) x stores a value of 8. (b) x is an 8-bit integer.
(c) Both A and B. (d) None of the above.
Ans: [b]

51. A pointer is a memory address. Suppose the pointer variable has p address 1000, and that p is declared to have type int*, and an int
is 4 bytes long. What address is represented by expression p + 2?
(a) 1002 (b) 1004
(c) 1006 (d) 1008
Ans: [d]

52. What output will be generated by the given code dsegment if:
Line 1 is replaced by “auto int a = 1;”
Line 2 is replaced by “register int a = 2;”
(a) 3 1 (b) 4 2
41 61
42 61
(c) 4 2 (d) 4 2
62 42
20 20
Ans: [d]
Explanation:- If we replace line 1 by “auto int a = 1;” and line 2 by “register int a = 2;”, then ‘a’ becomes non-static in prtFun(). The output of
first prtFun() remains same. But, the output of second prtFun() call is changed as a new instance of ‘a’ is created in second call. So “4 2″ is
printed again. Finally, the printf() in main will print “2 0″. Making ‘a’ a register variable won’t change anything in output.

53. What is an example of iteration in C?


(a) for (b) while
(c) do-while (d) all of the mentioned
Ans: [d]

54. Which of the following is the proper syntax for declaring macros in C?
(a) #define long long ll (b) #define ll long long
(c) #define ll (d) #define long long
Ans: [b]

55. What is the result after execution of the following code if a is 10, b is 5, and c is 10?
If ((a > b) && (a <= c))
a = a + 1;
else
c = c+1;
(a) a = 10, c = 10 (b) a = 11, c = 10
(c) a = 10, c = 11 (d) a = 11, c = 11
Ans: [b]

56. Which of the following is true


(a) gets() doesn't do any array bound testing and should not be used.
(b) fgets() should be used in place of gets() only for files, otherwise gets() is fine
(c) gets() cannot read strings with spaces
(d) None of the above
Ans: [a]

Page 11 of 46
C LANGUAGE MCQ TEST

Explanation:- Use of gets() generates the risk of an overflow of the allocated buffer. This happens because the function gets(), doesn't
know the size of the buffer, and continues reading until it finds a newline "\n" or encounters EOF, and so it may overflow the bounds of
the buffer it was given.

57. Functions can return enumeration constants in C?


(a) true
(b) false
(c) depends on the compiler
(d) depends on the standard
Ans: [a]

58. Which of the following is an exit controlled loop?


(a) While loop. (b) For loop.
(c) do-while loop. (d) None of the above.
Ans: [c]

59. Which one of the following is a loop construct that will always be executed once?
(a) for (b) while
(c) switch (d) do while
Ans: [d]
Explanation- The body of a loop is often executed at least once during the do-while loop. Once the body is performed, the condition is
tested. If the condition is valid, it will execute the body of a loop; otherwise, control is transferred out of the loop.

60. Which of the following operators can be applied on structure variables?


(a) Equality comparison ( == )
(b) Assignment ( = )
(c) Both of the above
(d) None of the above
Ans: [b]
Explanation:- A structure variable can be assigned to other using =, but cannot be compared with other using ==

61. Functions in C Language are always _________


(a) Internal
(b) External
(c) Both Internal and External
(d) External and Internal are not valid terms for functions
Ans: [b]

62. What is the size of the int data type (in bytes) in C?
(a) 4 (b) 8
(c) 2 (d) 1
Ans: [a]

63. Which of the following best describes the ordering of destructor calls for stack-resident objects in a routine?
(a) The first object created is the first object destroyed; last created is last destroyed.
(b) The first object destroyed is the last object destroyed; last created is first destroyed.
(c) Objects are destroyed in the order they appear in memory, the object with the lowest memory address is destroyed first.
(d) The order is undefined and may vary from compiler to compiler.
Ans: [b]

64. What is the use of "#pragma once"?


(a) Used in a header file to avoid its inclusion more than once.
(b) Used to avoid multiple declarations of same variable
(c) Used in a c file to include a header file at least once.

Page 12 of 46
C LANGUAGE MCQ TEST

(d) Used to avoid assertions


Ans: [a]

65. Which of following is not accepted in C?


(a) static a = 10; //static as
(b) static int func (int); //parameter as static
(c) static static int a; //a static variable prefixed with static
(d) all of the mentioned
Ans: [c]

66. What will be the output of the following code snippet?


#include <stdio.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void solve() {
int a = 3, b = 5;
swap(&a, &b);
printf("%d %d", a, b);
}
int main() {
solve();
return 0;
}
(a) 3 5 (b) 5 3
(c) 5 5 (d) 3 3
Ans: [b]

67. How many characters can a string hold when declared as follows?
char name[20]:
(a) 18 (b) 19
(c) 20 (d) None of the these
Ans: [b]

68. What does the following C statement mean?


scanf("%4s", str);
(a) Read exactly 4 characters from console
(b) Read maximum 4 characters from console.
(c) Read a string str in multiples of 4
(d) Nothing
Ans: [b]
Explanation:- Try following program, enter GeeksQuiz, the output would be "Geek"
#include <stdio.h>
int main()
{
char str[50] = {0};
scanf("%4s", str);
printf(str);
getchar();

Page 13 of 46
C LANGUAGE MCQ TEST

return 0;
}

69. Property which allows to produce different executable for different platforms in C is called?
(a) File inclusion
(b) Selective inclusion
(c) Conditional compilation
(d) Recursive macros
Ans: [c]
Explanation:- Conditional compilation is the preprocessor facility to produce a different executable.

70. How to declare a double-pointer in C?


(a) int *val (b) int **val
(c) int &val (d) int *&val
Ans: [b]

71. Directives are translated by the


(a) Pre-processor (b) Compiler
(c) Linker (d) Editor
Ans: [a]
Explanation:- In C language, the pre-processor is a macro processor that is dynamically used by the C programmer to modify the
program before it is properly compiled (Before construction, pro-processor directives are implemented).

72. Assume that a character takes 1 byte. Output of following program?


#include<stdio.h>
int main()
{
char str[20] = "GeeksQuiz";
printf ("%d", sizeof(str));
return 0;
}
(a) 9 (b) 10
(c) 20 (d) Garbage value
Ans: [c]
Explanation:- Note that the sizeof() operator would return size of array. To get size of string stored in array, we need to use strlen().
The following program prints 9.
#include <stdio.h>
#include <string.h>
int main()
{
char str[20] = "GeeksQuiz";
printf ("%d", strlen(str));
return 0;
}

73. What is #include <stdio.h>?


(a) Preprocessor directive
(b) Inclusion directive
(c) File inclusion directive
(d) None of the mentioned
Ans: [a]

Page 14 of 46
C LANGUAGE MCQ TEST

74. If p is an integer pointer with a value 1000, then what will the value of p + 5 be?
(a) 1020 (b) 1005
(c) 1004 (d) 1010
Ans: [a]
Explanation:- Since size of an integer is 4bytes, p + 5 will be at value 1000+5 * 4 = 1020.

75. How many bytes does "int = D" use?


(a) 0 (b) 1
(c) 2 or 4 (d) 10
Ans: [c]

76. Predict the output of following program, assume that a character takes 1 byte and pointer takes 4 bytes.
#include <stdio.h>
int main()
{
char *str1 = "GeeksQuiz";
char str2[] = "GeeksQuiz";
printf("sizeof(str1) = %d, sizeof(str2) = %d",
sizeof(str1), sizeof(str2));
return 0;
}
(a) sizeof(str1) = 10, sizeof(str2) = 10
(b) sizeof(str1) = 4, sizeof(str2) = 10
(c) sizeof(str1) = 4, sizeof(str2) = 4
(d) sizeof(str1) = 10, sizeof(str2) = 4
Ans: [b]
Explanation:- str1 is a pointer and str2 is an array.

77. C preprocessors can have compiler specific features.


(a) True
(b) False
(c) Depends on the standard
(d) Depends on the platform
Ans: [a]

78. What will be the output of the following code snippet?


#include <stdio.h>
#define VAL 3 * (2 + 6)
void solve() {
int a = 10 + VAL;
printf("%d", a);
}
int main() {
solve();
return 0;
}
(a) 104 (b) 24
(c) 10 (d) 34
Ans: [d]
Explanation:- The above code snippet shows the use of macros in C. The expression for VAL is directly put in its place in the code at
compile-time and then the expression is evaluated, which gives a value of 34.

Page 15 of 46
C LANGUAGE MCQ TEST

79. What feature makes C++ so powerful?


(a) Easy implementation
(b) Reusing the old code
(c) Easy memory management
(d) All of the above
Ans: [d]

80. What does the following program print?


#include
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf("%d %d n", i, j);
getchar();
return 0;
}
(a) 2 2 (b) 2 1
(c) 0 1 (d) 0 2
Ans: [d]
Explanation:- /* p points to i and q points to j */
void f(int *p, int *q)
{
p = q; /* p also points to j now */
*p = 2; /* Value of j is changed to 2 now */
}

81. Which of the following are C preprocessors?


(a) #ifdef (b) #define
(c) #endif (d) all of the mentioned
Ans: [d]

82. Which of the following are not standard header files in C?


(a) stdio.h (b) stdlib.h
(c) conio.h (d) None of the above.
Ans: [d]

83. Which of the following will copy the null-terminated string that is in array src into array dest?
(a) dest = src; (b) dest == src;
(c) strcpy(dest, src); (d) strcpy(src, dest);
Ans: [c]
Explanation:- strcpy is a string function that is used to copy the string between the two files. strcpy(destination, source)

84. The following C declarations


struct node
{

Page 16 of 46
C LANGUAGE MCQ TEST

int i;
float j;
};
struct node *s[10] ;
(a) An array, each element of which is a pointer to a structure of type node
(b) A structure of 2 fields, each field being a pointer to an array of 10 elements
(c) A structure of 3 fields: an integer, a float, and an array of 10 elements
(d) An array, each element of which is a structure of type node.
Ans: [a]

85. The C-preprocessors are specified with _________ symbol.


(a) # (b) $
(c) ” ” (d) &
Ans: [a]

86. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
printf("%d %d", (023), (23));
}
int main() {
solve();
return 0;
}
(a) 023 23 (b) 23 23
(c) 19 23 (d) 23 19
Ans: [c]
Explanation:- 23 is printed as it is. 023 is interpreted as an octal number which is equivalent to decimal 19.

87. What will be the result of the following code snippet?


#include <stdio.h>
void solve() {
char ch[10] = "abcdefghij";
int ans = 0;
for(int i = 0; i < 10; i++) {
ans += (ch[i] - 'a');
}
printf("%d", ans);
}
int main() {
solve();
return 0;
}
(a) 45 (b) 36
(c) 20 (d) 100
Ans:[45]

88. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
bool ok = false;

Page 17 of 46
C LANGUAGE MCQ TEST

printf(ok ? "YES" : "NO");


}
int main() {
solve();
return 0;
}
(a) Yes (b) No
(c) Compilation Error (d) None of the above
Ans: [c]

89. Which of the following is not possible statically in C language?


(a) Jagged Array (b) Rectangular Array
(c) Cuboidal Array (d) Multidimensional Array
Ans: [a]

90. How many number of pointer (*) does C have against a pointer variable declaration?
(a) 7 (b) 127
(c) 255 (d) No limits
Ans: [d]

91. Consider the following C program segment.


# include <stdio.h>
int main( )
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0' ;
printf ("%s", s1);
}
What will be printed by the program?
(a) 12 (b) 120400
(c) 1204 (d) 1034
Ans: [c]
Explanation:-
char s1[7] = "1234", *p;
p = s1 + 2; // p holds address of character 3
*p = '0' ; // memory at s1 + 3 now becomes 0
printf ("%s", s1); // All characters are printed

92. Each instance of a class has a different set of


(a) Class interfaces (b) Methods
(c) Return types (d) Attribute values
Ans: [d]
Explanation:- Each instance of the class has a different set of attribute values

93. Assuming int size is 4 bytes, what is going to happen when we compile and run the following program?
#include “stdio.h”
int main()
{
printf(“GeeksQuizn”);
main();

Page 18 of 46
C LANGUAGE MCQ TEST

return 0;
}
(a) We can’t use main() inside main() and compiler will catch it by showing compiler error.
(b) GeeksQuiz would be printed in 2147483647 times i.e. (2 to the power 31) - 1.
(c) It’ll print GeeksQuiz infinite times i.e. the program will continue to run forever until it’s terminated by other means such as
CTRL+C or CTRL+Z etc.
(d) GeeksQuiz would be printed only once. Because when main() is used inside main(), it’s ignored by compiler at run time. This is to
make sure that main() is called only once.
(e) GeeksQuiz would be printed until stack overflow happens for this program.
Ans: [e]
Explanation: - First of all, there’s no restriction of main( ) calling main( ) i.e. recursion can happen for main( ) as well. But there’s no explicit
termination condition mentioned here for this recursion. So main( ) would be calling main( ) after printing GeeksQuiz. This will go on until
Stack of the program would be filled completely. Please note that stack (internal to a running program) stores the calling function sequence
i.e. which function has called which function so that the control can be returned when called function returns. That’s why here in program,
main( ) would continue to call main( ) until complete stack is over i.e. stack-overflow occurs.

94. How many instances of a class can be declared?


(a) 1 (b) 10
(c) As per required (d) None of the these
Ans: [c]
Explanation:- You can always declare multiple instances of a class, as per required. Each object will hold its own individual inner
variables (unless they are static, in which case they are shared).

95. What will be the output of the following code snippet?


#include <stdio.h>
void solve() {
printf("%d %d %d", (076), (28), (0x87));
}
int main() {
solve();
return 0;
}
(a) 76 28 87 (b) 076 28 0x87
(c) 62 28 135 (d) 0 0 0
Ans: [c]

96. How is search done in #include and #include “somelibrary.h” according to C standard?
(a) When former is used, current directory is searched and when latter is used, standard directory is searched
(b) When former is used, standard directory is searched and when latter is used, current directory is searched
(c) When former is used, search is done in implementation defined manner and when latter is used, current directory is searched
(d) For both, search for ‘somelibrary’ is done in implementation-defined places
Ans: [b]

97. Consider the C program below.


#include <stdio.h>
int *A, stkTop;
int stkFunc (int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode)
{
case -1:

Page 19 of 46
C LANGUAGE MCQ TEST

size = val;
break;
case 0:
if (stkTop < size ) A[stkTop++]=val;
break;
default:
if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20];
A=B;
stkTop = -1;
stkFunc (-1, 10);
stkFunc (0, 5);
stkFunc (0, 10);
printf ("%dn", stkFunc(1, 0)+ stkFunc(1, 0));
}
The value printed by the above program is
(a) 9 (b) 10
(c) 15 (d) 17
Ans: [c]

98. In the statement "COUT << "javatpoint" << end1;", end1 is a ___________.
(a) Extractor (b) Inserter
(c) Manipulator (d) Terminator
Ans: [c]
Explanation:- End1 is an I/O manipulator that takes effect in printing a new line '\ n' character and then flushing the output stream.

99. _______________ is the preprocessor directive which is used to end the scope of #ifdef.
(a) #elif (b) #ifndef
(c) #endif (d) #if
Ans: [c]
Explanation:- The #ifdef preprocessor directive is used to check if a particular identifier is currently defined or not. If the particular
identifier is defined, the statements following this preprocessor directive are executed till another preprocessor directive #endif is
encountered. #endif is used to end the scope of #ifdef.

100. What will be the output of the following C code?


#include<stdio.h>
#define san 557
int main()
{
#ifndef san
printf("yes");
#endif
printf("no");
}
(a) error

Page 20 of 46
C LANGUAGE MCQ TEST

(b) yes
(c) no
(d) yes no
Ans: [c]
Explanation:- The output to the above code will be no. Since we have used the preprocessor directive, #ifndef and defined the identifier
san, “yes” is not printed. Hence only “no” is printed as output.

101. What will the result of num variable after execution of the following statements?
int num = 58;
num % = 11
(a) 3 (b) 5
(c) 8 (d) 11
Ans: [a]
Explanation: num = 58
num % = 11
num = num % 11
num = 58 % 11
num = 3

102. For the following declaration of a function in C, pick the best statement
int [] fun (void (*fptr) (int *));
(a) It will result in compile error.
(b) No compile error. fun is a function which takes a function pointer fptr as argument and return an array of int.
(c) No compile error. fun is a function which takes a function pointer fptr as argument and returns an array of int. Also, fptr is a function
pointer which takes int pointer as argument and returns void.
(d) No compile error. fun is a function which takes a function pointer fptr as argument and returns an array of int. The array of int
depends on the body of fun i.e. what size array is returned. Also, fptr is a function pointer which takes int pointer as argument and
returns void.
Ans: [a]
Explanation: As per C standard, a function can’t have an explicit array as return type. That’s why the above would result in compile error.
There’re indirect ways if we need an array as an output of a function call. For example, a pointer can be returned by function
by return statement while providing the size of array via other means. Alternatively, function argument can be used for this.

103. Which of the following return-type cannot be used for a function in C?


(a) char * (b) struct
(c) void (d) None of the mentioned
Ans: [d]

104. In which of the following languages is function overloading not possible?


(a) C (b) C++
(c) Java (d) Python
Ans: [a]
Explanation: Function Overloading is not possible in C as it is not an Object-Oriented Language.

105. What is the maximum number of characters that can be held in the string variable char address line [40]?
(a) 38 (b) 39
(c) 40 (d)41
Ans: [b]

106. Typically, library header files in C (e.g. stdio.h) contain not only declaration of functions and macro definitions but they contain
definition of user defined data types (e.g. struct, union etc), typedefs and definition of global variables as well. So, if we include the same
header file more than once in a C program, it would result in compile issue because re-definition of many of the constructs of the header
file would happen. So, it means the following program will give compile error.

Page 21 of 46
C LANGUAGE MCQ TEST

#include “stdio.h”
#include “stdio.h”
#include “stdio.h”
int main ()
{
printf (“Whether this statement would be printed?”)
return 0;
}
(a) True
(b) False
Ans: [b]
Explanation: It’s okay to include library header files multiple times in a program. But actually the content of the header file is included only
once. The way it’s achieved is due to usage of “#ifndef “, “#define” and “#endif”. That’s why it’s recommended to use these preprocessor
macros even in user defined header files. For an example and usage of this, please check out the “Discuss it” of this question.

107. The standard header _______ is used for variable list arguments (…) in C.
(a) <stdio.h > (b) <stdlib.h>
(c) <math.h> (d) <stdarg.h>
Ans: [d]

108. Which of the following function is used to open a file in C++?


(a) fopen (b) fclose
(c) fseek (d) fgets
Ans: (a)
Explanation: fopen is used to open a file in C++.

109. What will the result of num1 variable after execution of the following statements?
int j = 1, num1 = 4;
while (++j <= 10)
{
num1++;
}
(a) 11 (b)12
(c) 13 (d) 14
Ans: [c]

110. In C, 1D array of int can be defined as follows and both are correct.
int array1D [4] = {1,2,3,4};
int array1D [] = {1,2,3,4};
But given the following definitions (along-with initialization) of 2D arrays
int array2D [2][4] = {1,2,3,4,5,6,7,8}; /* (i) */
int array2D [][4] = {1,2,3,4,5,6,7,8}; /* (ii) */
int array2D [2] [] = {1,2,3,4,5,6,7,8}; /* (iii) */
int array2D [][] = {1,2,3,4,5,6,7,8}; /* (iv) */
(a) Only (i) is correct.
(b) Only (i) and (ii) are correct.
(c) Only (i), (ii) and (iii) are correct.
(d) All (i), (ii), (iii) and (iv) are correct.
Ans: [b]
Explanation: First of all, C language doesn’t provide any true support for 2D array or multidimensional arrays. A 2D array is simulated via
1D array of arrays. So a 2D array of int is actually a 1D array of array of int. Another important point is that array size can be derived from

Page 22 of 46
C LANGUAGE MCQ TEST

its initialization but that’s applicable for first dimension only. It means that 2D array need to have an explicit size of 2nd dimension.
Similarly, for a 3D array, 2nd and 3rd dimensions need to have explicit size. That’s why only (i) and (ii) are correct. But array2D[2][] and
array2D[][] are of incomplete type because their complete size can’t derived even from the initialization.

111. When a C program is started, O.S environment is responsible for opening file and providing pointer for that file?
(a) Standard input (b) Standard output
(c) Standard error (d) All of the mentioned
Ans: [d]

112. What is the return type of the fopen() function in C?


(a) Pointer to a FILE object.
(b) Pointer to an integer.
(c) An integer.
Ans: [a]
Explanation: The fopen() function returns a pointer to a FILE object.

113. What will the result of len variable after execution of the following statements?
int len;
char str1[] = {"39 march road"};
len = strlen(str1);
(a) 11 (b) 12
(c) 13 (d) 14
Ans: [c]
Explanation: strlen is a string function that counts the word and also count the space in the string. (39 march road) = 13

114. Pick the best statement for the following program snippet:
#include <stdio.h>
int main()
{
int var; /*Suppose address of var is 2000 */
void *ptr = &var;
*ptr = 5;
printf ("var=%d and *ptr=%d",var,*ptr);
return 0;
}
(a) It will print “var=5 and *ptr=2000”
(b) It will print “var=5 and *ptr=5”
(c) It will print “var=5 and *ptr=XYZ” where XYZ is some random address
(d) Compile error
Ans: [d]
Explanation: Key point in the above snippet is dereferencing of void pointer. It should be noted that dereferencing of void pointer isn’t
allowed because void is an incomplete data type. The correct way to assign value of 5 would be first to typecast void pointer and then use it.
So instead of *ptr, one should use *(int *)ptr. Correct answer is d.

115. In C language, FILE is of which data type?


(a) int (b) char *
(c) struct (d) None of the mentioned
Ans: [c]

116. What will be the output of the following code snippet?


#include <stdio.h>
void solve () {

Page 23 of 46
C LANGUAGE MCQ TEST

int ch = 2;
switch (ch) {
case 1: printf ("1 ");
case 2: printf ("2 ");
case 3: printf ("3 ");
default: printf("None");
}
}
int main () {
solve ();
return 0;
}
(a) 1 2 3 None (b) 2
(c) 2 3 None (d) None
Ans: [c]
Explanation: This is an example of Fall-Though in switch statements in absence of break statements.

117. Study the following statement


#include <stdio.h>
int main ()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf ("%d, %d/n", *ptr, a);
}
What will be the output?
(a) 10, 10 (b) 10, 11
(c) 11, 10 (d) 11, 11
Ans: [d]

118. Pick the best statement for the below program snippet:
struct {int a [2];} arr[] = {1,2};
(a) No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2
elements. arr[0]. a[0] would be 1 and arr[1].a[0] would be 2.
(b) No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements.
arr[0]. a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be
0.
(c) No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements.
arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.
(d) None of the above
Ans: (c)
Explanation: Since size of array arr isn’t given explicitly, it would be decided based on the initialization here. Without any curly braces, arr
is initialized sequentially i.e. arr[0].a[0] would be 1 and arr[0].a[1] would be 2. There’s no further initialization so size of arr would be 1.
Correct answer is C.

119. What is the size of(char) in a 32-bit C compiler?


(a) 1 bit (b) 2 bits
(c) 1 Byte (d) 2 Bytes
Ans: [c]

Page 24 of 46
C LANGUAGE MCQ TEST

120. What will be the output of the following code snippet?


#include <stdio.h>
void solve () {
int x = 1, y = 2;
printf (x > y? "Greater" : x == y ? "Equal" : "Lesser");
}
int main() {
solve();
return 0;
}
(a) Greater (b) Lesser
(c) Equal (d) None of the above.
Ans: [b]
Explanation: The above code snippet compares 2 numbers using ternary operators.

121. Given the following statement, what will be displayed on the screen?
int * aPtr;
*aPtr = 100;
cout << *aPtr + 2;
(a) 100 (b) 102
(c) 104 (d) 108
Ans: [b]
Explanation: aPtr is an integer pointer which value is 100.
= *aPtr + 2
= 100 + 2
= 102

122. #include <stdio.h>


int main ()
{
unsigned int i = 65000;
while (i++! = 0);
printf ("%d", i);
return 0;
}
(a) Infinite Loop (b) 0
(c) 1 (d) Run Time Error
Ans: [c]
Explanation: The result will be 1 but after a really long time because while loop will keep on going until i becomes 4294967295 (Assuming
unsigned int is stored using 4 bytes) and as i highest limit of unsigned int is 4294967295 in next ++ operation it will become zero and we'll
come out of loop and 1 will be printed.
Since the time taken is long, on-line compiler may terminate the program with time limit exceeded error. If instead of unsigned int, you use
unsigned short int then result (1) may come faster.

123. Which of the following is not an operator in C?


(a), (b) sizeof()
(c) ~ (d) None of the mentioned
Ans: [d]

124. What will be the output of the following code snippet?


#include <stdio.h>

Page 25 of 46
C LANGUAGE MCQ TEST

void solve () {
int n = 24;
int l = 0, r = 100, ans = n;
while (l <= r) {
int mid = (l + r) / 2;
if (mid * mid <= n) {
ans = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
printf ("%d", ans);
}
int main () {
solve ();
return 0;
}
(a) 5 (b) 4
(c) 3 (d) 6
Ans: [b]
Explanation: The code snippet basically uses binary search to calculate the floor of the square root of a number. Since the square root is
an increasing function, so binary search is applicable here. Here, for n = 24, the answer is 4.

125. Give the following declarations and an assignment statement. Which one is equivalent to the expression str [4]?
char str[80];
char * p;
p = str;
(a) p+4 (b) *p+4
(c) *(p+4) (d) p[3]
Ans: [c]

126. The following function computes the maximum value contained in an integer array p [] of size n (n >= 1)
int max (int *p, int n)
{
int a=0, b=n-1;
while (__________)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];

Page 26 of 46
C LANGUAGE MCQ TEST

}
The missing loop condition is
(a) a! =n (b) b! =0
(c) b>(a+1) (d) b! =a
Ans: [d]
Explanation:
#include < iostream >
int max (int *p, int n)
{
int a=0, b=n-1;
while (a! =b)
{
if (p[a] <= p[b])
{
a = a+1;
}
else
{
b = b-1;
}
}
return p[a];
}
int main ()
{
int arr[] = {10, 5, 1, 40, 30};
int n = sizeof(arr)/sizeof(arr[0]);
std: cout << max (arr, 5);
}

127 scanf () is a predefined function in______header file.


(a) stdlib. H (b) ctype. h
(c) stdio. H (d) stdarg. H
Ans: [c]
Explanation: scanf () is a predefined function in "stdio.h" header file.printf and scanf() carry out input and output functions in C. These
functions statements are present in the header file stdio.h.

128. What will be the output of the following code snippet?


#include <stdio.h>
void solve () {
printf ("%d ", 9 / 2);
printf ("%f", 9.0 / 2);
}
int main () {
solve ();
return 0;
}
(a) 4 4.5000 (b) 4 4.000
(c) 4.5000 4.5000 (d) 4 4

Page 27 of 46
C LANGUAGE MCQ TEST

Ans: [a]
Explanation: The first print statement implicitly converts the result to int type before printing it. Similarly, the 2nd print statement
implicitly converts the result to float type before printing it.

129. Which one is the correct description for the variable balance declared below?
int ** balance;
(a) Balance is a point to an integer
(b) Balance is a pointer to a pointer to an integer
(c) Balance is a pointer to a pointer to a pointer to an integer
(d) Balance is an array of integer
Ans: [b]
Explanation: This code description states that the remainder is a pointer to a pointer to an integer.

130. The value printed by the following program is


void f (int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
void main ()
{
int i=5, j=10;
f (&i, j);
printf ("%d", i+j);
}
(a) 10 (b) 20
(c) 30 (d) 40
Ans: [c]
Explanation:
#include"stdio.h"
void f (int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
int main ()
{
int i=5, j=10;
f (&i, j);
printf ("%d", i+j);
}
For i, address is passed. For j, value is passed. So in function f, p will contain address of i and m will contain value 10. Ist statement of f ()
will change m to 15. Then 15 will be added to value at address p. It will make i = 5+15 = 20. j will remain 10. print statement will print
20+10 = 30. So answer is (C).

131. What is meant by ‘a’ in the following C operation?


fp = fopen ("Random.txt", "a");
(a) Attach (b) Append

Page 28 of 46
C LANGUAGE MCQ TEST

(c) Apprehend (d) Add


Ans: [b]

132. What will be the output of the following code snippet?


#include <stdio.h>
void solve () {
int x = 2;
printf ("%d", (x << 1) + (x >> 1));
}
int main () {
solve ();
return 0;
}
(a) 5 (b) 4
(c) 2 (d) 1
Ans: [a]
Explanation: The above example uses the knowledge of bitwise shift operators. (x << 1) is equivalent to x * 2 and (x >> 1) is equivalent
to x / 2. So the result is 2 * 2 + 2 / 2 = 5.

133. A class D is derived from a class B, b is an object of class B, d is an object of class D, and pb is a pointer to class B object. Which of the
following assignment statement is not valid?
(a) d = d; (b) b = d;
(c) d = b; (d) *pb = d:
Ans: [c]
Explanation: A class D is derived from a class B, so "d" is not equal to b.

134. Consider the following C-program:


int fun ()
{
static int num = 16;
return num--;
}
int main ()
{
for (fun (); fun (); fun ())
printf ("%d ", fun ());
return 0;
}
What is output of above program?
(a) Infinite loop (b) 13 10 7 4 1
(c) 15 12 8 5 2 (d) 14 11 8 5 2
Ans: [d]

135. What will be the output of the following C code?


#include <stdio.h>
int main ()
{
int y = 10000;
int y = 34;
printf ("Hello World! %d\n", y);
return 0;

Page 29 of 46
C LANGUAGE MCQ TEST

}
(a) Compile time error
(b) Hello World! 34
(c) Hello World! 1000
(d) Hello World! followed by a junk value
Ans:[a]
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here

136. What will be the output of the following code snippet?


#include <stdio.h>
#define VAL 5
int get Input () {
return VAL;
}
void solve () {
const int x = get Input ();
printf ("%d", x);
}
int main () {
solve ();
return 0;
}
(a) 5 (b) Garbage Value
(c) Compilation Error (d) 0
Ans: [a]
Explanation: The above code snippet will return the value of VAL from the getInput () method which in turn gets printed in solve ().

137. Which of the following statement is not true?


(a) A pointer to an int and a pointer to a double are of the same.
(b) A pointer must point to a data item on the heap (free store).
(c) A pointer can be reassigned to point to another data item.
(d) A pointer can point to an array.
Ans: [b]

138. If n has 3, then the statement a[++n] =n++;


(a) assigns 3 to a[5]
(b) assigns 4 to a[5]
(c) assigns 4 to a[4]
(d) what is assigned is complier dependent.
Ans: [d]

139. What will happen if the following C code is executed?


#include < stdio.h>
int main()
{
int main = 3;

Page 30 of 46
C LANGUAGE MCQ TEST

printf(“%d” , main ) ;
return 0 ;
}
(a) It will cause a compile-time error
(b) It will cause a run-time error
(c) It will run without any error and prints 3
(d) It will experience infinite looping
Ans: [c]
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

140. How to find the length of an array in C?


(a) sizeof(a)
(b) sizeof (a [0])
(c) sizeof(a) / sizeof (a [0])
(d) sizeof(a) * sizeof(a[0])
Ans: [c]
Explanation: sizeof operator returns the memory accessed by the variable within it. The total memory accessed by the array divided by
memory accessed by its individual element will give the length of the array.

141. Which of the following SLT template class is a container adaptor class?
(a) Stack (b) List
(c) Deque (d) Vector
Ans: [a]
Explanation: Container Adaptors is the subset of Containers that provides many types interface for sequential containers, such as stack
and queue.

142. Consider the following C declaration


struct (
short s [5];
union {
float y;
long z;
} u;
} t;
Assume that the objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement
for variable t, ignoring alignment consideration, is
(a) 22 bytes (b) 18 bytes
(c) 14 bytes (d) 10 bytes
Ans: [b]
Explanation: Here structure creates the minimum of array and union, but union only creates the minimum for only 'long z' which is
max. So minimum required =18.

143. What will be the output of the following C code?


#include <stdio.h>
int main ()
{
signed char chr;
chr = 128;

Page 31 of 46
C LANGUAGE MCQ TEST

printf ("%d\n", chr);


return 0;
}
(a) 128
(b) -128
(c) Depends on the compiler
(d) None of the mentioned
Ans: [b]
Explanation: The range of signed character is from -128 to +127. Since we are assigning a value of 128 to the variable ‘chr’, the result
will be negative. 128 in binary is represented as “1000 0000” for character datatype. As you can see that the sign bit is set to 1, followed
by 7 zeros (0), its final decimal value will be -128 (negative 128).
Output:
$ cc pgm2.c
$ a.out
-128

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


(a) volatile (b) extern
(c) typedef (d) static
Ans: [a]
Explanation: Volatile is not a storage class specifier in C.

145. What kinds of iterators can be used with vectors?


(a) Forward iterator
(b) Bi-directional iterator
(c) Random access iterator
(d) All of the above
Ans: [d]
Explanation: An iteration is like a pointer, indicating an element inside the container. All these types of iterations can be used with
vectors.

146. If only one memory location is to be reserved for a class variable, no matter how many objects are instantiated, then the variable should
be declared as
(a) extern (b) static
(c) volatile (d) const
Ans: [b]

147. What will be the output of the following C code on a 64-bit machine?
#include <stdio.h>
union Sti
{
int nu;
char m;
};
int main()
{
union Sti s;
printf ("%d", sizeof(s));
return 0;
}
(a) 8 (b) 5

Page 32 of 46
C LANGUAGE MCQ TEST

(c) 9 (d) 4
Ans: [d]
Explanation: Since the size of a union is the size of its maximum data type, here int is the largest data type. Hence the size of the union is
4.
Output:
$ cc pgm7.c
$ a.out
4

148. Which of the following will occur if we call the free () function on a NULL pointer?
(a) Compilation Error.
(b) Runtime Error.
(c) Undefined Behaviour.
(d) The program will execute normally.
Ans: [d]
Explanation: Calling the free () function on a NULL pointer is totally valid and will not cause any error.

149. Let p1 be an integer pointer with a current value of 2000. What is the content of p1 after the expression p1++ has been evaluated?
(a) 2001 (b) 2002
(c) 2004 (d) 2008
Ans: [c]
Explanation: The size of one pointer integer is 4 bytes. The current value of p1 is 2000. p1++ = p1 + 1
p1++ = 2004

150. In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to
each other.
(a) Loop unrolling (b) Strength reduction
(c) Loop concatenation (d) Loop jamming
Ans: [d]
Explanation: In loop jamming, the bodies of the two loops are merged together to form a single loop provided that they do not make
any references to each other.
In loop concatenation, the bodies of the two loops are concatenated together to form a series of loop, loop concatenation, sometimes help
to reduce complexity.
In loop unrolling, we try to optimize a program's execution speed. It is also known as space–time tradeoff.
In strength reduction compiler optimize expensive operations with equivalent but less expensive operations. So, option (D) is correct.

151. What will be the output of the following C function?


#include <stdio.h>
enum birds {SPARROW, PEACOCK, PARROT};
enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
int main ()
{
enum birds m = TIGER;
int k;
k = m;
printf ("%d\n", k);
return 0;
}
(a) 0 (b) Compile time error
(c) 1 (d) 8
Ans: [d]
Explanation: m is an integer constant; hence it is compatible.

Page 33 of 46
C LANGUAGE MCQ TEST

Output:
$ cc pgm5.c
$ a.out
8

152. Which of the following should be used to free memory from a pointer allocated using the “new” operator?
(a) free() (b) delete
(c) realloc () (d) None of the above.
Ans: [b]
Explanation: The delete keyword is used to free memory from a pointer allocated using the new keyboard.

153. Let p1 and p2 be integer pointers. Which one is a syntactically wrong statement?
(a) p1=p1=p2; (b) p1=p1-9;
(c) p2=p2+9; (d) cout < < p1-p2;
Ans: [a]

154. The translator which performs macro calls expansion is called:


(a) Macro processor (b) Micro pre-processor
(c) Macro pre-processor (d) Dynamic Linker
Ans: [c]
Explanation: The translator which performs macro calls expansion is called Macro pre - processor.
A Macro processor is a program that copies a stream of text from one place to another, making a systematic set of replacements as it
does so.
A Dynamic linker is the part of an operating system that loads and links the shared libraries.
The C preprocessor is a microprocessor that is used by compiler to transform your code before compilation. It is called micro pre-
processor because it allows us to add macros.
So, option (C) is correct.

155. What will be the output of the following C code?


#include <stdio.h>
int const print ()
{
printf("Sanfoundry.com");
return 0;
}
void main ()
{
print ();
}
(a) Error because function name cannot be preceded by const
(b) Sanfoundry.com
(c) Sanfoundry.com is printed infinite times
(d) Blank screen, no output
Ans: [b]

156. Which data structure is used to handle recursion in C?


(a) Stack (b) Queue.
(c) Deque. (d) Trees.
Ans: [a]
Explanation: Stacks are used to handling recursion in C.

Page 34 of 46
C LANGUAGE MCQ TEST

157. Suppose that cPtr is a character pointer, and its current content is 300. What will be the new value in cPtr after the following
assignment?
cPtr = cPtr + 5;
(a) 305 (b) 310
(c) 320 (d) 340
Ans: [a]
Explanation: cPtr = cPtr + 5
cPtr = 300 + 5
cPtr = 305

158. A language with string manipulation facilities uses the following operations.
head(s)- returns the first character of the string s
tail(s)- returns all but the first character of the string s
concat (sl, s2)- concatenates string s1 with s2.
The output of concat(head(s), head(tail(tail(s)))), where s is acbc is
(a) ab (b) ba
(c) ac (d) as
Ans: [a]

159. Will the following C code compile without any error?


#include <stdio.h>
int main ()
{
for (int k = 0; k < 10; k++);
return 0;
}
(a) Yes
(b) No
(c) Depends on the C standard implemented by compilers
(d) Error
Ans: [c]
Explanation: Compilers implementing C90 do not allow this, but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

160. What is the output of this C code?


#include <stdio.h>
main ()
{
int n = 0, m = 0;
if (n > 0)
if (m > 0)
printf("True");
else
printf("False");
}
(a) True

Page 35 of 46
C LANGUAGE MCQ TEST

(b) False
(c) No Output will be printed
(d) Run Time Error
Ans: [c]

161. What will be the output of the following code snippet?


#include <stdio.h>
#define CUBE(x) x * x * x
void solve () {
int ans = 216 / CUBE (3);
printf ("%d", ans);
}
int main () {
solve ();
return 0;
}
(a) 8 (b) 648
(c) 72 (d) None of the above.
Ans: [b]
Explanation: The macros are first written out exactly as labeled before the value of the expression is calculated. So, the expression
evaluates to (216 / 3) * 3 *3 = 648.

162. Which is valid expression in c language?


(a) int my_num = 100,000;
(b) int my_num = 100000;
(c) int my num = 1000;
(d) int my num == 10000;
Ans: [b]

163. Consider the following pseudocode:


x:=1;
i:=1;
while (x ≤ 500)
begin
x:=2x ;
i:=i+1;
end

What is the value of i at the end of the pseudocode?


(a) 4 (b) 5
(c) 6 (d) 7
Ans: [b]
Explanation: The program executes as:
In iteration 1: x = 2 and i = 2
In iteration 2: x = 4 and i = 3
In iteration 3: x = 16 and i = 4
In iteration 4: x = 16*16 and i = 5
In iteration 5: Condition incorrect.
So, option (B) is correct.

164. What is the output of this C code?

Page 36 of 46
C LANGUAGE MCQ TEST

#include <stdio.h>
int main ()
{
float f = 0.1;
if (f == 0.1)
printf("True");
else
printf("False");
}
(a) True
(b) False
Ans: [b]

165. What will be the output of the following code snippet?


#include <stdio.h>
struct School {
int age, rollNo;
};
void solve () {
struct School sc;
sc.age = 19;
sc. rollNo = 82;
printf ("%d %d", sc.age, sc.rollNo);
}
int main () {
solve ();
return 0;
}
(a) 19 82 (b) Compilation Error
(c) 82 19 (d) None of the above.
Ans: [a]
Explanation: The above code snippet assigns and then prints the contents of the struct given in the code.

166. If addition had higher precedence than multiplication, then the value of the expression (1 + 2 * 3 + 4 * 5) would be which of the
following?
(a) 27 (b) 47
(c) 69 (d) 105
Ans: [d]

167. What is the output of this C code?


#include <stdio.h>
main()
{
char *p = "Sanfoundry C-Test";
p[0] = 'a';
p[1] = 'b';
printf("%s", p);
}
(a) abnfoundry C-Test
(b) Sanfoundry C-Test

Page 37 of 46
C LANGUAGE MCQ TEST

(c) Compile time error


(d) Run time error
Ans: [d]
Output: $ cc pgm.c
$ a.out
Segmentation fault (core dumped)

168. Which of the following is not true about structs in C?


(a) No Data Hiding.
(b) Functions are allowed inside structs.
(c) Constructors are not allowed inside structs.
(d) Cannot have static members in the struct body.
Ans: [b]

169. What will be the output of this program?


int main()
{
int a=10, b=20;
printf("a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d b=%d",a,b);
return 0;
}
(a) a=20, b=20 b) a=10, b=20
(c) a=20, b=10 (d) a=10, b=10
Ans: [c]

170. Consider the following ANSI C code segment:


z=x + 3 + y->f1 + y->f2;
for (i = 0; i < 200; i = i + 2)
{
if (z > i)
{
p = p + x + 3;
q = q + y->f1;
} else
{
p = p + y->f2;
q = q + x + 3;
}
}
Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q,
and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and
the dereference operations (of the form y ->f1 or y ->f2) in the optimized code, respectively, are:
(a) 403 and 102 (b) 203 and 2
(c) 303 and 102 (d) 303 and 2
Ans: [d]

171. Comment on the output of following C code.

Page 38 of 46
C LANGUAGE MCQ TEST

#include <stdio.h>
main()
{
char *p = 0;
*p = 'a';
printf("value in pointer p is %c\n", *p);
}
(a) It will print a (b) It will print 0
(c) Compile time error (d) Run time error
Ans: [d]
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

172. What will be the output of the following code snippet?


#include <stdio.h>
struct School {
int age, rollNo;
};
void solve() {
struct School sc;
sc.age = 19;
sc.rollNo = 82;
printf("%d", (int)sizeof(sc));
}
int main() {
solve();
return 0;
}
(a) 1 (b) 4
(c) 8 (d) 16
Ans: [c]
Explanation: The size of a struct is equal to the sum of the sizes of its individual variables. With 2 integer types, the size is 4 + 4 = 8bytes.

173. The following statements are about EOF. Which of them is true?
(a) Its value is defined within stdio.h
(b) Its value is implementation dependent
(c) Its value can be negative
(d) Its value should not equal the integer equivalent of any character
(e) All of these
Ans: [e]

174. Which part of the program address space is p stored in the following C code?
#include <stdio.h>
int *p;
int main()
{
int i = 0;
p = &i;

Page 39 of 46
C LANGUAGE MCQ TEST

return 0;
}
(a) Code/text segment (b) Data segment
(c) Bss segment (d) Stack
Ans: [c]

175. What will be the output of the following code snippet?


#include <stdio.h>
union School {
int age, rollNo;
double marks;
};
void solve() {
union School sc;
sc.age = 19;
sc.rollNo = 82;
sc.marks = 19.04;
printf("%d", (int)sizeof(sc));
}
int main() {
solve();
return 0;
}
(a) 4 (b) 8
(c) 16 (d) 12
Ans: [b]
Explanation: The size of a Union is equal to the size of the largest variable which is a part of it. Here the variable is double which of size
8bytes.

176. What will be the output of the following code snippet?


#include <stdio.h>
#include<string.h>
void solve() {
char s[] = "Hello";
printf("%s ", s);
char t[40];
strcpy(t, s);
printf("%s", t);
}
int main() {
solve();
return 0;
}
(a) Hello Hello (b) Hello
(c) Compilation Error (d) None of the above.
Ans: [a]
Explanation: The strcpy() is a string library function of C, which copies the contents of one string into another string.

177. What will be the output of the following C function when EOF returns?
int fputs(char *line, FILE *fp)

Page 40 of 46
C LANGUAGE MCQ TEST

(a) ‘�’ character of array line is encountered


(b) ‘n’ character in array line is encountered
(c) ‘t’ character in array line is encountered
(d) When an error occurs
Ans: [d]

178. What will the output after execution of the following statements?
main()
{
printf ("\\n ab");
printf ("\\b si");
printf ("\\r ha");
}
(a) absiha (b) asiha
(c) haasi (d) hai
Ans: [d]

179. Study the following array definition


int num[10] = {3, 3, 3};
Which of the following statement is correct?
(a) num[9] is the last element of the array num
(b) The value of num [8] is 3
(c) The value of num [3] is 3
(d) None of the above
Ans: [a]

180. Study the following statement


for (i = 3; i < 15; i + = 3)
{printf ("%d", i);
++i;
}
What will be the output?
(a) 3 6 9 12 (b) 3 6 9 12 15
(c) 3 7 11 (d) 3 7 11 15
Ans: [c]

181. What is the output of the following code snippet?


#include <stdio.h>
#include<stdlib.h>
void set(int *to) {
to = (int*) malloc(5 * sizeof(int));
}
void solve() {
int *ptr;
set(ptr);
*ptr = 10;
printf("%d", *ptr);
}
int main() {
solve();
return 0;

Page 41 of 46
C LANGUAGE MCQ TEST

}
(a) 10 (b) Garbage value
(c) Cannot Say (d) The program may crash
Ans: [d]
Explanation: Since the set function makes a copy of the original pointer, the changes are not reflected back to the original pointer *ptr.
Now, if the pointer *ptr points to a random memory location before and after the set function, when we try to dereference it, the
program may crash.

182. Array is a _________ data structure.


(a) Non-linear (b) Primary
(c) Linear (d) Data type
Ans: [c]
Explanation:- An array is a non-primitive and linear data structure that only stores a similar data type.

183. Which of the following statement is correct about the array?


(a) In the array, users can only allocate the memory at the run time.
(b) In the array, users can only allocate the memory at the compile time.
(c) The array is a primitive and non-linear data structure that only stores a similar data type.
(d) All of the these
Ans: [b]
Explanation:- An array is a non-primitive and linear data structure that only stores a similar data type. In array, users can only allocate
the memory at the compile time.

184. Which of the following statement is correct about the C language?


(a) The C language is a binary language with some extra features.
(b) The C language is a high-level language with some low features.
(c) The C language is a mid-level language with some high features.
(d) The C language is a low-level language.
Ans: [c]
Explanation:- C is considered a middle-level language because it supports the feature of both low-level and high-level languages.
Today, many programmers refer to C as a low-level language because it lacks a large runtime system (no garbage collection, etc.). It
supports only scalar operations and provides direct memory addressing.

185. In the following program fragment, s and b are two integers:


b=s+b
s=b-s
b=b-s
What does it intend to do?
(a) Exchange the values of s and b
(b) Transfer the values of s and b
(c) Transfer the values of b and s
(d) Add or subtract the values of s and b
Ans: [a]
Explanation:- The intention of this program fragment is to exchange (swap) the values of s and b. Let us take an example for better
understand:
s=1
b=2
b=s+b
b=1+2
b=3
s=b-s

Page 42 of 46
C LANGUAGE MCQ TEST

s=3-1
s=2
b=b-s
b=3-2
b=1

186. Study the following program fragment


int i = 263;
putchar(i);
What will be the output of this program fragment?
(a) prints 263
(b) prints the ASCII equivalent of 263
(c) rings the bell
(d) Prints garbage
Ans: [c]
Explanation:- 263 is equivalent to binary number 100000111. If the user tries to print an integer as a character, only the last 8 bits are
considered, and the remaining ones are ignored. In this program, the ASCII value of 100000111 will be 00000111 (i.e., decimal 7).
Therefore, this program will print "ringing the bell".

186. Study the following statement


printf ("%d", 9/5);
What will be the output of this statement?
(a) 1.8 (b) 1.0
(c) 2.0 (d) None of the these
Ans: [d]
Explanation: -On execution, 9/5 will produce integer 1. If we print 1 as a floating number, only garbage will be printed.

187. A global variable is declared __________.


(a) Outside of the function
(b) Inside of the function
(c) With the function
(d) Anywhere in the program
Ans: [a]
Explanation:- A global variable is a variable that is declared outside of the function. A global variable can be used in all functions.

188. Who defines the user-defined function?


(a) Compiler (b) Computer
(c) Compiler library (d) Users
Ans: [d]
Explanation:- The user-defined functions are those functions that are defined by the user while writing the program. The user can
define these functions according to their needs.

189. Which of the following functions is already declared in the "header file"?
(a) User-define function (b) Built-in function
(c) C function (d) None of the these
Ans: [b]
Explanation:- Built-in functions are those functions whose prototypes are preserved in the header file of the "C" programming
language. These functions are called and executed only by typing their name in the program. For example, scanf(), printf(), strcat(), etc.

190. Which of the following operations cannot be performed in file handling?


(a) Open the file (b) Read the file
(c) To write a file (d) None of the these

Page 43 of 46
C LANGUAGE MCQ TEST

Ans: [d]
Explanation:- File handling is a process in which data is stored in a file using a program. The following operations can be performed
in file handling:
• Create a new file
• Open file
• Read the file
• Write the file
• Delete file
• File closing

191. Which of the following function is used to write the integer in a file?
(a) getw() (b) putw()
(c) int value (d) f_int()
Ans: [b]
Explanation:- The putw() is used to write the integer in a file.
Syntax:
putw(int i, FILE *fp);

192. Which of the following statement is correct about the ftell() function?
(a) It returns the current position.
(b) It sets the file pointer to the given position.
(c) It sets the file pointer at the beginning of the file.
(d) It reads a character from the file.
Ans: [a]
Explanation:- The ftell() function returns the current position of the file pointer in a stream.
Syntax of ftell() function:
long int ftell(FILE *stream)

193. Study the following program:


#include <stdio.h>
int main() {
int i = 5;
printf("%d", i = ++i == 6);
return 0;
}
What will be the output of this program?
(a) 2 (b) 6
(c) 4 (d) 1
Ans: [a]
Explanation: The expression can be treated as i = (++i == 6), because "==" is of higher precedence than "=" operator. In the inner
expression, ++i is equal to 6. Hence, the result is 1.

194. In which of the following modes, the user can read and write the file?
(a) r (b) w
(c) r + (d) b +
Ans: [c]
Explanation: r+ mode opens the text file in both reads and writes modes.

195. Which of the following keywords is used to prevent any kind of change in a variable?
(a) continue (b) const
(c) struct (d) extern
Ans: [b]

Page 44 of 46
C LANGUAGE MCQ TEST

Explanation: Constant is a variable whose value cannot be changed once assigned. Constant is also called literals. It can be of any basic
data type such as char, integer, float, and string. It can be defined anywhere in the program but in a new line. It is represented by the
const keyword.

196. Which of the following declarations is invalid in C language?


(a) char *str = "javatpoint is the best platform for learn";
(b) char str[] = "javatpoint is the best platform for learn";
(c) Char str[20] = "javatpoint is the best platform for learn";
(d) char[] str = "javatpoint is the best platform for learn";
Ans: [d]
Explanation: This declaration is valid in java language, but not in C language.

197. The enum keyword is used to assign names to the ________ constants.
(a) Integer (b) String
(c) Character (d) All of the these
Ans: [a]
Explanation: Enumeration is a user-defined data type in C language that is used to assign names to integral constants. It is represented
by the "enum" keyword.

198. Study the following program:


#include<stdio.h>
enum flg{a, b, c};
enum glf{c, e, f};
main()
{
enum flg h;
h = b;
printf("%d", h);
return 0;
}
What will be the output of this program?
(a) 1
(b) error: redeclaration of an enumerator
(c) h
(d) 3
Ans: [b]
Explanation: There is a declaration error in the output of this program because the declaration of the enum function is the same.

199. Which of the following operator's precedence order is correct (from highest to lowest)?
(a) %, *, /, +, - (b) %, +, /, *, -
(c) +, -, %, *, / (d) %, +, -, *, /
Ans: [a]
Explanation: The precedence of operator species that which operator will be evaluated first and next. When two operators share an
operand, the operator with the higher precedence goes first.

200. Study the following program:


main ()
{
if(5 < '5')
printf("5")
else
printf("Not equal to 5.")

Page 45 of 46
C LANGUAGE MCQ TEST

}
What will be the output of this program?
(a) ENQ (b) 5
(c) I (d) Not equal to 5
Ans: [b]
Explanation: This program will print 5 because '5' is a decimal value, and it is equal to 53 in the ASCII table. Therefore, the condition
is true and returns 5.

Page 46 of 46

You might also like