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 outpout of the following program?

#include<stdio.h>

main() 
{
      enum { india, is=7, GREAT };

      printf("%d %d", india, GREAT);
}

A - 0 1.

B - 0 2

C - 0 8

D - Compile error

Answer : C

Explanation

0 8, enums gives the sequence starting with 0. If assigned with a value the sequence continues from the assigned value.

Q 2 - What is the value of ‘y’ for the following code snippet?

#include<stdio.h>

main()
{
   int x = 1;
   
   float y = x>>2;
   
   printf( "%f", y );
}

A - 4

B - 0.5

C - 0

D - 1

Answer : C

Explanation

0, data bits are lost for the above shift operation hence the value is 0.

Q 3 - Function fopen() with the mode "r+" tries to open the file for __

A - reading and writing

B - reading and adding new content

C - only for reading

D - it works only for directories

Answer : A

Explanation

Option (a), the file should exist and opens for both reading & writing.

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 - C is the successor of ___ programming language.

A - C++

B - B++

C - B

D - Mini C

Answer : C

Explanation

B is a programming language developed at Bell Labs in 1969. It is derived from BCPL (Basic Combined Programming Language). It is designed by Ken Thompson with Dennis Ritchie.

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 - The given below program allocates the memory, what function will you use to free the allocated memory?

#include<stdio.h>
#include<stdlib.h>

#define MAXROW 4
# define MAXCOL 5

int main ()
{
   int **p, i, j
   
   p = (int **) malloc(MAXROW * sizeof(int*));
   return 0;
}

A - memfree(int p);

B - free(p);

C - dealloc(p);

D - Both, free(p); & dealloc(p);

Answer : B

Explanation

free() is the function in C language to release the allocated memory by any dynamic memory allocating built in library function.

#include<stdio.h>
#include<stdlib.h>

#define MAXROW 4
# define MAXCOL 5

int main ()
{
   int **p, i, j
   
   p = (int **) malloc(MAXROW * sizeof(int*));
   return 0;
}

Q 8 - How many times the given below program will print "India"?

#include<stdio.h>

int main ()
{
   int x;
   
   for(x=-1; x<=20; x++)int i;
   {
   if(x < 10)
      continue;
   else
      break;
   printf("India");
}

A - Unlimited times

B - 21 times

C - 0 times

D - 20 times

Answer : C

Explanation

Following for loop there is only one statement, that is int i; break & continue are appearing out side for block which is compile error

#include<stdio.h>

int main ()
{
   int x;
   
   for(x=-1; x<=20; x++)int i;
   {
   if(x < 10)
      continue;
   else
      break;
   printf("India");
}

Q 9 - What will be the resultant of the given below program?

#include<stdio.h>
#include<stdarg.h>

Void fun(char *msg, ...);
int main ()
{
   fun("IndiaMAX", 1, 4, 7, 11, 0);
   return 0;
}
   void fun(char *msg, ...)  
{
va_list ptr;{
   int num;
   va_start(ptr, msg);
   num = va_arg(ptr, int);    
   num = va_arg(ptr, int);
   printf("%d", num);
}

A - IndiaMAX 1, 7, 11, 0

B - IndiaMAX 1, 7

C - Only 4

D - 1, 7, 11, 0

Answer : C

Explanation

va_list ptr is an argument pointer that traverse through the variable arguments passed in the function, where, va_start points 'ptr' to the first argument (IndiaMix). In each call to the va_arg, ptr shift to the next variable argument and after two calls ptr ends up at '7' and return the number '4'.

#include<stdio.h>
#include<stdarg.h>

Void fun(char *msg, ...);
int main ()
{
   fun("IndiaMAX", 1, 4, 7, 11, 0);
   return 0;
}
   void fun(char *msg, ...)  
{
va_list ptr;{
   int num;
   va_start(ptr, msg);
   num = va_arg(ptr, int);    
   num = va_arg(ptr, int);
   printf("%d", num);
}

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