MCQ

100 Multiple Choice Questions In C Programming – Part 6

This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.
 

1. In which case the return statement is not mandatory?

A When the function takes no input parameters

B When the function return void

C When the function must return 0

D None of the above

B
When the function return void. The return statement is not mandatory. Example:

void show(void) {
  printf("Hello World\n"); 
}

 

 

2. What are the parameters of a function?

A Hints on the name of the function

B Details of the value it should return

C Variables that are sent to it and used in its processing

D None of the above

C
The parameters of a function are variables that are sent to it and used in its processing.

 

 
 

3. Which of the following statements is false?

A A function does not have to return a value

B A function can return a value of any type

C A function can return multiple values

D None of the above

C
A function does not have to return a value and can return a value of any type, but can’t return multiple values.

 

 

4. What is the output of the following C program?
#include <stdio.h>

int main()
{
    int x = 20000;
    double y = 26;
    int *p = &x;
    double *q = &y;
    printf("The size of p = %d and q = %d", sizeof(p), sizeof(q));
    return 0;
}

A The size of p = 4 and q = 4

B The size of p = 4 and q = 8

C erreur de compilation

D The size of p = 8 and q = 8

A, D
The size of any pointer type is 4 on a 32-bit machine and 8 on a 64-bit machine. The output is as follows on a 32-bit machine:

$gcc prog2.c
$ a.out
The size of p = 4 and q = 4

The output is as follows on a 64-bit machine:

$gcc prog2.c
$ a.out
The size of p = 8 and q = 8

 

 

4. Which conversion is correct regarding the size of the data types?

A char > int > float

B int > char > float

C char < int < double

D double > char > int

C
char has fewer bytes than int and int has fewer bytes than double in any system.

 

 
 

5. What is a global variable?

A A variable that accessible everywhere

B A variable that can accept any type (int, float, char…)

C A variable declared in the main function

D None of the above

A
A global variable is a variable that accessible everywhere. Example:

#include <stdio.h>
 
// global variable declaration
int x;
 
int main () {

  // local variable declaration
  int a;
 
  // initialization
  x = 1;
  a = 2;
 
  printf ("x = %d, a = %d\n", x, a);
 
  return 0;
}

Output:

x = 1, a = 2

 

 

6. What is the output of the following C program (on a 64-bit machine)?
#include <stdio.h>

union S
{
     int a;
     char b;
};

int main()
{
    union S s;
    printf("%d", sizeof(s));
    return 0;
}

A 8

B 5

C 9

D 4

D
Since the size of a variable of type “union” is the maximum size of its elements, here “int” is the largest so it is 4. The output is as follows:

$gcc prog3.c
$ a.out
4

 

 

7. What is the output of the following C program?
#include <stdio.h>

int main()
{
    float y = 'a';
    printf("%f", y);
    return 0;
}

A a

B runtime error

C a.0000000

D 97.000000

D
Since the ASCII value of “a” is 97, the same thing is assigned to the float variable, i.e. 97.000000. The output is as follows:

$gcc prog4.c
$ a.out
97.000000

 

 
 

8. Which of these data types has a variable size?

A int

B struct

C float

D double

B
Since the size of a structure depends on its fields, it has a variable size compared to other types.

 

 

9. How to include a standard library?

A #include <windows.h>

B #include "windows.h"

C #include [windows.h]

D #include {windows.h}

A
To include a standard library use #include <windows.h>.

 

 

10. What is the output of the following C program?
#include <stdio.h>

void main()
{
     float a = 0.2;
     if (a == 0.2)
         printf("OK");
     else
         printf("KO");
}

A KO

B OK

C Runtime error

D Compilation error

A
Float numbers should not be compared with the “==” operator. We compare 0.2 with a double precision. To get the expected result, we must use:

if (a == 0.2f) //check if 'a' is exactly 0.2f

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *