Programming Logic Concepts For TCS NQT
Programming Logic Concepts For TCS NQT
Answer : A
function modify(a,b)
Integer c,d=2
c= a*d+ b
return c
function calculate()
integer a = 5, b = 20, c
integer d= 10
c = modify(a, b);
c = c+ d
print c
A) 80
B) 40
C) 32
D) 72
Answer: B
Explaination : c=? d=2 c=a*d+b c=a*2+b then a=5 b=20 , c=? c=c+d c=a*2+b+d c=5*2+20+10=40
Q3. What is the term given to the variable whose scope is beyond all the scopes i.e., it can be accessed
by all the scopes?
A) Universal Variable
B) Global Variable
C) External Variable
D) Auto Variable
Answer : B
Q4.Anu wants to make a function that is not bound to any identifier.which of the following functions
should she incorporate in her program?
A) Anonymous Function
B) Friend Function
C) Null Function
D) Global Function
Answer: A
Q5. Which of the following accessibility modes can be the specifier of a top level class’?Top-level classes
can only have public, abstract, and final modifiers, and it is also possible to not define any class
modifiers at all. This is called default/package accessibility. Besides that, private, protected, and static
modifiers cannot be used when declaring top-level classes. Private, Protected, Public, No Modifier
A) Only Private
B) Protected and Private
D) Only No Modifier
Answer: C
Q6. Choose the correct answer. A pseudo-code which is similar to that of C++ and self-explanatory An
accessible member function or data member for an object are accessed by the statement
objectname.functionname or objectname. data member name respectively.
class brush
Private:
public:
function putdata(){…}
function main
brush b1, b2
b2.getdata() //Statement 4
A)Statement 1
B)Statement 2
C)Statement 3
D)Statement 4
Answer: D
print “Hello !”
print MyStr
return 1 // statement 2
MyDisplay(str) //statement4
A)Statement 1
B)Statement 2
C)Statement 3
D)Statement 4
Answer: B
Q8) Choose the correct answer Tanuj writes the code for a function that takes as input n and calculates
the sum of first n natural numbers.
Function sum( n )
{
if(??)
return 1
else
return (n + sum(n-1))
end
A) n equals 1
B) n equals 2
C) n >= 1
D) n > 1
Answer: A
Shrishti writes the code for a function that computes the factorial of the inputted number n.
function factorial(n)
if(n equals 1)
return 1
else
— MISSING STATEMENT —
end
A) return factorial(n-1)
B) return n*factorial(n)
C) return n*(n-1)
D) return n*factorial(n-1)
Answer: D
#include <stdio.h>
int main() {
int y = 10000;
int y = 34;
return 0;
B)Hello World! 34
Answer:A
Explaination: Compile time error will be raised, as the compiler will get confused which of the value is
to be printed
Q11) 1.Choose the correct answer Saumya writes a code which has a function which calls itself. Which
programming concept is Saumya using?
B) Recursion
C) Decision Making
D) Overloading
Answer:B
function calculate( n )
if(n equals 5)
return 5
else
return (n + calculate(n-5))
end
Shishir calls the function by the statement, calculate(20). What value will the function return?
A) 50
B) 200
C) 35
D) 20
Answer: A
=20+calc(15)
=20+15+calc(10)
=20+15+10+calc(5)
=20+15+10+5
=50
Tanuj writes the code for a function that takes as input n and calculates the sum of first n natural
numbers.
Function sum( n )
if(??)
return 1
else
return (n + sum(n-1))
end
A) n equals 1
B) n equals 2
C) n >= 1
D) n > 1
Answer :B
Sol: =3+sum(2)
=3+2+sum(1)
Afzal writes a piece of code, where a set of three lines occur around 10 times in different parts of the
program. Whatprogramming concept can he use to shorten his program code length?
B) Use functions
C) Use arrays
D) Use classes
Answer: B
Explanation : for example if afzal wants to take a input several times, then he must declare a input
function and call it anytime when he wants to take the input
Q15) Talika wants to implement heterogeneous linked list for her project. Which of the following will
help her do the same.
A) Void pointer
B) Null pointer
C) Wild pointer
D) Heterogeneous list follows the same procedure as the homogeneous list. Hence no different pointer
is required.
Answer: A
Explanation : The heterogeneous linked list contains different data types in its nodes and we need a link,
pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer.
Void pointer is capable of storing pointer to any type as it is a generic pointer type.
Q16) In C, if you pass an array as an argument to a function, what actually gets passed?
Ans: C
Sol : The statement \'C\' is correct. When we pass an array as a funtion argument, the base address of
the array will be passed.
int (*ptr)[10];
Answer: Option B
Sol : A. ptr is array of pointers to 10 integers. ptr is a pointer to an array of 10 integers. ptr is an array of
10 integers.
Q18) What will happen if in a C program you assign a value to an array element whose subscript exceeds
the size of array?
Ans: C
Sol : 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.
Q19) Which of the following 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)
Answer: Option C
This function is the same as the modulus operator. But fmod() performs floating point divisions.
#include<stdio.h>
int main()
i = ++a[1];
j = a[1]++;
m = a[i++];
return 0;
A)2, 1, 15
B)1, 2, 5
C)3, 2, 15
D)2, 3, 20
Ans: C
Sol : Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5
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
Q21) What will be the output of the below program if the below code is run with three command line
parameters mentioned below:
#include
int j = 3, i;
items = argv;
printf("%c", **p);
j--;
return 0;
A)PIP
B)Pen
C)Pap
D)Ink
Ans: A
Q22) What is the data type that occupies the least storage in “C” language?
A)int
B)char
C)float
D)double
Ans: 2
Sol: C is associated with different data types to each variable and occupies different amount of
memory. The char data type is used to store a single character and is the most basic data type in C. It
requires only one byte of memory for storage and can contain both positive and negative values.
A) Array is a dynamic data structure whose size can be changed while stacks are static data structures
whose sizes are fixed.
B) Array elements can be accessed and modified(elements can be added or removed) only at the ends of
the array while any elements of the stack can be accessed or modified randomly through their indices.
A) a
B) b
C) c
D) d
Ans. D
Sol: Elements of a linked list can be accessed only sequentially as on each node you will get the address
of the next node only, you cannot jump out any node
Q24) Improper formation of which of the following data-structures can cause un-intentional looping of a
program that uses it.
A) Linked List
B) Array
C) Queue
D) Stack
Ans: A
Sol : It has one common cause, for example, is that the programmer intends to iterate over sequence of
nodes in a data structure such as linked list or tree, executing the loop code once for each node
A) a
B) b
C) c
D) d
Ans: B
Sol : Linear search compares each of the element sequentially thats why is it does'nt need a sorted list,
its binary search that needs a sorted list
Q26) Eesha wrote a function fact( ) in “C” language to calculate factorial of a given number and saved
the file as fact.c. She forgot to code the main function to call this fact function. Will she be able to
compile this fact.c without the main() function?
A) Yes, she can compile provided the compiler option -no strict-checking is enabled.
B) No, she can not compile as the main function is required to compile any C program file.
D) Yes, she can compile and run as the system will supply default values to fact function.
Ans: B
C) Declaration associates type to the variable whereas definition associates scope to the variable.
D) Declaration associates type to the variable whereas definition gives the value to the variable.
A) a
B) b
C) c
D)d
Ans: D
Sol: Declaration of a variable is for informing to the compiler the following information: name of the
variable, type of value it holds and the initial value if any it takes. i.e., declaration gives details about the
properties of a variable. Whereas, Definition of a variable says where the variable gets stored.
Q28) The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively.
The post-order traversal of the binary tree is:
A) d e b f g c a
B) d e f g c a
C) e d b f g c a
D) e d b f f c a
Ans: A
Sol: The inorder and preorder traversals of a binary tree T is DBHEAFGC and ABDEHCFG respectively. ...
So, the traversal of the null node is… null, the traversal of a single node is itself, and then you get into
some fun, based on finding that [root] entry in the traversallist.
#include<stdio.h>
int main()
int x, y;
for(x=5;x>=1;x--)
for(y=1;y<=x;y++)
printf("%d\n",y);
A) 15
B) 11
C) 10
D) 13
Ans. A
Sol: The code will be executed 15 times, as the code has two loops which are inter-related
#include <stdio.h>
int main() {
return 0;
A) Hello World! X;
D) Hello World!
Ans: C
A)int_v1;
B)int_1v;
C)int_V1
D)None
ANS: D
A) It is not standardized
B) To avoid conflicts since assemblers and loaders use such names
Answer: Option C
A)Int number;
B)Float rate;
C)Int variable_count;
D)Int $main;
Answer: Option D
B)It is not an error to declare a variable to be one of the keywords(like goto, static)
Answer: Option C
SOL: According to the syntax for C variable name, it cannot start with a digit.
#include <stdio.h>
int main()
{
int main = 5;
printf(“%d”, main);
return 0;
Ans: C
A)Camelcase letters
B)Uppercase letter
C)Lowercase letters
D)None
Ans: C
Q37)The format identifier ‘%i’ is also used for _____ data type?
A)Char
B)Double
C)Float
D)Int
Ans: D
Ans: D
Sol: typedef and struct are used to define user-defined data types.
Ans: A
#include
<stdio.h>
int main()
chr = 128;
printf(“%d\n”, chr);
return 0;
A)128
B)-128
Ans: B
Q41)Neelam wants to share her code with a colleague, who may modify it. Thus she wants to include
the date of the program creation, the author and other she wants to include the date of the program
creation, the author and other information with the program. What component should she use?
A)Header files
B)Iteration
C)Comments
D)Pre proccessor
Ans: C
Q42)What is the output of the following code statements? The compiler saves the first integer at the
memory location 4165 and the rest at consecutive memory spaces in order of declaration. Integer is one
byte long.
integer a
pointer c, d
a = 30c = &a
c = &a
d=c
a = a + 10
print *c
A)30
B)40
C)4165
D)4166
Ans: B
Q43)A data type is stored as a 6 bit signed integer. Which of the following cannot be represented by this
data type?
A)-12
B)32
C)18
D)6
Ans : B
Q44)A language has 28 different letters in total. Each word in the language iscomposed of maximum 7
letters. You want to create a data-type to store a word ofthis language. You decide to store the word as
an array of letters. How many bits will you assign to the data-type to be able to store all kinds of words
of the language.
A)35
B)28
C)7
D)196
Ans: A
A)0 to 1000
B)0 to 1024
C)0 to 1023
D)0 to 1025
Ans: C
Q46)Parul takes as input two numbers: a and b. a and b can take integer valuesbetween 0 and 255. She
stores a, b and c as 1-byte data type. She writes the following code statement to process a and b and put
the result in c.
c=a+2*bTo her surprise her program gives the right output with some input values of a and b, while
gives an erroneous answer for others. For which of the following inputs will it give a wrong answer?
A)a = 200 b = 10
B)a = 10 b = 200
C)a = 100 b = 50
D)a = 50 b = 100
Ans: B
A)Linker
B)Loader
C)Executer
D)Compiler
Ans: D
Q48)Tricha wants to store a list of binary data.Which of following data types should she use?
A)Integer
B)Float
C)Character
D)Boolean
Ans: D
Q49)Which of the following options is an exception to being a part of composite data types?
A)Arrays
B)Structure
C)Union
D)Stacks
Ans: D
Q50)Which data type is most suitable for storing a number 65000 in a 32-bit system?
A)Short
B)Int
C)Long
D)Double
Ans: A
A)4 Bytes
B)8 Bytes
D)Cannot be determined
Ans:C
A)Int
B)Struct
C)Float
D)Double
Ans: B
#include <stdio.h>
int main()
{
float x = ‘a’;
printf(“%f”, x);
return 0;
A)97.000000
C)a.0000000
D)a
Ans: A
Ans: C
Q55)Variable name resolving (number of significant characters for uniqueness of variable) depends on
C) C language
Answer: A
Explanation: It depends on the standard to which compiler and linkers are adhering.
Q56)Consider on following declaration:
(ii)static i=10;
(iii)unsigned i=10;
(iv)const i=10;
Ans: D
A)Float PI = 3.14;
B)#define PI3.14;
D)Int PI : 3.14;
Ans: B
Q58)The format identifier ‘%i’ is also used for _____ data type?
A)Char
B)Double
C)Int
D)Float
Ans: C
Q59)Literal means ?
A)A string
C)A charatcter
D)An alphabet
Ans: B
#include <stdio.h>
int main() {
int main = 3;
printf(“%d”, main);
return 0;
Ans: C
Q61.function main()
integer a=5,b=7
switch(a)
case 5 :print “I am 5”
break
break
default:print “I am different”
A)I am 5
B)I am not 5
C)I am different
D)Error
Answer:D
Explanation:
Q62)Ashima wants to print a pattern which includes checking and changing a variables value iteratively
She decides to use a loop/condition Which of the following options should she use such that the body of
the loop/condition is executed atleast once whether the variable satisfies the entering condition or not?
A)For Loop
B)While Loop
D)Switch Case
Answer:C
Sol: Do-while loop is the only loop which executes once, even if the starting condition is wrong
Q63)The construct “if (condition) then A else B” is for which of the following purposes? 1) 2) 3) 4)
A)Decision-Making
B) Iteration
C) Recursion
Answer:A
Sol: In this condition it is checked if the first condition is right then we'll go with A otherwise we'll go
with B
Q64)Ravi and Rupali are asked to write a program to sum the rows of 2X2 matrices stored in the array A.
Ravi writes the following code (Code A): for n = 0 to 1 sumRow1[n] = A[n][1] + A[n][2] end Rupali writes
the following code (Code B): sumRow1[0] = A[0][1] + A[0][2] sumRow1[1] = A[1][1] + A[1][2] Comment
upon these codes (Assume no loop- unrolling done by compiler):
Answer:B
Sol: both codes are taking 2 steps of operation, therefore same complexity.
But if we consider the overhead of looping (as it takes time to increment counter) then code b will be
faster.
Q65.Integer a =40, b =35, c=20, d =10 Comment about the output of the following two statements •
Print a*b/c-d
Print a*b/(c-d)
A)Differ by 80
B)Same
C)Differ by 50
D)Differ by 160
Answer:A
Int a =456,b,c,d=10;
b=a/d; c=a-b;
print c;
A)411.4
B)411
C)410.4
D)410
Answer: B
Sol:
)b=456/10=45(Quotient)
c=456-45=411
Integer i=0.7
Else If(m>i)
Print(“I am greater”)
Else
Print(“I am lesser”)
B)I am greater
C)I am lesser
Answer:D
Answer: C
Explanation: In recursion, the solution of a problem depends on the solution of smaller instances of the
same problem.
A) Factorial of a number
C) Length of a string
D) All of the mentioned
Answer: D
Explanation: All of the above mentioned problems can be solved using recursion.
A) Switch Case
B) Loop
C) If-else
Answer: b
Q71) In recursion, the condition for which the function will stop calling itself is ____________
A) Best case
B) Worst case
C) Base case
Answer: C
Explanation: For recursion to end at some point, there always has to be a condition for which the
function will not call itself. This condition is known as base case.
void my_recursive_function()
my_recursive_function();
}
int main()
my_recursive_function();
return 0;
B) The code will be executed successfully and random output will be generated
D) The code will run for some time and stop when the stack overflows
Answer: d
Explanation: Every function call is stored in the stack memory. In this case, there is no terminating
condition(base case). So, my_recursive_function() will be called continuously till the stack overflows and
there is no more space to store the function calls. At this point of time, the program will stop abruptly.
void my_recursive_function(int n)
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
int main()
{
my_recursive_function(10);
return 0;
A)return
B) printf(“%d “, n)
C) if(n == 0)
D) my_recursive_function(n-1)
Answer: c
Explanation: For the base case, the recursive function is not called. So, “if(n == 0)” is the base case.
Q74) How many times is the recursive function called, when the following code is executed?
void my_recursive_function(int n)
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
int main()
my_recursive_function(10);
return 0;
A) 9
B) 10
C) 11
D) 12
Answer: C
Answer: B
Explanation: Recursion uses more memory compared to iteration because every time the recursive
function is called, the function call is stored in stack.
int cnt=0;
void my_recursive_function(int n)
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
int main()
{
my_recursive_function(123456789);
printf("%d",cnt);
return 0;
A)123456789
B)10
C)9
D)None
Ans: C
Sol: The program prints the number of digits in the number 123456789, which is 9.
void my_recursive_function(int n)
if(n == 0)
printf("False");
return;
if(n == 1)
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
printf("False");
return;
int main()
my_recursive_function(100);
return 0;
A)True
B)False
Ans: B
Sol: The function checks if a number is a power of 2. Since 100 is not a power of 2, it prints false.
int cnt = 0;
{
if(s[i] == '\0')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
int main()
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
A)6
B) 4
C) 9
D) 10
Ans: A
Sol: The function counts the number of vowels in a string. In this case the number is vowels is 6.
if(idx == len)
printf("-1");
return ;
if(arr[idx] == val)
printf("%d",idx);
return;
my_recursive_function(arr,val,idx+1,len);
int main()
int value = 2;
return 0;
A)3
B)4
C)5
D)6
Ans: B
Sol: The program searches for a value in the given array and prints the index at which the value is found.
In this case, the program searches for value = 2. Since, the index of 2 is 4(0 based indexing), the program
prints 4.
integer a=5,b=7
switch(a)
case 5 :print “I am 5”
break
break
default:print “I am different”
A) I am 5
B) I am not 5
C) I am different
D) Error
Ans: D