TCS Coding Programming Questions Details
TCS Coding Programming Questions Details
Q2. Which of the following statements about stdout and stderr are true?
a) They both are the same
b) Run time errors are automatically displayed in stderr
c) Both are connected to the screen by default.
d) stdout is line buffered but stderr is unbuffered.
Section 4 Coding
Q1 : Write a C program to calculate the factorial of a non-negative integer N. The factorial of a
number N is defined as the product of all integers from 1 up to N. Factorial of 0 is defined to be 1. The
number N is a non-negative integer that will be passed to the program as the first command line
parameter. Write the output to stdout formatted as an integer WITHOUT any other additional text. You
may assume that the input integer will be such that the output will not exceed the largest possible
integer that can be stored in an int type variable.
Q2: Write a C program to find the area of a triangle given the base and the corresponding height. The
values base and height are both positive integers passed to the program as the first and second
command line parameters respectively. Write the output to stdout formatted as a floating point number
rounded to EXACTLY 2 decimal precision WITHOUT any other additional text. Scientific
format(such as 1.00E+5) should NOT be used while printing the output. You may assume that the
inputs will be such that the output will not exceed the largest possible real number that can be stored in
a float type variable.
Answer will be –
DDCCB
1) It would have been 5 5 5 if it were **m and not **p.
2) a) False. b) Not by default. c) Not by default. d) True.
3) Only d) is False as static variables are initialised with 0.
4) Access to static functions is restricted to the file where they are declared. Therefore, when we want
to restrict access to functions, we make them static. 5) while( 0==0) {} is equivalent to while(1) {} —
——————————————————————————————————-
#include<“stdio.h”>
int main(int argc, char *argv[])
{
int n,f=1,i; n = atoi(argv[1]);
for( i = 1; i <=n ; ++i ) { f = f*i; } printf(“%d”,f);
return 0;
} ————————————————————————————————————-
#include<“stdio.h”>
int main(int argc, char *argv[])
{
int base = atoi(argv[1]);
int height = atoi(argv[2]);
float area = 0.5*base*height; printf(“%.2f”,area);
return 0;
}
These are some questions you should know the answers to. The underlined options are the correct
ones.
Q3. Strings are character arrays. The last index of it contains the null-terminated character
1. \t
2. \1
3. \0
4. \n
Q4. Which of the following is a collection of different data types?
1. String
2. Structure
3. Array
4. Files
Q5. What function should be used to free the memory allocated by calloc() ?
1. free();
2. malloc(variable_name, 0)
3. dealloc();
4. memalloc(variable_name, 0)
Q6. In the standard library of C programming language, which of the following header file is
designed for basic mathematical operations?
1. conio.h
2. stdio.h
3. math.h
4. dos.h
1. What will happen if in a C program you assign a value to an array element whose subscript
exceeds the size of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D. The array size would appropriately grow.
Answer: Option C
Explanation:
If the index of the array size is exceeded, the program will crash. Hence “option c” is the correct
answer. But the modern compilers will take care of this kind of errors.
6. Are the expressions arr and &arr same for an array of 10 integers?
A.Yes
B.No
Answer: Option B
Explanation:
Both mean two different things. arr gives the address of the first int, whereas the &arr gives the address
of array of ints.
7. Which of the fplowing statements should be used to obtain a remainder after dividing 3.14 by
2.1?
A.rem = 3.14 % 2.1;
B.rem = modf(3.14, 2.1);
C.rem = fmod(3.14, 2.1);
D.Remainder cannot be obtain in floating point division.
Answer: Option C
Explanation:
fmod(x,y) – Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.
Here are the next set of questions for you! We will be posting 10 Technical questions everyday for
your preparation. Happy learning!
Question 1: How would you round off a value from 1.66 to 2.0?
A. ceil (1.66)
B. floor (1.66)
C. roundup (1.66)
D. Round to (1.66)
Answer: A
/* Example for ceil() and floor() functions: */
#include<stdio.h>
#include<math.h>
int main()
{
printf(“\n Result : %f” , ceil(1.44) );
printf(“\n Result : %f” , ceil(1.66) );
printf(“\n Result : %f” , floor(1.44) );
printf(“\n Result : %f” , floor(1.66) );
return 0;
}
// Output:
// Result : 2.000000
// Result : 2.000000
// Result : 1.000000
// Result : 1.000000
Question 3: A long double can be used if range of a double is not enough to accommodate a real
number.
A. True
B. False
Answer: A
True, we can use long double; if double range is not enough.
Double = 8 bytes.
Long double = 10 bytes.
Question 5: If the definition of the external variable occurs in the source file before its use in a
particular function, then there is no need for an extern declaration in the function.
A. True
B. False
Answer: A
True, when a function is declared inside the source file, that function (local function) get a priority than
the extern function. So there is no need to declare a function as extern inside the same source file
Question 6: If the definition of the external variable occurs in the source file before its use in a
particular function, then there is no need for an extern declaration in the function.
A. True
B. False
Answer: A
True, When a function is declared inside the source file, that function(local function) get a priority than
the extern function. So there is no need to declare a function as extern inside the same source file
Question 7: Size of short integer and long integer can be verified using the size of() operator.
A. True
B. False
Answer: A
True, we can find the size of short integer and long integer using the sizeof() operator.
Question 8: Range of double is -1.7e-38 to 1.7e+38 (in 16 bit platform – Turbo C under DOS)
A. True
B. False
Answer: B
False, the range of double is -1.7e-308 to 1.7e+308.
Question 9: Size of short integer and long integer would vary from one platform to another.
A. True
B. False
Answer: A
True, Depending on the operating system/compiler/system architecture you are working on, the range
of data types can vary.
Question 10: Range of float id -2.25e-308 to 2.25e+308
A. True
B. False
Answer: Option B
False, the range of float is -3.4e-38 to 3.4e+38.
Question 2: What does the format %10.2 mean when included in a printf statement?
Answer:
This format is used for two things: to set the number of spaces allotted for the output number and to set
the number of decimal places. The number before the decimal point is for the allotted space, in this
case it would allot 10 spaces for the output number. If the number of space occupied by the output
number is less than 10, addition space characters will be inserted before the actual output number. The
number after the decimal point sets the number of decimal places, in this case, it’s 2 decimal spaces.
Question 10: What are macros? What are its advantages and disadvantages?
Answer:
Macros are processor directive which will be replaced at compile time.
The disadvantage with macros is that they just replace the code they are not function calls. Similarly
the advantage is they can reduce time for replacing the same values.
Question 4: Can you list out the areas in which data structures are applied extensively?
Answer:
Compiler Design,
Operating System,
Database Management System,
Statistical analysis package,
Numerical Analysis,
Graphics,
Argument counter
Argument vector
Environment vector
Question 10: Write a program to swap two numbers without using a temporary variable.
Answer:
void swap(int &i, int &j)
{
i=i+j;
j=i-j;
i=i-j;
}