0% found this document useful (0 votes)
470 views7 pages

MCQ-4 String

This document contains 25 multiple choice questions related to strings in C and C++. The questions cover topics like string manipulation, string functions like strlen(), pointers to strings, comparing strings with strcmp(), and accessing characters in strings using indexes and pointers.

Uploaded by

Catalyst Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
470 views7 pages

MCQ-4 String

This document contains 25 multiple choice questions related to strings in C and C++. The questions cover topics like string manipulation, string functions like strlen(), pointers to strings, comparing strings with strcmp(), and accessing characters in strings using indexes and pointers.

Uploaded by

Catalyst Gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Page (1/7)

MCQ-4 Strings

1. What will be the output of the program ?


#include<stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
A. No output
B. 30
C. 3
D. 300

2. What will be the output of the program ?


#include<iostream.h>
int main()
{
cout<< 7["MatriXComputer"];
return 0;
}
A. Error: in printf
B. Nothing will print
C. print "o" of MatriXComputer
D. print "7"

3. What will be the output of the program ?


#include<iostream.h>
int main()
{
char str[] = "peace";
char *s = str;
cout<<s++ +3;
return 0;
}
A. peace
B. eace
C. ace
D. ce

4. What will be the output of the program ?


#include<stdio.h>
int main()
Page (2/7)

{
const char *p="hello";
cout<<*&*&p;
return 0;
}
A. llo
B. hello
C. ello
D. h

5. What will be the output of the program ?


#include<iostream.h>
#include<string.h>
int main()
{
int i, n;
char str[]="Alice";
char *x=str;
n = strlen(x);
*x = x[n];
for(i=0; i<=n; i++)
{
cout<<x;
x++;
}
return 0;
}
A. Alice
B. ecilA
C. Aliceliceicecee
D. liceicecee

6. What will be the output of the program ?


#include<iostream.h>
int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
cout<<str;
return 0;
}
A. Mello
B. Hello
C. HMello
D. MHello

7. In CPP, the size of the character array should be one larger than the number of
characters in the string.
a. True
b. False
Page (3/7)

8. Predict the output of following program


#include<iostream>
using namespace std;
int main()
{
const char* p = "12345";
const char **q = &p;
*q = "abcde";
const char *s = ++p;
p = "XYZWVU";
cout << *++s;
return 0;
}
a. Compiler Error
b. c
c. b
d. Garbage Value

9.What is the output of this program?


int main()
{
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 5;
cout << ptr;
return 0;
}
a) fg
b) cdef
c) defg
d) abcd

10. What is the output of following program?


#include<stdio.h>
int main()
{
char str1[] = "GeeksQuiz";
char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
cout<<"n1="<<n1<<"n2="<<n2;
return 0;
}
a. n1 = 10, n2 = 9
b. n1 = 10, n2 = 10
c. n1 = 9, n2 = 9
d. n1 = 9, n2 = 10

11. What is the output of following program?


#include<stdio.h>
void swap(char *str1, char *str2)
Page (4/7)

{
char *temp = str1;
str1 = str2;
str2 = temp;
}
int main()
{
char *str1 = "Matrix";
char *str2 = "Computer";
swap(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
return 0;
}
a. str1 is Computer, str2 is Matrix
b. str1 is Matrix, str2 is Computer
c. str1 is Matrix, str2 is Matrix
d. str1 is Computer, str2 is Computer

12. Predict the output?


#include <stdio.h>
int fun(char *str1)
{
char *str2 = str1;
while(*++str1);
return (str1-str2);
}
int main()
{
char *str = "GeeksQuiz";
printf("%d", fun(str));
return 0;
}
a. 10
b. 9
c. 8
d. Random Number

13. #include<stdio.h>
int main()
{
char str[] = "GeeksQuiz";
printf("%s %s %s\n",&str[5],&5[str], str+5);
printf("%c %c %c\n",*(str+6),str[6], 6[str]);
return 0;
}
a. Runtime Error
b. Compiler Error
c. uiz uiz uiz u u u
d. Quiz Quiz Quiz u u u

14. In below program, what would you put in place of “?” to print “Quiz”?
#include <stdio.h>
Page (5/7)

int main()
{
char arr[] = "GeeksQuiz";
printf("%s", ?);
return 0;
}
a. arr
b. (arr+5)
c. (arr+4)
d. Not possible

15. Consider the following C program segment:


char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length — i];
printf("%s", p);
The output of the program is?
a. gnirts
b. gnirt
c. string
d. no output is printed

16. Predict the output of the following program:


#include <stdio.h>
int main()
{
char str[] = "%d %c", arr[] = "GeeksQuiz";
printf(str, 0[arr], 2[arr + 3]);
return 0;
}
a. G Q
b. 71 81
c. 71 Q
d. Compile-time error

17. int main()


{
char p[] = "geeksquiz";
char t;
int i, j;
for(i=0,j=strlen(p); i<j; i++)
{
t = p[i];
p[i] = p[j-i];
p[j-i] = t;
}
printf("%s", p);
return 0;
}
Page (6/7)

Output?
a. ziuqskeeg
b. Nothing is printed on the screen
c. geeksquiz
d. gggggggg

18. Assume that a character takes 1 byte. Output of following program?


#include<stdio.h>
int main()
{
char str[20] = "GeeksQuiz";
printf ("%d", sizeof(str));
return 0;
}
a. 9
b. 10
c. 20
d. Garbage Value

19. Predict the output of following program, assume that a character takes 1 byte and
pointer takes 4
bytes.
#include <stdio.h>
int main()
{
char *str1 = "GeeksQuiz";
char str2[] = "GeeksQuiz";

printf("sizeof(str1) = %d,sizeof(str2)=%d",sizeof(str1), sizeof(str2));


return 0;
}
a. sizeof(str1) = 10, sizeof(str2) = 10
b. sizeof(str1) = 4, sizeof(str2) = 10
c. sizeof(str1) = 4, sizeof(str2) = 4
d. sizeof(str1) = 10, sizeof(str2) = 4

20. Consider the following C program segment.


# include <stdio.h>
int main( )
{
char s1[7] = "1234", *p;
p = s1 + 2;
*p = '0' ;
printf ("%s", s1);
}
What will be printed by the program?
a. 12
b. 120400
c. 1204
d. 1034

21. If the two strings are identical, then strcmp() function returns
Page (7/7)

A. -1
B. 1
C. 0
D. Yes

22. The library function used to find the last occurrence of a character in a string is
A. strnstr()
B. laststr()
C. strrchr()
D. strstr()

23. Which of the following function is used to find the first occurrence of a given string
in another string?
A. strchr()
B. strrchr()
C. strstr()
D. strnset()

24. A string is terminated by a


a. Null character
b. Boolean expression
c. Semicolon
d. All of them

25. #include <stdio.h>


int main(void)
{
char p;
char buf[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (buf + 1)[5];
printf("%d", p);
return 0;
}
a. 5
b. 6
c. 9
d. Error

You might also like