Sastra College C Programming MCQ Answer & Solution
Sastra College C Programming MCQ Answer & Solution
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.
6. #include <stdio.h>
Page 1 of 46
C LANGUAGE MCQ TEST
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.
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]
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]
Page 4 of 46
C LANGUAGE MCQ TEST
return 0;
}
(a) 2 (a) 15
(c) 16 (d) 18
Ans: [c]
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]
Page 5 of 46
C LANGUAGE MCQ TEST
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.
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]
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.
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’
Page 9 of 46
C LANGUAGE MCQ TEST
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.
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]
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.
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.
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]
Page 12 of 46
C LANGUAGE MCQ TEST
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]
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.
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.
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.
Page 15 of 46
C LANGUAGE MCQ TEST
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)
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]
Page 17 of 46
C LANGUAGE MCQ TEST
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]
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.
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]
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.
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.
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]
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]
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.
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.
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.
Page 24 of 46
C LANGUAGE MCQ TEST
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
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);
}
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.
Page 28 of 46
C LANGUAGE MCQ TEST
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.
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
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
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.
Page 31 of 46
C LANGUAGE MCQ TEST
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.
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]
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]
Page 35 of 46
C LANGUAGE MCQ TEST
(b) False
(c) No Output will be printed
(d) Run Time Error
Ans: [c]
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]
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]
Page 37 of 46
C LANGUAGE MCQ TEST
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)
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]
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
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]
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.
Page 42 of 46
C LANGUAGE MCQ TEST
s=3-1
s=2
b=b-s
b=3-2
b=1
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.
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)
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.
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.
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.
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