0% found this document useful (0 votes)
37 views

Assignment 7 July 2022 Solution

This document contains solutions to 10 questions about C programming concepts like arrays, strings, functions, and pointers. The key points covered are: 1) An integer array does not terminate with a null character. 2) strcmp() returns 0 if two strings are identical. 3) gets() is more appropriate than scanf() for reading a multi-word string. 4) 3[a] accesses the 4th element of the array in the given code.

Uploaded by

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

Assignment 7 July 2022 Solution

This document contains solutions to 10 questions about C programming concepts like arrays, strings, functions, and pointers. The key points covered are: 1) An integer array does not terminate with a null character. 2) strcmp() returns 0 if two strings are identical. 3) gets() is more appropriate than scanf() for reading a multi-word string. 4) 3[a] accesses the 4th element of the array in the given code.

Uploaded by

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

Assignment 7 July 2022 Solution

1. Which of the following statement/s are false?


I. Array elements are stored in memory in contiguous locations.
II. An integer array always terminates with ‘\0’ (NULL).

a) I
b) II
c) Both I and II
d) None
Solution: (b)

2. If two strings are identical, then strcmp() function returns


a) 1
b) 0
c) -1
d) None of these
Solution: (b)

3. Which of the following function is more appropriate for reading in a multi word
string?
a) scanf()
b) gets()
c) printf()
d) puts()

Solution: (b)

4. What will be printed after execution of the following code?


#include<stdio.h>
int main()
{
int a[20] = {10, 20, 30, 40,50,60};
printf("%d", 3[a]);
return 0;
}

Solution: 40
3[a] denotes the 4th element of the array.

5. What will be the output of the program?


#include<stdio.h>
int main()
{
char str[] = "Array\0String";
printf("%s", str);
return 0;
}
Assignment 7 July 2022 Solution

a) Array
b) Array String
c) Array\0String
d) Compilation error

Solution:(a)

6. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20] = "Programming", str2[20] = "Language";
printf("%s", strcpy(str2, strcat(str1, str2)));
return 0;
}

a) Programming
b) Language
c) ProgrammingLanguage
d) LanguageProgramming

Solution: (c) ProgrammingLanguage


str1=programming, str2=Language.
After strcat(str1,str2), str1= ProgrammingLanguage . And strcpy makes
str2=ProgrammingLanguage .

7. What will be the output?


#include <stdio.h>
int main()
{
char str1[] = "I-LOVE-C";
char str2[] = {'I', '-', 'L', 'O', 'V', 'E', '-', 'C'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1=%d, n2=%d", n1, n2);
return 0;
}
a) n1= 8, n2=8
b) n1=9, n2=9
c) n1=8, n2=9
d) n1=9, n2=8

Solution: (d)
Assignment 7 July 2022 Solution

8. What will be the output?


# include <stdio.h>
int main()
{
int i, m=1, num[6] = {1,2,3,4,5,6};
for(i=0; i<=5; i++)
m=m*num[i];
printf(" %d", m);
return 0;
}

Solution: 720 (short answer type)


This is a factorial program. 6!=720.

9. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "assignment";
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;
}

a) assignment
b) tnemngissa
c) nothing will be printed
d) tttttttttt
Solution: (c) nothing will be printed as the string termination character ‘\0’ is assigned to first
element of array p[].

10. What will be the output?


#include<stdio.h>
#include<string.h>
int main()
{
char p[] = "welcome", q[]="welcome";
Assignment 7 July 2022 Solution

if(p==q)
{
printf("Two strings are equal");
}
return 0;
}

a) Two strings are equal


b) Two strings are not equal
c) Would not print anything
d) Compilation error

Solution: (c) No output, as we are comparing both base addresses and they are not same.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy