250+ TOP MCQs on Data Types and Sizes and Answers

1. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a[5] = {1, 2, 3, 4, 5};
  5.         int i;
  6.         for (i = 0; i < 5; i++)
  7.             if ((char)a[i] == '5')
  8.                 printf("%dn", a[i]);
  9.             else
  10.                 printf("FAILn");
  11.     }

a) The compiler will flag an error
b) The program will compile and print the output 5
c) The program will compile and print the ASCII value of 5
d) The program will compile and print FAIL for 5 times
Answer: d
Clarification: The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAIL
FAIL
FAIL
FAIL
FAIL

2. The format identifier ‘%i’ is also used for _____ data type.
a) char
b) int
c) float
d) double
Answer: b
Clarification: Both %d and %i can be used as a format identifier for int data type.

3. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
Answer: b
Clarification: 65000 comes in the range of short (16-bit) which occupies the least memory. Signed short ranges from -32768 to 32767 and hence we should use unsigned short.

4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
Answer: d
Clarification: typedef and struct are used to define user-defined data types.

5. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
Answer: c
Clarification: The size of the data types depend on the system.

6. What will be the output of the following C code?

  1.     #include  
  2.     int main()
  3.     {
  4.        signed char chr;
  5.        chr = 128;
  6.        printf("%dn", chr);
  7.        return 0;
  8.     }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
Answer: b
Clarification: signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128

7. What will be the output of the following C code?

  1.     #include  
  2.     int main()
  3.     {
  4.         char c;
  5.         int i = 0;
  6.         FILE *file;
  7.         file = fopen("test.txt", "w+");
  8.         fprintf(file, "%c", 'a');
  9.         fprintf(file, "%c", -1);
  10.         fprintf(file, "%c", 'b');
  11.         fclose(file);
  12.         file = fopen("test.txt", "r");
  13.         while ((c = fgetc(file)) !=  -1)
  14.             printf("%c", c);
  15.         return 0;
  16.     }

a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
Answer: a
Clarification: None.
Output:
$ cc pgm3.c
$ a.out
a

8. What is short int in C programming?
a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned
Answer: c
Clarification: None.

250+ TOP MCQs on Conditional Expressions and Answers

C Objective Questions on “Conditional Expressions”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Conditional Expressions” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 0;
  5.         int z = (y++) ? y == 1 && x : 0;
  6.         printf("%dn", z);
  7.         return 0;
  8.     }

a) 0
b) 1
c) Undefined behaviour
d) Compile time error
Answer: a
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 1;
  5.         int y =  x == 1 ? getchar(): 2;
  6.         printf("%dn", y);
  7.     }

a) Compile time error
b) Whatever character getchar function returns
c) Ascii value of character getchar function returns
d) 2
Answer: c
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 1;
  5.         short int i = 2;
  6.         float f = 3;
  7.         if (sizeof((x == 2) ? f : i) == sizeof(float))
  8.             printf("floatn");
  9.         else if (sizeof((x == 2) ? f : i) == sizeof(short int))
  10.             printf("short intn");
  11.     }

a) float
b) short int
c) Undefined behaviour
d) Compile time error
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 2;
  5.         int b = 0;
  6.         int y = (b == 0) ? a :(a > b) ? (b = 1): a;
  7.         printf("%dn", y);
  8.     }

a) Compile time error
b) 1
c) 2
d) Undefined behaviour
Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int y = 1, x = 0;
  5.         int l = (y++, x++) ? y : x;
  6.         printf("%dn", l);
  7.     }

a) 1
b) 2
c) Compile time error
d) Undefined behaviour
Answer: a
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 8;
  5.         int m = 7;
  6.         int z = k < m ? k++ : m++;
  7.         printf("%d", z);
  8.     }

a) 7
b) 8
c) Run time error
d) 15
Answer: a
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 8;
  5.         int m = 7;
  6.         int z = k < m ? k = m : m++;
  7.         printf("%d", z);
  8.     }

a) Run time error
b) 7
c) 8
d) Depends on compiler
Answer: b
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         1 < 2 ? return 1 : return 2;
  5.     }

a) returns 1
b) returns 2
c) Varies
d) Compile time error
Answer: d
Clarification: None.

250+ TOP MCQs on Break and Continue and Answers

’s MCQs on C helps anyone preparing for placement in Microsoft and other companies. Anyone looking for Microsoft placement papers should practice these questions continuously for 2-3 months, thereby ensuring a top position in placements.

Here is a listing of basic C questions on “Break and Continue” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         if (i == 0)
  6.         {
  7.             printf("Hello");
  8.             continue;
  9.         }
  10.     }

a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
Answer: d
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int i = 0;
  5.         if (i == 0)
  6.         {
  7.             printf("Hello");
  8.             break;
  9.         }
  10.     }

a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
Answer: d
Clarification: None.

3. What will be the output of the following C code?

  1.      #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         do
  6.         {
  7.             i++;
  8.             if (i == 2)
  9.                 continue;
  10.                 printf("In while loop ");
  11.         } while (i < 2);
  12.         printf("%dn", i);
  13.     }

a) In while loop 2
b) In while loop in while loop 3
c) In while loop 3
d) Infinite loop
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0, j = 0;
  5.         for (i; i < 2; i++){
  6.             for (j = 0; j < 3; j++)
  7.             {
  8.                 printf("1n");
  9.                 break;
  10.             }
  11.             printf("2n");
  12.         }
  13.         printf("after loopn");
  14.     }

a)

b)

c)

d)

View Answer

Answer: c
Clarification: None.

 
 

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         while (i < 2)
  6.         {
  7.             if (i == 1)
  8.                 break;
  9.                 i++;
  10.                 if (i == 1)
  11.                     continue;
  12.                     printf("In while loopn");
  13.         }
  14.         printf("After loopn");
  15.     }

a)

   In while loop
   After loop

b) After loop
c)

   In while loop
   In while loop
   After loop

d) In while loop
Answer: b
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         char c = 'a';
  6.         while (i < 2)
  7.         {
  8.             i++;
  9.             switch (c) 
  10.             {
  11.                case 'a':
  12.                    printf("%c ", c);
  13.                    break;
  14.                    break;
  15.             }
  16.         }
  17.         printf("after loopn");
  18.     }

a) a after loop
b) a a after loop
c) after loop
d) error
Answer: b
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         printf("before continue ");
  5.         continue;
  6.         printf("after continuen");
  7.     }

a) Before continue after continue
b) Before continue
c) After continue
d) Compile time error
Answer: d
Clarification: None.

250+ TOP MCQs on Automatic Variables and Answers

C helps anyone preparing for Samsung and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Samsung interviews on C Programming language.

Here is a listing of online C test questions on “Automatic Variables” along with answers, explanations and/or solutions:

1. Automatic variables are _________
a) Declared within the scope of a block, usually a function
b) Declared outside all functions
c) Declared with the auto keyword
d) Declared within the keyword extern
Answer: a
Clarification: None.

2. What is the scope of an automatic variable?
a) Exist only within that scope in which it is declared
b) Cease to exist after the block is exited
c) Exist only within that scope in which it is declared & exist after the block is exited
d) All of the mentioned
Answer: c
Clarification: None.

3. Automatic variables are allocated memory in ___________
a) heap
b) Data segment
c) Code segment
d) stack
Answer: d
Clarification: None.

4. What will be the x in the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int x;
  5.     }

a) automatic variable
b) static variable
c) register variable
d) global variable
Answer: a
Clarification: None.

5. Automatic variables are initialized to ___________
a) Zero
b) Junk value
c) Nothing
d) Both Zero & Junk value
Answer: b
Clarification: None.

6. Which of the following storage class supports char data type?
a) register
b) static
c) auto
d) all of the mentioned
Answer: d
Clarification: None.

7. A local variable declaration with no storage class specified is by default _________
a) auto
b) extern
c) static
d) register
Answer: a
Clarification: None.

contest

250+ TOP MCQs on Address Arithmetic and Answers

C helps anyone preparing for Tech Mahindra and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Tech Mahindra interviews on C Programming language.

Here is a listing of C programming questions on “Address Arithmetic” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char *s = "hello";
  5.         char *p = s * 3;
  6.         printf("%ct%c", *p, s[1]);
  7.     }

a) h e
b) l e
c) Compile time error
d) l h
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char *s= "hello";
  5.         char *p = s + 2;
  6.         printf("%ct%c", *p, s[1]);
  7.     }

a) l e
b) h e
c) l l
d) h l
Answer: a
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         void *p;
  5.         int a[4] = {1, 2, 3, 8};
  6.         p = &a[3];
  7.         int *ptr = &a[2];
  8.         int n = p - ptr;
  9.         printf("%dn", n);
  10.     }

a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer: b
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         void *p;
  5.         int a[4] = {1, 2, 3, 4};
  6.         p = &a[3];
  7.         int *ptr = &a[2];
  8.         int n = (int*)p - ptr;
  9.         printf("%dn", n);
  10.     }

a) 1
b) Compile time error
c) Segmentation fault
d) 4
Answer: a
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a[4] = {1, 2, 3, 4};
  5.         int b[4] = {1, 2, 3, 4};
  6.         int n = &b[3] - &a[2];
  7.         printf("%dn", n);
  8.     }

a) -3
b) 5
c) 4
d) Compile time error
Answer: a
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a[4] = {1, 2, 3, 4};
  5.         int *p = &a[1];
  6.         int *ptr = &a[2];
  7.         ptr = ptr * 1;
  8.         printf("%dn", *ptr);
  9.     }

a) 2
b) 1
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a[4] = {1, 2, 3, 4};
  5.         int *ptr  =  &a[2];
  6.         float n = 1;
  7.         ptr = ptr + n;
  8.         printf("%dn", *ptr);
  9.     }

a) 4
b) 3
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a[4] = {1, 2, 3, 4};
  5.         void *p = &a[1];
  6.         void *ptr = &a[2];
  7.         int n = 1;
  8.         n = ptr - p;
  9.         printf("%dn", n);
  10.     }

a) 1
b) 4
c) Compile time error
d) Depends on the compiler
Answer: b
Clarification: None.

250+ TOP MCQs on Complicated Declarations and Answers

C helps anyone preparing for EMC and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear EMC interviews on C Programming language.

Here is a listing of C multiple choice questions on “Complicated Declarations” along with answers, explanations and/or solutions:

1. Read the following expression?

a) ptr is pointer to int that converts its type to void
b) ptr is pointer to function passing int returning void
c) ptr is pointer to void that converts its type to int
d) ptr is pointer to function passing void returning int
Answer: b
Clarification: None.

2. Which of the following expression is true for the following C statement?

ptr is array with 3 elements of pointer to function returning pointer of int

a) int **ptr[3]();
b) int *(*ptr[3])();
c) int (*(*ptr[3])());
d) None of the mentioned
Answer: b
Clarification: None.

3. What makes the following declaration denote?

a) ptr is a function pointer that returns pointer to int type
b) ptr is a pointer to an int pointer
c) ptr is a pointer to pointer to type int
d) none of the mentioned
Answer: b
Clarification: None.

4. What makes the following declaration denote?

a) str is an array of 5 element pointer to type char
b) str is a pointer to an array of 5 elements
c) str is a function pointer of 5 elements returning char
d) none of the mentioned
Answer: a
Clarification: None.

5. Comment on the following declaration.

    int (*ptr)(); // i)
    char *ptr[]; // ii)

a) Both i) and ii) and cannot exist due to same name
b) i) is legal, ii) is illegal
c) i) is illegal, ii) is legal
d) Both i) and ii) will work legal and flawlessly
Answer: d
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         int no;
  5.         char name[20];
  6.     }
  7.     void main()
  8.     {
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

a) Compile time error
b) Nothing
c) hello
d) Varies
Answer: a
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         int no = 5;
  5.         char name[20];
  6.     };
  7.     void main()
  8.     {
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

a) Nothing
b) Compile time error
c) hello
d) Varies
Answer: b
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         int no;
  5.         char name[20];
  6.     };
  7.     void main()
  8.     {
  9.         student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

a) Nothing
b) hello
c) Compile time error
d) Varies
Answer: c
Clarification: None.

9. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         struct student
  5.         {
  6.             int no;
  7.             char name[20];
  8.         };
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("%d", s.no);
  12.     }

a) Nothing
b) Compile time error
c) Junk
d) 8
Answer: d
Clarification: None.

10. Is the below declaration legal?

a) True
b) False
c) Undefined behaviour
d) Depends on the standard
Answer: b
Clarification: None.