MCQ

100 Multiple Choice Questions In C Programming – Part 14

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

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

int main()
{
    int x = 010;
    printf("%d", x);
}

A 2

B 8

C 9

D 10

B
010 is an octal representation for 8. The output is as follows:

$gcc prog3.c
$ a.out
8

 

 

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

enum bird {SPARROW, CANARY, Hen};
enum animal {LION = 16, RABBIT, ZEBRA, TIGER};

int main()
{
        enum bird b = LION;
        int k;
        k = b;
        printf("%d\n", k);
        return 0;
}

A 0

B Compilation error

C 1

D 16

D
“o” receives an integer, so “k” and “b” are compatible. The output is as follows:

$gcc prog5.c
$ a.out
16

 

 
 

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

enum animal {LION = MAX + 1, RABBIT = LION + MAX};

int main()
{
    enum animal a = RABBIT;
    printf("%d\n", a);
    return 0;
}

A Compilation error

B 7

C Undefined value

D 3

B
The MAX value is 3 and therefore RABBIT will get 4 + 3 = 7. The output is as follows:

$gcc prog6.c
$ a.out
7

 

 

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

int main()
{
    char *str = "y";
    char c = 'y';
    char array[1];
    array[0] = c;
    printf("%d %d", strlen(str), strlen(array));
    return 0;
}

A 1 1

B 2 1

C 2 2

D 1 (undefined value)

D
Strlen(str)’s value is 1 because the variable str contains a single character, but strlen(array)’s value changes each time you compile the program, so it’s undefined.

$gcc prog7.c
$ a.out
1 (undefined value)

 

 

5. Enumeration types are handled by ____?

A Compiler

B Preprocessor

C Linker

D Assembler

A
Enumeration types are handled by the compiler.

 

 
 

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

int main()
{
     printf("Hello\rworld!\n");
     return 0;
}

A Hello\rworld!

B Helloworld!\n

C world!

D world!Hello

C
The “r” character is a carriage return and moves the cursor backwards. Hello is replaced by world!

 

 

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

int main()
{
     printf("Hello\r\nworld!\n");
     return 0;
}

A Hello\rworld!

B Hello
world!

C world!

D world!Hello

B
The combination “\r\n” moves the cursor to the next line. The output is as follows:

$gcc prog1.c
$a.out
Hello
    world!

 

 

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

int const display()
{
     printf("Hello world!");
     return 0;
}

void main()
{
     display();
}

A Error because the function name cannot be preceded by const

B Hello world!

C Hello world! is displayed in infinity

D No output

B
A function becomes const when the keyword const is used in the function declaration. The idea behind const functions is not to allow them to modify the object on which they are called. It is recommended to define as many const functions as possible to avoid any modification of the objects. The output is as follows:

$gcc prog5.c
$a.out
Hello world!

 

 
 

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

int main()
{
     int const q = 7;
     q++;
     printf("q =  %d", q);
}

A q = 8

B Error due to int followed by const

C Error, because a constant variable can only be modified twice

D Error, because a constant variable cannot be modified

D
A constant variable must be declared and defined at the same time. Trying to change it will result in an error. The output is as follows:

$gcc prog4.c
$a.out
prog4.c: In function ‘main’:
prog4.c:5: error: increment of read-only variable ‘q’

 

 

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

int main()
{
    int q = 8;
    int *const p = &q;
    int a = 6;
    p = &a;
    printf("%d", p);
}

A Address of q

B Address of a

C Compilation error

D Address of q plus address of a

C
Since the pointer p is declared constant, trying to assign it a new value generates an error. The output is as follows:

$gcc prog3.c
$a.out
prog3.c: In function ‘main’:
prog3.c:7: error: assignment of read-only variable ‘p’
prog3.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’

 

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 *