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

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

#include<stdio.h>

main() 
{
    char c = 'A'+255;
   
    printf("%c", c);
}

A - A

B - B

C - Overflow error at runtime

D - Compile error

Answer : A

Explanation

A, the range of ASCII values for the ASCII characters is 0-255. Hence the addition operation circulates back to ‘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 - A single line comment in C language source code can begin with _____

A - ;

B - :

C - /*

D - //

Answer : D

Explanation

Two immediate forward slashes are used to comment a single line. A single can be commented by beginning with /* and should be terminated with */ , in general used for multi-line comments.

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.

Answer : B

Explanation

All the local variables are stored in a memory called as stack.

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 - What function can be used to free the memory allocated by calloc()?

A - dealloc();

B - strcat();

C - free();

D - memcpy();

Answer : C

Explanation

calloc(): Allocates space for an array elements, initializes to zero and then returns a pointer to memory.

Free(): Dellocate the space allocated by calloc()

Q 8 - In the given below code, the P2 is

   Typedef int *ptr;
   
   ptr p1, p2;

A - Integer

B - Integer pointer

C - Both, Integer & Integer pointer

D - None of above

Answer : B

Explanation

Ptr is an alias to int*.

Q 9 - What will be the output of the given below program in TurboC

#include<stdio.h>

int fun(int **ptr);

int main()
{
    int i = 10, j = 20;
    const int *ptr = &i;
    
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    return 0;
}

A - i= FFE6 ptr=30 j=FFE4 ptr=36

B - i= FFE0 ptr=04 j=FFE1 ptr=30

C - i= FFE4 ptr=10 j=FFE2 ptr=20

D - None of the above

Answer : C

Explanation

First the pointer holds I’s address and changes later to hold j’s address. Hence printing the 10 & 20 respectively.

#include<stdio.h>

int fun(int **ptr);

int main()
{
    int i = 10, j = 20;
    const int *ptr = &i;
    
    printf(" i = %5X", ptr);
    printf(" ptr = %d", *ptr);
    ptr = &j;
    printf(" j = %5X", ptr);
    printf(" ptr = %d", *ptr);
    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;
}


cprogramming_questions_answers.htm

Advertisements