PracticeGuide P&T Part1

Embed Size (px)

Citation preview

  • 8/11/2019 PracticeGuide P&T Part1

    1/22

    CONFIDENTIAL

    Practice Guide for Programming & Testing

  • 8/11/2019 PracticeGuide P&T Part1

    2/22

    CONFIDENTIAL

    COPYRIGHT NOTICE

    All ideas and information contained in this document are the intellectual property ofEducation and Research Department, Infosys Limited. This document is not for

    general distribution and is meant for use only for the person they are specifically

    issued to. This document shall not be loaned to anyone, within or outside Infosys,including its customers. Copying or unauthorized distribution of this document, inany form or means including electronic, mechanical, photocopying or otherwise isillegal.

    Education and Research Department

    Infosys Limited

    Electronics City

    Hosur Road

    Bangalore - 561 229, India.

    Tel: 91 80 852 0261-270

    Fax: 91 80 852 0362

    www.infy.com

    mailto:E&[email protected]

  • 8/11/2019 PracticeGuide P&T Part1

    3/22

    Infosys Limited Table of Contents

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 iii

    Contents

    CONTENTS................................................................................................................. III

    CONTEXT ...................................................................................................................1

    GUIDELINES ................................................................................................................1

    PRACTICE QUESTION 1:CODING STANDARDS AND BEST PRACTICES.........................................................1

    PRACTICE QUESTION 2:CODING STANDARDS AND BEST PRACTICES.........................................................2

    PRACTICE QUESTION 3:VARIABLES,CONSTANTS AND DATA TYPES ..........................................................3

    PRACTICE QUESTION 4:VARIABLES,CONSTANTS AND DATA TYPES ..........................................................3

    PRACTICE QUESTION 5 :CONDITIONAL CONSTRUCTS.......................................................................4

    PRACTICE QUESTION 6 :USE OF FOR LOOP ................................................................................4

    PRACTICE QUESTION 7:USE OF WHILE LOOP ...............................................................................4

    PRACTICE QUESTION 8:USE OF DO WHILE LOOP ...........................................................................5

    PRACTICE QUESTION 9 :CONDITIONAL CONSTRUCTS.......................................................................6

    PRACTICE QUESTION 10:USE OF FOR LOOP................................................................................7

    PRACTICE QUESTION 11:USE OF WHILE LOOP..............................................................................8

    PRACTICE QUESTION 12:USE OF DO WHILE LOOP ..........................................................................8

    PRACTICE QUESTION 13:1D-ARRAYS.....................................................................................8

    PRACTICE QUESTION 14:1D-ARRAYS.....................................................................................9

    PRACTICE QUESTION 15:2D-ARRAYS................................................................................... 10

    PRACTICE QUESTION 16:STRINGS........................................................................................ 11

    PRACTICE QUESTION 17:STRINGS........................................................................................ 12

    PRACTICE QUESTION 18:VARIABLES,CONSTANTS AND DATA TYPES....................................................... 12

    PRACTICE QUESTION 19 :PREPROCESSOR DIRECTIVE .................................................................... 14

    PRACTICE QUESTION 20 :CONDITIONAL CONSTRUCTS ................................................................... 15

    PRACTICE QUESTION 21:USE OF FOR LOOP ............................................................................. 15

    PRACTICE QUESTION 22:USE OF WHILE LOOP............................................................................ 16

    PRACTICE QUESTION 23:USE OF DO WHILE LOOP ........................................................................ 18

    PRACTICE QUESTION 24:1D-ARRAYS................................................................................... 18

    PRACTICE QUESTION 25:2D-ARRAYS................................................................................... 18

    PRACTICE QUESTION 26:STRINGS........................................................................................ 19

  • 8/11/2019 PracticeGuide P&T Part1

    4/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 1 of 1

    ContextThis document contains Practice Questions to be completed as part of the practice session for

    the subject Programming & Testing.

    Guidelines The practice guide has been designed to give hands on experience to map theconcepts learnt in the theory session with practical Practice Questions and provide

    more hands on practice

    The Practice Questions are organized based on topics

    These Practice Questions contain coding exercises, debugging exercises, coding

    standards exercises and many code snippets for analyzing and predicting the output

    Solving these exercises methodically would provide more practice and hence

    confidence to the learner to attempt the module and comprehensive exams

    There are three categories of Practice Questions

    o Must to know The concepts in these Exercises are fundamental and must

    be practiced and known by the trainee

    o Need to know- The concepts in these Exercises need to be learnt by the

    trainees and practiced

    o Good to know- These concepts are some additional concepts which can

    be practiced by the trainees after completing the essential Assignments and

    exercises in the lab guide and threaded Exercises respectively.

    Practice Questions from 1 to 17 must be submitted at the end of the Programming

    & Testing module.

    Practice Question 1: Coding Standards and Best PracticesIdentify and correct the following code snippet for coding standards

    /*******************************************************************

    * Filename : CodingStandards.c

    * Author : Education & Research Dept, Infosys Limited

    * Date : 22-June-2011

    Description : Helps to understand the coding standards for a program*********************************************************************/

    /* Include files */

    #include

    /********************************************************************

    * Function : main()

  • 8/11/2019 PracticeGuide P&T Part1

    5/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 2 of 2

    * Description : main function of the program explains use of type

    casting

    Input Parameters:

    int argc - Number of command line arguments

    char **argv The command line arguments passed

    Returns: 0 on success to the operating system*********************************************************************/

    int main(int argc,char** argv){

    /*Variable declaration */

    int iValue = 10;

    float iNumber = 2.0f;

    double dEmpsalary = 10000;

    /* Displaying the values */

    printf("Integer = %d\n",iValue);printf("Float = %0.2f\n",iNumber);

    printf("Employee salary = %0.2lf\n",dEmpsalary);

    }

    /********************************************************************

    * End of CodingStandards.c

    *********************************************************************/

    Practice Question 2: Coding Standards and Best PracticesIdentify and correct the following code snippet for coding standards

    /*********************************************************************

    * Filename :

    * Author : Education & Research Dept, Infosys Limited

    * Date : 22-June-2011

    * Description : Helps to understand the coding standards for a program

    ********************************************************************/

    /* Include files */

    #include

    /*************************************************************

    Description: main()

    Input Parameters:

    argc Command line arguments count

    argv - Command line arguments

    Returns : 0 on successful execution to the operating system

  • 8/11/2019 PracticeGuide P&T Part1

    6/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 3 of 3

    and 1 if is unsuccessful.

    *************************************************************/

    int main(int argc,char** argv){

    /*declaring and initializing variables */

    int iNumber;double fValue;

    printf("Enter the values\n");

    scanf("%d %lf", &iNumber,&fValue); /* Reading from the user*/

    /*displaying values */

    printf("%d %d %0.2lf",iNumber,iNumber_2,fValue);

    }

    /*********************************************************************

    *End of CodingStandards2.c*********************************************************************/

    Practice Question 3: Variables, constants and data types

    Write a pseudo code to accept marks in 3 subjects, find the average and display the same.Convert this pseudo code into a program

    Practice Question 4: Variables, constants and data typesCompile and correct the code to have 0 warnings

    /************************************************************

    * Filename : Operators1.c

    * Author : Education & Research Dept, Infosys

    * Limited

    * Date : 22-June-2011

    * Description : To understand the use of typecasting

    *****************************************************************/

    /* Include files */

    #include

    /****************************************************************

    * Function : main()

    * Description : main fuction to understand the use of

    * typecasting

    * Input Parameters:

    * int argc - Number of command line arguments

    * char **argv The command line arguments passed

    * Returns: 0 on success to the operating system

  • 8/11/2019 PracticeGuide P&T Part1

    7/22

  • 8/11/2019 PracticeGuide P&T Part1

    8/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 5 of 5

    }

    printf("%d ",iNumber);

    return0;

    }

    Practice Question 8: Use of do while LoopFind the output of the following programs by dry run through trace table and then comparethe result by executing the program

    (1)#include

    intmain(intargc, char** argv)

    {

    intiIndex=1,iSum=0;

    do

    {

    iSum=iIndex*iIndex+iSum;

    printf("%d\n",iSum);

    }while(iIndex==1);

    /* Return a success code to the Operating System */

    return0;

    }

    (2)

    #include

    intmain(intargc, char** argv)

    {

    intiIndex=1,iSum=0;

    do

    {

    iSum=iIndex*iIndex+iSum;

    printf("%d\n",iSum);

    }while(iIndex=1);

    /* Return a success code to the Operating System */

    return0;

    }

    (3)

    #include

    intmain(intargc, char** argv)

    {

    intiIndex=1,iSum=0;

    do

    {

    iSum=iIndex*iIndex+iSum;

    printf("%d\n",iSum);

    }while(iIndex

  • 8/11/2019 PracticeGuide P&T Part1

    9/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 6 of 6

    Practice Question 9 : Conditional constructsa)

    Write a pseudo code for finding the bigger number among given two numbers and convertthe same to a program

    b) Understand the logic of the following program and fill the code wherever font is red incolor

    /******************************************************************

    * Filename : LogicalOperators.c

    * Author : Education & Research Dept, Infosys Limited

    * Date : 22-June-2011

    Description : Program to check whether the given year is leap year or

    not using

    logical operators

    *****************************************************************/

    /* Include files */

    #include

    /*********************************************************************

    *********

    * Function : main()

    * Description : main fuction to check whether the given year is leap

    year or not using

    logical operators

    Input Parameters:

    int argc - Number of command line arguments

    char **argv The command line arguments passed

    Returns: 0 on success to the operating system

    *****************************************************************/

    int main(int argc,char **argv) {

    /*Variable declaration*/

    int iYear;

    printf("enter the year\n");

    scanf("%d",&iYear);

    /*The year is Leap if it is century year and is divisible by 400)

    The year is Leap if it is no-century year and is divisible by 4)*/

    if(){

    printf("Leap Year\n");

    }

    else {

    printf("Not a Leap Year\n");

  • 8/11/2019 PracticeGuide P&T Part1

    10/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 7 of 7

    }

    return 0;

    }

    /****************************************************************

    *End of LogicalOperators.c*****************************************************************/

    Practice Question 10: Use of for Loopa)

    Write a program to print 1 to 50 which are divisible by 4b) Find the output of the following programs by dry run through trace table and then

    compare the result by executing the program

    (1)#include

    #defineNUMBER 200

    intmain(intargc,char**argv) {

    /*Variable declaration*/

    intiCount1,iCount2,iFlag=0;

    printf("Prime numbers from 1 to 200 are: \n\n");

    /* loop from 1 to 200 */

    for(iCount1 = 1; iCount1

  • 8/11/2019 PracticeGuide P&T Part1

    11/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 8 of 8

    /* loop to display characters and its equivalent ASCII values*/

    for(iIndex = 0; iIndex

  • 8/11/2019 PracticeGuide P&T Part1

    12/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 9 of 9

    b) Dry run the following programs and also verify the result by executing the followingprograms

    (1)int main (int argc, char** argv){

    int aiEmployeeNumber[] = {15090, 15091, 15092};

    printf("%d ", aiEmployeeNumber[1]);

    printf("\nenter the value for element\n");scanf("%d", &aiEmployeeNumber[1]);

    printf("%d \n", aiEmployeeNumber[1]);

    return 0;

    }

    (2)#include

    int main(int argc,char**argv) {

    int aiArray1[2] = {1,2};

    int aiArray2[2] = {2,3};

    int iIndex;

    for(iIndex=0;iIndex

  • 8/11/2019 PracticeGuide P&T Part1

    13/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 10 of 10

    int main (int argc, char** argv){

    int aiEmployeeNumber[] = {15090, 15091, 15092, 15093};

    int *piPointer=NULL;

    piPointer = aiEmployeeNumber;

    printf("\n%d ", aiEmployeeNumber[0]);

    printf("\n%d ", piPointer[0]);printf("\n%d ", *(piPointer + 0));

    printf("\n%d ", *piPointer);

    printf("\n\n%d ", *(piPointer + 2));

    return 0;

    }

    (2)

    #include

    #define N 10

    int main(int argc,char **argv){int iNumber[N],iSize,iCount1,iCount2,iTemp;

    printf("\n how many no's in array");

    scanf("%d",&iSize);

    printf("\n enter %d elements in array",iSize);

    for(iCount1=0;iCount1

  • 8/11/2019 PracticeGuide P&T Part1

    14/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 11 of 11

    int iEmpId,iFound =0,iCount;

    char cChoice;

    /* Enter the employee id */

    printf("Enter the Employee id\n");

    scanf("%d", &iEmpId);

    do{

    for(iCount=0; iCount){

    printf("Employee Id is present\n");

    iFound = 1;

    break;

    }

    }/* Getting department code and salary for the found employee id

    */

    if(1 == iFound){

    printf("Enter the Department code in the range 101-

    105\n");

    scanf("%d",&aiEmployeeInfo[1][iCount]);

    printf("Enter the Salary\n");

    scanf("%d",&aiEmployeeInfo[2][iCount]);

    }

    else{/* Display error message and terminate the program */

    printf("Employee Id not Found\n");

    return 0;

    }/* Read the choice from user to continue or not */

    printf("Do you want to continue(Y/y)");fflush(stdin);

    scanf("%c",&cChoice);

    }while(cChoice == 'Y' || cChoice == 'y');

    /* Return a code back to OS!*/

    return 0;

    }

    Practice Question 16: Strings

    a)

    Dry run and also verify the result by execution(1)

    int main (int argc, char** argv){

    char acTown[] = "Guildford";

    char *pcString

    pcString = acTown + 5;

    puts(acTown);

    puts(pcString);

    pcString = acTown;

  • 8/11/2019 PracticeGuide P&T Part1

    15/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 12 of 12

    *pcString = 'B';

    pcString += 5;

    *pcString = '\0';

    puts(acTown);

    }

    Practice Question 17: Strings

    Accept a string and check whether it contains only alphabets (a-z or A-Z)

    Good to know

    Practice Question 18: Variables, constants and data types

    a)

    Identify the error in the following program and also debug the same for successfulexecution of the program

    /*********************************************************************

    * Filename : Debugging.c

    * Author : Education & Research Dept, Infosys

    * Limited

    * Date : 22-June-2011

    Description : Helps to understand the debugging of programs

    ********************************************************************/

    /* Include files */#include

    /*********************************************************************

    * Function : main()

    * Description : main fuction of the program explains use of type

    casting

    Input Parameters:

    int argc - Number of command line arguments

    char **argv The command line arguments passed

    Returns: 0 on success to the operating system

    *********************************************************************/

    int main(int argc,char **argv) {

    int iEmpCode ;

    printf("Enter the Employee Id of an employee\n");

    scanf("%d", iEmpCode);

    printf("Enter the employee department code\n");

  • 8/11/2019 PracticeGuide P&T Part1

    16/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 13 of 13

    scanf("%d", &iDeptCode);

    printf("Employee code = %d\n",iEmpCode);

    printf("Department code = %d\n",iDeptCode);

    return 0;

    }

    /********************************************************************End of Debugging.c

    ********************************************************************/

    b)

    Debug the following Program to understand the use of modulus operator

    /************************************************************

    * Filename : Operators.c

    * Author : Education & Research Dept, Infosys Limited

    * Date : 22-June-2011

    * Description : To understand the use modulas operator

    *****************************************************************/

    /* Include files */

    #include

    #define NUMBER 2

    /****************************************************************

    * Function : main()

    * Description : main fuction to understand the use modulas

    * operator

    * Input Parameters:

    * int argc - Number of command line arguments

    * char **argv The command line arguments passed* Returns: 0 on success to the operating system

    *****************************************************************/

    int main(int argc,char **argv) {

    /*Variable declaration*/

    float fValue = 5.2,iResult;

    /* Calculating sum of two numbers */

    iResult = fValue % NUMBER ;

    /* Displaying the calculated sum */

    printf("Result = %d", iResult);

    }

    /****************************************************************

    * End of Operators.c

    *****************************************************************/

  • 8/11/2019 PracticeGuide P&T Part1

    17/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 14 of 14

    c) Analyze the output of the following program

    #include

    int main(int argc,char**argv) {printf("The value of 4

  • 8/11/2019 PracticeGuide P&T Part1

    18/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 15 of 15

    Practice Question 20 : Conditional constructs

    a) Write a pseudo code to find whether a given number is odd or even and also convertthe same into a program

    b) Write a pseudo code to perform arithmetic operations on the given numbers. i.e.Addition, Subtraction, Multiplication and Division operations on the given numbers.Accept the choice of operation from the user. Convert this into a program

    c)

    What is the output of the following code snippet?

    int main(int argc,char **argv) {if(-2){

    printf("-2 means true");}

    if(0){printf("0 means true");}

    if(1){printf("1 means true");

    }}

    Practice Question 21 : Use of for Loop

    a)

    Write a pseudo code to find the factorial of a given number also convert the same to aprogramb)

    Find the output of the following programs by dry run through trace table and thencompare the result by executing the program

    (1)

    (3)

    #include

    intmain(intargc, char**argv)

    {

    for(;printf("hi")!=2;)

    {

    printf("hi");}

    }

    (4)

    #include

    intmain(intargc,char**argv) {

    intiIndex;

  • 8/11/2019 PracticeGuide P&T Part1

    19/22

  • 8/11/2019 PracticeGuide P&T Part1

    20/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 17 of 17

    /*Variable declaration*/

    floatfOverTimePay;

    intiHour,iIndex=1;

    /* Over time pay calculation for 3 employees */

    while(iIndex = 40){

    fOverTimePay = (iHour - 40) * 12;

    printf("No. of Hours worked = %d\n \

    OvertimePay = Rs. %f",iHour,fOverTimePay);

    }

    else{

    fOverTimePay = 0;

    printf("\nNo. of hours worked (%d) is less \

    than 40hrs.\nHence no overtime pay ",iHour);

    }

    iIndex++;}

    return0;

    }

    (4)

    #include

    intmain(intargc,char**argv) {

    /*Variable declaration*/

    intiIndex=1,iFirstNumber,iSecondNumber,iThirdNumber;

    printf("ARMSTRONG NUMBERS BETWEEN 1 & 500 ARE\n");

    while(iIndex

  • 8/11/2019 PracticeGuide P&T Part1

    21/22

    Infosys Limited Practice Guide for Programming & Testing

    ER/CORP/CRS/SE1035/007 CONFIDENTIAL Version No. 1.1 18 of 18

    Practice Question 23: Use of do while Loopa)

    Use the Practice Question No. 4 (d) and modify it to accept the user choice and repeat ittill the user chooses to exit the program

    Practice Question 24: 1D - Arrays

    a)

    Write a pseudo code to find the number of occurrences of a given number in an array ofelements and also convert the same to a program

    b)

    Create an array that stores the marks scored by studentsin a subject. You can assume the size of the student array to be 10 . The range of the marks

    must be 0 to 25. Write a program to accept the marks of the students and count the numberof students who have scored each marks in the range, i.e you need to count how manystudents have scored 0, how many students scored 1, how many students scored 2 ... howmany students scored 25

    c) Create two integer arrays. merge the elements of both the arrays into the third array asgiven in the example below:

    Ex: Array1 contains { 0,1,2,3,4 }

    Array2 contains { 5,6,7,8,9 }

    resultant array contains { 0,5,1,6,2,7,3,8,4,9}

    Practice Question 25: 2D - Arraysa)

    Write a program to create 2D -Array of 2x3 matrix and display its Elements

    b)

    Write a program to accept two matrices and find the multiplication of two matricesAnd store the result in the third matrix

    c)

    Create an array that stores the names of 5 students. Create a 2D array that stores themarks of 5 students in 5 different subjects. Find the total marks of the students. Also findwhich student has scored the highest total.

    d) Create an array that stores the sales made by the salespersons during the month. Eachsalesperson gets a salary of Rs.1000 plus 10% commission (commission on the sales made.Ex: if the sales is 3000 then commission is 10%of 3000) only if the sales made during themonth crosses Rs.3000. Otherwise his salary is zero. Populate the salary of the salespersons in another array after calculating the salaries of the salespersons.(You can assume

    a fixed size for the array) .Print in a neat format how many sales people earned salaries ineach of the following ranges:

    Rs.1000 to Rs.2000Rs.2001 to Rs.3000Rs.3001 to Rs.4000Rs.4001 to Rs.5000

  • 8/11/2019 PracticeGuide P&T Part1

    22/22

    Infosys Limited Practice Guide for Programming & Testing

    Practice Question 26: Strings

    a) Write a program to find the length of a string without using string handling functionb)

    Write a program to count the number of vowels in a given string

    c)

    Accept a string from the user and reverse the string using pointersd)

    Accept a string from the user. Write programs to convert the same to:

    all upper caseall lower casetoggle case

    e)

    Accept a string of 'n' characters length. 'n' can be fixed using #define. Replace characters

    in the string as follows:Vowels: a by 1, e by 2, i by 3, o by 4, u by 5All consonants to be replaced by 9All other characters including special characters to be replaced by 8