• C Programming Video Tutorials

C Programming - Online Quiz



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() 
{
   for()printf("Hello");
}

A - Infinite loop

B - Prints “Hello” once.

C - No output

D - Compile error

Answer : D

Explanation

Compiler error, semi colons need to appear though the expressions are optional for the ‘for’ loop.

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.

Answer : B

Explanation

(b). Linker links the object code of your program and library code to produce executable.

Q 4 - Choose the invalid predefined macro as per ANSI C.

A - __FILE__

B - __DATE__

C - __TIME__

D - __C++__

Answer : D

Explanation

There is no macro define with the name __C++__, but __cplusplus is defined by ANSI)

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

#include<stdio.h>

main()
{  
   #undef NULL
   char *s = "Hello";
   
   while(*s != NULL)
   {
      printf("%c", *s++);
   }
}

A - Hello

B - Compile error: there is no macro called “undef”

C - Compile error: improper place of #undef

D - Compile error: NULL is undeclared.

Answer : D

Explanation

NULL is equivalent to ‘\0’ in value. Statement *s++ prints the character first and increments the address later.

Answer : A

Explanation

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

 int main()
{
   float x = 3.6;	
   
   int y = (int)(x + 0.5);
   
   printf ("Result = %d\n", y );
   return 0; 

}

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 - The return keyword used to transfer control from a function back to the calling function.

A - Yes

B - Switch

C - go back

D - goto

Answer : A

Explanation

In C, the return function stops the execution of a function and returns control with value to the calling function. Execution begins in the calling function by instantly following the call.

Q 9 - Which files will get closed through the fclose() in the following program?

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

A - "ABC"

B - "ACD"

C - "ADF"

D - Return error

Answer : D

Explanation

The syntax of fclose() function is wrong; it should be int fclose(FILE *stream); closes the stream. Here, fclose(fp, fs, ft); is using separator(,) which returns error by saying that extra parameter in call to fclose() function.

#include<stdio.h>

int main ()
{
   FILE *fs, *ft, *fp;
   
   fp = fopen("ABC", "r");
   fs = fopen("ACD", "r");
   ft = fopen("ADF", "r");
   fclose(fp, fs, ft);
return 0;
}

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;
}

Advertisements