250+ TOP MCQs on String Operations and Answers

C Objective Questions on “String Operations – 3”.

1. What is the return value of strxfrm()?
a) length of the transformed string, not including the terminating null-character
b) length of the transformed string, including the terminating null-character
c) display the transformed string, not including the terminating null character
d) display the transformed string, not including the terminating null-character
Answer: a
Clarification: This function returns the length of the transformed string, not including the terminating null character.

2. Is there any function declared as strstr()?
a) true
b) false
Answer: a
Clarification: This function returns a pointer to the first occurrence in s1 of any of the entire sequence of characters specified in s2, or a null pointer if the sequence is not present in s1.
char *strstr(const char *s1, const char *s2)

3. The C library function _________ breaks string s1 into a series of tokens using the delimiter s2.
a) char *strtok(char *s1,const char *s2);
b) char *strtok(char *s2,const char *s1);
c) char *strstr(char *s1,const char *s2);
d) char *strstr(char *s2,const char *s1);
Answer: a
Clarification: The C library function char *strtok(char *s1, const char *s2) breaks string s1 into a series of tokens using the delimiter s2.

4. The______function returns a pointer to the first character of a token.
a) strstr()
b) strcpy()
c) strspn()
d) strtok()
Answer: d
Clarification: The strtok() function returns a pointer to the first character of a token, if there is no token then a null pointer is returned.

5. which of the following function returns a pointer to the located string or a null pointer if string is not found.
a) strtok()
b) strstr()
c) strspn()
d) strrchr()
Answer: b
Clarification: The strstr() function is used to return a pointer to the located string, or if string is not found a null pointer is returned.

6. Which of the given function is used to return a pointer to the located character?
a) strrchr()
b) strxfrm()
c) memchar()
d) strchar()
Answer: d
Clarification: The strchr() function is used to return a pointer to the located character if character does not occur then a null pointer is returned.

7. The strpbrk() function is used to return a pointer to the character, or a null pointer if no character from s2 occurs in s1.
a) true
b) false
Answer: a
Clarification: char *strpbrk(const char *s1,const char *s2);
The first occurrence in the string s1 of any character from the string s2 is done by strpbrk().

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

const char str1[] = "abcdef";
const char str2[] = "fgha";
char *mat;
mat= strpbrk(str1, str2);
if(mat)
printf("First matching character: %cn", *mat);
else
printf("Character not found");

a) g
b) a
c) h
d) f
Answer: d
Clarification: The strpbrk() function is used to locate the first occurrence in the string str1 of any character from the string str2.

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

char str1[] = "Helloworld ";
char str2[] = "Hello";
len = strspn(str1, str2);
printf("Length of initial segment matching %dn", len );

a) 6
b) 5
c) 4
d) no match
Answer: b
Clarification: The length of the maximum initial segment of the string str1 which consists entirely of characters from the string str2 is computed by strspn().

10. The______ function returns the number of characters that are present before the terminating null character.
a) strlength()
b) strlen()
c) strlent()
d) strchr()
Answer: b
Clarification: The strlen() function is used to return the number of characters that are present before the terminating null character.size-t strlen(const char *s);The length of the string pointed to by s is computed by strlen().

250+ TOP MCQs on Non-Local Jumps and Answers

C MCQs on “Non-Local Jumps – 1”.

1. Which of the following header file defines one function longjmp(), and one variable type jmp_buf?
a) stdarg.h
b) locale.h
c) setjmp.h
d) stdlib.h
Answer: c
Clarification: setjmp.h header file defines the macro setjmp(), one function longjmp(), and one variable type jmp_buf.

2. Which of the given options is an array type used for holding information?
a) longjmp
b) setjmp
c) jmp_buf
d) no such variable
Answer: c
Clarification: jmp_buf is an array type used for holding information of macro setjmp() and function longjmp().This is a variable type defined under the header file setjmp.h.

3. Which macro saves the current environment into the variable environment for later use by the function longjmp().
a) setjmp
b) longjmp
c) jmp
d) set_jmp
Answer: a
Clarification: The setjmp() macro saves its calling environment in its jmp-buf argument to be used later by the longjmp() function.

4. If setjmp() macro returns directly from the macro invocation, it______
a) returns zero
b) returns non-zero
c) produces error
d) nothing can be said
Answer: a
Clarification: The setjmp() macro returns zero if the return is from a direct invocation.

5. A non-zero value is returned, if setjmp() returns from a longjmp() function call.
a) false
b) true
Answer: b
Clarification: The setjmp() macro returns a nonzero value, if the return is from a call to the longjmp() function.

6. Select the correct declaration of setjmp().
a) int setjmp(jmp_buf environment)
b) int setjmp(long_jmp environment)
c) int setjmp(jmp_buf )
d) int setjmp(long_jmp)
Answer: a
Clarification: Declaration of the macro setjmp() is int setjmp(jmp_buf environment). ’environment’ is a the object of type jmp_buf.

7. How many times can the macro setjmp() return?
a) one time
b) two times
c) three times
d) many times
Answer: b
Clarification: setjmp() macro returns twice. First time, setjmp() returns zero on its direct invocation. Second time macro returns when longjmp is called with the information written to the environment; now, it returns the value passed to longjmp as second argument.

8. longjmp() function is the only function defined under the header file setjmp.h?
a) true
b) false
Answer: a
Clarification: The only function defined under the header file setjmp.h is longjmp().LONGJMP() resets the registers to the values saved in an environment.

9. Which function restores the environment saved by the most recent invocation of the setjmp() macro in the same invocation of the program?
a) jmp_buf
b) longjmp
c) jmpbuf
d) long_jmp
Answer: b
Clarification: longjmp() is the only function defined under the header file setjmp.h. What setjmp() does is save the contents of the registers so that longjmp() can restore the contents later.

10. Choose the right declaration of longjmp() function.
a) void longjmp(jmp_buf environment, int value)
b) void longjmp(setjmp environment, int value)
c) void longjmp(int value, jmp_buf environment)
d) void longjmp(int value, setjmp environment)
Answer: a
Clarification: The right declaration of the function longjmp() is void longjmp(jmp_buf environment, int value). This function works in pair with setjmp() macro.

250+ TOP MCQs on Token Concatenation and Answers

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

1. Which of the following operators is used to concatenate two strings without space?
a) #
b) < >
c) **
d) ##
Answer: d
Clarification: The operator ## is used for token concatenation (to concatenate two strings without space).

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

#include 
#define p( n ) printf( "t" #n " = %d", t##n )
int t3=10;
int main()
{
   p(3);
}

a) t=10
b) t3=10
c) t10=3
d) t=3
Answer: b
Clarification: The code shown above uses the ## operator to concatenate ‘t’ and 3. t3 has been declared as 10 in the code, Hence the output of the code shown above is: t3=10

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

#include 
#define p( n,m ) printf( "%d", m##n )
int main()
{
   p(3,4);
}

a) Error
b) Junk value
c) 34
d) 43
Answer: d
Clarification: In the code shown above, we have concatenated the two tokens in the order: mn. The value of m is equal to 4 and that of n is equal to 3. Hence the output of this code is 43.

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

#include 
#define p( n,m ) printf( "%d", m##n )
#define q(a,b) printf("%d",a##b)
main()
{
   p(3,4);
   q(5,5);
}

a) 4356
b) 3456
c) 4365
d) 3465
Answer: a
Clarification: In the code shown above we have concatenated m and n in one statement and a and b in another. Since we have not used a new line character, the output of this code will be equal to 4356.

5. The following C code results in an error.

#include 
#define world( n ) printf( "t^^" #n" = %c", t##n )
int t3=1;
int main()
{
   world(3);
}

a) True
b) False
Answer: b
Clarification: The code shown above does not result in an error. The output of this code is t^^3=junk value.

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

#include 
#define display( n ) printf( "a" #n " = %d", a##n )
int main()
{
   display(3);
}

a) a3
b) 31
c) a 3
d) error
Answer: d
Clarification: The code shown above results in an error because we have not explicitly declared a3. Had we declared a3 in this code, it would not have thrown an error.

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

#include 
#define hello( n ) a##n
int a3;
int main()
{
    int x;
    x=hello(3);
    if(x!=0)
        printf("hi");
    else
        printf("good");
}

a) error
b) a3
c) good
d) hi
Answer: c
Clarification: The code shown will print ‘hi’ if x is not equal to zero and ‘good’ if x is equal to zero. In the above code, we have declared x (equal to zero). Hence ‘good’ is printed as output.

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

#include 
#define hello( n ) printf( "a" #n "= %d", a##n )
int a3=3;
int main()
{
    #ifdef a3
       hello(3);
   #else
       printf("sorry");
   #endif
}

a) a3=3
b) error
c) a=3
d) sorry
Answer: d
Clarification: The code shown above prints a3=3 if as is defined. Since a3 is not defines, ‘sorry’ is printed as output.

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

#include 
#define p( n ) printf( "t*" #n " = %s", t##n )
char tsan[]="tsan";
int main()
{
    int x;
    x=p(san);
}

a) error
b) tsan=tsan
c) t*san=t*san
d) t*san=tsan
Answer: d
Clarification: The code shown above uses the ## operator to concatenate the tokens t* and san. We have decalred tsan[]=tsan. Hence the output of the code shown will be: t*san=tsan.

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

#include 
#define p( n ) printf( "t%n" #n " = %d", t##n )
int t3=10;
int main()
{
    int x;
    x=p(3);
}

a)

   t
   3=10

b) t3=10
c) t%3=10
d)

    t
    %3=10

View Answer

Answer: a
Clarification: In this code, operator ## is used to concatenate t and 3. However, a new line character is a part of this statement. The variable t3 has been declared as 10. Hence the output of this code will be: t
3=10

 
 

250+ TOP MCQs on Type Conversions and Answers

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

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

1. function tolower(c) defined in library works for ___________
a) Ascii character set
b) Unicode character set
c) Ascii and utf-8 but not EBCDIC character set
d) Any character set
Answer: d
Clarification: None.

2. What will be the output of the following C code considering the size of a short int is 2, char is 1 and int is 4 bytes?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i = 20;
  5.         char c = 97;
  6.         printf("%d, %d, %dn", sizeof(i), sizeof(c), sizeof(c + i));
  7.         return 0;
  8.     }

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

3. Which type of conversion is NOT accepted?
a) From char to int
b) From float to char pointer
c) From negative int to char
d) From double to char
Answer: b
Clarification: Conversion of a float to pointer type is not allowed.

4. What will be the data type of the result of the following operation?

(float)a * (int)b / (long)c * (double)d

a) int
b) long
c) float
d) double
Answer: d
Clarification: None.

5. Which of the following type-casting have chances for wrap around?
a) From int to float
b) From int to char
c) From char to short
d) From char to int
Answer: b
Clarification: None.

6. Which of the following typecasting is accepted by C?
a) Widening conversions
b) Narrowing conversions
c) Widening & Narrowing conversions
d) None of the mentioned
Answer: c
Clarification: None.

7. When do you need to use type-conversions?
a) The value to be stored is beyond the max limit
b) The value to be stored is in a form not supported by that data type
c) To reduce the memory in use, relevant to the value
d) All of the mentioned
Answer: d
Clarification: None.

250+ TOP MCQs on Switch Statements and Answers

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

1. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include 
  2.     void main()
  3.     {
  4.         double ch;
  5.         printf("enter a value between 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
Answer: a
Clarification: None.

2. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include 
  2.     void main()
  3.     {
  4.         char *ch;
  5.         printf("enter a value between 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) Compile time error
c) 2
d) Run time error
Answer: b
Clarification: None.

3. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 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
Answer: c
Clarification: None.

4. What will be the output of the following C code? (Assuming that we have entered the value 2 in the standard input)

  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 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
Answer: d
Clarification: None.

5. What will be the output of the following C code? (Assuming that we have entered the value 1 in the standard input)

  1.     #include 
  2.     void main()
  3.     {
  4.         int ch;
  5.         printf("enter a value between 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
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int a = 1, b = 1;
  5.         switch (a)
  6.         {
  7.            case a*b:
  8.               printf("yes ");
  9.            case a-b:
  10.               printf("non");
  11.               break;
  12.         }
  13.     }

a) yes
b) no
c) Compile time error
d) yes no
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 97;
  5.         switch (x)
  6.         {
  7.            case 'a':
  8.               printf("yes ");
  9.               break;
  10.            case 97:
  11.               printf("non");
  12.               break;
  13.         }
  14.     }

a) yes
b) yes no
c) Duplicate case value error
d) Character case value error
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         float f = 1;
  5.         switch (f)
  6.         {
  7.            case 1.0:
  8.               printf("yesn");
  9.               break;
  10.            default:
  11.               printf("defaultn");
  12.         }
  13.     }

a) yes
b) yes default
c) Undefined behaviour
d) Compile time error
Answer: d
Clarification: None.

250+ TOP MCQs on Scope of a Variable and Answers

basic C questions on “Scope of a Variable”. One shall practice these basic 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 basic C questions on “Scope of a Variable” along with answers, explanations and/or solutions:

1. What will be the sequence of allocation and deletion of variables in the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int a;
  5.         {
  6.             int b;
  7.         }
  8.     }

a) a->b, a->b
b) a->b, b->a
c) b->a, a->b
d) b->a, b->a
Answer: b
Clarification: None.

2. Array sizes are optional during array declaration by using ______ keyword.
a) auto
b) static
c) extern
d) register
Answer: c
Clarification: None.

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

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

a) 4
b) 3
c) 0
d) Undefined
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int x = 5;
  3.     void main()
  4.     {
  5.         int x = 3;
  6.         m();
  7.         printf("%d", x);
  8.     }
  9.     void m()
  10.     {
  11.         x = 8;
  12.         n();
  13.     }
  14.     void n()
  15.     {
  16.         printf("%d", x);
  17.     }

a) 8 3
b) 3 8
c) 8 5
d) 5 3
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int x;
  3.     void main()
  4.     {
  5.         m();
  6.         printf("%d", x);
  7.     }
  8.     void m()
  9.     {
  10.         x = 4;
  11.     }

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

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

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

a) 9
b) 5
c) 4
d) 0
Answer: c
Clarification: None.

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

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

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