C Programming Online Quiz


Advertisements


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

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

#include<stdio.h>

main() 
{
   int x = 5;
   
   if(x=5)
   {	
      if(x=5) printf("Hello");
   } 
   printf("Hi");
}

A - HelloHi

B - Hi

C - Hello

D - Compiler error

Answer : A

Explanation

HelloHi, both the if statement’s expression evaluates to be true.

Q 2 - Which operator is used to continue the definition of macro in the next line?

A - #

B - ##

C - $

D - \

Answer : D

Explanation

\, the first two are stringize and token pasting operators respectively. There is no such operator called $.

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

#include<stdio.h>

main()
{ 
   int a[3] = {2,,1};
   
   printf("%d", a[a[0]]); 
}

A - 0

B - 1

C - 2

D - Compile error

Answer : D

Explanation

Compile error, invalid syntax in initializing the array.

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 : A

Explanation

stdout is the identifier declared in the header file stdio.h which is connected to standard output device (monitor).

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

#include<stdio.h>

main()
{ 
   int x = 3;
   
   x += 2;
   x =+ 2;
   printf("%d", x); 
}

A - 2

B - 5

C - 7

D - Compile error

Answer : A

Explanation

+ in unary form is dummy operator, therefore ‘x’ is overwritten with the value +2 finally.

Q 6 - Which header statement is missing in the given below program to get the desired output?

#include<stdio.h>
#include<math.h>

int main ()
{
   double x = 1234321;
  
   double result = sqrt(x);
  
   printf("The square root of %.2lf is %.2lf\n", x, result);
   return 0;
}

A - #include<defs.h>

B - #include<math.h>

C - #include<stdlib.h>

D - Above program is absolutely correct to give desired result

Answer : B

Explanation

In C programming, math.h is a header file in the standard library designed for basic mathematical operations.

   Output of above code: The square root of 1234321.00 is 1111.00
#include<stdio.h>
#include<math.h>

int main ()
{
   double x = 1234321;
  
   double result = sqrt(x);
  
   printf("The square root of %.2lf is %.2lf\n", x, result);
   return 0;
}

Q 7 - What function can be used to free the memory allocated by calloc()?

A - dealloc();

B - strcat();

C - free();

D - memcpy();

Answer : C

Explanation

calloc(): Allocates space for an array elements, initializes to zero and then returns a pointer to memory.

Free(): Dellocate the space allocated by calloc()

Q 8 - Which of the following variable cannot be used by switch-case statement?

A - char

B - int

C - float

D - Double

Answer : C

Explanation

Switch Case only accepts integer values for case label, float values can’t be accepted in case of Switch Case.

#include<stdio.h>

int main ()
{
   i = 1.8
   
   switch ( i )
   {
      case 1.6:
               printf ("Case 1.6");
               break;
      case 1.7:
               printf ("Case 1.7");
               break;
      case 1.8:
               printf ("Case 1.8");
               break;
       default :
               printf ("Default Case ");
   }

}

Answer : B

Explanation

As per the operators preference.

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;
}


cprogramming_questions_answers.htm

Advertisements