Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Friday

C Programming MCQs and Answers

Download C programming Multiple Choice Questions and Answers. C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie. It is an excellent language to learn to program for beginners. 

Sample C Programming Questions & Answers

1. Comment on the output of following code:

  1. #include <stdio.h>

  2. main()

  3. {

  4. char *p = 0;

  5. *p = 'a';

  6. printf("value in pointer p is %c\n", *p);

  7. }

a) It will print a
b) It will print 0
c) Compile time error
d) Run time error

Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

2. What is the output of this C code?

  1. #include <stdio.h>

  2. main()

  3. {

  4. if (sizeof(int) > -1)

  5. printf("True");

  6. else

  7. printf("False");

  8. }

a) True
b) False

Answer:b
Output:
$ cc pgm.c
$ a.out
False

3. What is the output of this C code?

  1. #include <stdio.h>

  2. main()

  3. {

  4. char *p = "Sanfoundry C-Test";

  5. p[0] = 'a';

  6. p[1] = 'b';

  7. printf("%s", p);

  8. }

a) abnfoundry C-Test
b) Sanfoundry C-Test
c) Compile time error
d) Run time error

Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

4. What is the output of this C code?

  1. #include <stdio.h>

  2. int main()

  3. {

  4. float f = 0.1;

  5. if (f == 0.1)

  6. printf("True");

  7. else

  8. printf("False");

  9. }

a) True
b) False

Answer:a
Output:
$ cc pgm.c
$ a.out
False

5. What is the output of this C code?

  1. #include <stdio.h>

  2. main()

  3. {

  4. int n = 0, m = 0;

  5. if (n > 0)

  6. if (m > 0)

  7. printf("True");

  8. else

  9. printf("False");

  10. }

a) True
b) False
c) No Output will be printed
d) Run Time Error

Answer:c
Output:
$ cc pgm.c
$ a.out
$


C Program — Display Even and Odd Numbers


Displaying even and odd numbers between a range of numbers.

Even numbers are numbers divisible by 2, while odd numbers are numbers whose remainder equals 1 if divided by two.

This program displays even and odd numbers between 1 and a specified limit. The limit can be changed by changing the value of the last number variable. The initial even or initial odd number value can also be changed to specify the start number.


#include<stdio.h>
void main()
{
 int i,end_number;
 clrscr();
 /* Accept command-line input */
 printf("\n Enter last number : ");
 scanf("%d", &end_number);
 
 //Display Even Numbers
 printf("\n Even Numbers:\n ");
 i = 2;//initial even number
 while(i<=end_number)
 {
  printf(" %d",i);
  i=i+2;//increment i by 2
 }
 //Display Odd Numbers
 printf("\n\n Odd Numbers:\n ");
 i=1;//initial odd number
 while(i<=end_number)
 {
  printf(" %d",i);
  i=i+2;//increment i by 2
 }
 getchar();
}
Download program here.

See another example that displays even and odd number between 1 and 30

#include<stdio.h>

void main()
{ int i,end_number; end_number = 30;//specific end number

//Display Even Numbers
printf("\n Even Numbers:\n ");
i = 2;//initial even number
while(i<=end_number)
{
printf(" %d",i);
i = i + 2;//increment i by 2
} //Display Odd Numbers
printf("\n\n Odd Numbers:\n ");
i=1;//initial odd number
while(i <= end_number)
{
printf(" %d",i);
i = i + 2;//increment i by 2
}
getchar();
}
Download program here.