0% found this document useful (0 votes)
521 views5 pages

Assignment Solution 7 Jan 2020

The document contains solutions to 10 questions from a week 7 assignment. Question 1 asks about the end character of a C string, question 2 asks about the address of an element in a 2D array, and question 3 asks about the output of a code snippet comparing the sizes of two strings.

Uploaded by

VRAJ PATEL
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)
521 views5 pages

Assignment Solution 7 Jan 2020

The document contains solutions to 10 questions from a week 7 assignment. Question 1 asks about the end character of a C string, question 2 asks about the address of an element in a 2D array, and question 3 asks about the output of a code snippet comparing the sizes of two strings.

Uploaded by

VRAJ PATEL
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/ 5

WEEK 7 ASSIGNMENT SOLUTION

1. In C language the last character of a string is


a) \n
b) \0
c) \b
d) \t
Solution: (b) \0 is the indicator of the end of the string.

2. For an array A[100][100], let us assume that the main memory is byte-
addressable and that the array is stored starting from memory address 0.
So the address of a[40][50] is
a) 4040
b) 4050
c) 5040
d) 5050

Solution: (b) Address of a[40][50] = Base address + 40*100*element_size +


50*element_size

= 0 + 4000*1 + 50*1 [element size is 1 byte as byte-addressable]

= 4050

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


# include <stdio.h>
int main()
{
char str1[] = "Week-7-Assignment";
char str2[] = {'W', 'e', 'e', 'k', '-', '7', '-', 'A', 's','s','i','g','n','m','e','n','t'};
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=18, n2=17
b) n1=18, n2=18
c) n1=17, n1=17
d) n1=17, n2=18

Solution: (a) The size of str1 is 18 and size of str2 17.


WEEK 7 ASSIGNMENT SOLUTION

Note, in case of n1, the null character at the end of the string is also counted.
Therefore, it becomes 18. Whereas, for n2 its not, hence n2=17.
4. Which of the following is a disadvantage of linear search?
a) Requires more space
b) Greater time complexities compared to other searching algorithms
c) Not easy to understand
d) All of the mentioned

Solution: (b)

5. What will be the output?

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

a) hello
b) world
c) world hello
d) hello world

Solution: (d) hello world.


str1=hello, str2=world.
After strcat(str1, str2), str1=hello world. And strcpy makes str2=hello world.
WEEK 7 ASSIGNMENT SOLUTION

6. To get the output as “Hwi orea”, what should be the condition


inside the ‘if’ statement?
#include<stdio.h>
int main()
{
int i;
char s[] = "How is your exam";
for(i = 0; s[i]!= '\0'; ++i)
{
if(_______)
printf("%c", s[i]);
}
return 0;
}

a) (i%3) == 0
b) (i%2) == 0
c) (s[i]%3) == 0
d) (s[i]%2) == 0

Solution: (b)

Note, to make "How is your exam" to  “Hwi orea” we need to take all the
even position element of the string which can be done if we do the (i%2) == 0
condition checking inside the if statement.

7. What will be the value of ‘i’ after the execution of


the C code fragment given below?

static char str1[] = "dills";


static char str2[20];
static char str3[] = "daffo";
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "daffodills");

a) 0
b) 1
c) -1
d) None

Solution: (a) 0
WEEK 7 ASSIGNMENT SOLUTION

strcat(str3, strcpy(str2, str1)) makes it “daffodills”, hence strcmp(“daffodills”,


“daffodills”)=0.

8. 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[].

9. What will be the output?


#include<stdio.h>
void fu(int, int);
int main()
{
char x=67, y='C';
fu(x,y);
return 0;
}
void fu(int x, int y)
{
printf("%d,%d", x, y);
}
WEEK 7 ASSIGNMENT SOLUTION

Solution: 67,67 (short answer type)

As y is declared as int inside the function fu(), the ASCII value of ‘C’ is passed
to the function which is 67. Hence, 67,67 will be printed.

10.What will be returned by the function f(1)?


int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}

Solution: 7
A static variable inside a function keeps its value between invocations. Step
numbers represents the function calls.

Step 1: n=2 and i=2 which calls f(2)


Step 2: n=4 and i=3 which calls f(4)
Step 3: n=7 and i=4 which calls f(7)
After step 3, the if condition satisfies and the return statement is executed.
Hence, it returns the value of n at step 3, which is 7.

You might also like