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

TCS Doc-3-1

The document contains questions related to C programming concepts and coding questions. It includes 13 multiple choice questions related to string handling, operators, data types, functions etc. It also provides 5 coding questions related to calculating area of circle, checking prime number, converting binary to decimal, finding sum of prime numbers in a range, and checking palindrome. The coding questions expect the inputs from command line arguments and output the results without any extra information.

Uploaded by

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

TCS Doc-3-1

The document contains questions related to C programming concepts and coding questions. It includes 13 multiple choice questions related to string handling, operators, data types, functions etc. It also provides 5 coding questions related to calculating area of circle, checking prime number, converting binary to decimal, finding sum of prime numbers in a range, and checking palindrome. The coding questions expect the inputs from command line arguments and output the results without any extra information.

Uploaded by

mohan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1. which one of the following functions are not available in string.

a) strcpy()

b)strcat()

c)strptr()

d)strlen()

Explanation:

Strptr() is not available in string.h

2. what will be the output of the below code

#include<stdio.h>

int main()

float f=0.1;

if(f==0.1)

printf(“yes”);

else

printf(“no”);

return 0;

a)yes

b)no

c)all of the above

d)none of the above

Explanation: float and double are compared hence the output is no

3.The operator  is used to

a) assign a pointer to a variable


b)compare values

c)access structure pointer member

d) none of the above

Explanation: -> is used to access the structure pointer member

4. Output:

int main()

char *str1=”Hello”;

char *str2=”Hello”;

if(str1==str2)

printf(“Same”);

else

printf(“different”);

return 0;

a) runtime error

b) compile time error

c) same

d) different

Two pointers pointing to the same string, hence the both pointers point to the
same base address, hence the output is same.
5. Output:

int main()

int i=5;

if(i==5)

i==7;

else

i==10;

printf(“%d”,i);

return 0;

a)5

b)0

c)1

d)10

Explanation: i==5 evaluates true i will be compared to 7 ,since the assignment


is 5

Output is 5

6. Which library function is used to release the allocated memory in C

a) dealloc()

b) release()

c) freemem()

d)free()

7. while declaring parameters for main,the second parameter argv should be


declared as
a) char *argv

b) char* argv[]

c) char **argv

d) char argv;

8. which of the below has the highest precision

a) long

b) float

c) double

d) unsigned long int

9 In the below code the program expects the user to enter a word .If the user
enters the word as “ABRACADABRA”, what is the output value printed??

#include<stdio.h>

#define MAX 20

char *fun(char[]);

int main()

char str[MAX];

char *rev;

printf(“enter a word of size not more than 15 characters “);

scanf(“%s”,str);

r=fun(str);

printf(“%s”,r);

return 0;

char *fun(char str[])


{

static int i=0;

static char r[MAX];

if(*str)

fun(str+1);

r[i++]=*str;

Answer:

Compilation Error

Since the variable rev is not declared,

If the variable is declared the answer would be the reverse of the string .

11. In the below code what concept is used

#include<stdio.h>

long int fact(int n);

int main()

int n;

printf(“Enter a positive integer”);

scanf(“%d”,&n);

printf(“factorial of %d is %ld”,n,fact(n));

return 0;

long int fact(int n)

if(n>=1)
return n*fact(n-1);

else

return 1;

a) Segmentation

b) Iteration

c) Recursion

d) Encapsulation

12.C is preferred over other languages when

a) Ease of programming matters


b) Availability of GUI library matters
c) Performance matters
d) Rapid programming matters

13. Which one of the following statements is true about C Language

a) Realloc can be used to change the size of an array

b) It is not possible to write a working program in C without using semicolons

c) Unary Operators takes only one operand

d) If a function explicitly does not return a value ,then default value of 0 will be
returned

Coding Questions

1. Write a C program that will find the area of a Circle,Given the diameter .

The value D will be given a positive integer number that will be made available
to the program through its first command line parameter .Consider the value
of PI as 3.14. print the output to stdout in floating point format with exactly 2
decimal precision other than the floating point number , no other extra
information should be printed to stdout.
Given

INPUT: D=10

OUTPUT: 78.50

int main(int argc char *argv[])

float d;

float area;

if(argc!=2)

exit(1);

r=atoi(argv[1]);

area=3.14*d/2.0*d/2.0;

area=floor((area));

printf(“%7.2f”,area);

return 0;

Explanation:

1. If the argument count is not equal to 2 then exit from the program .

2. Input the diameter as the first command line argumrent

3. Declare the area as float and apply the formula

2. Write a C program which will check whether a given number n is prime


or not .If the number n is prime then find its square root and print that
value to stdout as a floating point number either exactly 2 decimal
precision. If the given number n is not prime then print the value 0.00 to
stdout. The given number n will be a positive non-zero integer and it will
be passed to the program using the first command line parameter, other
than the floating point result no other extra information should be
printed to stdout .

Solution:

int main(int argc ,char*argv[])

if(argc!=2)

exit(1);

int n=atoi(argv[1]);

int i,c=0;

for(i=2;i<=n/2;i++)

if(n%i==0)

c++;

if(c==1)

printf(“not prime”);

else

printf(“prime”);

return 0;

}
3. Write a C program which will convert a given binary number N to its
decimal equivalent, the given binary number n will be passed to the
program using the first command line parameter print the equivalent
decimal number to stdout in integer format other than the decimal
number; no other extra information should be printed to stdout.

INPUT: 111001

OUTPUT: 57

int main(int argc char *argv[])

int n,r,sum=0,p=1;

n=atoi(argv[1]);

while(n!=0)

r=n%2;

n=n/2;

sum=sum+p*r;

p=p*10;

printf(“%d”,sum);

return 0;

4. Write a C program that will find the sum of all prime numbers in a given
range .The Range will be specified as command line parameters .The first
command line parameter,N1 which is a positive integer, will contain the lower
bound of the range .The Second command line parameter N2 ,which is also a
positive integer will contain the upper bound of the range .The Program shoud
consider all the prime numbers within the range, excluding the upper bound
and lower bound . Print the output in integer format in stdout. other than the
integer number , no extra information should be printed to stdout
int main(int argc,char *argv[])

int i,j,sum=0,start,end;

if(argc!=3)

exit(1);

start=atoi(argv[1]);

end=atoi(argv[2]);

for(i=start;i<end;i++)

for(j=2;j<=i/2;j++)

if(i%j==0)

break;

if(j>i/2)

sum=sum*i;

printf(“%d”,sum);

return 0;

Explanation:

Take the input from command Line Arguments.

start and end are variables


finding the primes and the count store it in the sum variable .

5. Write a C program that will check whether the given number N is a


palindrome. An Integer is a palindrome if the reverse of that number is equal to
the original number. The Given number N will be a positive 5 digit number and
will be asked to the program using the first command line parameter .If the
given Number is a palindrome ,then print the word ”YES” to stdout .If the given
number is not a palindrome then print “NO” to stdout.

Note that the words YES or NO have to be in Upper Case.

Other than the word YES or NO, no extra information should be printed to
stdout.

int main(int argc ,char*argv[])

if(argc!=2)

exit(1);

int i=0,c=0,j;

for(i=;argv[1][i]!=‘\0’;i++);

c=i;

for(i=0,j=c-1;i<j;i++,j--)

if(argv[1][i]!=argv[1][j];

printf(“NO”);
exit(1);

else

printf(“YES”);

return 0;

6. Write a C program that will find the length of hypotenuse of a right angle
triangle given the length of other sides S1 and S2. The values of the other 2
sides S1 and S2 will be passed by the program through the first and second
command line parameters. The command line parameters will be of positive
integer type. Print the output to stdout in floating point format with exactly
2decimal precision. Other than the floating point number no extra information
should be printed to stdout.

INPUT: 10, 20

OUTPUT: 22.36

int main(int argc ,char *argv[])

float ans;

int num=atoi(argv[1]);

int num1=atoi(argv[2]);

num=num*num;

num1=num1*num1;

ans=float(sqrt(num+num1));

printf(“%f”,ans);

return 0;
}

float sqrt(float n)

float x=n;

float y =1;

float e=0.000001;

while(x-y>e)

x=(x+y)/2;

y=n/x;

return x;

Method 2: Palindrome for a 5 digit Number

int main(int argc,char*argv[])

int num=atoi(argv[1]);

int rem,rev=0,num1,i,count=0;

num=num1;

while(num>0)

rem=num%10;

rev=rem+rev*10;
num=num/10;

count++;

if(count==5)

if(num1==rev)

printf(“YES”);

else

Printf(“NO”);

return 0;

You might also like