TCS Coding Questions 2019 and Programming Questions With Answers
TCS Coding Questions 2019 and Programming Questions With Answers
#include <stdio.h>
void main()
int k = 5;
int *p = &k;
om
int **m = &p;
c
s.
printf(“%d%d%d\n”, k, *p, **p);
le
irc
}
sc
a) 5 5 5
nt
de
b) 5 5 junk
tu
c) 5 junk junk
.s
w
Q2. Which of the following statements about stdout and stderr are true?
w
1) main() function should always be the first function present in a C program file
3) A void pointer can hold address of any type and can be typcasted to any type
om
A) 2,3
c
s.
B) 1,2
le
irc
C) 1,2,3
sc
D) 1,2,3,4
nt
B) all the variable declared inside the function automatically will be assigned initial value of zero
w
w
C) It should be called only within the same source code / program file.
w
while (0 == 0) { }
C) It compares 0 with 0 and since they are equal it will exit the loop immediately
D) It has syntax error as the same number is being compared with itself
Section 4 Coding
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
om
text. You may assume that the input integer will be such that the output will not exceed the largest
c
s.
Q2: Write a C program to find the area of a triangle given the base and the corresponding height.
le
The values base and height are both positive integers passed to the program as the first and
irc
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.
sc
Scientific format(such as 1.00E+5) should NOT be used while printing the output. You may assume
nt
that the inputs will be such that the output will not exceed the largest possible real number that can
de
Answer will be –
.s
w
DDCCB
w
w
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)
{} ———————————————————————————————————-
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
#include<“stdio.h”>
return 0;
om
} ————————————————————————————————————-
c
s.
#include<“stdio.h”>
le
irc
int main(int argc, char *argv[])
sc
{
nt
return 0;
w
These are some questions you should know the answers to. The underlined options are the correct
ones.
3. None of these
1. Linked Lists
2. Array of structures
om
3. All of these
c
s.
4. Binary Tree
le
irc
Q3. Strings are character arrays. The last index of it contains the null-terminated character
sc
1. \t
nt
2. \1
de
tu
3. \0
.s
4. \n
w
w
1. String
2. Structure
3. Array
4. Files
Q5. What function should be used to free the memory allocated by calloc() ?
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
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?
om
1. conio.h
c
2. stdio.h
s.
3. math.h
le
irc
4. dos.h
sc
nt
1. Pointer to integer
tu
.s
2. None of these
w
3. Pointer to pointer
w
w
4. Invalid declaration
1. (underscore)
2. – (hyphen)
3. | (pipeline)
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
4. * (asterisk)
1. Uppercase letters
2. None of these
3. Lowercase letters
om
Q10. What should the program below print?
c
s.
#include <stdio.h>
le
irc
#include <string.h>
sc
#include <stdlib.h>
nt
++param;
.s
}
w
w
int main(){
w
strcpy(string, “hello_World”);
myfunc(&string);
myfunc(&string);
printf(“%s\n”, string);
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
return 0;
1. hello_World
2. ello_World
3. lo_World
om
4. llo_World
c
s.
1. What will happen if in a C program you assign a value to an array element whose
Answer: Option C
w
w
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.
int (*ptr)[10];
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
Answer: Option B
om
A.Value of elements in array
c
s.
B.First element of the array
le
irc
C.Base address of the array
sc
Answer: Option C
de
tu
Explanation:
.s
The statement ‘C’ is correct. When we pass an array as a function argument, the base address of
w
#include<stdio.h>
int main()
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
return 0;
om
}
c
s.
A.2, 1, 15
le
irc
B.1, 2, 5
sc
C.3, 2, 15
nt
D.2, 3, 20
de
tu
Answer: Option C
.s
Explanation:
w
w
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5
w
and it is initiapzed to
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so
i=3)
Step 6: printf(“%d, %d, %d”, i, j, m); It prints the value of the variables i, j, m
om
int fun(int arr[2]);
c
A.Yes
s.
B.No
le
irc
Answer: Option B
sc
nt
Explanation:
de
No, both the statements are same. It is the prototype for the function fun() that accepts one integer
tu
6. Are the expressions arr and &arr same for an array of 10 integers?
w
A.Yes
w
B.No
Answer: Option B
Explanation:
Both mean two different things. arr gives the address of the first int, whereas the &arr gives the
7. Which of the fplowing statements should be used to obtain a remainder after dividing
3.14 by 2.1?
om
Answer: Option C
c
Explanation:
s.
fmod(x,y) – Calculates x modulo y, the remainder of x/y.
le
irc
This function is the same as the modulus operator. But fmod() performs floating point divisions.
sc
nt
D.Internal
Answer: Option B
Explanation:
Internal pnkage-> means static variables and functions with file scope.
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
A.* (asterisk)
B.| (pipepne)
C.-(hyphen)
D._(underscore)
om
Answer: Option D
c
s.
Explanation:
le
irc
Variable names in C are made up of letters (upper and lower case) and digits. The underscore
character (“_”) is also permitted. Names must not begin with a digit.
sc
nt
=> foo
tu
.s
=> Bar
w
=> BAZ
w
w
=> foo_bar
=> _foo42
=> _
=> QuUx
2 : int fun();
D.None of these
om
Answer: Option B
c
s.
Explanation:
le
irc
extern int fun(); declaration in C is to indicate the existence of a global function and it is defined
int fun(); declaration in C is to indicate the existence of a function inside the current module or in
de
Here are the next set of questions for you! We will be posting 10 Technical questions everyday for
w
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
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
#include<stdio.h>
#include<math.h>
int main()
om
printf(“\n Result : %f” , ceil(1.66) );
c
s.
printf(“\n Result : %f” , floor(1.44) );
le
irc
printf(“\n Result : %f” , floor(1.66) );
sc
return 0;
nt
}
de
tu
// Output:
.s
// Result : 2.000000
w
w
// Result : 2.000000
w
// Result : 1.000000
// Result : 1.000000
#include<stdio.h>
int X=40;
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
int main()
int X=20;
printf(“%d\n”, X);
return 0;
om
A.20
c
s.
B.40
le
irc
C.Error
sc
D.No Output
nt
Answer: A
de
tu
Whenever there is conflict between a local variable and global variable, the local variable gets
.s
priority.
w
Question 3: A long double can be used if range of a double is not enough to accommodate a real
w
number.
w
A. True
B. False
Answer: A
Double = 8 bytes.
Long double = 10 bytes.
A.True
B. False
Answer: A
om
True,
float = 4 bytes.
c
s.
Double = 8 bytes.
le
Question 5: If the definition of the external variable occurs in the source file before its use in a
irc
particular function, then there is no need for an extern declaration in the function.
sc
A. True
nt
B. False
de
Answer: A
tu
True, when a function is declared inside the source file, that function (local function) get a priority
.s
than the extern function. So there is no need to declare a function as extern inside the same
w
source file
w
w
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
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
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)
om
A. True
c
s.
B. False
le
Answer: B irc
False, the range of double is -1.7e-308 to 1.7e+308.
sc
Question 9: Size of short integer and long integer would vary from one platform to another.
nt
de
A. True
B. False
tu
Answer: A
.s
w
True, Depending on the operating system/compiler/system architecture you are working on, the
w
A. True
B. False
Answer: Option B
scanf(%d,whatnumber);
Answer:
An ampersand ‘&’ symbol must be placed before the variable name whatnumber. Placing & means
whatever integer value is entered by the user is stored at the address of the variable name.
This is a common mistake for programmers, often leading to logical errors.
om
Question 2: What does the format %10.2 mean when included in a printf statement?
c
s.
Answer:
le
irc
This format is used for two things: to set the number of spaces allotted for the output number and
sc
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
nt
output number is less than 10, addition space characters will be inserted before the actual output
de
number. The number after the decimal point sets the number of decimal places, in this case, it’s 2
tu
decimal spaces.
.s
Answer:
w
A linked list is composed of nodes that are connected with another. In C programming, linked lists
are created using pointers. Using linked lists is one efficient way of utilizing memory for storage.
Answer:
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
Binary trees are actually an extension of the concept of linked lists. A binary tree has two pointers,
a left one and a right one. Each side can further branch to form additional nodes, which each node
Answer:
om
5. Pointer goes backstage in JAVA while C requires explicit handling of pointers.
c
s.
Answer: Functions are declared within header file. That is function prototypes exist in a header
file, not function bodies. They are defined in library (lib).
le
irc
sc
Answer:
de
There are four types of storage classes in C. They are extern, register, auto and static.
tu
.s
Answer:
w
Static is an access qualifier. If a variable is declared as static inside a function, the scope is limited
to the function, but it will exists for the life time of the program. Values will be persisted between
Answer:
Question 10: What are macros? What are its advantages and disadvantages?
Answer:
Similarly the advantage is they can reduce time for replacing the same values.
Answer:
om
Pass by value just passes the value from caller to calling function so the called function cannot
c
s.
modify the values in caller function. But Pass by reference will pass the address to the caller
le
function instead of value if called function requires to modify any value it can directly modify.
irc
Question 2: What is an object?
sc
Answer:
nt
de
Object is a software bundle of variables and related methods. Objects have state and behaviour.
tu
Answer:
w
w
Class is a user-defined data type in C++. It can be created to solve a particular kind of problem.
After creation the user need not know the specifics of the working of a class.
Answer:
Structure: Initially (in C) a structure was used to bundle different type of data types together to
perform a particular functionality. But C++ extended the structure to contain functions also.
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
The major difference is that all declarations inside a structure are by default public.
Class: Class is a successor of Structure. By default all the members inside the class are private.
Answer:
Pointer is a variable in a program is something with a name, the value of which can vary. The way
the compiler and linker handles this is that it assigns
a specific block of memory within the computer to hold the value of that variable.
om
Question 6: What is the difference between null and void pointer?
c
s.
Answer:
le
A Null pointer has the value 0. Void pointer is a generic pointer introduced by ANSI. Generic
irc
pointer can hold the address of any data type.
sc
Answer:
tu
Function overloading is a feature of C++ that allows us to create multiple functions with the same
.s
{
w
return nX + nY;
}
Answer:
A friend function for a class is used in object-oriented programming to allow access to public,
private, or protected data in the class from the outside.
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
Normally, a function that is not a member of a class cannot access such information; neither can an
external class. Occasionally, such access will be advantageous for the programmer. Under these
circumstances, the function or external class can be declared as a friend of the class using the
friend keyword
Answer: The idea behind inline functions is to insert the code of a called function at the point
where the function is called. If done carefully, this can improve the application’s performance in
exchange for increased compile time and possibly (but not always) an increase in the size of the
om
generated binary executables.
c
Question 10: Tell me something about abstract classes?
s.
Answer:
le
irc
An abstract class is a class which does not fully represent an object. Instead, it represents a broad
sc
range of different classes of objects. However, this representation extends only to the features that
nt
those classes of objects have in common. Thus, an abstract class provides only a partial
de
Answer:
w
For Array memory allocated is static and continuous. For List memory allocated is dynamic and
random.
Array: User need not have to keep in track of next memory allocation.
List: User has to keep in Track of next location where memory is allocated.
Array uses direct access of stored members; list uses sequential access for members.
Answer:
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
Arrays are a group of similar data types but Structures can be group of different data types.
Answer:
A data structure is a way of organizing data that considers not only the items stored, but also their
relationship to each other. Advance knowledge about the relationship between data items allows
designing of efficient algorithms for the manipulation of data.
Question 4: Can you list out the areas in which data structures are applied extensively?
om
Answer:
c
s.
Compiler Design,
Operating System,
le
irc
Database Management System,
sc
Numerical Analysis,
Graphics,
de
tu
Answer:
w
w
It permits code reusability. Reusability saves time in program development. It encourages the
w
reuse of proven and debugged high-quality software, thus reducing problem after a system
becomes functional.
Answer:
Macro gets to see the Compilation environment, so it can expand #defines. It is expanded by the
pre-processor.
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
Answer:
Getting the arguments from command prompt in c is known as command line arguments. In c main
function has three arguments. They are:
Argument counter
Argument vector
Environment vector
om
Question 8: What are the 4 basics of OOP?
c
s.
Answer:
Answer:
de
Create two pointers, each set to the start of the list. Update each as follows:
tu
while (pointer1) {
.s
pointer1 = pointer1->next;
w
if (pointer1 == pointer2) {
w
print (“circular\n”);
}
}
Question 10: Write a program to swap two numbers without using a temporary variable.
Answer:
9/30/2020 TCS Coding Questions 2019 and Programming Questions with Answers
i=i+j;
j=i-j;
i=i-j;
}
c om
s.
le
irc
sc
nt
de
tu
.s
w
w
w