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

NITT Coding Que

The document is a past examination paper for a basic programming course. It contains multiple choice and short answer questions testing various C programming concepts including: - Writing the output of code snippets containing loops, conditional statements, functions and recursion. - Finding values returned by recursive functions. - Writing programs to print patterns, calculate sequences, reverse strings and find time differences.

Uploaded by

Shubham Kaushal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

NITT Coding Que

The document is a past examination paper for a basic programming course. It contains multiple choice and short answer questions testing various C programming concepts including: - Writing the output of code snippets containing loops, conditional statements, functions and recursion. - Finding values returned by recursive functions. - Writing programs to print patterns, calculate sequences, reverse strings and find time differences.

Uploaded by

Shubham Kaushal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Department of Computer Science and Engineering

National Institute of Technology, Tiruchirappalli-15

CSIR11 - BASICS OF PROGRAMMING

Semester Examination
Date: 18.03.2021 year/Semester : I/I
Max Marks: 30 Max Duration : 2Hr
Answer all the Questions
Note: You can assume that #include<stdio.h>, #include<conio.h>, and
#include<math.h> is included in the program.

1. Write the output of the following programs. You can assume that there is no
error in the program. Fifty percent marks for correct answer and 50 % marks for
justification.

(a) int main()


{ int x=2, y=3; z=2;
for(; y; printf("%d %d\n", x, y))
{ y = (++z+ x++ <= 10);
printf("\n");
}
return 0;
} (1.5)
(b) int main()
{ char c = 'Y';
switch(c)
{
case 'Y': printf("Yes/No");
case 'N': printf("No/ Yes ");
default: printf("other") break;
}
} (1)
(c) void myfunc(int X)
{
if(X > 0)
myfunc( --X );
printf("%d", X);
}
int main()
{
myfunc(5);
return 0;
} (1)
(e)
int main()
{
int i = 1;
do
{
i++;
if (i < 4)
continue;
i++;
} while (i%4);
printf("%d", i);
return 0;
} (1)

(f)
int funcf (int x);
int funcg (int y);
main()
{
int x = 2, y = 5, count;
y += funcf(x) + funcg(x);
printf ("%d ", y);
}

funcf(int x)
{ int y;
y = funcg(x);
return (y);
}

funcg(int x)
{ static int y = 10;
y += 1;
return (y+x); (2)
}

g. Consider the following C program:


#include <stdio.h>
int main()
{
int a[ ] = {2, 4, 6, 8, 10};
int i = 2, sum = 0, *b = a + 4;
sum = sum + (*b – i) – *(b – i);
printf (“%d\n”, sum);
return 0;
}
Output is
(1)
h)
Consider the following C program.
#include<stidio.h>
#include<string.h>
int main () {
char* c = "GATECSIT2017";
char* p = c;
printf ("%d", (int)strlen (c+2[p]-6[p]-1));
return 0;
}
The output of the program is (1)

(j) Suppose base address is 1000 (a=1000) for the following problems. Write the
output of the following programs.
int main () { int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
printf("%d%d%d\n", (*(a+2))[1], *(a+2)+1, *a[1] + 1 );
return 0;
(2)
(k)
int main()
{
char s1[ ] = "beautiful big sky country",
s2[ ] = "how now brown cow";
strcpy(s1+8,s2+8);
printf("%s\n", s1);
return 0;
} (1)

(l)
struct {
int s[5];
union {
float y;
char z;
}u;
} t;

Size of struct t is … …. bytes (1)

2(a). Following questions, if there is no error in the program then you have to write
objective of the program else you write the error in the program.
(i) void main() {
int a[]={6,7,8,9,10};
a = a + 4;
for (i =0, i<4, i++);
printf("%d",*(a-i);) } (1)
(ii) int main()
{ char *str[10], *t;
int i,j;
for(i=0;i<5;i++)
scanf("%s",str[i]);
for(i=0; i<5; i++)
for(j=i+1; j<5; j++)
if (strcmp(str[j-1], str[j]) > 0)
{
t=str[j];
str[j]=str[j-1];
str[j-1]=t;
}}}
printf("\n");
for(i=0;i<5;i++)
printf("%s\n",&str[i]); } (1.5)

(b) What is the objective of following function.


int XYZ(int x, int y)
{ if (x < y)
{ printf("The … is %d\n", x);
return 0;
}
return 1 + XYZ(x - y, y);
} (1)

(c) Consider the following recursive C function that takes two arguments
unsigned int foo(unsigned int n, unsigned int r) {
if (n > 0) return (n%r + foo (n/r, r ));
else return 0;
}
(i) What is the return value of the function foo when it is called as foo(513, 2)?
(ii) What is the return value of the function foo when it is called as foo(345, 10)?
(2.5)

3.(a) Kunwar sequence: sequence in which each number is the sum of the three
preceding numbers, starting from 0, 0 and 1.
Write the program in C which displays Kunwar sequence up to nth term where n is
entered by user. (2)

(b) Write the function Revstrhalf in c which takes string (with even length) and
returns the same string with first half is reversed and second half is same.
char* Revstrhalf (char*s)
For example: input: abcdxyzu
Output: dcbaxyzu (2.5)

(c) Write a program to print the following pattern.


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

… … … …

… … … … … (3)

(d) Write a C-program to calculate difference between two time period. For example
difference between 2:54:55 - 4:00:50 is 1:05:55 (h:m:s). While taking input from
user, you should not assume that first time period is greater than second or vice-
versa. You have to use structure and function (named difference to compute time
difference). Time difference should be printed in main function (Hint: call by
reference). (4)

end

You might also like