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(1;2;3)
      printf("Hello");
}

A - Infinite loop

B - Prints Hello once.

C - No output

D - Compile error

Answer : A

Explanation

infinite loop, as the second expression is non-0, hence the condition is always true.

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

#include<stdio.h>

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

A - 1

B - 2

C - Compile error

D - Runtime error

Answer : B

Explanation

2, as p holds the base address then we can access array using p just like with a

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 - What is the output of the following program?

#include<stdio.h>

main() 
{
}

A - No output

B - Garbage

C - Compile error

D - Runtime error

Answer : A

Explanation

It is valid to have main() function empty, therefore producing no displayable output.

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

#include<stdio.h>

main()
{ 
   int x;
   float y;
   
   y = x = 7.5;
   printf("x=%d y=%f", x, y);
}

A - 7 7.000000

B - 7 7.500000

C - 5 7.500000

D - 5 5.000000

Answer : A

Explanation

x gets the integral value from 7.5 which is 7 and the same is initialized to y.

Q 6 - A Variable name in C includes which special symbols?

A - * (asterisk)

B - # (Hash)

C - + (Addition)

D - _ (underscore)

Answer : D

Explanation

Characters which are allowed and not allowed with variable name,

  • Underscore(_) allowed
  • Capital Letters ( A Z ) allowed
  • Small Letters ( a z ) allowed
  • Digits ( 0 9 ) allowed
  • First Charactershould bealphabet or Underscore
  • Blanks & Commasnot allowed
  • Special Symbols not allowed, but Underscore(_)allowed
  • Reserved Word not allowed

Q 7 - Which library function can convert aninteger/longto astring?

A - ltoa()

B - ultoa()

C - sprintf()

D - None of the above

Answer : A

Explanation

In C, ltoa() function converts long/integer data type to string data type.

char *ltoa(long N, char *str, int base);

Q 8 - What is x in the following program?

#include<stdio.h>

int main ()
{
   typedef char (*(*arrfptr[3])())[10];
   arrfptr x
   return 0;
}

A - x is a character pointer

B - x is an array of pointer

C - x is an array of three function pointers

D - Wrong declaration

Answer : C

Explanation

Here, x is an array of three function pointers

#include<stdio.h>

int main ()
{
   typedef char (*(*arrfptr[3])())[10];
   arrfptr x
   return 0;
}

Q 9 - Which files will get closed through thefclose()in the following program?

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

A - "ABC"

B - "ACD"

C - "ADF"

D - Return error

Answer : D

Explanation

The syntax of fclose() function is wrong; it should be int fclose(FILE *stream); closes the stream. Here, fclose(fp, fs, ft); is using separator(,) which returns error by saying that extra parameter in call to fclose() function.

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

Q 10 - extern int fun(); - The declaration indicates the presence of a global function defined outside the current module or in another file.

A - True

B - False

Answer : A

Explanation

Extern is used to resolve the scope of global identifier.

Advertisements