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 program?

#include<stdio.h>

void f() 
{ 
   static int i;
   
   ++i; 
   printf("%d", i); 
}

main()
{ 
   f(); 
   f(); 
   f(); 
}

A - 1 1 1

B - 0 0 0

C - 3 2 1

D - 1 2 3

Answer : D

Explanation

1 2 3, A static local variables retains its value between the function calls and the default value is 0.

Q 2 - Which operator is used to continue the definition of macro in the next line?

A - #

B - ##

C - $

D - \

Answer : D

Explanation

\, the first two are stringize and token pasting operators respectively. There is no such operator called $.

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

#include<stdio.h>

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

A - 0

B - 1

C - 2

D - 3

Answer : B

Explanation

1, The inner indirection evaluates to 1, and the value at index 1 for outer indirection is 1.

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 - What is the output of the following program?

#include<stdio.h>

main()
{ 
   char *s = "Abc";
   
   while(*s)
      printf("%c", *s++);
}

A - Abc

B - bc

C - Compile error

D - Runtime error

Answer : A

Explanation

Loop continues until *s not equal to ‘\0’, hence printing “Abc” where character is fetched first and address is incremented later.

Q 6 - In the standard library of C programming language, which of the following header file is designed for basic mathematical operations?

A - math.h

B - conio.h

C - dos.h

D - stdio.h

Answer : A

Explanation

math.h is a header file in the standard library designed for basic mathematical operations

Q 7 - Which standard library function can return a pointer to the last occurrence of a character in a string?

A - stchar()

B - strrchr()

C - strchar() & stchar()

D - strrchar()

Answer : B

Explanation

The strrchr() function finds string for the last occurrence of a character and place the pointer to the last occurrence of character.

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 following program?

#include<stdio.h>

int main()
{
   const int x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   return 0;
}

A - 10

B - 20

C - 0

D - The program will return error

Answer : D

Explanation

The above program will return error

#include<stdio.h>

int main()
{
   const int x = 5;
   
   const int *ptrx;
   ptrx = &x;
   *ptrx = 10;
   printf("%d\n", x);
   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;
}


cprogramming_questions_answers.htm

Advertisements