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()
{
   int i = 1;
   
   while(++i <= 5)
      printf("%d ",i++);
}

A - 1 3 5

B - 2 4

C - 2 4 6

D - 2

Answer : B

Explanation

2 4, at while first incremented and later compared and in printf printed first and incremented later

Q 3 - A macro can execute faster than a function.

A - true

B - false

Answer : A

Explanation

As the code of macro gets expanded at the line of call, therefore macro gets executed faster with no overhead of context switch.

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

#include<stdio.h>

main() 
{
   char *p = NULL;
   
   printf("%c", *p);
}

A - NULL

B - 0

C - Compile error

D - Runtime error.

Answer : D

Explanation

It is invalid to access the NULL address hence giving run time error.

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

#include<stdio.h>

main()
{
   printf("%d", -1<<1 );  
}

A - 2

B - -2

C - 1

D - -1

Answer : B

Explanation

A negative number stored in two’s compliment of positive number. After shifting we get 1110, which is equivalent to -2.

Q 6 - Which header statement is missing in the given below program to get the desired output?

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

int main ()
{
   double x = 1234321;
  
   double result = sqrt(x);
  
   printf("The square root of %.2lf is %.2lf\n", x, result);
   return 0;
}

A - #include<defs.h>

B - #include<math.h>

C - #include<stdlib.h>

D - Above program is absolutely correct to give desired result

Answer : B

Explanation

In C programming, math.h is a header file in the standard library designed for basic mathematical operations.

   Output of above code: The square root of 1234321.00 is 1111.00
#include<stdio.h>
#include<math.h>

int main ()
{
   double x = 1234321;
  
   double result = sqrt(x);
  
   printf("The square root of %.2lf is %.2lf\n", x, result);
   return 0;
}

Q 7 - Which of the following operator can be used to access value at address stored in a pointer variable?

A - *

B - #

C - &&

D - @

Answer : A

Explanation

Pointer operator,

* (Value Operator) = Gives Value stored at Particular address

& (Address Operator) = Gives Address of Variable

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 function free() 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 x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;
}

A - 10

B - 20

C - 0

D - The program will return error

Answer : D

Explanation

The above program will return error

#include<stdio.h>

int main()
{
   const int x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;
}

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