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.

250+ TOP MCQs on Unions and Answers

C Objective Questions on “Unions”. 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 “Unions” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     union
  3.     {
  4.         int x;
  5.         char y;
  6.     }p;
  7.     int main()
  8.     {
  9.         p.x = 10;
  10.         printf("%dn", sizeof(p));
  11.     }

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(int)
Answer: d
Clarification: None.

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

  1.     #include 
  2.     union
  3.     {
  4.         int x;
  5.         char y;
  6.     }p;
  7.     int main()
  8.     {
  9.         p.y = 60;
  10.         printf("%dn", sizeof(p));
  11.     }

a) Compile time error
b) sizeof(int) + sizeof(char)
c) Depends on the compiler
d) sizeof(char)
Answer: c
Clarification: None.

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

  1.     #include 
  2.     union p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         union p p, b;
  10.         p.y = 60;
  11.         b.x = 12;
  12.         printf("%dn", p.y);
  13.     }

a) Compile time error
b) Depends on the compiler
c) 60
d) Undefined behaviour
Answer: c
Clarification: None.

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

  1.     #include 
  2.     union p
  3.     {
  4.         int x;
  5.         char y;
  6.     }k = {1, 97};
  7.     int main()
  8.     {
  9.         printf("%dn", k.y);
  10.     }

a) Compile time error
b) 97
c) a
d) 1
Answer: d
Clarification: None.

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

  1.     #include 
  2.     union p
  3.     {
  4.         int x;
  5.         char y;
  6.     }k = {.y = 97};
  7.     int main()
  8.     {
  9.         printf("%dn", k.y);
  10.     }

a) Compile time error
b) 97
c) a
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  2.     union p
  3.     {
  4.         int x;
  5.         float y;
  6.     };
  7.     int main()
  8.     {
  9.         union p p, b;
  10.         p.x = 10;
  11.         printf("%fn", p.y);
  12.     }

a) Compile time error
b) Implementation dependent
c) 10.000000
d) 0.000000
Answer: b
Clarification: None.

7. Which of the following share a similarity in syntax?

1. Union, 2. Structure, 3. Arrays and 4. Pointers

a) 3 and 4
b) 1 and 2
c) 1 and 3
d) 1, 3 and 4
Answer: b
Clarification: None.

8. What will be the output of the following C code? (Assuming size of char = 1, int = 4, double = 8)

  1.     #include 
  2.     union utemp
  3.     {
  4.         int a;
  5.         double b;
  6.         char c;
  7.     }u;
  8.     int main()
  9.     {
  10.         u.c = 'A';
  11.         u.a = 1;
  12.         printf("%d", sizeof(u));
  13.     }

a) 1
b) 4
c) 8
d) 13
Answer: c
Clarification: None.

250+ TOP MCQs on String Operations and Answers

C programming questions on “String Operations”. One shall practice these 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 programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming questions on “String Operations” 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.         char *str = "hello, world";
  5.         char *str1 = "hello, world";
  6.         if (strcmp(str, str1))
  7.             printf("equal");
  8.         else
  9.             printf("unequal");
  10.     }

a) equal
b) unequal
c) Compilation error
d) Depends on the compiler
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world";
  5.         char str1[15] = "hello wo 9";
  6.         strcpy(str, str1);
  7.         printf("%s", str1);
  8.     }

a) Compilation error
b) Segmentation Fault
c) hello, world
d) hello, wo 9
Answer: b
Clarification: None.

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

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         char *str = "hello, world";
  6.         char str1[9];
  7.         strncpy(str1, str, 9);
  8.         printf("%s %d", str1, strlen(str1));
  9.     }

a) hello, world 11
b) hello, wor 9
c) Undefined behaviour
d) Compilation error
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, worldn";
  5.         printf("%d", strlen(str));
  6.  
  7.     }

a) Compilation error
b) Undefined behaviour
c) 13
d) 11
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[11] = "hello";
  5.         char *str1 = "world";
  6.         strcat(str, str1);
  7.         printf("%s %d", str, str[10]);
  8.     }

a) helloworld 0
b) helloworld anyvalue
c) worldhello 0
d) Segmentation fault/code crash
Answer: a
Clarification: None.

6. Strcat() function adds null character.
a) Only if there is space
b) Always
c) Depends on the standard
d) Depends on the compiler
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[10] = "hello";
  5.         char *str1 = "world";
  6.         strncat(str, str1, 9);
  7.         printf("%s", str);
  8.     }

a) helloworld
b) Undefined behaviour
c) helloworl
d) hellowor
Answer: a
Clarification: None.

250+ TOP MCQs on File Operations and Answers

C MCQs on “File Operations – 1”.

1. Which one of the following is correct syntax for opening a file.
a) FILE *fopen(const *filename, const char *mode)
b) FILE *fopen(const *filename)
c) FILE *open(const *filename, const char *mode)
d) FILE open(const*filename)
Answer: a
Clarification: fopen() opens the named file, and returns a stream, or NULL of the attempt fails.

2. What is the function of the mode ‘ w+’?
a) create text file for writing, discard previous contents if any
b) create text file for update, discard previous contents if any
c) create text file for writing, do not discard previous contents if any
d) create text file for update, do not discard previous contents if any
Answer: b
Clarification: w+ is a mode used to open a text file for update (i. e., writing and reading), discard previous contents if any.

3. If the mode includes b after the initial letter, what does it indicates?
a) text file
b) big text file
c) binary file
d) blueprint text
Answer: c
Clarification: If the mode consists of letter b after the first letter as in, “rb” or “w+b”, it indicates binary file.

4. fflush(NULL) flushes all ____________
a) input streams
b) output streams
c) previous contents
d) appended text
Answer: b
Clarification: fflush(FILE *stream) – fflush() causes any buffered but unwritten to be written on an Output stream. On an input stream, the effect is undefined. fflush(NULL) flushes all output streams.

5. _____removes the named file, so that a subsequent attempt to open it will fail.
a) remove(const *filename)
b) remove(filename)
c) remove()
d) fclose(filename)
Answer: a
Clarification: remove(const *filename) removes the named file, so that a subsequent attempt to open it will fail. It returns non-zero of the attempt fails.

6. What is the function of FILE *tmpfile(void)?
a) creates a temporary file of mode “wb+”
b) creates a temporary file of mode “wb”
c) creates a temporary file of mode ” w”
d) creates a temporary file of mode “w+”
Answer: a
Clarification: A temporary file is created by tmpfile() function of mode “wb+” that will be automatically removed when closed or when the program terminates normally.

7. What does tmpfile() returns when it could not create the file?
a) stream and NULL
b) only stream
c) only NULL
d) does not return anything
Answer: a
Clarification: tmpfile() returns a stream or NULL if it could not create the file.

8. Choose the right statement for fscanf() and scanf()
a) fscanf() can read from standard input whereas scanf() specifies a stream from which to read
b) fscanf() can specifies a stream from which to read whereas scanf() can read only from standard input
c) fscanf() and scanf() has no difference in their functions
d) fscanf() and scanf() can read from specified stream
Answer: b
Clarification: The fscanf() is similar to the scanf() function, except that the first argument of fscanf() specifies a stream from which to read whereas scanf() can read from standard input.

9. EOF is an integer type defined in stdio. hand has a value ____________
a) 1
b) 0
c) NULL
d) – 1
Answer: d
Clarification: EOF is an integer type defined in stdio. hand has a value – 1.

10. fwrite() can be used only with files that are opened in binary mode.
a) true
b) false
Answer: a
Clarification: fwrite() can be used to write characters, integers, or structures to a file. However, fwrite() can be used only with files opened in binary mode.

250+ TOP MCQs on General Utilities and Answers

C Multiple Choice Questions & Answers on “General Utilities – 2”.

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

char word[20 ] = "1.234555 WELCOME"; 
char *w; double dis; 
dis= strtod(word, &w); 
printf("The number is %lfn", dis); 
printf("String  is |%s|", w);

a) The number is 1.234555 String is |WELCOME|
b) The number is 1.2345550 String is |WELCOME|
c) The number is 1.234555 String is |1.234555 WELCOME|
d) Errror
Answer: a
Clarification: strtod() function is used to return the converted floating point number as a double value, else zero value (0.0) is returned.

2. Which statement is correct work reference to endptr?

double strtod(const char *nptr, char **endptr);

a) A pointer to the starting string is stored in the object pointed to by endptr, provided that endptr is a null pointer
b) A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer
c) A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is a null pointer
d) A pointer to the starting string is stored in the object pointed to by endptr, provided that endptr is not a null pointer
Answer: b
Clarification: If endptr is not NULL then endptr stores the pointer to a character after the last character.

3. Which of the following functions decomposes the input string into three pans: an initial, possibly empty, sequence of white-space characters?
a) strtod()
b) atof()
c) atol()
d) strtol()
Answer: d
Clarification: The strtol() function is used to convert the initial portion of the string pointed to by, to long int representation. First, it decomposes the input string into three pans: an initial, empty, white-space characters (as specified by the isspace function).

4. The______function is used to convert the initial portion of the string pointed to by, to unsigned long int representation.
a) strtod()
b) atol()
c) strtoul()
d) strtol()
Answer: c
Clarification: unsigned long int strtoul(const char *p, char **ptr, int base) function is used to convert the initial part of the string in p to an unsigned long int value according to the given base,it must be between 2 and 36 inclusive, or be the special value 0.

5. Which of the following is the correct syntax of the function strtoul()?
a) unsigned long int strtoul(const char *n, char **ptr, int base)
b) unsigned long int strtoul(const char *n, char **ptr)
c) unsigned long int strtoul(const char *n)
d) int strtoul(const char *n)
Answer: a
Clarification: usigned long int strtoul(conmt char *n, char **ptr, int base); The strtoul() function is used to convert the initial portion of the string pointed to by n to unsigned long int representation.

6. Select the right statement with reference to malloc() and calloc().
a) malloc() does not set the memory to zero whereas calloc() sets allocated memory to zero
b) malloc() sets the memory to zero whereas calloc() does not set allocated memory to zero
c) malloc() sets the memory to zero whereas calloc() sets allocated memory to zero
d) malloc() does not set the memory to zero whereas calloc() does not set allocated memory to zero
Answer: a
Clarification: The difference in malloc() and calloc() is that calloc() sets the memory to zero whereas malloc()does not sets allocated memory to zero.

7. The calloc() function allocates space for an array of n objects, each of whose size is defined by size. Space is initialized to all bits zero.
a) true
b) false
Answer: a
Clarification: void *calloc(size-t n, size-t size);
This function is used to allocate the requested memory and returns a pointer to it.

8. Is this right explanation to the given code?

void *calloc(size_t n, size_t size) 
#n -- This is the number of elements to be allocated.
#size -- This is the size of elements.

a) true
b) false
Answer: a
Clarification: void *calloc(size_t n, size_t size) The calloc() function allocates space for an array of n objects, each of whose size is given by size. The space is initialized to all bits zero.

9. Which among the given function does not return a value?
a) strtoul()
b) strtol()
c) rand()
d) srand()
Answer: d
Clarification: void srand(unsigned int seed);
The srand() function uses argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). The srand() function returns no value.

10. Which function returns a pseudo-random integer?
a) srand()
b) rand()
c) malloc()
d) alloc()
Answer: b
Clarification: int rand (void) ;
The rand() function is used to compute a sequence of pseudo-random integers in the range 0 to RAND-MAX. The rand function returns a pseudo-random integer.