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 - int x=~1; What is the value of 'x'?

A - 1

B - -1

C - 2

D - -2

Answer : D

Explanation

-2, the ones compliment of 1 is 1110 (binary) which is equivalent to twos compliment of 2, ie -2.

Q 2 - What is the size of int?

A - 2

B - 4

C - 8

D - Compiler dependent

Answer : D

Explanation

The size of int depends upon the complier i.e. whether it is a 16 bit or 32 bit.

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

#include<stdio.h>

main()
{ 
   int i = 13, j = 60;
   
   i ^= j;
   j ^= i;
   i ^= j;
   
   printf("%d %d", i, j);
}

A - 73 73

B - 60 13

C - 13 60

D - 60 60

Answer : B

Explanation

60 13, its swapping.

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()
{ 
   int x;
   float y;
   
   y = x = 7.5;
   printf("x=%d y=%f", x, y);
}

A - 7 7.000000

B - 7 7.500000

C - 5 7.500000

D - 5 5.000000

Answer : A

Explanation

x gets the integral value from 7.5 which is 7 and the same is initialized to y.

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 - Which of the following operator can be used to access value at address stored in a pointer variable?

A - *

B - #

C - &&

D - @

Answer : A

Explanation

Pointer operator,

* (Value Operator) = Gives Value stored at Particular address

& (Address Operator) = Gives Address of Variable

Q 8 - Which of the following is a logical AND operator?

A - !

B - &&

C - ||

D - &

Answer : B

Explanation

Two immediate ampersand (&) symbols is logical AND operator.

Answer : B

Explanation

As per the operators preference.

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