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 below code snippet?

#include<stdio.h>

main() 
{
   for()printf("Hello");
}

A - Infinite loop

B - Prints Hello once.

C - No output

D - Compile error

Answer : D

Explanation

Compiler error, semi colons need to appear though the expressions are optional for the for loop.

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 read mode

B - Trying to open a.txt in write mode.

C - stream is an invalid identifier

D - Compile error

Answer : D

Explanation

Compile error, second argument for fopen is invalid, should be a string.

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

#include<stdio.h>

main()
{	
   union abc {
      int x;
      char ch;
   }var;

   var.ch = 'A';
   printf("%d", var.x);
}

A - A

B - Garbage value

C - 65

D - 97

Answer : C

Explanation

65, as the union variables share common memory for all its elements, x gets A whose ASCII value is 65 and is printed.

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 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 - The binary equivalent of 50 is,

A - 110010

B - 1010110

C - 101

D - 101.011.00.00

Answer : A

Explanation

#include <stdio.h>

 int main()
{
   longintdecimalNumber,remainder,quotient;
intbinaryNumber[100], i = 1, j;

 printf("Enter any decimal number: ");
 scanf("%ld",&decimalNumber);
 quotient = decimalNumber;
while(quotient!=0){
           binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
 }
 printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return0;
}

Q 7 - The equivalent pointer expression by using the array elementa[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 elementis a[i][j] = *(*(a+i)+j)

If, the array elementis a[i][j][k]= *(*(*(a+i)+j)+k)

Q 8 - Which of the following statement can be used to free the allocated memory?

A - remove(var-name);

B - free(var-name);

C - vanish(var-name);

D - erase(var-name);

Answer : B

Explanation

The library functionfree()deallocates the memory allocated by calloc(), malloc(), or realloc().

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 aconstvariable cant be modified.

#include<stdio.h>

int main()
{
   const int i = 0;
    
   printf("%d\n", i++);
   return 0;
}

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