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

#include<stdio.h>

main() 
{ 
   int a = 5, b = 3, c = 4; 
   
   printf("a = %d, b = %d\n", a, b, c);
}

A - a=5, b=3

B - a=5, b=3, c=0

C - a=5, b=3, 0

D - compile error

Answer : A

Explanation

a=5,b=3 , as there are only two format specifiers for printing.

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 - Identify the invalid constant used in fseek() function as ‘whence’ reference.

A - SEEK_SET

B - SEEK_CUR

C - SEEK_BEG

D - SEEK_END

Answer : C

Explanation

SEEK_BEG, all the rest are valid constants defined in ‘stdio.h’

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

#include<stdio.h>

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

A - Compile error

B - Runtime error

C - 5

D - 10

Answer : D

Explanation

The returned address is global variables and 10 being stored in it. Therefore x is 10.

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’.

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 library functions help users to dynamically allocate memory?

A - memalloc()and alloc() 

B - malloc() and memalloc()

C - malloc() and calloc()

D - memalloc() and calloc()

Answer : C

Explanation

malloc():Allocates an area of memory that is large enough to hold "n” number of bytes and initializes contents of memory to zeroes.

calloc(): Allocates "size" of bytes of memory and contains garbage values.

Q 8 - In the following code, what is 'P'?

   Typedef char *charp;
   
   const charp P;

A - P is a constant

B - P is a character type

C - P is a pointer

D - None of the above

Answer : A

Explanation

Const charp P;

Although, the code itself indicates the keyword "const", so that; P is a constant.

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 - If, the given below code finds the length of the string then what will be the length?

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}

A - Code returns error

B - Code returns the length 8

C - Code returns the length 6

D - Code returns the length 2

Answer : B

Explanation

Here, *s is char pointer that holds character string. To print whole string we use printf("%s",s) by using base address. s contains base address (&s[0]) and printf will print characters until '\0' occurs. *s only gives first character of input string, but s++ will increase the base address by 1 byte. When *s=='\0' encountered, it will terminate loop.

#include<stdio.h>

int xstrlen(char *s)
{
   int length = 0;
   
   while(*s!='\0')
   {length++; s++;}
   return (length);
}
   int main()
{
   char d[] = "IndiaMAX";
    
   printf("Length = %d\n", xstrlen(d));
   return 0;
}


cprogramming_questions_answers.htm

Advertisements