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

#include<stdio.h>

main() 
{ 
   char s[]="hello", t[]="hello";
   
   if(s==t){
	   printf("eqaul strings");
	}
}

A - Equal strings

B - Unequal strings

C - No output

D - Compilation error

Answer : C

Explanation

No output, as we are comparing both base addresses and they are not same.

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

#include<stdio.h>

main()
{  
   float t = 2;

   switch(t)
   {
       case 2: printf("Hi");
       default: printf("Hello");
   }
}

A - Hi

B - HiHello

C - Hello

D - Error

Answer : D

Explanation

Error, switch expression can’t be float value.

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

#include<stdio.h>

int* f() 
{
   int x = 5;
   
   return &x;
}	
main() 
{
   printf("%d", *f());
}

A - 5

B - Address of ‘x’

C - Compile error

D - Runtime error

Answer : D

Explanation

It is invalid to return local variable address as the local variable gets removed after the functions execution is completed.

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

#include<stdio.h>

#define sqr(i) i*i
main()
{
   printf("%d %d", sqr(3), sqr(3+1)); 
}

A - 9 16

B - 9 7

C - Error: macro cannot be defined in lower case.

D - None of the above.

Answer : B

Explanation

The equivalent expansion is -> printf("%d %d",3*3,3+1*3+1);

Answer : A

Explanation

In C programming, NULL indicates the value 0 of a pointer, which means; pointer that initializes with NULL value considered as NULL pointer.

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 - Which of the following is a logical AND operator?

A - !

B - &&

C - ||

D - &

Answer : B

Explanation

Two immediate ampersand (&) symbols is logical AND operator.

Answer : A

Explanation

Preprocessing enlarges and boosts the C programming language by replacing preprocessing directive “#include<stdio.h>” with the content of the file stdio.h.

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