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 : A

Explanation

infinite loop, with second expression of for being absent it is considered as true by default.

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

#include<stdio.h>

{ 
   int x = 1;
   switch(x) 
   {
      default: printf("Hello");
      case 1: printf("hi"); break;
   }
}

A - Hello

B - Hi

C - HelloHi

D - Compile error

Answer : B

Explanation

Hi, control reaches default-case after comparing the rest of case constants.

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

#include<stdio.h>

main()
{
   printf("%d", -1<<1 );  
}

A - 2

B - -2

C - 1

D - -1

Answer : B

Explanation

A negative number stored in twos compliment of positive number. After shifting we get 1110, which is equivalent to -2.

Q 6 - The binary equivalent of 50 is,

A - 110010

B - 1010110

C - 101

D - 101.011.00.00

Answer : A

Explanation

#include <stdio.h>

 int main()
{
   longintdecimalNumber,remainder,quotient;
intbinaryNumber[100], i = 1, j;

 printf("Enter any decimal number: ");
 scanf("%ld",&decimalNumber);
 quotient = decimalNumber;
while(quotient!=0){
           binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
 }
 printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return0;
}

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, theP2is

   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 - Choose the correct unary operators in C a) !, b) ~, c) ^&, d) ++

A - a, b, d

B - a, b, c

C - b, c, d

D - c, d, a

Answer : A

Explanation

In C, these are the unary operators,

   Logical NOT = !
   Address-of = &
   Cast Operator = ( )
   Pointer dereference = *
   Unary Plus = +
   Increment = ++
   Unary negation = 

Q 10 - The library functionstrrchr() finds the first occurrence of a substring in another string.

A - Yes

B - Strstr()

C - strchr()

D - strnset()

Answer : B

Explanation

Strstr() finds the first occurrence of a substring in another string.

Advertisements