TCS Programming Logic Questions - 2

TCS Programming Logic Questions


Ques51. There are two matrices A and B of size nXn. The data in both these matrices resides only at positions where both the indices are a perfect square. Rest all positions have 0 as the data. Manuj has available a third matrix initialized with 0's at all positions. He writes an efficient code to put the sum of A and B in C. What is the time complexity of Manuj's program?

Op 1: &theta(n^2)
Op 2: &theta(n)
Op 3: &theta(n1/2)
Op 4: &theta(log(n))
Op 5:

Correct Op : 2

Ques52. Ravi has to add an strictly upper triangular (no elements at diagonal) and a strictly lower triangular square matrix (no elements at diagonal) and put the result in a third matrix. What is the time complexity of Ravi's algorithm? Assume that storing a value in a memory space takes negligible time, while each addition between values takes the dominating amount of time.
Op 1: &theta(n^2)
Op 2: &theta(n)
Op 3: &theta(1)
Op 4: None of these Op 5:

Correct Op : 3

Ques53. We have two 100X3 (rowsXcolumn) matrices containing mid-term exam marks and end-term exam marks of 100 students. Each row refers to a particular student, while columns refer to marks in English, Social Sciences and Maths. The end-term
and mid-term marks of each student in each subject have to be added to get his total score in each subject, to be put in a third matrix (100X3). Parinidhi writes a code (Code A), where the outer loop iterates over the rows, while the inner loop iterates over the columns. Shashi writes a code (Code B), where the outer loop iterates over the columns, while the inner loop iterates over rows. Which of the following is true with regard to their code ignoring any caching or memory storage effects?
Op 1: Code A is faster than Code B 
Op 2: Code B is faster than Code A
Op 3: Code A and Code B will run in the same amount of time
Op 4: The comparison between the speed of the codes cannot be made. 
Op 5:

Correct Op : 2

Ques54. A code takes the following code steps (equivalently time unit) to execute:   5 n3 + 6 n2 + 1. Which of the following is not true about the time complexity of the program?
Op 1: It has a time complexity of O(n3
)
Op 2: It has a time complexity of O(n4
)
Op 3: It has a time complexity of O(n2
)
Op 4: It has a time complexity of &theta(n3
)
Op 5:

Correct Op : 3

Ques55. We have two programs. We know that the first has a time complexity O(n2
),
while the second has a complexity &omega(n2
). For sufficiently large n, which of the following cannot be true?

Op 1: Both codes have same complexity
Op 2: The first code has higher time complexity than the second
Op 3: The second code has lower time complexity than the first code. Op 4: Both codes are the same.
Op 5:

Correct Op : 2

Ques56. The time complexity of code A is &theta(n), while for Code B it is &theta(log(n)). Which of the following is true for sufficiently large n?

Op 1: Both code have the same time complexity 
Op 2: Code A has higher time complexity
Op 3: Code B has higher time complexity
Op 4: No comparison can be made between the time complexity of the two codes. Op 5:

Correct Op : 2

Ques57. Rajini is given an efficient code for summing two nXn matrices and putting the result in a third matrix. She is asked to find it's time complexity. She realizes that the number of iterations required is more than n. What can she claim with regard to the complexity of the code?

Op 1: It is O(n) 
Op 2: It is O(n2)
Op 3: It is &theta(n) 
Op 4: It is &omega(n) 
Op 5:

Correct Op : 4

Ques58. Gautam is given two codes, A and B, to solve a problem, which have complexity &theta(n) and &theta(n2) respectively. His client wants to solve a problem of size k, which Gautam does not know. Which code will Gautam deliver to the client, so that the execution is faster?

Op 1: Code A 
Op 2: Code B
Op 3: Gautam cannot determine
Op 4: Both codes have the same execution time, so deliver any. 
Op 5:

Correct Op : 3
 
Ques59. Surbhi is given two codes, A and B, to solve a problem, which have complexity O(n3) and &omega(n4) respectively. Her client wants to solve a problem of size k, which is sufficiently large. Which code will Surbhi deliver to the client, so that the execution is faster?
Op 1: Code A 
Op 2: Code B
Op 3: Surbhi cannot determine
Op 4: Both codes have the same execution time, so deliver any. 
Op 5:

Correct Op : 1

Ques60. Vibhu is given two codes, A and B, to solve a problem, which have complexity O(n4) and &omega(n3) respectively. Her client wants to solve a problem of size k, which is sufficiently large. Which code will Gautam deliver to the client, so that the execution is faster?

Op 1: Code A 
Op 2: Code B
Op 3: Vibhu cannot determine
Op 4: Both codes have the same execution time, so deliver any. Op 5:

Correct Op : 3

Ques61. Pavithra is given two codes, A and B, to solve a problem, which have complexity &theta(n3) and &omega(n3) respectively. Her client wants to solve a problem of size k, which is sufficiently large. Which code should she deliver to the client in the present scenario?

Op 1: Code A 
Op 2: Code B
Op 3: Both codes have the same execution time, so deliver any. 
Op 4: None of these
Op 5:

Correct Op : 1



Ques61. Code A has to execute 4 n2 + 64 program statements, while Code B has to execute 32 n program statements for a problem of size n. The time for executing a
single program statement is same for all statements. Rajesh was given a problem with a certain size k and he delivered Code A. What could be the possible value of k? 

Op 1: 1000
Op 2: 5
Op 3: 10
Op 4: 3
Op 5:

Correct Op : 4

Ques63. Saumya writes a code which has a function which calls itself. Which programming concept is Saumya using?
Op 1: This is bad programming practice and should not be done. 
Op 2: Recursion
Op 3: Decision Making 
Op 4: Overloading
Op 5:

Correct Op : 2

Ques64. 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
}
Fill in the missing statement. 

Op 1: return factorial(n-1) 
Op 2: return n factorial(n) 
Op 3: return n (n-1)
Op 4: return n factorial(n-1) 
Op 5:

Correct Op : 4
 
Ques65. 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
}
Fill in ?? in the code. 

Op 1: n equals 1
Op 2: n equals 2
Op 3: n >= 1
Op 4: n > 1
Op 5:

Correct Op : 1


Ques66. Saloni writes the code for a function that takes as input n, an even integer and calculates the sum of first n even natural numbers.
function sum( n )
{
if(n equals 2)
return 2 else
return (n + sum(n-2)) end
}
She then calls the function by the statement, sum(30). How many times will the function sum be called to compute this sum.

Op 1: 1
Op 2: 30
Op 3: 15
Op 4: 16
Op 5:

Correct Op : 3

Ques67. Consider the following function
 
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?

Op 1: 50
Op 2: 200
Op 3: 35
Op 4: 20
Op 5:

Correct Op : 1

Ques68. Ravi is writing a program in C++. C++ uses the 'for' keyword for loops. Due to distraction, Ravi writes 'gor' instead of 'for'. What will this result to?

Op 1: The code will not compile.
Op 2: The code will give an error while in execution
Op 3: The code may work for some inputs and not for others. 
Op 4: It will create no problems.
Op 5:

Correct Op : 1

Ques69. What does a compiler do?

Op 1: Converts code from a high level language to a low level language 
Op 2: Necessarily converts the code into assembly language
Op 3: Converts code from a low level language to a high level language 
Op 4: Necessarily converts the code into machine language
Op 5:

Correct Op : 1

Ques70. A program is compiled by Tarun on his machine. Whether it will run on a different computer will depend upon:

Op 1: Operating system on the computer
Op 2: Hardware configuration of the computer
Op 3: Both operating system and hardware configuration 
Op 4: The language of the program
Op 5:

Correct Op : 3
 
Ques71. Sakshi writes a code in a high-level programming language on a Pentium-III machine, which she wants to execute on a Motorola chip. What of the following will she run on the code?

Op 1: An interpreter 
Op 2: A compiler
Op 3: A cross-compiler 
Op 4: Linker
Op 5:

Correct Op : 3

Ques72. Shahaana has a 10,000 line code. She is trying to debug it. She knows there is a logical error in the first 25 lines of the code. Which of the following will be an
efficient way of debugging:

Op 1: Compile the whole code and step into it line by line 
Op 2: Use an interpreter on the first 25 lines.
Op 3: Compile the whole code and run it 
Op 4: None of these
Op 5:

Correct Op : 2

Ques73. Farhan writes a code to find the factorial of an inputted number. His code gives correct answer for some inputs and incorrect answers for others. What kind of error does his program have?

Op 1: Syntactical error 
Op 2: Run-time Error 
Op 3: Logical Error
Op 4: None of these 
Op 5:

Correct Op : 3

Ques74. Reshama is debugging a piece of code which takes several iterations of modifying and executing code, while Mohammad has to deliver a product to the customer, which the customer will run multiple times. Reshama wants her debug cycle to take minimum possible time, while Mohammad wants that his products run time is minimum. What tools should Reshama and Mohammad respectively use on their code?

Op 1: Compiler, Interpreter 
Op 2: Interpreter, Compiler 
Op 3: Compiler, Compiler
Op 4: Interpreter, Interpreter 
Op 5:
 
Correct Op : 2

Ques75. Gautam writes a program to run on a Motorola processor on his Pentium computer. He wants to see how the program will execute on the Motorola processor using his Pentium machine. What tool will he use?

Op 1: Compiler
Op 2: Interpreter
Op 3: Assembler
Op 4: Simulator
Op 5:

Correct Op : 4

Ques76. Consider the following code:
function modify(y,z)
{
y = y + 1; z = z + 1;
return y - z
}
function calculate( )
{
integer a = 5, b = 10, c c = modify(a, b); print a
print space print c
}
Assume that a and b were passed by value. What will be the output on executing function calculate( )?
Op 1: 11 -5
Op 2: 10 -5
Op 3: 6 -5
Op 4: 5 -5
Op 5:

Correct Op : 4

Ques77. Consider the following code: function modify(b,a)
{
return a - b
}
function calculate( )
{
integer a = 5, b = 12, c c = modify(a, b); print c
}
Assume that a and b were passed by reference. What will be the output of the program on executing function calculate( ) ?

Op 1: 7
Op 2: -7
Op 3: Error
Op 4: 8
Op 5:

Correct Op : 1

Ques78. Consider the following code:
function modify(y,z)
{
y = y + 1 z = z + 1
return y - z
}
function calculate( )
{
integer a = 12, b = 20, c c = modify(a, b);
print a print space print c
}
Assume that a and b were passed by reference. What will be the output of the function calculate( ) ?

Op 1: 12 -8
Op 2: 13 -8
Op 3: 12 8
Op 4: 13 8
Op 5:

Correct Op : 2


Ques79. Afzal writes a piece of code, where a set of three lines occur around 10 times in different parts of the program. What programming concept can he use to shorten his program code length?

Op 1: Use for loops
Op 2: Use functions 
Op 3: Use arrays 
Op 4: Use classes 
Op 5:

Correct Op : 2

Ques80. Geetika writes a piece of code, where a set of eight lines occur around 10 times in different parts of the program (Code A). She passes on the code to Deva. Deva puts the set of eight lines in a function definition and calls them at the 10 points in the program (Code B). Which code will run faster using an interpreter? 

Op 1: Code A
Op 2: Code B
Op 3: Code A and Code B will run with the same speed 
Op 4: None of these
Op 5:

Correct Op : 1

Ques81. Consider the following code:
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
}
Assume that a and b were passed by value. What will be the output of the function calculate( ) ?

Op 1: 80
Op 2: 40
Op 3: 32
Op 4: 72
Op 5:

Correct Op : 2

Ques82. Consider the following code:
 
function modify(w,u)
{
w = w + 2 u = u - 3
return (w - u)
}
function calculate( )
{
integer a = 10, b = 20, c c = modify(a, b);
print a print space print b
}
Assume that a was passed by value and b was passed by reference. What will be the output of the program on executing function calculate( ) ?

Op 1: 12 17
Op 2: 10 17
Op 3: 12 20
Op 4: 10 20
Op 5:

Correct Op : 2

Ques83. Consider the following function:
function run( )
{
integer a = 0 // Statement 1 while (a < 5)
{
integer c = 0 // Statement 2 c = c + 1 // Statement 3
a = a + 1
}
print c // Statement 4
}
At which statement in this program will the compiler detect an error? 

Op 1: Statement 1
Op 2: Statement 2
Op 3: Statement 3
Op 4: Statement 4
Op 5:

Correct Op : 4
 
Ques85. Which one of the following is the lowest level format to which the computer converts a higher language program before execution?

Op 1: English code 
Op 2: Machine Code
Op 3: Assembly Language 
Op 4: System Language 
Op 5:

Correct Op : 2

Ques86. If you want to write a function that swaps the values of two variables, you must pass them by:

Op 1: Value only
Op 2: Reference only 
Op 3: Either A or B 
Op 4: Neither A nor B 
Op 5:

Correct Op : 2

Ques87. Consider the following code:
if (condition 1) { if (condition 2)
{ // Statement A } else
if (condition 3)
{ // Statement B } else
{ // Statement C } else
if (condition 4)
{ // Statement D } else
{ // Statement E}
}
Which of the following conditions will allow execution of statement C? 

Op 1: condition1 AND condition3
Op 2: condition1 AND condition4 AND !condition2 
Op 3: NOT(condition2) AND NOT(condition3)
Op 4: condition1 AND NOT(condition2) AND NOT(condition3) 
Op 5:

Correct Op : 4
 
Ques88. Consider the following code:
if (condition 1) { if (condition 2)
{ // Statement A } else
if (condition 3)
{ // Statement B} else
{// Statement C } else
if (condition 4)
{// Statement D} else
{// Statement E}
}
Which of the following conditions will allow execution of statement E? 

Op 1: condition1 AND condition3
Op 2: NOT(condition1) AND condition2 AND NOT(condition4) 
Op 3: NOT(condition2) AND NOT(condition3)
Op 4: condition1 AND condition4 AND NOT(condition2) AND NOT(condition3) 
Op 5:

Correct Op : 2

Ques89. Consider the following code:
if (condition 1) { if (condition 2)
{ // Statement A } else
if (condition 3)
{ // Statement B} else
{// Statement C } else
if (condition 4)
{// Statement D} else
{// Statement E}
}
Which of the following condition will allow execution of statement A? 

Op 1: NOT(condition2) AND NOT(condition3)
Op 2: condition1 AND condition4 AND NOT(condition2) AND NOT(condition3) 
Op 3: condition1 AND condition2 AND condition4
Op 4: NOT(condition1) AND condition2 AND NOT(condition4) 
Op 5:

Correct Op : 3

Ques90. What does the following function do? function operation (int a, int b)
{
if (a < b)
{ return operation(b, a) } else
{ return a }
}

Op 1: Returns the max of (a,b) 
Op 2: Returns the min of (a,b) 
Op 3: Loops forever
Op 4: Always returns the second parameter Op 5:

Correct Op : 1

Ques91. What does the following function do? function operation (int a, int b)
{
if (a > b)
{ return operation(b, a) } else
{ return a; }
}

Op 1: Always returns the first parameter 
Op 2: Returns the min of (a,b)
Op 3: Returns the max of (a,b) 
Op 4: Loops forever
Op 5:

Correct Op : 2

Ques92. function g(int n)
{
if (n > 0) return 1;
else return -1;
}
function f(int a, int b)
{
if (a > b) return g(b-a);
 
if (a < b) return g(a-b); return 0;
}
If f(a,b) is called, what is returned? 

Op 1: Always -1
Op 2: 1 if a > b, -1 if a < b, 0 otherwise 
Op 3: -1 if a > b, 1 if a < b, 0 otherwise 
Op 4: 0 if a equals b, -1 otherwise
Op 5:

Correct Op : 4

Ques93. function g(int n)
{
if (n > 0) return 1;
else return -1;
}
function f(int a, int b)
{
if (a > b) return g(a-b); if (a < b) return g(b-a); return 0;
}
If f(a,b) is called, what is returned? 
Op 1: 1 if a > b, -1 if a < b, 0 otherwise 
Op 2: Always +1
Op 3: 0 if a equals b, +1 otherwise
Op 4: -1 if a > b, 1 if a < b, 0 otherwise 
Op 5:

Correct Op : 3

Ques94. function g(int n)
{
if (n > 0) return 1;
else return -1;
}
function f(int a, int b)
{
if (a > b) return g(a-b); if (a < b) return g(-b+a); return 0;
}
If f(a,b) is called, what is returned?
 
Op 1: Always +1
Op 2: 1 if a > b, -1 if a < b, 0 otherwise 
Op 3: -1 if a > b, 1 if a < b, 0 otherwise 
Op 4: 0 if a equals b, -1 otherwise
Op 5:

Correct Op : 2

Ques95. function g(int n)
{
if (n > 0) return 1;
else return -1;
}
function f(int a, int b)
{
if (a > b) return g(b-a); if (a < b) return g(-a+b); return 0;
}
If f(a,b) is called, what is returned? 

Op 1: Always +1
Op 2: -1 if a > b, 1 if a < b, 0 otherwise 
Op 3: 1 if a > b, -1 if a < b, 0 otherwise 
Op 4: 0 if a equals b, -1 otherwise
Op 5:

Correct Op : 2

Ques96. Consider the following code:
for i= m to n increment 2
{ print "Hello!" }
Assuming m < n and exactly one of (m,n) is even, how many times will Hello be printed?
Op 1: (n - m + 1)/2
Op 2: 1 + (n - m)/2
Op 3: 1 + (n - m)/2 if m is even, (n - m + 1)/2 if m is odd 
Op 4: (n - m + 1)/2 if m is even, 1 + (n - m)/2 if m is odd 
Op 5:

Correct Op : 1

Ques97. Consider the following code:
for i= m to n increment 2
{ print "Hello!" }
Assuming m < n and (m,n) are either both even or both odd, How many times will Hello be printed? 
Op 1: (n - m + 1)/2
Op 2: 1 + (n - m)/2
Op 3: 1 + (n - m)/2 if m is even, (n - m + 1)/2 if m is odd 
Op 4: (n - m + 1)/2 if m is even, 1 + (n - m)/2 if m is odd 
Op 5:

Correct Op : 2

Ques98. Assuming n > 2, What value does the following function compute for odd n? function f (int n)
{
if (n equals 1) { return 1 }
if (n equals 2) { return f(n-1) + n/2 } return f(n-2) + n;
}

Op 1: 1 + 2 + 3 + 4 + ... + n
Op 2: 1 + 3 + 5 + 7 + ... + n
Op 3: n/2 + (1 + 3 + 5 + 7 + ... + n)
Op 4: 1 + (1 + 3 + 5 + 7 + ... + n)
Op 5:

Correct Op : 2

Ques99. Assuming n > 2, What value does the following function compute for even n? int f (int n)
{
if (n equals 1) { return 1 }
if (n equals 2) { return f(n-1) + n/2 } return f(n-2) + n
}

Op 1: 1 + 2 + 3 + 4 + ... + n
Op 2: 1 + (2 + 4 + 6 + 8 + ... + n)
Op 3: 1 + n/2 + (4 + 6 + 8 + ... + n)
Op 4: 2 + 4 + 6 + 8 + ... + n
Op 5:

Correct Op : 4

Ques100. The for loop is equivalent to a while loop when 

Op 1: There is no initialization expression
Op 2: There is no increment expression 
Op 3: A and B combined are true
Op 4: It is never equivalent 
Op 5:
 
Correct Op : 3

                  Next Page

Comments