Sail E0 Webinar

MCQs

Total Questions : 37 | Page 1 of 4 pages
Question 1.

Which of the following statement is correct?


  1.    strcmp(s1, s2) returns a number less than 0 if s1>s2
  2.    strcmp(s1, s2) returns a number greater than 0 if s1
  3.    strcmp(s1, s2) returns 0 if s1==s2
  4.    strcmp(s1, s2) returns 1 if s1==s2
 Discuss Question
Answer: Option C. -> strcmp(s1, s2) returns 0 if s1==s2

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0

From the above statements, that the third statement is only correct.


Question 2.

Which of the following statements are correct ?
1:A string is a collection of characters terminated by ''.
2:The format specifier %s is used to print a string.
3:The length of the string can be obtained by strlen().
4:The pointer CANNOT work on string.


  1.    1, 2
  2.    1, 2, 3
  3.    2, 4
  4.    3, 4
 Discuss Question
Answer: Option B. -> 1, 2, 3

Clearly, we know first three statements are correct, but fourth statement is wrong. because 

we can use pointer on strings. 


Question 3.

Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";

1: There is no difference in the declarations and both serve the same purpose.

2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing 

    to a non-const pointer.

3: The pointer p can be modified to point to another string, whereas the individual characters within 

   array a can be changed.

4: In both cases the '' will be added at the end of the string "Sanjay".


  1.    1, 2
  2.    2, 3, 4
  3.    3, 4
  4.    2, 3
 Discuss Question
Answer: Option B. -> 2, 3, 4

No answer description available for this question. 


Question 4.


Which of the following statements are correct about the program below?


#include<stdio.h>
int main()
{
char str[20], *s;
printf("Enter a string\n");
scanf("%s", str);
s=str;
while(*s != '\0')
{
if(*s >= 97 && *s
  1.    The code converts a string in to an integer
  2.    The code converts lower case character to upper case
  3.    The code converts upper case character to lower case
  4.    Error in code
 Discuss Question
Answer: Option B. -> The code converts lower case character to upper case

This program converts the given string to upper case string.


Question 5.


What will be the output of the following program in 16 bit platform assuming that
1022 is memory address of the string "Hello1" (in Turbo C under DOS) ?


#include<stdio.h>
int main()
{
printf("%u %s\n", &"Hello1", &"Hello2");
return 0;
}
  1.    1022 Hello2
  2.    Hello1 1022
  3.    Hello1 Hello2
  4.    1022 1022
  5.    Error
 Discuss Question
Answer: Option A. -> 1022 Hello2

In printf("%u %s`setminus`n", &"Hello", &"Hello");.

The %u format specifier tells the compiler to print the memory address of the "Hello1".

The %s format specifier tells the compiler to print the string "Hello2".

Hence the output of the program is "1022 Hello2".


Question 6.


What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
printf("%c\n", "abcdefgh"[4]);
return 0;
}
  1.    Error
  2.    d
  3.    e
  4.    abcdefgh
 Discuss Question
Answer: Option C. -> e

printf("%c`setminus`n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".

Hence the output is 'e'.


Question 7.


What will be the output of the program ?


#include<stdio.h>
int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
if(str1 == str2)
printf("Equal\n");
else
printf("Unequal\n");
return 0;
}
  1.    Equal
  2.    Unequal
  3.    Error
  4.    None of above
 Discuss Question
Answer: Option B. -> Unequal

Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters

 and initialized with a string "Hello".

Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters 

and initialized with a string "Hello".

We have use strcmp(s1,s2) function to compare strings.

Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address 

of both variable is not same. Hence the if condition is failed.

Step 4: At the else part it prints "Unequal".


Question 8.


What will be the output of the program ?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[5], str2[5];
int i;
gets(str1);
gets(str2);
i = strcmp(str1, str2);
printf("%d\n", i);
return 0;
}
  1.    Unpredictable integer value
  2.    0
  3.    -1
  4.    Error
 Discuss Question
Answer: Option A. -> Unpredictable integer value

gets() gets collects a string of characters terminated by a new line from the standard

 input stream stdin.

The gets(str1) read the input string from user and store in variable str1.

The gets(str2) read the input string from user and store in variable str2.

The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other 

negative or positive values. So the value of i is "unpredictable integer value".

printf("%dn", i); It prints the value of variable i.


Question 9.


What will be the output of the program ?


#include<stdio.h>
int main()
{
int i;
char a[] = "\0";
if(printf("%s", a))
printf("The string is not empty\n");
else
printf("The string is empty\n");
return 0;
}
  1.    The string is not empty
  2.    The string is empty
  3.    No output
  4.    0
 Discuss Question
Answer: Option B. -> The string is empty

The function printf() returns the number of charecters printed on the console.

Step 1: char a[] = '�'; The variable a is declared as an array of characters and it initialized 

with "�". It denotes that the string is empty.

Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). 

Hence the if condition is failed.

In the else part it prints "The string is empty".


Question 10.


If the size of pointer is 4 bytes then What will be the output of the program ?


#include<stdio.h>
int main()
{
char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
printf("%d, %d", sizeof(str), strlen(str[0]));
return 0;
}
  1.    22, 4
  2.    25, 5
  3.    24, 5
  4.    20, 2
 Discuss Question
Answer: Option C. -> 24, 5

Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared

 as an pointer to the array of 6 strings.

Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));

sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'

strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';

Hence the output of the program is 24, 5

Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. 

Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), 

the output will be 24, 5 (because the size of pointer is 4 bytes).


Latest Videos

Latest Test Papers