loader image
Teachics

Fundamentals of C Programming- MCQ

Here are the most important Multiple choice questions(MCQ) on the basics of C programming. The MCQs are from the following sections,

  • Introduction to C
  • Data types
  • Variables
  • Constants

  1. Who invented C?
    1. Charles Babbage
    2. Bill Gates
    3. Dennis Ritchie
    4. Steve Jobs

Answer: C

C was invented by Dennis Ritchie at the Bell Laboratories in 1972.


  1. C is a …………………. type of language?
    1. Procedural
    2. Object-oriented
    3. Functional
    4. Bit-level

Answer: A

C is a procedural programming language. Its concept is based on procedure calls. Procedures are nothing but a series of computational steps to be carried out (functions, routines, or subroutines).


  1. Which of the following is not a valid variable name declaration in C?
    1. int Mark_1;
    2. int mark1;
    3. int __mark1;
    4. int 1mark;

Answer: D

Variable names must begin with an alphabet or an underscore.


  1. Find the output of the following C code?
    1. Hello world a
    2. Hello world 0
    3. Hello world followed by a random value
    4. Compile-time error
#include <stdio.h>
int main() {
    printf("Hello world %d", a);
    return 0;
}

Answer: D

The program will result in an error since the variable a is used without declaring it first. The output of the program will look like the following

hello.c: In function 'main':
hello.c:3:30: error: 'a' undeclared (first use in this function)
    3 |     printf("Hello world %d", a);
      |                              ^
hello.c:3:30: note: each undeclared identifier is reported only once for each function it appears in

  1. Which of the following is not a valid variable name in C?
    1. mark
    2. number_1
    3. _count
    4. cash_$

Answer: D

Variable names can be made up of alphabets, digits, and the underscore character. No other special character is allowed.


  1. Which of the statement is true for naming variables in C?
    1. Variable names can be made up of alphabets, digits, and special characters.
    2. Variable names can begin with a digit.
    3. White spaces are not allowed in variable names.
    4. Variable names cannot contain the underscore character.

Answer: C

White spaces are not allowed in variable names in C.


  1. What will be the output of the following C code?
    1. Hello World 100
    2. Hello World 25
    3. Hello World followed by a random number
    4. Compile-time error
#include <stdio.h>
int main()
{
    int x = 100;
    int x = 25;
    printf("Hello World %d", y);
    return 0;
}

Answer: D

Since the variable x is already defined, redefining it will cause a compile-time error. The output of the program will look like the following,

hello.c: In function 'main':
hello.c:5:9: error: redefinition of 'x'
    5 |     int x = 25;
      |         ^
hello.c:4:9: note: previous definition of 'x' was here
    4 |     int x = 100;
      |         ^
hello.c:6:31: error: 'y' undeclared (first use in this function)
    6 |     printf("Hello World! %d", y);
      |                               ^
hello.c:6:31: note: each undeclared identifier is reported only once for each function it appears in

  1. Which of the following cannot be a variable name in C?
    1. marks_1
    2. teacher
    3. SCORE
    4. for

Answer: D

for is C keyword and keywords cannot be used a variable name.


  1. Determine the output of the following C code?
    1. 1147
    2. 1185
    3. Run time error
    4. Compile-time error
#include <stdio.h>
    int main()
    {
        int total_Marks = 1185;
        int total_marks = 1147;
        printf("%d", total_marks);
        return 0;
    }

Answer: A

1147

In C, Uppercase and lowercase letters are significant. That is, the variable total_Marks is not the same as total_marks.


  1. The format identifier “%d” is used for …………………. datatype?
    1. int
    2. char
    3. long
    4. float

Answer: A

%d and %i can be used as a format identifier for int data type.


  1. Which is the correct order of data types with respect to their size?
    1. int > char > float
    2. char > int > float
    3. char < double < int
    4. char < int < double

Answer: D


  1. Which of the following datatype has varying sizes?
    1. int
    2. double
    3. float
    4. struct

Answer: D

The size of a structure depends on its members.


  1. Which of the following is not a data type in C?
    1. int
    2. real
    3. float
    4. char

Answer: B


  1. Which of the following statement is true?
    1. ANSI C treats the variable ‘name’ and ‘Name’ to be the same.
    2. Character constants are coded using double-quotes.
    3. All variables must be given a type when they are declared.
    4. Any valid printable ANSII character can be used in an identifier.

Answer: C

All variables in C must be given a data type when they are declared.


  1. What will be the output of the following C code?
    1. pi is 3.14
    2. Compile-time error
    3. pi is followed by a random value
    4. Run time error
#include <stdio.h>
    int main()
    {
        const float pi;
        pi = 3.14;
        printf("pi is %f", pi);
        return 0;
    }

Answer: D

The code will result in a run time error because the constants have to be declared and initialized at the same time. The output of the program should be as follows,

pierror.c -lm
pierror.c: In function 'main':
pierror.c:5:12: error: assignment of read-only variable 'pi'
    5 |         pi = 3.14;
      |            ^

  1. A global variable is also known as …………………. variable.
    1. external
    2. internal
    3. static
    4. dynamic

Answer: A

A global variable is also known as an external variable.


  1. Which of the following statement is true?
    1. Every line in a C program should end with a semicolon.
    2. Every C program ends with an END word.
    3. Comments cause the computer to print the text enclosed between /* and */ when executed.
    4. printf statement can generate only one line of output

Answer: A

Every line in a C program should end with a semicolon.


  1. Identify the correct output for the following C code?
    1. Compile-time error.
    2. 8
    3. 8.0000000
    4. Garbage value
 #include <stdio.h>
void main()
{
    int a = 8;
    float a = 8;
    printf("%d", a);
}

Answer: A

Since the variable a is defined as both an integer and float, the code will result in an error. The output will be,

gcc multiple.c -lm
multiple.c: In function 'main':
multiple.c:5:11: error: conflicting types for 'a'
    5 |     float a = 4;
      |           ^
multiple.c:4:9: note: previous definition of 'a' was here
    4 |     int a = 4;
      |         ^
multiple.c:6:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
    6 |     printf("%d", a);
      |             ~^   ~
      |              |   |
      |              int double
      |             %f

  1. Which of the following declaration is not supported in C?
    1. String str;
    2. int a;
    3. float pi=3.14;
    4. char *str;

Answer: A

The string data type is not supported in C.


  1. If an integer needs two bytes of storage, then the maximum value of an unsigned integer is
    1. 2^{16} - 1
    2. 2^{15} - 1
    3. 2^{16}
    4. 2^{15}

Answer: A


  1. If an integer needs two bytes of storage, then the maximum value of a signed integer is
    1. 2^{16} - 1
    1. 2^{15} - 1
    1. 2^{16}
    1. 2^{15}

Answer: B

In the signed magnitude form, one bit is used to store the sign(negative or positive). Only the remaining 15 bits are available to store the magnitude.


  1. The statement, printf("%d",printf("hello"));
    1. results in a syntax error
    2. outputs hello5
    3. outputs garbage
    4. prints hello and terminates

Answer: B

Any function, including main(), returns a value to the calling environment. printf returns the number of characters it printed. So the output will be hello5 since it printed the five characters h, e, l, l, o.


  1. What will be the output of the following fragment of code if the input is abc,
    1. Syntax error
    2. Fatal error
    3. 3
    4. none of the above
#include <stdio.h>
int main() {
    char x, y, z;
    printf ("%d", scanf("%c%c%c", &x, &y, &z ));
    return 0;
}

Answer: C

Any function, including main(), returns a value to the calling environment. The scanf function returns the number of successful matches. In this case, 3.


  1. Choose the correct statements.
    1. An identifier may start with an underscore
    2. An identifier may end with an underscore
    3. IF is a valid identifier
    4. All of above

Answer: D

Since “IF” is written in upper case it is not a keyword.


  1. Which of the following are not keywords in C? :
    1. printf
    2. main
    3. IF
    4. All of above

Answer: C

Since “IF” is written in upper case it is not a keyword.


Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments