TCS Company Specific 2021 Programming Questions

1. What will be the output of below C program?

#include<stdio.h>
int main(){
	int a = 25, b;
	int *ptr, *ptr1;
	ptr = &a;
	ptr1 = &b;
	b = 36;
	printf("%d %d",*ptr, *ptr1);
	return 0;
}
  • 25  45632845
  • 25  36
  • 25 Garbage Value
  • Error

Ans: b

2. Write a code to print all the numbers which are multiple of 5 in the range of 500 to 550.

The range() function

The range() function is mainly used in for loops, to loop through a section of code a specific number of times. The first two parameters given to range are called the start and the stop range. The numbers that range() function generates begin with the number given as the first parameter and end with the number that’s one less than the second parameter. 

The range() function actually returns a special object called an iterator that repeats an action a number of times.We can convert the iterator into a list by using the function list().

For example, print(list(range(0, 50))) will print a list of numbers from 0 to 49.

We can also add a third parameter to range() function called step. If the step value is not included, the number 1 is used as the step by default. 

For example, count_by_twos = list(range(0, 30, 2)), where each number in the list increases by two from the previous number.

We can also use negative steps as below.
count_down_by_twos = list(range(40, 10, -2),where each number in the list decreases by two from the previous number.

arr = []
for i in range(500,550+1):
    if i % 5 == 0:
        arr.append(i)
print(arr)

3. Which of the following operations cannot be performed on an array arr in C/C++?

I)  ++arr

II) arr+1

III) arr++

IV) arr*2

  • I and II
  • I, II and III
  • II and III
  • I, III and IV

Ans: d

4. Which of the following keyword convert an integer to hexadecimal string in python?

  • ord(x)
  • chr(x)
  • hex(x)
  • All of the above

Ans: c

5. From where break statement causes an exit?

  • Only from innermost loop
  • Terminates a program
  • Only from innermost switch
  • From innermost loops or switches

Ans: d

6. No of the covid case from day 0 till day (n-1) is stored in an array. you are given that array. Print “Found” by returning true if the cases increased for three consecutive day else print “Not found” by return false from the given function.

For example,

Input array: [5, 4, 3, 7, 6, 1, 9]

Output: Found

Explanation: All of the following triplets satisfy the given condition in array A:

(5, 7, 9)
(4, 7, 9)
(3, 7, 9)
(5, 6, 9)
(4, 6, 9)
(3, 6, 9)

# Check for a sorted triplet in a given array
def findTriplet(A):
    n = len(A)
    if n < 3:
        return ()

    min = [-1] * n
 
    min_index_so_far = 0
 
    for i in range(1, n):
        if A[i] < A[min_index_so_far]:
            min_index_so_far = i
        elif A[i] > A[min_index_so_far]:
            min[i] = min_index_so_far
 
    max = [-1] * n
 
    max_index_so_far = n - 1
 
    for i in reversed(range(n - 1)):
        if A[i] > A[max_index_so_far]:
            max_index_so_far = i
        elif A[i] < A[max_index_so_far]:
            max[i] = max_index_so_far
 
    for i in range(n):
        if min[i] != -1 and max[i] != -1:
            return min[i], i, max[i]
#sauravhathi
    return ()

if __name__ == '__main__':
  n = int(input())
  A = []
  for i in range(n):
    A.append(int(input()))
  if findTriplet(A):
      print("Found")
  else:
      print("Not found")

7. What is the output of this C code?

#include <stdio.h>
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
  • nullp nullq
  • q
  • p
  • p q

Ans: a

8. There are n blocks (numbered 1 through n), For each valid i, the weight of the i-th block is Wi kilograms, stored in an array. You have to find the length of a subsequence which contains elements for a given weight array, in sorted order, lowest to highest. This subsequence is not necessarily contiguous or unique.

Write a program that completes the above-given task and print the result.

Example:

Input: arr = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]

Output: 6

Explanation: The LIS of the given array arr are, [0, 2, 6, 9, 11, 15], [0, 4, 6, 9, 11, 15], and [0, 4, 6, 9, 13, 15]. All of these subsequences have a length equal to 6.

import java.util.*;
 
class Main {
    // Function to find the length of the longest increasing subsequence of a given array
    //sauravhathi
    public static int LIS(int[] arr){
              int n = arr.length;
        int[] lis = new int[n];
        int max = 0;
        for (int i = 0; i < n; i++) {
            lis[i] = 1;
            for (int j = 0; j < i; j++) {
                if (arr[i] > arr[j] && lis[i] < lis[j] + 1) {
                    lis[i] = lis[j] + 1;
                }
            }
            if (lis[i] > max) {
                max = lis[i];
            }
        }
        return max;
    }
 
    public static void main(String[] args){
      	Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[n];
        //sauravhathi
        for(int i=0; i<n; i++){
            arr[i] = sc.nextInt();
        }
      
        System.out.print(LIS(arr));
    }
}

9. What is the value of x in this C code?

#include <stdio.h>
int main()
{
int x = 5 * 9 / 3 + 9;
printf("%d",x);
}
  • 3.75
  • Depends on compiler
  • 24
  • 3

Ans: 24 (c)

10. Which is true about an anonymous inner class?

  • It can extend exactly one class and implement exactly one interface.
  • It can extend exactly one class and can implement multiple interfaces.
  • It can extend exactly one class or implement exactly one interface.
  • It can implement multiple interfaces regardless of whether it also extends a class.

Ans: a

11. Which of these functions is called to display the output of an applet ?

  • display()
  • paint()
  • displayApplet()
  • PrintApplet()

Ans: paint()

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *