C Programming - Online Quiz



Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answers and click over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer button. You can use Next Quiz button to check new set of questions in the quiz.

Questions and Answers
cprogramming_questions_answers.htm

Q 1 - What is the output of the following code snippet?

#include<stdio.h>

main() 
{
   short unsigned int i = 0; 
   
   printf("%u\n", i--);
}

A - 0

B - Compile error

C - 65535

D - 32767

Answer : A

Explanation

0, with post decrement operator value of the variable will be considered as the expressions value and later gets decremented.

Q 2 - What is the output of the following program?

#include<stdio.h>

main()
{ 
   struct { int x;} var = {5}, *p = &var;
   
   printf("%d %d %d",var.x,p->x,(*p).x); 
}

A - 5 5 5

B - 5 5 garbage value

C - 5 5 0

D - Compile error

Answer : A

Explanation

5 5 5, the two possible ways of accessing structure elements using pointer is by using -> (arrow operator) OR *.

Q 3 - First operating system designed using C programming language.

A - DOS

B - Windows

C - UNIX

D - Mac

Answer : C

Explanation

UNIX. C actually invented to write an operation 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>

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.

Q 5 - 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 : B

Explanation

After s++, s points the string ++.

Q 6 - Turbo C in 16 bit DOS OS, the correct range of long double is,

A - 3.4E-4932to 3.4E+4932

B - 3.4E-4932to 1.1E+4932

C - 4.1E-4932to 5.1E+4932

D - 0.7E-4932to 1.8E+4932

Answer : B

Explanation

Explanation: The integral and precession value varies depending upon the number of bytes designated for a particular data type.

Q 7 - For a structure, if a variable behave as a pointer then from the given below operators which operator can be used to access data of the structure via the variable pointer?

A - .

B - %

C - ->

D - #

Answer : C

Explanation

For a structure, Dot(.) operator can be used to access the data using normal structure variable and arrow (->)can be used to access the data using pointer variable.

Q 8 - In the given below code, the functionfopen()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 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;
}

Answer : A

Explanation

Unary operator acts on single expression.

Q 10 - If, the given below code finds the length of the string then what will be the length?

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}

A - Code returns error

B - Code returns the length 8

C - Code returns the length 6

D - Code returns the length 2

Answer : B

Explanation

Here, *s is char pointer that holds character string. To print whole string we use printf("%s",s) by using base address. s contains base address (&s[0]) and printf will print characters until '\0' occurs. *s only gives first character of input string, but s++ will increase the base address by 1 byte. When *s=='\0' encountered, it will terminate loop.

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}

Advertisements