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
cprogramming_questions_answers.htm

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

#include<stdio.h>

main() 
{ 
   const int a = 5; 
   
   a++; 
   printf("%d", a); 
}

A - 5

B - 6

C - Runtime error

D - Compile error

Answer : D

Explanation

Compile error - constant variable cannot be modified.

main() 
{ 
   const int a = 5; 
   
   a++; 
   printf("%d", a); 
}

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

#include<stdio.h>

void f() 
{
    printf(“Hello\n”);
}
main() 
{
 ;
}

A - No output

B - Error, as the function is not called.

C - Error, as the function is defined without its declaration

D -Error, as the main() function is left empty

Answer : A

Explanation

No output, apart from the option (a) rest of the comments against the options are invalid.

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

#include<stdio.h>

main()
{
   int i = 1;
   
   Charminar:
   printf("%d ",i++);
   if(i==3) break;
   if(i<=5) goto Charminar;
}

A - 1 2

B - 1 2 3

C - 1 2 4 5

D - Compile error

Answer : D

Explanation

Compile error, wrong place for ‘break’ to appear.

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>

int main();
void main()
{
   printf("Okay"); 
}

A - Okay

B - No output

C - Compile error. We cannot declare main() function.

D - Compile error. Mismatch in declaration & definition.

Answer : D

Explanation

It’s compile error as the declaration of main() mismatches with the definition.

Q 6 - int fun(); - The declaration indicates the presence of a function defined inside the current module or in the same file.

A - True

B - False

Answer : A

Explanation

The function definition can even appear in another source code and can be linked from library while linking.

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 function fopen()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 : B

Explanation

square parenthesis signify as array at declaration and type is int*, so array of integer pointers.

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

Advertisements