[go: up one dir, main page]

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

Assignment7 July 2024

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Problem Solving through Programming in C

Week 07 Assignment Solution

1. In C, the placement of elements of a two dimensional array is


a) Row wise
b) Column wise
c) Diagonal wise
d) Bottom to top wise

Solution: (a) In C the placement of 2D array in memory is row wise.

2. Array passed as an argument to a function is interpreted as


a) Address of the array
b) Value of the first element of the array
c) Address of the first element of the array
d) Number of element of the array

Solution: (c) Address of the first element of the array or the base address of the array.

3. Applications of multidimensional array are?


a) Matrix-Multiplication
b) Minimum Spanning Tree
c) Finding connectivity between nodes
d) All of the mentioned

Solution: (d) For all of the above cases, multi-dimensional arrays are used.

4. What will be the output?


# 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.


When an array is initialized with string in double quotes, compiler adds a ‘\0’ at the end.
Problem Solving through Programming in C
Week 07 Assignment Solution

5. What is the output of the following C code?


#include <stdio.h>
int main()
{
int ary[2][3];
ary[ ][ ] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", ary[1][0]);
return 0;
}

a) Compile time error


b) 4
c) 1
d) 2
Solution: (a) The initialization method of the array is not valid in C. The second dimension must be
specified.

6. 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[].
Problem Solving through Programming in C
Week 07 Assignment Solution

7. What will be the output?


#include <stdio.h>
int main()
{
int a[2][3] = {1, 2, 3, 4};
int i = 0, j = 0;
for (i = 0; i< 2; i++)
for (j = 2; j >=0; j--)
printf("%d", a[i][j]);
return 0;
}

Solution: 321004
In a[2][3] = {1, 2, 3, 4}; only 4 values are given. The rest will be taken as 0. So, finally a[2][3] = {{1, 2,
3}, {4,0,0}}; So, 321004 will be printed as per the given for loop.

8. 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 str1=hello world.
Problem Solving through Programming in C
Week 07 Assignment Solution

9. What will be the output?


#include<stdio.h>
int main()
{
int i;
char a[] = "";
if(printf("%s", a))
printf("The string is empty");
else
printf("The string is not empty");
return 0;
}

a) The string is empty


b) The string is not empty
c) Error
d) None
Solution: (b) The string is not empty

10. What is the output of the following C program?


#include <stdio.h>
#include <ctype.h>
int main ()
{
int i = 0;
char c;
char str[] = "Programming Language";
while(str[i]!=' ')
{
putchar (toupper(str[i]));
i++;
}
return 0;
}

a) Programming
b) PROGRAMMING LANGUAGE
c) PROGRAMMING
d) Syntax error
Solution: (c) While loop is executed till the space between the words. toupper() will convert the lower case
letter to upper case. Thus, PROGRAMMING will be the output.
Problem Solving through Programming in C
Week 07 Assignment Solution

You might also like