KEYC Mid-Term CSO101

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

INDIAN INSTITUTE OF TECHNOLOGY (BHU) VARANASI

■ Computer Programming ■ CSO101


■ Mid Term Examination 2022■ 60 Marks ■ 2 Pages
■ UGD/IDD ODD Semester 2022-23 ■ Time: 2hrs ■ Answer ALL Questions
■ Answers of all questions from a Section must appear in contiguous spaces.
(Necessary header files are assumed to be included in the following C codes.)

Section A : 10 Marks (No Partial Mark)


Answer the following questions. (Correct Answer +1 mark, Wrong answer -0.5 mark)

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.

3. Represent +30 and -32 in 7-bit two’s complement binary representation.


Answer: +30= 0011110 ; -32= 1100000

4. What will be the output of the following C code?


int x=0; if(!x); x=5; printf("The value=%d", x);
Answer: x=5;

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);

7. What will be output if mentioned statement gets executed.

Answer: 4

8. What will the output of the following piece of C code:


main(){int c=22; printf(" %d ", c >= − − c) ;}
Answer: 1

9. What are the four storage classes supported by C language?


Answer: Auto, Register, Static, Extern.

10. unsigned short int j; The j has 1 byte storage space. What would be the range of values for j?
Answer: [0,255].

Section B: 20 Marks (No Partial Mark)


Mention the output/Compilation Error.

1. Mention the output of the C code:


int main() { int a=5, b=1; double c=6.0; (a>0)? ++b : ++c;
printf("%d", sizeof((a>0)? b : c));
return 0;
} [2]
Answer: 8/ Error. [Warning: Format specifier mismatching; %d should be %ld.]

1
2. Mention the output of the following piece of C code? int k=10; printf("%d ", k+++k); [2]
Answer: 21

3. Consider the following switch statement?


char c=’Y’;
switch(c)
{ case ’Y’: printf("YES"); case ’y’: printf("NO"); break; default: printf("BYE");
} [2]
Answer: YesNO.

4. Mention the output/error of the following code:


main() {int a =78, b=1; printf("%d", a >> + + b); } [2]
Answer: 19

5. Consider the following piece of C code. Mention the output/Compilation Error


int A[] = {0,1,2,3,4,5}, j = 1, i;
i = A[++A[j++]]++;
printf("i = %d, j = %d",i,j); [2]
Answer: 2, 2

6. Consider the following piece of C code. Mention the output/Compilation Error.


int main() { char a[5]; a="Hello"; printf("%s",a); return 0; } [2]
Answer: Error: a="Hello" ; assignment to expression with array type

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]

Figure 1: The C Code in Q. 10

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

Figure 2: Answer related to Q. 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;
}

5. Describe the usefulness of local and global variables in C language. [2]


Answer: Global variable can be accessed by all functions in a program. It is useful
whenever all functions needs to access the same data.
Scope of a local variable lies within a block where it is defined. Therefore, same variable
identifier can be used in other blocks.

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; }

int lower(int a[][10], int t)


{ int i,j; int flag=0;
for(i=0;i<t;i++)
for(j=i+1;j<t;j++)
if(a[i][j]!=0) flag=1; break;
if(flag==1) break;

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]

Figure 3: Answer related to Q. 4

You might also like