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) 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.

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

#include<stdio.h>

main()
{
   int x = 1;
   
   do
      printf("%d ", x);
   while(x++<=1);
}

A - 1

B - 1 2

C - No output

D - Compile error

Answer : B

Explanation

1 2, do..while is an entry control loop. As the expression x++ is post form loop continues for 2nd time also.

Q 3 - Function fopen() with the mode "r+" tries to open the file for __

A - reading and writing

B - reading and adding new content

C - only for reading

D - it works only for directories

Answer : A

Explanation

Option (a), the file should exist and opens for both reading & writing.

Q 4 - To store a word/sentence declare a variable of the type ‘string’.

A - true

B - false

Answer : B

Explanation

There is no such data type called ‘string’ in C language.

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

#include<stdio.h>

main()
{ 
   char *s = "Abc";
   
   while(*s)
      printf("%c", *s++);
}

A - Abc

B - bc

C - Compile error

D - Runtime error

Answer : A

Explanation

Loop continues until *s not equal to ‘\0’, hence printing “Abc” where character is fetched first and address is incremented later.

Q 6 - In normalized form, if the binary equivalent of 5.375 is “0100 0000 1010 1100 0000 0000 0000 0000” then what will be the output of the program in Intel core machine?

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

int main ()
{
   float a = 5.375;
   char *p;
   int i;
   
   p = (char*)&a;
   for(i=0; i <= 3; i++)
      printf("%02x\n", (unsigned char)p[i]);
   return 0;
}

A - 40 AC 00 00

B - 00 00 AC 40

C - 00 00 CA 04

D - None

Answer : B

Explanation

Depends upon the machine whether its big endian or small endian machine. Byte by byte is fetched from right end.

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

int main ()
{
   float a = 5.375;
   char *p;
   int i;
   
   p = (char*)&a;
   for(i=0; i <= 3; i++)
      printf("%02x\n", (unsigned char)p[i]);
   return 0;
}

Q 7 - Which header file supports the functions - malloc() and calloc()?

A - stdlib.h

B - memory.h

C - math.h

D - stdio.h

Answer : A

Explanation

void *malloc(size_t size) : Allocates the requested memory and returns a pointer to it.

void *calloc(size_t nitems, size_t size): Allocates the requested memory and returns a pointer to it.

Q 8 - fgets() function is safer than gets() because in fgets() function you can specify the size of the buffer into which the supplied string will be stored.

A - True

D - False

Answer : A

Explanation

Both functions retrive and store a string from console or file, but fgets() functions are more safer to use then gets() because gets() doesn't facilitate to detail the length of the buffer to store the string in and fgets() facilitates to specify a maximum string length.

   char *fgets(char *s, int size, FILE *stream);
   char *gets(char *s);

Q 9 - 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 use to print out float value and %lf can be use to print out  double value.

Q 10 - 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.


cprogramming_questions_answers.htm

Advertisements