Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Multiple choice question for engineering

Set 1

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

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
  5.         printf("PEACH = %dn", PEACH);
  6.     }

a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6

View Answer

Answer: c [Reason:] In enum, the value of constant is defined to the recent assignment from left. Output: $ cc pgm1.c $ a.out PEACH = 5

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("C programming %s", "Class byn%s Aidlo", "WOW");
  5.     }

a) C programming Class by
    WOW Aidlo
b) C programming Class byn%s Aidlo
c) C programming Class by
    %s Aidlo
d) Compilation error

View Answer

Answer: c [Reason:] This program has only one %s within first double quotes, so it does not read the string “WOW”. The %s along with the Aidlo is not read as a format modifier while new line character prints the new line. Output: $ cc pgm2.c $ a.out C programming Class by %s Aidlo

3. For the following code snippet:
       char *str = “aidlo.com ” “training classes”;
       The character pointer str holds reference to string:
a) aidlo.com
b) aidlo.com training classes
c) aidlo.comtraining classes
d) Invalid declaration

View Answer

Answer: b [Reason:] ‘ ’ is accepted as a char in the string. Even though strlen will give length of string “aidlo.com”, in memory str is pointing to entire string including training classes”

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     #define a 10
  3.     int main()
  4.     {
  5.         const int a = 5;
  6.         printf("a = %dn", a);
  7.     }

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error

View Answer

Answer: c [Reason:] The #define substitutes a with 10 leaving no identifier and hence compilation error. Output: $ cc pgm3.c pgm3.c: In function ‘main’: pgm3.c:5: error: expected identifier or ‘(’ before numeric constant

5. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int var = 010;
  5.         printf("%d", var);
  6.     }

a) 2
b) 8
c) 9
d) 10

View Answer

Answer: b [Reason:] 010 is octal representation of 8. Output: $ cc pgm4.c $ a.out 8

6. What is the output of this C code?

  1.     #include <stdio.h>
  2.     enum birds {SPARROW, PEACOCK, PARROT};
  3.     enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
  4.     int main()
  5.     {
  6.         enum birds m = TIGER;
  7.         int k;
  8.         k = m;
  9.         printf("%dn", k);
  10.         return 0;
  11.     }

a) 0
b) Compile time error
c) 1
d) 8

View Answer

Answer: d [Reason:] m is an integer constant, hence compatible. Output: $ cc pgm5.c $ a.out 8

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     #define MAX 2
  3.     enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
  4.     int main()
  5.     {
  6.         enum bird b = PARROT;
  7.         printf("%dn", b);
  8.         return 0;
  9.     }

a) Compilation error
b) 5
c) Undefined value
d) 2

View Answer

Answer: b [Reason:] MAX value is 2 and hence PARROT will have value 3 + 2. Output: $ cc pgm6.c $ a.out 5

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     #include <string.h>
  3.     int main()
  4.     {
  5.         char *str = "x";
  6.         char c = 'x';
  7.         char ary[1];
  8.         ary[0] = c;
  9.         printf("%d %d", strlen(str), strlen(ary));
  10.         return 0;
  11.     }

a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)

View Answer

Answer: d [Reason:] str is null terminated but ary is not. Output: $ cc pgm7.c $ a.out 1 5

Set 2

1. Comment on the output of this C code?

  1.     #include <stdio.h>
  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) Program will compile and print the output 5
c) Program will compile and print the ASCII value of 5
d) Program will compile and print FAIL for 5 times

View Answer

Answer: d [Reason:] The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only. Output: $ cc pgm1.c $ a.out FAILED FAILED FAILED FAILED FAILED

2. The format identifier ‘%i’ is also used for _____ data type?
a) char
b) int
c) float
d) double

View Answer

Answer: b [Reason:] 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

View Answer

Answer: b [Reason:] 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

View Answer

Answer: d [Reason:] 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

View Answer

Answer: c [Reason:] The size of the data types depend on the system.

6. What is the output of this C code?

  1.     #include  <stdio.h>
  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

View Answer

Answer: b [Reason:] signed char will be a negative number. Output: $ cc pgm2.c $ a.out -128

7. Comment on the output of this C code?

  1.     #include  <stdio.h>
  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

View Answer

Answer: a [Reason:] None. Output: $ cc pgm3.c $ a.out a

8. What is short int in C programming?
a) Basic datatype of C
b) Qualifier
c) Short is the qualifier and int is the basic datatype
d) All of the mentioned

View Answer

Answer: c

Set 3

1. The output of the code below is

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5;
  5.         if (x < 1)
  6.             printf("hello");
  7.         if (x == 5)
  8.             printf("hi");
  9.         else
  10.             printf("no");
  11.     }

a) hi
b) hello
c) no
d) none of the mentioned

View Answer

Answer: a

2. The output of the code below is

  1.     #include <stdio.h>
  2.     int x;
  3.     void main()
  4.     {
  5.         if (x)
  6.             printf("hi");
  7.         else
  8.             printf("how are u");
  9.     }

a) hi
b) how are you
c) compile time error
d) none of the mentioned

View Answer

Answer: b

3. Comment on the following code below

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5;
  5.         if (true);
  6.             printf("hello");
  7.     }

a) It will display hello
b) It will throw an error
c) Nothing will be displayed
d) Compiler dependent

View Answer

Answer: b

4. The output of the code below is

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 0;
  5.         if (x == 0)
  6.             printf("hi");
  7.         else
  8.             printf("how are u");
  9.             printf("hello");
  10.     }

a) hi
b) how are you
c) hello
d) hihello

View Answer

Answer: d

5. The output of the code below is

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5;
  5.         if (x < 1);
  6.             printf("Hello");
  7.  
  8.     }

a) Nothing
b) Run time error
c) Hello
d) Varies

View Answer

Answer: c

6. The output of the code below is(when 1 is entered)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         double ch;
  5.         printf("enter a value btw 1 to 2:");
  6.         scanf("%lf", &ch);
  7.         switch (ch)
  8.         {
  9.         case 1:
  10.             printf("1");
  11.             break;
  12.         case 2:
  13.             printf("2");
  14.             break;
  15.         }
  16.     }

a) Compile time error
b) 1
c) 2
d) Varies

View Answer

Answer: a

7. The output of the code below is(When 1 is entered)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *ch;
  5.         printf("enter a value btw 1 to 3:");
  6.         scanf("%s", ch);
  7.         switch (ch)
  8.         {
  9.         case "1":
  10.             printf("1");
  11.             break;
  12.         case "2":
  13.             printf("2");
  14.             break;
  15.         }
  16.     }

a) 1
b) 2
c) Compile time error
d) No Compile time error

View Answer

Answer: c

8. When 1 is entered, The output of the code below is?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch)
  8.         {
  9.         case 1:
  10.             printf("1n");
  11.         default:
  12.             printf("2n");
  13.         }
  14.     }

a) 1
b) 2
c) 1 2
d) Run time error

View Answer

Answer: c

9. When 2 is entered, The output of the code below is?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch)
  8.         {
  9.         case 1:
  10.             printf("1n");
  11.             break;
  12.             printf("Hi");
  13.         default:
  14.             printf("2n");
  15.         }
  16.     }

a) 1
b) Hi 2
c) Run time error
d) 2

View Answer

Answer: d

10. When 1 is entered, The output of the code below is?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value btw 1 to 2:");
  6.         scanf("%d", &ch);
  7.         switch (ch, ch + 1)
  8.         {
  9.         case 1:
  10.             printf("1n");
  11.             break;
  12.         case 2:
  13.             printf("2");
  14.             break;
  15.         }
  16.     }

a) 1
b) 2
c) 3
d) Run time error

View Answer

Answer: b

Set 4

1. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 5;
  5.         int *p = &k;
  6.         int **m  = &p;
  7.         printf("%d%d%dn", k, *p, **m);
  8.     }

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Run time error

View Answer

Answer: a

2. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 5;
  5.         int *p = &k;
  6.         int **m  = &p;
  7.         printf("%d%d%dn", k, *p, **p);
  8.     }

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Compile time error

View Answer

Answer: d

3. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int k = 5;
  5.         int *p = &k;
  6.         int **m  = &p;
  7.         **m = 6;
  8.         printf("%dn", k);
  9.     }

a) 5
b) Compile time error
c) 6
d) Junk

View Answer

Answer: c

4. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[3] = {1, 2, 3};
  5.         int *p = a;
  6.         int *r = &p;
  7.         printf("%d", (**r));
  8.     }

a) 1
b) Compile time error
c) Address of a
d) Junk value

View Answer

Answer: b

5. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[3] = {1, 2, 3};
  5.         int *p = a;
  6.         int **r = &p;
  7.         printf("%p %p", *r, a);
  8.     }

a) Different address is printed
b) 1 2
c) Same address is printed.
d) 1 1

View Answer

Answer: c

6. How many number of pointer (*) does C have against a pointer variable declaration?
a) 7
b) 127
c) 255
d) No limits.

View Answer

Answer: d

7. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a = 1, b = 2, c = 3;
  5.         int *ptr1 = &a, *ptr2 = &b, *ptr3 = &c;
  6.         int **sptr = &ptr1; //-Ref
  7.         *sptr = ptr2;
  8.     }

a) ptr1 points to a
b) ptr1 points to b
c) sptr points to ptr2
d) None of the mentioned

View Answer

Answer: b

8. What is the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int a[3] = {1, 2, 3};
  5.         int *p = a;
  6.         int **r = &p;
  7.         printf("%p %p", *r, a);
  8.     }

a) Different address is printed
b) 1 2
c) Same address is printed.
d) 1 1

View Answer

Answer: c

Set 5

1. C99 standard guarantees uniqueness of ____ characters for internal names.
a) 31
b) 63
c) 12
d) 14

View Answer

Answer: b [Reason:] ISO C99 compiler may consider only first 63 characters for internal.

2. C99 standard guarantess uniqueness of _____ characters for external names.
a) 31
b) 6
c) 12
d) 14

View Answer

Answer: a [Reason:] ISO C99 compiler may consider only first 31 characters for external variables having 31 characters due to which it may not be unique.

3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned

View Answer

Answer: d

4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a

View Answer

Answer: c [Reason:] Variable name cannot start with a digit.

5. Variable names beginning with underscore is not encouraged. Why?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system

View Answer

Answer: c

6. All keywords in C are in
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned

View Answer

Answer: a

7. Variable name resolving (number of significant characters for uniqueness of variable) depends on
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None of the mentioned

View Answer

Answer: a [Reason:] It depends on the standard to which compiler and linkers are adhering to.

8. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $main;

View Answer

Answer: d [Reason:] Since only underscore and no other special character is allowed in a variable name, it results in an error.

9. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length

View Answer

Answer: c [Reason:] According to the syntax for C variable name, it cannot start with a digit.