Mcq on C Programming
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 statement
Answer : D
Explanation
Option (d), as the size of int and unsigned is same, no problem in allocating memory.
Q 2 - What is the size of the following union definition?
#include<stdio.h>
union abc {
char a,b,c,d,e,f,g,h;
int i;
}abc;
main()
{
printf( "%d", sizeof( abc ));
}
A - 1
B - 2
C - 4
D - 8
Answer : C
Explanation
union size is biggest element size of it. All the elements of the union share common memory.
Q 3 - Identify the invalid constant used in fseek() function as ‘whence’ reference.
A - SEEK_SET
B - SEEK_CUR
C - SEEK_BEG
D - SEEK_END
Answer : C
Explanation
SEEK_BEG, all the rest are valid constants defined in ‘stdio.h’
Q 4 - Choose the invalid predefined macro as per ANSI C.
A - __FILE__
B - __DATE__
C - __TIME__
D - __C++__
Answer : D
Explanation
There is no macro define with the name __C++__, but __cplusplus is defined by ANSI)
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 - The C library function rewind() reposition the file pointer at the beginning of the file.
A - True
B - False
Answer : A
Explanation
void rewind(FILE *stream): In C, the rewind function reposition the file position to the beginning of the file of the given stream. It also erases the error and end-of-file indicators for stream.
Q 7 - Similarity between a structure, union and enumeration,
A - All are helpful in defining new variables
B - All are helpful in defining new data types
C - All are helpful in defining new pointers
D - All are helpful in defining new structures
Answer : B
Explanation
A structure, union and enumeration all of them can define a new data type.
Q 8 - In the given below code, the P2 is
Typedef int *ptr;
ptr p1, p2;
A - Integer
B - Integer pointer
C - Both, Integer & Integer pointer
D - None of above
Answer : B
Explanation
Ptr is an alias to int*.
Q 9 - What will be the resultant of the given below program?
#include<stdio.h>
#include<stdarg.h>
Void fun(char *msg, ...);
int main ()
{
fun("IndiaMAX", 1, 4, 7, 11, 0);
return 0;
}
void fun(char *msg, ...)
{
va_list ptr;{
int num;
va_start(ptr, msg);
num = va_arg(ptr, int);
num = va_arg(ptr, int);
printf("%d", num);
}
A - IndiaMAX 1, 7, 11, 0
B - IndiaMAX 1, 7
C - Only 4
D - 1, 7, 11, 0
Q 10 - What will be the output of the given below code?
#include<stdio.h>
int main()
{
const int *ptr = &i;
char str[] = "Welcome";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}
A - Welcome
B - 0
C - Wel
D - Come
Answer : A
Explanation
Although, char str[] = "Welcome"; and s = str;, the program will print the value of s.
#include<stdio.h>
int main()
{
const int *ptr = &i;
char str[] = "Welcome";
s = str;
while(*s)
printf("%c", *s++);
return 0;
}
No comments:
Post a Comment