TCS Interview Experience | Set 15 (Coding Question – New 2017 Pattern)

1.6

In TCS 2017 campus placement, first round has introduced new pattern. This year onward, the test shall consist of 4 sections, instead of the earlier two. The duration, however, shall remain the same, i.e. 90 minutes. The details are:

1. Verbal Test (10 Mins) : This shall be similar to previous year’s section.

2. Quantitative Test (40 Mins) : This section will have only 20 questions now.

3. Test on Programming Language Proficiency (20 Mins) : This section shall have MCQ based questions related to basic programming concepts (on C language).

4. Coding Test (20 Mins) : This section shall require the student to solve a coding problem real time using an in built compiler (on C Language).

To solve this 4th section you can’t use keywords like “scanf, getc, getch, getchar” so to solve this type of coding problem. You have to use input from command line arguments.

Sample Program to Print all Command Line argument integers


// Program to print all value of
// command line argument
// once we get the value from command
// line we can use them to solve our problem.
#include <stdio.h> // this is used to print the result using printf
#include <stdlib.h> // this is used for function atoi() for converting string into int

// argc tells the number of arguments
provided+1 +1 for file.exe 
// char *argv[] is used to store the command line 
arguments in the pointer to char array i.e string format
int main(int argc, char *argv[])
{
    // means only one argument exist that is file.exe
    if (argc == 1) {
        printf("No command line argument exist Please provide them first \n");
        return 0;
    } else {
        int i;
        // actual arguments starts from index 1 to (argc-1)
        for (i = 1; i < argc; i++) {
            int value = atoi(argv[i]);
            // print value using stdio.h library's printf() function
            printf("%d\n", value);
        }
        return 0;
    }
}

Output:

24
50

If command line arguments will be 24 50.
Here is another sample question for this type of questions.
Problem Statement : 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.
Example:

If the argument is 4, the value of N is 4. 
So, 4 factorial is 1*2*3*4 = 24.
Output : 24
#include <stdio.h> // for printf
#include <stdlib.h> // for function atoi() for converting string into int
// Function to return fact value of n
int fact(int n)
{
    if (n == 0)
        return 1;
    else {
        int ans = 1;
        int i;
        for (i = 1; i <= n; i++) {
            ans = ans * i;
        }
        return ans;
    }
}
// argc tells the number of arguments
// provided+1 +1 for file.exe
// char *argv[] is used to store the
// command line arguments in the string format
int main(int argc, char* argv[])
{
    // means only one argument exist that is file.exe
    if (argc == 1) {
        printf("No command line argument exist Please provide them first \n");
        return 0;
    } else {
        int i, n, ans;
        // actual arguments starts from index 1 to (argc-1)
        for (i = 1; i < argc; i++) {
            // function of stdlib.h to convert string
            // into int using atoi() function
            n = atoi(argv[i]);

            // since we got the value of n as usual of
            // input now perform operations
            // on number which you have required

            // get ans from function
            ans = fact(n);

            // print answer using stdio.h library's printf() function
            printf("%d\n", ans);
        }
        return 0;
    }
}

Output:

24 if command line argument is 4.

This article is contributed by Alok Shakya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.6 Average Difficulty : 1.6/5.0
Based on 3 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.