250+ TOP MCQs on String Operations and Answers

C Multiple Choice Questions & Answers (MCQs) on “String Operations – 4”.

1. What will be returned in the following C code?

size- t strlen(const char *s)
const char *sc;
for(sc = s; *sc!= '  O ' ; ++sc)
return(sc - s) ;

a) number of characters equal in sc
b) length of string s
c) doesn’t return any value
d) displays string s
Answer: b
Clarification: The strlen() function is used to return the length of the string s.

2. The function strsp() is the complement of strcspn().
a) true
b) false
Answer: a
Clarification: Both strcspn() and strpbrk() perform the same function. Only strspn() the return values differ. The function strspn() is the complement of strcspn() .

3. What will the following C code do?

char * strrchr(const char *s, int c )
char ch = c;
char *sc;
for(sc = NULL; ; ++s)
if(*s == ch)
SC = 9;
i f (*s =='O' )
return (( char *) s);

a) find last occurrence of c in char s[ ].
b) find first occurrence of c in char s[ ].
c) find the current location of c in char s[ ].
d) There is error in the given code
Answer: a
Clarification:The strrchr() function locates the last occurrence of c (converted to a char) in the string pointed to by s. String contains null character as a terminating part of it.

4. This function offers the quickest way to determine whether two character sequences of the same known length match character for the character up to and including any null character in both.
a) strcmp()
b) memcmp()
c) strncmp()
d) no such function
Answer: c
Clarification: The strncmp() function is used to compare not more than n characters (characters that follow a null character are not compared) from the array pointed to by one, to the array pointed to by other.

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

char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat= strncmp(str1, str2, 4);
if(mat< 0)
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1");
else
printf("both are equal");

a) str1 is not greater than str2
b) str2 is not greater than str1
c) both are equal
d) error in given code
Answer: b
Clarification: The C library function int strncmp(const char *str1, const char *str2, size_t n) is used to compare at most the first n bytes of str1 and str2.

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

void *memset(void *c, int c, size-t n) 
unsigned char ch = c;
unsigned char *su;
for (su = s; 0 < n; ++su, --n)
<br>
*su = ch;
<br>

a) store c throughout unsigned char s[n]
b) store c throughout signed char s[n]
c) find first occurrence of c in s[n]
d) find last occurrence of c in s[n]
Answer: a
Clarification: This is the safe way to store the same value throughout a contiguous sequence of elements in a character array.

7. Use_______to determine the null-terminated message string that corresponds to the error code errcode.
a) strerror()
b) strstr()
c) strxfrm()
d) memset()
Answer: a
Clarification: Use strerror (errcode) to determine the null-terminated message string that corresponds to the error code errcode.

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

const char str1[10]="Helloworld";
const char str2[10] = "world";
char *mat;
mat = strstr(str1, str2);
printf("The substring is:%sn", mat);

a) The substring is:world
b) The substring is:Hello
c) The substring is:Helloworld
d) error in the code
Answer: a
Clarification: The C library function char *strstr(const char *str1, const char *str2) is used to find the first occurrence of the substring str2 in the string str1.The terminating ‘ ’ characters are not compared.

9. void *memcpy(void *dest, const void *src, size_t n) What does the following code do?
a) copies n characters from src to dest
b) copies n character from dest to src
c) transform n character from dest to src
d) transform n character from src to dest
Answer: a
Clarification: In the C library function memcpy() is used to copy n characters from memory area src to memory area dest.

10. What will the given C code do?

int memcmp(const void *str1, const void *str2, size_t n)

a) compares the first n bytes of str1 and str2
b) copies the first n bytes of str1 to str2
c) copies the first n bytes of str2 to str1
d) invalid function
Answer: a
Clarification: In the C library function int memcmp(const void *str1, const void *str2, size_t n)) is used to compare the first n bytes of memory area str1 and memory area str2.

250+ TOP MCQs on Non-Local Jumps and Answers

C Multiple Choice Questions & Answers on “Non-Local Jumps – 2”.

1. The header file setjmp.h is used to __________
a) set location specific information
b) control low-level calls and returns to and from functions
c) handle signals reported during a program’s execution
d) manipulate strings (character arrays)
Answer: b
Clarification: The header file setjmp.h is used to control low-level calls and returns to and from functions.

2. The void longjmp( jmp-buf env, int val) function causes setjmp() macro to return _______ value; if val is 0.
a) zero
b) one
c) null
d) no return
Answer: b
Clarification: The longjmp() function cannot cause the setjmp() to return value zero,if the variable val is zero. It always return one when the value of val is 0.

3. Which is the true statement with respect to the function longjmp()?
a) the function where setjmp() was called has terminated, then the results are undefined
b) the function where setjmp() was called has terminated, then the results are defined
c) the function where jmp_buf was called has terminated, then the results are undefined
d) the function where jmp_buf was called has terminated, then the results are defined
Answer: a
Clarification: The longjmp() function restores the environment saved by the most recent invocation of the set jmp macro in the same invocation of the program, with the corresponding jmp buf argument. The behavior is undefined, if there has been no such invocation, or if the function containing the invocation of the setjmp() macro has terminated execution in the interim.

4. Which of the given statement is not true with respect to void longjmp( jmp-buf env, int val)?
a) The variable value cannot be zero
b) env is the object containing information to restore the environment at the jmp_buf’s calling point
c) This function does not return any value
d) This function restores the environment saved by the most recent call to setjmp() macro
Answer: b
Clarification: In the function void longjmp( jmp-buf env, int val), env is the object containing information to restore the environment at the setjmp’s calling point.

5. What is the function of the given longjump(jmp_buf buf, i)?
a) go back to place buf is pointing to and return i
b) go back to place buf is pointing to and return 0
c) uses buf to remember current position and returns 0
d) uses buf to remember current position and returns i
Answer: a
Clarification: In the given function longjmp(jmp_buf buf,i), it goes back to place buf is pointing to and return i.setjmp(jmp_buf buf) uses buf to remember current position to and return 0.

6. How many times does the function longjmp() returns?
a) once
b) twice
c) thrice
d) never
Answer: d
Clarification: longjmp() function defined under setjmp.h header file does not return any value.

7. A less common use of setjmp.h is to create syntax similar to ____________
a) errno
b) variable arguments
c) coroutines
d) retval
Answer: c
Clarification: setjmp.h is sometime is used to create syntax similar to coroutines. Coroutines are computer program components.

8. What will the following C statement do?

#include < setjmp.h >
int setjmp(jmp_buf env);

a) save the current state of the registers into env
b) resets the registers to the values saved in env
c) provides a definition for env structure
d) accept variable(env) argument lists to be written
Answer: a
Clarification: The given statement is used to save the current state of the registers into env. The state of the program is dependent on the contents of its memory(code, stack, globals and heap) and the contents of its registers.

9. What are the contents of the register?
a) sp, fp only
b) sp only
c) fp, pc only
d) sp, fp, pc
Answer: d
Clarification: The contents of the registers includes sp, fp and cp. sp, fp and pc stands for stack pointer, frame pointer, and program counter.

10. setjmp takes a jmp_buf type and different other type variables as input.
a) true
b) false
Answer: b
Clarification: setjmp takes jmp_buf type variable as the only input.

contest

250+ TOP MCQs on Endianness and Answers

C Multiple Choice Questions & Answers (MCQs) on “Endianness”.

1. A machine in which the least significant byte is stored in the smallest address is __________
a) Big endian machine
b) Bi-endian machine
c) Binary bit machine
d) Little endian machine
Answer: d
Clarification: In little endian machines, last byte of binary representation (least significant byte) of a multi byte data type is stored first, whereas in big endian method, the first byte of binary representation of a multi byte data type is stored first.

2. If the output of the following C code is “Big endian”, then what will be the value of *a is?

#include 
int main()
{
   unsigned int i = 1;
   char *a = (char*)&i;
   if (*a)
       printf("Little endian");
   else
       printf("Big endian");
   getchar();
   return 0;
}

a) -1
b) 0
c) 1
d) 2
Answer: b
Clarification: In the code shown above, a character pointer ‘a’ points to an integer ‘i’. Since the size of the character is 1 byte, when the character pointer is de-referenced, it contains 1 integer byte. If the machine is a little endian machine then the value of *a will be 1, since the last byte is stored first. If the machine is big endian, the value of *a will be 0.

3. It is possible for a processor to support both little and big endian methods.
a) True
b) False
Answer: a
Clarification: It is possible for a processor to support both little and big endian methods. Such machines are known as bi-endian machines.

4. The standard byte order for networks is ____________
a) Bit-Binary
b) Little endian
c) Big endian
d) Bi-endian
Answer: c
Clarification: The standard byte order (network byte order) for networks is big endian. Before transferring data on a network, data is converted to big endian.

5. Which of the following is not an example of big endian machines?
a) Power PC
b) Motorola 68K
c) SPARC processors
d) ARM processors
Answer: d
Clarification: ARM processor is an example of little endian machine. Current generation ARM processor is an example of bi-endian machine. Power PC, Motorola 68K and SPARC are examples of big endian mahines.

6. Suppose we transfer a file written on a little endian machine to a big endian machine and there is no transformation from little endian to big endian, then what happens?
a) The big endian machine throws an error when it receives the file
b) The big endian machine reads the file in the reverse order
c) The big endian machine does not read the file
d) The big endian machine reads the file in the normal order
Answer: b
Clarification: If there is no transformation from little endian to big endian on transfer of a file from a little endian machine to a big endian machine, the big endian machine reads the file in reverse order.

7. File formats which have _________ as a basic unit are independent of endianness.
a) 1 byte
b) 2 bytes
c) 3 bytes
d) 4 bytes
Answer: a
Clarification: File formats which have 1 byte as the basic unit are not endianness dependent. Eg: ASCII files. Other file formats have fixed endianness formats.

8. If the code shown below is executed on a little endian machine, then what will be the output of the following C code?

#include
main()
{
    int y=1;
    printf("%d", (*(char*)&y));
}

a) 1
b) 1000
c) 9999
d) 0
Answer: a
Clarification: The output of the above code will be one when it is run on a little endian machine. This is because a little endian machine stores the least significant byte in the smallest address.

9. If the data “ABCD” is to be stored in a little endian machine, it will be stored as _________
a) ABCD
b) DCBA
c) CDAB
d) BCDA
Answer: b
Clarification: In a little endian machine, the least significant byte (right most) is stored in the smallest address. Hence the data “ABCD” will be stored in the form of “DCBA”.

10. The sequential order used to interpret a range of bytes in the memory of a computer is known as _________
a) Ordering
b) Sequencing
c) Endianness
d) Byte prediction
Answer: c
Clarification: Endianness is the sequential order used to interpret a range of bytes in the memory of a system.

250+ TOP MCQs on Increment and Decrement Operators and Answers

online C test on “Increment and Decrement Operators”. One shall practice these test 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 online test questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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

1. What is the difference between the following 2 codes?

  1.     #include  //Program 1
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ + ++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }
  1.     #include  //Program 2
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ +++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }

a) No difference as space doesn’t make any difference, values of a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not
Answer: d
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 1, b = 1, c;
  5.         c = a++ + b;
  6.         printf("%d, %d", a, b);
  7.     }

a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 1, b = 1, d = 1;
  5.         printf("%d, %d, %d", ++a + ++a+a++, a++ + ++b, ++d + d++ + a++);
  6.     }

a) 15, 4, 5
b) 9, 6, 9
c) 9, 3, 5
d) Undefined (Compiler Dependent)
Answer: d
Clarification: None.

4. For which of the following, “PI++;” code will fail?
a) #define PI 3.14
b) char *PI = “A”;
c) float PI = 3.14;
d) none of the Mentioned
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 10, b = 10;
  5.         if (a = 5)
  6.         b--;
  7.         printf("%d, %d", a, b--);
  8.     }

a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         int j = i++ + i;
  6.         printf("%dn", j);
  7.     }

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

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

  1.      #include 
  2.     int main()
  3.     {
  4.         int i = 2;
  5.         int j = ++i + i;
  6.         printf("%dn", j);
  7.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 2;
  5.         int i = i++ + i;
  6.         printf("%dn", i);
  7.     }

a) = operator is not a sequence point
b) ++ operator may return value with or without side effects
c) it can be evaluated as (i++)+i or i+(++i)
d) = operator is a sequence point
Answer: a
Clarification: None.

250+ TOP MCQs on For Loops and Answers

C language Objective Questions on “For Loops”. 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 language Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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

1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________
a) break
b) exit(0)
c) abort()
d) terminate
Answer: a
Clarification: None.

2. What will be the correct syntax for running two variable for loop simultaneously?
a)

   for (i = 0; i < n; i++)
   for (j = 0; j < n; j += 5)

b)

for (i = 0, j = 0; i < n, j < n; i++, j += 5)

c)

   for (i = 0; i < n;i++){}
   for (j = 0; j < n;j += 5){}

d) none of the mentioned
Answer: b
Clarification: None.

3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
Answer: d
Clarification: None.

4. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3)?
a) variable
b) function
c) typedef
d) macros
Answer: d
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         short i;
  5.         for (i = 1; i >= 0; i++)
  6.             printf("%dn", i);
  7.  
  8.     }

a) The control won’t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully terminate
d) This program will get into an infinite loop and keep printing numbers with no errors
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 0;
  5.         for (k)
  6.             printf("Hello");
  7.     }

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 0;
  5.         for (k < 3; k++)
  6.         printf("Hello");
  7.     }

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         double k = 0;
  5.         for (k = 0.0; k < 3.0; k++)
  6.             printf("Hello");
  7.     }

a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely
Answer: b
Clarification: None.

250+ TOP MCQs on Static Variables and Answers

C questions and puzzles on “Static Variables” 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.         m();
  5.         m();
  6.     }
  7.     void m()
  8.     {
  9.         static int x = 5;
  10.         x++;
  11.         printf("%d", x);
  12.     }

a) 6 7
b) 6 6
c) 5 5
d) 5 6

Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         printf("x is %d", x);
  6.     }

a) 0
b) 1
c) Junk value
d) Run time error

Answer: a
Clarification: None.

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

  1.     #include 
  2.     static int x;
  3.     void main()
  4.     {
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) 0
b) Junkvalue
c) Run time error
d) Nothing

Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static double x;
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) Nothing
b) 0
c) Compile time error
d) Junkvalue

Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         if (x++ < 2)
  6.         main();
  7.     }

a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice

Answer: d
Clarification: None.

6. Which of following is not accepted in C?
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) all of the mentioned

Answer: c
Clarification: None.

7. Which of the following cannot be static in C?
a) Variables
b) Functions
c) Structures
d) None of the mentioned

Answer: d
Clarification: None.