Top 50 C PROGRAMMING MCQ Questions and Answers with Explanation

Q 1 - What is your comment on the below C statement?
signed int *p=(int*)malloc(sizeof(unsigned int));
A - Improper type casting
B - Would throw Runtime error
C - Memory will be allocated but cannot hold an int value in the memory
D - No issue with the statement
Answer : D
Explanation
Option (d), as the size of int and unsigned, is the same, no problem in allocating memory.
Q 2 - What is the following program doing?
#include<stdio.h>
main()
{
FILE *stream=fopen("a.txt",'r');
}
A - Trying to open “a.txt” in reading mode
B - Trying to open “a.txt” in write mode.
C - “stream” is an invalid identifier
D - Compile error
Answer : D
Explanation
Compile error, the second argument for fopen is invalid, should be a string.
Q 3 - First operating system designed using a C programming language.
A - DOS
B - Windows
C - UNIX
D - Mac
Answer: C
Explanation
UNIX. C actually invented to write an operating system called UNIX. By 1973 the entire UNIX OS is designed using C.
Q 4 - What is the output of the following program?
#include<stdio.h>
main()
{
fprintf(stdout,"Hello, World!");
}
A - Hello, World!
B - No output
C - Compile error
D - Runtime error
Answer: C
Explanation
stdout is the identifier declared in the header file stdio.h, need to include the same.
Q 5 - What is the built-in library function to compare two strings?
A - string_cmp()
B - strcmp()
C - equals()
D - str_compare()
Answer: B
Explanation
strcmp() is the built in function from “string.h” to compare two strings. Returns 0 if both are same strings. Returns -1 if first < second string. Returns 1 first > second.
Q 6 - Choose the correct function which can return a reminder by dividing -10.0/3.0?
A - rem = mod(-10.0, 3.0);
B - rem = fmod(-10.0, 3.0);
C - rem = modf(-10.0, 3.0);
D - Division of floating-point values can’t return reminder
Answer: B
Explanation
In the C Programming Language, the fmod() function compute and returns the floating-point remainder when x is divided by y.
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = -10.0, y = 3.0, z;
z = fmod( x, y );
printf( "The remainder of %.2f / %.2f is %f\n", w, x, z );
}
Q 7 - Which of the following is a logical NOT operator?
A - !
B - &&
C - &
D - All of the above
Answer: A
Explanation
Logical NOT operator will make false.
Q 8 - What is x in the following program?
#include<stdio.h>
int main ()
{
typedef char (*(*arrfptr[3])())[10];
arrfptr x
return 0;
}
A - x is a character pointer
B - x is an array of pointer
C - x is an array of three function pointers
D - Wrong declaration
Answer: C
Explanation
Here, x is an array of three function pointers
#include<stdio.h>
int main ()
{
typedef char (*(*arrfptr[3])())[10];
arrfptr x
return 0;
}
Q 9 - What will be the output of the following program?
#include<stdio.h>
int main()
{
const int i = 0;
printf("%d\n", i++);
return 0;
}
A - 100
B - Infinity
C - 0
D - Return error
Answer : D
Explanation
It is because ++needs a value and a const variable can’t be modified.
#include<stdio.h>
int main()
{
const int i = 0;
printf("%d\n", i++);
return 0;
}
Q 10 - extern int fun(); - The declaration indicates the presence of a global function defined outside the current module or in another file.
A - True
B - False
Answer: A
Explanation
Extern is used to resolving the scope of global identifiers.
Q11 - Which of the following functions disconnects the stream from the file pointer.
A - fremove()
B - fclose()
C - remove()
D - file pointer to be set to NULL
Answer: B
Explanation
fclose(), it flushes the buffers associated with the stream and disconnects the stream with the file.
Q12 - According to ANSI specification, how to declare the main () function with command-line arguments?
A - int main(int argc, char *argv[])
B - int char main(int argc, *argv)
C - int main()
{
Int char (*argv argc);
}
D - None of the above
Answer: A
Explanation
Sometimes, it becomes necessary to deliver command line values to the C programming to execute the particular code when the code of the program is controlled from outside. Those command line values are called command-line arguments. The command-line arguments are handled by the main() function.
Declaration of main () with command-line argument is,
int main(int argc, char *argv[])
Where argc refers to the number of arguments passed, and argv[] is a pointer array that points to each argument passed to the program.
Q13 - Which statement can print \n on the screen?
A - printf("\\n");
B - printf("n\");
C - printf("n");
D - printf('\n');
Answer: A
Explanation
Option A is the correct answer. In the C programming language, "\n" is the escape sequence for printing a new line character. In printf("\\n"); statement, "\\" symbol will be printed as "\" and “n” will be known as a common symbol.
Q14 - What is the output of the following program?
#include<stdio.h>
void main()
{
char s[] = "C++";
printf("%s ",s);
s++;
printf("%s",s);
}
A - C++ C++
B - C++ ++
C - ++ ++
D - Compile error
Answer : D
Explanation
‘s’ refers to a constant address and cannot be incremented.
Q15 - To print a float value which format specifier can be used?
A - %f
B - %lf
C - %Lf
D - None of the above
Answer: A
Explanation
%f can be used to print out float value and %lf can be used to print out the double value.
Q16 - What is the output of the following program?
#include<stdio.h>
main()
{
fprintf(stdout,"Hello, World!");
}
A - Hello, World!
B - No output
C - Compile error
D - Runtime error
Answer: C
Explanation
stdout is the identifier declared in the header file stdio.h, need to include the same.
Q17 - In the given below code, the function fopen()uses "r" to open the file “source.txt” in binary mode for which purpose?
#include<stdio.h>
int main ()
{
FILE *fp;
fp = fopen("source.txt", "r");
return 0;
}
A - For reading
B - For reading and writing
C - For creating a new file "source.txt" for reading
D - For creating a new file "source.txt" for writing
Answer: A
Explanation
To open a file in C programming, we can use the library function fopen(). In the given above code, fopen() function is opening a file “source.txt” for reading. Here, “r” stands for reading. If, fopen() function does not find any file for reading, returns NULL
#include<stdio.h>
int main ()
{
FILE *fp;
fp = fopen("source.txt", "r");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
Q18 - In the given below code, what will be the value of a variable x?
#include<stdio.h>
int main()
{
int y = 100;
const int x = y;
printf("%d\n", x);
return 0;
}
A - 100
B - 0
C - Print x
D - Return Error
Answer: A
Explanation
Although, integer y = 100; and constant integer x is equal to y. here in the given above program, we have to print the x value, so that it will be 100.
Q19 - The type name/reserved word ‘short’ is ________.
A - short long
B - short char
C - short float
D - short int
Answer : D
Explanation
short is used as an alternative of short int.
Q20 - What is the output of the following code snippet?
#include<stdio.h>
main()
{
char c = 'A'+255;
printf("%c", c);
}
A - A
B - B
C - Overflow error at runtime
D - Compile error
Answer: A
Explanation
A range of ASCII values for the ASCII characters is 0-255. Hence the addition operation circulates back to ‘A’
Q21 - What is the output of the following program?
#include<stdio.h>
main ()
{
int i, j;
for(i=5, j=1; i>j; i--, ++j)
}
A - 5 2, 4 2
B - Compile error
C - 4 2
D - 5 1, 4 2
Answer : D
Explanation
5 1, 4 2- The condition fails when i=j=3.
#include<stdio.h>
main ()
{
int i, j;
for(i=5, j=1; i>j; i--, ++j)
}
Q22 - In C, what is the correct hierarchy of arithmetic operations?
A - */ + -
B - * +- /
C - / *+ -
D - + - / *
Answer: C
Explanation
In C, there are 5 arithmetic operators (+, -, *, /, and %) that can be used in performing arithmetic operations.
Q23 - Linker generates ______ file.
A - Object code
B - Executable code
C - Assembly code
D - None of the above.
Answer: B
Explanation
(b). The linker links the object code of your program and library code to produce an executable.
Q24 - What is the built-in library function to compare two strings?
A - string_cmp()
B - strcmp()
C - equals()
D - str_compare()
Answer: B
Explanation
strcmp() is the built in function from “string.h” to compare two strings. Returns 0 if both are same strings. Returns -1 if first < second string. Returns 1 first > second.
Q25 - What is the output of the following program?
#include<stdio.h>
main()
{
int a[] = {1,2}, *p = a;
printf("%d", p[1]);
}
A - 1
B - 2
C - Compile error
D - Runtime error
Answer: B
Explanation
2, as ‘p’ holds the base address then we can access the array using ‘p’ just like with 'a’
Q26 - The default executable generation on UNIX for a C program is ___
A - a.exe
B - a
C - a.out
D - out.a
Answer: C
Explanation
"a.out" is the default name of the executable generated on both the UNIX and Linux operating systems.
Q27 - What is the output of the following program?
#include<stdio.h>
main ()
{
int a=1, b=2, *p=&a, *q=&b, *r=p;
p = q; q = r;
printf("%d %d %d %d\n",a,b,*p,*q);
}
A - 1 2 2 1
B - 2 1 2 1
C - 1 2 1 2
D - Compile error
Answer: A
Explanation
1 2 2 1, the pointers are swapped not the values.
#include<stdio.h>
main ()
{
int a=1, b=2, *p=&a, *q=&b, *r=p;
p = q; q = r;
printf("%d %d %d %d\n",a,b,*p,*q);
}
Q28 - Which library functions help users to dynamically allocate memory?
A - memalloc()and alloc()
B - malloc() and memalloc()
C - malloc() and calloc()
D - memalloc() and calloc()
Answer: C
Explanation
the malloc(): Allocates an area of memory that is large enough to hold the "n” number of bytes and initializes contents of memory to zeroes.
calloc(): Allocates "size" of bytes of memory and contains garbage values.
Q29 - The correct order of evaluation for the expression “z = x + y * z / 4 % 2 – 1”
A - * / % = + -
B - / * % - + =
C - - + = * % /
D - * / % + - =
Answer : D
Explanation
* / % holds highest priority than + - . All with left to right associativity.
Q30 - Identify the incorrect file opening mode from the following.
A - r
B - w
C - x
D - a
Answer: C
Explanation
x, there is no such mode called “x”.
Q31 - The equivalent pointer expression by using the array element a[i][j][k][2],
A - ((((a+m)+n)+o)+p)
B - *(*(*(*(a+i)+j)+k)+2)
C - *( (((a+m)+n)+o+p)
D - *( ((a+m)+n+o+p)
Answer: B
Explanation
If, the array element is a[i][j] = *(*(a+i)+j)
If, the array element is a[i][j][k]= *(*(*(a+i)+j)+k)
Q32 - Which of the following is a logical AND operator?
A - !
B - &&
C - ||
D - &
Answer: B
Explanation
Two immediate ampersands (&) symbols are logical AND operator.
Q33 - The library function strrchr() finds the first occurrence of a substring in another string.
A - Yes
B - Strstr()
C - strchr()
D - strnset()
Answer: B
Explanation
strstr() finds the first occurrence of a substring in another string.
Q34 - What is the built-in library function to adjust the allocated dynamic memory size.
A - malloc
B - calloc
C - realloc
D - resize
Answer: C
Explanation
There is no built-in function with the name resize(). Malloc() & calloc() allocates memory but won’t resize.
Q35 - Choose the invalid identifier from the below
A - Int
B - volatile
C - DOUBLE
D - __0__
Answer: B
Explanation
volatile is the reserved keyword and cannot be used an identifier name.
Q36 - Does the following program compiles?
#include “stdio.h”
A - It fails as there is no main() function
B - It fails as the header file is enclosed in double quotes
C - It compiles and executes to produce no displayable output
D - It compiles.
Answer : D
Explanation
(d), It compiles successfully and can’t be executed. Use gcc –c option to compile the same with the command-line compiler (UNIX/Linux) or just compile without build in an IDE.
Q37 - Which of the following statement shows the correct implementation of nested conditional operation by finding the greatest number out of three numbers?
A - max = a>b ? a>c?a:c:b>c?b:c
B - a=b ? c=30;
C - a>b : c=30 : c=40;
D - return (a>b)?(a:b) ?a:c:b
Answer: A
Explanation
Syntax is exp1?exp2:exp3. Any ‘exp’ can be a valid expression.
Q38 - int x=~1; What is the value of 'x'?
A - 1
B - -1
C - 2
D - -2
Answer : D
Explanation
-2, the one’s complement of 1 is 1110 (binary) which is equivalent to two’s complement of 2, ie -2.
Q39 - In DOS, how many bytes exist for near, far, and huge pointers?
A - Near: 2, far: 4, huge: 7
B - near: 4, far: 2, huge: 8
C - near: 2, far: 4, huge: 4
D - near: 4, far: 0, huge: 0
Answer: C
Explanation
In DOS, numbers of byte exist for near pointer = 2, far pointer = 4 and huge pointer = 4.
In Windows and Linux, numbers of bytes exist for near pointer = 4, far pointer = 4, and huge pointer = 4.
Q40 - Identify the C compiler of UNIX.
A - gcc
B - cc
C - Borland
D - vc++
Answer: B
Explanation
‘cc’ full form is C Compiler and is the compiler for UNIX. gcc is a GNU C compiler for Linux. Borland and vc++ (Microsoft visual c++) for windows.
Q41 - What is the output of the following program?
#include<stdio.h>
void f()
{
static int i = 3;
printf("%d ", i);
if(--i) f();
}
main()
{
f();
}
A - 3 2 1 0
B - 3 2 1
C - 3 3 3
D - Compile error
Answer: B
Explanation
As the static variable retains its value from the function calls, the recursion happens thrice.
Q42 - The return keyword used to transfer control from a function back to the calling function.
A - Yes
B - Switch
C - go back
D - goto
Answer: A
Explanation
In C, the return function stops the execution of a function and returns control with value to the calling function. Execution begins in the calling function by instantly following the call.
Q43 - What do you mean by “int (*ptr)[10]”
A - ptr is an array of pointers to 10 integers
B - ptr is a pointer to an array of 10 integers
C - ptr is an array of 10 integers
D - Invalid statement
Answer: B
Explanation
Explanation: with or without the brackets surrounding the *p, still the declaration says it’s an array of pointer to integers.
Q44 - In the given below statement, what does the “pf” indicate?
int (*pf)();
A - pf is a pointer of a function that returns an int
B - pf is a pointer
C - pf is a function pointer
D - None of the above
Answer: A
Explanation
pf is a pointer as well holds some functions reference.
Q45 - What is the output of the following program?
#include<stdio.h>
main()
{
int x = 65, *p = &x;
void *q=p;
char *r=q;
printf("%c",*r);
}
A - Garbage character.
B - A
C - 65
D - Compile error
Answer: B
Explanation
The void pointer is a generic pointer and can hold any variable’s address. ASCII character for the value 65 is ‘A’
Q46 - How many times the given below program will print "India"?
#include<stdio.h>
int main ()
{
int x;
for(x=-1; x<=20; x++)int i;
{
if(x < 10)
continue;
else
break;
printf("India");
}
A - Unlimited times
B - 21 times
C - 0 times
D - 20 times
Answer: C
Explanation
Following for loop, there is only one statement, that is int i; break & continue are appearing outside for block which is compile error
#include<stdio.h>
int main ()
{
int x;
for(x=-1; x<=20; x++)int i;
{
if(x < 10)
continue;
else
break;
printf("India");
}
Q47 - What is the output of the following code snippet?
#include<stdio.h>
main()
{
int x = 5;
if(x==5)
{
if(x==5) break;
printf("Hello");
}
printf("Hi");
}
A - Compile error
B - Hi
C - HelloHi
D - Hello
Answer: A
Explanation
compile error, keyword break can appear only within loop/switch statement.
Q48 - What is the output of the following code snippet?
#include<stdio.h>
main()
{
int x = 5;
if(x=5)
{
if(x=5) break;
printf("Hello");
}
printf("Hi");
}
A - Compile error
B - Hi
C - HelloHi
D - Compiler warning
Answer: A
Explanation
compile error, keyword break can appear only within loop/switch statement.
Q49 - Which of the following is used in the mode string to open the file in binary mode?
A - a
B - b
C - B
D - bin
Answer: B
Explanation
To perform unformatted data I/O a file is opened in binary mode and is represented with the alphabet ‘b’ in the mode string.
Also Read:
Commonly Asked C PROGRAMMING Interview Questions and Answers for Experienced and Freshers