MCQ

100 Multiple Choice Questions In C Programming – Part 12

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 name of the #include at the beginning of a code?

A Procedure directives

B Preprocessor directives

C Program directors

D All the answers are true

B

 

 

2. Why do I need a pointer?

A Set the value of a variable

B To access the RAM address of a variable

C Change the RAM address of a variable

D All the answers are true

B

 

 
 

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

void myFunction(const int *);

int main()
{
    const int i = 20;
    printf("%d ", i);
    myFunction(&i);
    printf("%d", i);
 
}
void myFunction(const int *i)
{
    *i = 30;
}

A 20

B 20 30

C Compilation error

D Undefined value

C
You cannot change a const value. The output is as follows:

$ cc prog1.c
prog1.c: In function ‘myFunction’:
prog1.c:13: error: assignment of read-only location ‘*i’

 

 

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

int main()
{
    const int i = 20;
    int *ptr = &i;
    *ptr = 30;
    printf("%d\n", i);
    return 0;
}

A Compilation error

B Display “Warning” and 30

C Will throw an exception

D 20

B
Changing const variable via non-constant pointers invokes the “Warning” message. The output is as follows:

$ cc prog2.c
prog2.c: In function ‘main’:
prog2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
30

 

 

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

int main()
{
     a = 5;
     printf("%d\n", a++);
     return 0;
}

A 0

B 5

C 6

D Compilation error

D
The variable “a” is not defined. The output is as follows:

$ cc prog3.c
prog3.c: In function ‘main’:
prog3.c:4: error: ‘a’ undeclared (first use in this function)
prog3.c:4: error: (Each undeclared identifier is reported only once
prog3.c:4: error: for each function it appears in.)

 

 
 

6. Does it compile without errors?
#include <stdio.h>

int main()
{
    for (int i = 0; i < 5; i++);
         return 0;
}

A Depends on the C standard implemented by the compilers

B Yes

C No

D None of the above

A
Compilers implementing C90 do not allow it, but compilers implementing C99 do. The output is as follows:

$ cc prog4.c
prog4.c: In function ‘main’:
prog4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
prog4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

 

 

7. Does it compile without errors?
#include <stdio.h>

int main()
{
     int i;
     {
         int i;
         for (i = 0; i < 5; i++);
     }
}

A Depends on the C standard implemented by the compilers

B Yes

C No

D None of the above

B
You can have blocks inside. The variables have only the scope of the block where they are declared.

 

8. Which of the following statements is not supported by C?

A float str = 6e3;

B String str;

C char *str;

D Both A and B

B
It is correct in C++, but not in C.

 

 
 

9. Which of the following format identifiers cannot be used for the variable ‘var’?
#include <stdio.h>

int main()
{
    char *var = "Welcome to StackHowTo!";
}

A %s

B %f

C %d

D %c

B
%c can be used to display the index position. %d can still be used to display its ASCII value. %s is recommended. %f cannot be used.

 

 

10. Which of the following statements is incorrect?

A char[] str = “Hello world!”;

B char *str = “Hello world!”;

C char str[25] = “Hello world!”;

D char str[] = “Hello world!”;

A

 

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 *