KEYC Mid-Term CSO101
KEYC Mid-Term CSO101
KEYC Mid-Term CSO101
1. What will be the size/length of the array mentioned here: char str[]="BHU";
Answer: 4.
2. Mention the output for the C statement mentioned herewith. printf ("%d ", 3? 4:5);
Answer: 4.
5. What will be the output if the C statement gets executed? printf("%d %d",0101,0x21);
Answer: 65, 33
6. Convert the following for loop in C language into the corresponding while loop.
for(int i=0; i>=0; i++) printf("IIT-BHU");
Answer: int i=0; while(i ≥ 0){i + +; }||while(1);
Answer: 4
10. unsigned short int j; The j has 1 byte storage space. What would be the range of values for j?
Answer: [0,255].
1
2. Mention the output of the following piece of C code? int k=10; printf("%d ", k+++k); [2]
Answer: 21
7. Consider the following C code with a user defined function func. Find Output/Error.
int func(int) int main() {int t=5; t=func(t); return 0; } int func (int a) {return a++;}
[2]
Answer: Compilation Error | int func(int) -> int func(int);
8. Mention the return value/character of the following user defined function func() [2]
char func() { static char C=’A’; C+=3; if(C>=’L’) return C; else func(); }
Answer: M
9. Find output/error.
void main() { int k=5; const int t=5; printf("%d %d ",(k)++, t++); } [2]
Answer: Compilation Error; increment of read-only variable ‘t’
10. Consider the function given below (Figure 1). The function Binary() is called in main() as follows.
main () {int t=Binary(19,2); printf("%d ",t); } Mention the output. [2]
Answer: 9
2
Section C: 15 Marks
1. Represent 6.75 using IEEE 754 single precision format. Show all the 32 binary bits. [3]
Answer: A = 0 10000001 1011000000000000000000
One has to show the steps!
(6.75)10 = (110.11)2 × 20 = 1.1011 × 22 ; Exponent= 2+127=129=(10000001)2
2. An array of integers is declared as int A[3][4];. What will be the output of the following C statement
if base address of the array A is assumed to be 1000. printf("%ld",(long int)&A[2][2]); [2]
Answer: 1040 (1020, If sizeof(int) provides 2 bytes.)
3. Write a recursive function to convert a decimal integer into corresponding binary number. [5]
Answer: Figure 3
If you find solution other than mentioned above, please show the solution to Faculty
member present there.
4. Write a C program that splits a given string into two parts (Equal parts if the given string length is
even, length of first part is one more than 2nd part, otherwise) and and print them. (String functions
defined in stringh.h such as strlen(), strrev() cannot be used. ) [3]
include <stdio.h>
int main()
{ char a[50]; char b[50]; char c[50];
scanf("%s",a); int i=0;
while(a[i]!=’NULL’) { i++; /* Computing the length of the string / }
Check rest of the part also!
/* One Solution may be as follows
if(i%2==0) for( j=0;j<i/2;j++) b[j]=a[j];
b[j] = String delimiter;
for(k=0;j<i;k++) c[k]=a[j++]; c[k]=0; else
3
for( j=0;j<(i/2+1);j++) b[j]=a[j];
b[j]=0;
for( k=0;j<i;k++) c[k]=a[j++];
c[k]=0;
printf("%s %s",b,c); */
return 0;
}
Section D: 15 Marks
1. Write a recursive function to compute xk , where x is a number and k is a positive integer. [3]
Answer:
float powerr(float, int);
int main() { int k; float x; /* int x is also OK. */ scanf("%f %d",&x,&k);
float t=power(x,k); printf("%f ",t); return 0; }
float power(float x, int k) { if(k==0) return 1; else return (x*power(x,k-1)); }
2. Write a function to find whether a given matrix is lower tringular or not. (A square matrix is called
lower triangular if all the entries above the main diagonal are zeros.) The given matrix will be passed
as an argument to the function in main() function.
[6]
Answer:
int lower(int [][10], int);
int main() { int a[10][10],i,j,n;
printf("Give the size of the Matrix"); scanf("%d",&n);
printf("Give Matrix");
for(i=0;i<n;i++)
for(j=0;j<n;j++) scanf("%d",&a[i][j]);
int t=lower(a,n);
if(t==0) printf(" Given Matrix is not Lower Tringular");
else printf(" Given Matrix is Lower Tringular"); return 0; }
4
if(flag==0 & i==t) return 1; else return 0; }
One will get full credit if s/he writes only the function!
3. Write a C program to count of inputs that are even and odd among three positive integers without
using if-then-else. (Do not use if-then-else) [3]
Answer:
#include <stdio.h>
int main() { int a; int t=0;
for(int i=0;i<3;i++)
{scanf("%d",a);
(a%2)?t++:b; }
printf("Even=%d Odd=%d",3-t,t); return 0; }
Important: One cannot use if, if-then, if-then-else
4. Draw the block diagram of computer system with brief description. [3]