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() 
{
   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 - The type name/reserved word ‘short’ is ___

A - short long

B - short char

C - short float

D - short int

Answer : D

Explanation

short is used as an alternative of short int.

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

#include<stdio.h>

main()
{ 
   int a[] = {2,1};
   
   printf("%d", *a); 
}

A - 0

B - 1

C - 2

D - Compile error.

Answer : C

Explanation

2, as ‘a’ refers to base address.

Q 4 - The default executable generation on UNIX for a C program is ___

A - a.exe

B - a

C - a.out

D - out.a

Answer : C

Explanation

"a.out" is the default name of the executable generated on both the UNIX and Linux operating systems.

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

#include<stdio.h>

main()
{ 
   char s[] = "Hello\0Hi";
   
   printf("%d %d", strlen(s), sizeof(s));
}

A - 5 9

B - 7 20

C - 5 20

D - 8 20

Answer : A

Explanation

Length of the string is count of character upto ‘\0’. sizeof – reports the size of the array.

Answer : B

Explanation

Prototype of a function can be used to declare a function. It is necessary in order to provide information (return type, parameter list and function name, etc) about the function to the compiler.

Q 7 - The equivalent pointer expression by using the array element a[i][j][k][2],

A - ((((a+m)+n)+o)+p)

B - *(*(*(*(a+i)+j)+k)+2)

C - *( (((a+m)+n)+o+p)

D - *( ((a+m)+n+o+p)

Answer : B

Explanation

If, the array element is a[i][j] = *(*(a+i)+j)

If, the array element is a[i][j][k]= *(*(*(a+i)+j)+k)

Q 8 - fgets() function is safer than gets() because in fgets() function you can specify the size of the buffer into which the supplied string will be stored.

A - True

D - False

Answer : A

Explanation

Both functions retrive and store a string from console or file, but fgets() functions are more safer to use then gets() because gets() doesn't facilitate to detail the length of the buffer to store the string in and fgets() facilitates to specify a maximum string length.

   char *fgets(char *s, int size, FILE *stream);
   char *gets(char *s);

Q 9 - What will be the output of the following program?

#include<stdio.h>

int main()
{
   const int i = 0;
    
   printf("%d\n", i++);
   return 0;
}

A - 100

B - Infinity

C - 0

D - Return error

Answer : D

Explanation

It is because ++needs a value and a const variable can’t be modified.

#include<stdio.h>

int main()
{
   const int i = 0;
    
   printf("%d\n", i++);
   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