+ All Categories
Home > Documents > unit3_QA

unit3_QA

Date post: 03-Apr-2018
Category:
Upload: alysonmicheaala
View: 219 times
Download: 0 times
Share this document with a friend

of 31

Transcript
  • 7/28/2019 unit3_QA

    1/31

    QUESTION AND ANSWERS

    Sub: :- COMPUTER CONCEPTS AND C PROGRAMMINGUnit-3

    Q1. What is an array?Ans:- An array is a collection of variables of the same type. Individual array elements

    are identified by an integer index. In C the index begins at zero and is always written inside square brackets.

    Q2. How arrays are declared?Ans:- ARRAY DECLARATION

    1. ONE DIMENSIONAL ARRAYdata type variable name[ size]

    Ex: int a[10]; char name [10] ; float x [5];2. TWO DIMENSIONAL ARRAYdata type variable [size][size];

    Ex: int mat_a[10][10]; char name[10][20];Arrays can have more dimensions, in which case they might be declared as

    int results_2d[20][5];int results_3d[20][5][3];Each index has its own set of square brackets.

    Q3. What is searching? Explain the method of linear searching.

    Ans:- Searching:- It is a process of finding a key element in a given array of elements. If the key element is found in the array of elements then the search is said to besuccessful otherwise unsuccessful.1.Linear searching.:- In this method the key element is compared with theelements of the array one by one linearly till the key element is found in the arrayor the search area is exhausted.#includemain(){float a[10],x;int i,n,flag=0;

    printf("\n enter the no.of elements in the array ");scanf("%d",&n);

    printf("\n enter the elements of the array\n");for(i=0;i

  • 7/28/2019 unit3_QA

    2/31

    if(a[i]==x){

    printf("\n the key element %f is found at location %d",x,i+1);flag=1;}

    if(flag==0) printf("\n the key element %f is not found",x);getch();

    }

    Q4. What is binary searching? Write a C code for binary searching.Ans:- This method is more efficient than linear search, when size of array is large. This

    method of searching is performed by following steps:1. arrange the array in the ascending order.2. read the key element to be searched3. compare the key element with the mid element of the array

    Now there are 3 possibilitiesa. if the key element is equal to mid element then the searching is declared

    as successful. b. If the key element is greater than the mid element, then conduct the

    search in 2 nd half of the array.c. If the key element is less than the mid value then conduct the search in

    the 1 st half of the array.

    Binary searching:-

    #includemain(){int i,n,mid,low=0,high,probe=0;float a[10],temp,x;

    printf("\n enter the no.of elements to be sorted ");scanf("%d",&n);

    printf("\n enter %d elements in ascending order\n",n);for(i=0;i

  • 7/28/2019 unit3_QA

    3/31

    high=mid-1; probe++;}

    else if(x>a[mid]){

    low=mid+1; probe++;}

    else if(x==a[mid]){

    probe++; printf("\n search is successful"); printf("\n the no.of probes=%d",probe);getch();exit();}

    } printf("\n unsuccessful search");getch();}

    Q5. Write a C program to sort N numbers in ascending order using bubble sort ?

    Ans:- #includemain(){int i,j,n;float a[10],temp;

    printf("\n enter the no.of elements to be sorted ");scanf("%d",&n);

    printf("\n enter %d elements \n",n);for(i=0;i

  • 7/28/2019 unit3_QA

    4/31

    }printf("\n the sorted array is\n");for(i=0;i

  • 7/28/2019 unit3_QA

    5/31

    Function_type_function_name (parameter list)Parameter definition;

    {variable declaration ; /* within the function */

    executable statement 1;executable statement 2...

    return statement ( value_computed);}

    Q10. What is formal parameter?Ans:- Formal Parameter List

    The parameter list declares the variables that will receive the data sent by thecalling program. They serve as input data to the function to carry out thespecified task. Since they represent actual input values, they are often referred toas formal parameters. These parameters can also be used to send values to thecalling programs. This aspect will be covered later when we discuss more aboutfunctions. The parameters are also known as arguments.

    Q11. Describe Return statement along with syntax?Ans:- As pointed out earlier, a function may or may not send back any value to the

    calling function. If it does, it is done through the return statement. While it is possible to pass to the called function any number of values, the called functioncan only return one value per call, at the most.

    The return statement can take one of the following formreturn;or return (expression);The first, the `plain return does not return any value; it acts much as the closing

    brace of the function. When a return is encountered, the control is immediately passed back to the calling function. An example of the use of a simple return is asfollows;

    If (error)return;

    The second form of return with an expression returns the value of expession. For example the function

    Int mul (int x, int y){int p;p = x *y;

  • 7/28/2019 unit3_QA

    6/31

    return (p) ;}

    returns the value of p which is the product of the values of x and y. The last twostatements can be combined into one statement as follows:

    retun (x *y);

    A function may have more than one return statements. This situation arises whenthe value returned is based on certain conditions. For example:If (x

  • 7/28/2019 unit3_QA

    7/31

    The contents of the string variable input will be assigned tothe string variable messge.

    ii) strcat()The strcat function joins two strings together, the general form is

    Strcat ( string1, string 2);

    String 1 and string2 are character arrays, when the function is executed ,string2 is appended to string1, it does so by removing the null character atthe end of string 1 and placing string2 from there

    Ex : - str1= ElectricalStr2= Engineering

    Strcat(str1,str2);After execution the result will be

    Electrical Engineering \0 iii) strlen()

    This function counts and return the value the number of characters in astring,

    Variable_name = strlen(string);The variable name is an integer variable which receives the value of thelength of the string

    Ex:-Str1= GOOD

    A=strlen(str1)Gives 4 as the result and assign 4 to A

    iv) strcmp()strcmp function alphabetically compares two strings and returns an integer

    based on the outcomeif string 1 is less than string2 it gives value less than zeroif string 1 is equal to string2 it gives value 0if string 1 is greater than string 2 it gives value greater than zero

    strcmp(string1,string2)it gives the difference of the first non matching characters ASCII value

    Ex :- strcmp(rama,rama);It returns value = 0;

    v) strncat()This function is used to append first n characters of the second string atthe end of the first string

    Strncat(string1,string2,n);

    Ex:- strncat (system,software engg,8);

  • 7/28/2019 unit3_QA

    8/31

    The result will be systemsoftware

    Q15. What is a Pointer ?Ans:- A pointer is a variable which contains the address in memory of another variable.

    We can have a pointer to any variable type.

    Q16. Explain the operators of the pointers ?Ans:-

    The unary or monadic operator & gives the address of a variable.

    The indirection or dereference operator * gives the contents of an object pointed to by a pointer.

    Q17. How to declare a pointer ?Ans:- To declare a pointer to a variable do:

    int *pointer; NOTE: We must associate a pointer to a particular type: You can't assign theaddress of a short int to a long int , for instance.

    Consider the effect of the following code:int x = 1, y = 2;int *ip;ip = &x; y = *ip;x = ip;

    *ip = 3; It is worth considering what is going on at the machine level in memory to fully

    understand how pointer work. Consider Fig(next slide). Assume for the sake of this discussion that variable x resides at memory location 100, y at 200 and ip at1000. Note A pointer is a variable and thus its values need to be storedsomewhere. It is the nature of the pointers value that is new.

    IMPORTANT : When a pointer is declared it does not point anywhere. You mustset it to point somewhere before you use it.

    So ...int *ip;

    *ip = 100; will generate an error (program crash!!).

    The correct use is:int *ip;

    int x; ip = &x;*ip = 100;

    Q18. What are the different storage classes in C?Ans:- Storage class (Scope & lifetime of a variable)

  • 7/28/2019 unit3_QA

    9/31

    Based on where the variables are declared, the scope & lifetime of variableschange.

    1) Global or External variables2) Local or automatic variables3) Static variables.

    4) Register variable.

    Q19. what is external variable explain with an example?Ans:- Global/External variables: These variables are usually defined before all

    functions. Memory is allocated for these variables only once & are initialized tozero. These variables can be accessed by any function and are alive & activethroughout the programs. Any function can use it & change its value. Memory isdestroyed once the execution is over.

    Ex: # include (stdio.h)Int a=10;Int b=20;Int c;

    Void add ( ){c=a+b;}void subtract ( ){c=a-b;}main ( ){add ( );

    printf (a=%d & b=%d sum=%d, a,b,c);subtract);

    printf (a=%d b=%d diff=%d, a,b,c);}

    Q20 . what is automatic variable explain with an example?Ans:-

    Automatic variables are defined at the beginning of a function in which theyare used. Where function is called, memory is allocated to these variables & asthe control comes out the memory is deallocated.Therefore these ar3 local/private to the function is which they are defined. Theyare also called Internal variable.Scope of these are limited I cannot be accessed outside

  • 7/28/2019 unit3_QA

    10/31

    Ex: void add (int a, int b){ int c;c=a+b;

    print f (Result=%d\n, c);}

    main ( ){int a=10;int b=20;}print f (a=%d\n b=%d\n, a,b);}

    C is local variable to function add, after the cofntrol goes out of function C cannot be accessed. If we use C outside, it has to be redefined, Otherwise an erorundefined symbol C is displayed by the compiler. A&b are also local variables.

    Q21. what is static variable explain with an example? Ans:- Unlike as automatic variable, memory allocated for static variable will not bedestroyed as the control goes out of the function. So, the current value of thestatic variables is available where the function is called next time.

    Note: Static variables like automatic variables cannot be accessed by any other function except the function in which they are defined. So it has some char of local variable & some characteristic of a global variable.

    # include void display ( )

    {static int i=o;i++;print f (%d\n,i);}main ( ){int j;for (j=0, j

  • 7/28/2019 unit3_QA

    11/31

    int x;class ( );for (x=; x

  • 7/28/2019 unit3_QA

    12/31

    Q23. Explain the construction and working of a laser printer.Ans:- LASER PRINTER:- Photo copying technology has been used in laser printers, A

    drum coated with positively charged photo conductive material is scanned by alaser beam. The positive charges that are illuminated by the beam are dissipated,then a negatively charged toner powder is spread over the drum. It adheres to the

    positive charges, thus creating a page image that is then transferred to the paper.As the last step the drum is cleaned of any excess toner material to prepare it for printing the next page.Laser printers offer better quality and speed , Text and graphics can be combinedand high print speeds like 20,000 lines per minute can be achieved, Its price ishigh.

    Q24. List the different input devices.Ans:- Input devices are

    KEYBOARD,MOUSE,

    LIGHTPEN,JOYSTICK ,SCANNER ,DIGITIZER,OPTICAL SCANNERS ETC,.

    Q25 . Explain the features of the keyboardAns:- KEYBOARD:

    The keyboard is the most friendly input peripheral, both data and program can bekeyed in through it, In addition , certain commands to software can be given fromthe keyboard, it is almost impossible to use a computer without a keyboard.Keyboard consists of a set of key switches. There is one keyswithch for eachletter, number, symbol etc., much like a typewriter, when a key is pressed akeyswitch is activated, there are two types of keyboards, I) serial keyboard, sendsthe data bit by bit and ii) a parallel keyboard, sends the data as a byte in parallelform, all the bits are sent simultaneously on different lines. The function of thekeyboard electronics are , sensing a key depression, encoding and sending thecode to computer,.Ordinary keyboard will have

    1 Functional keys (F1 . . . . F 12)2 Print screen, Scroll lock, Pause/Break 3 Num/Caps? Scroll Lock (LEDs)4 Escape key5 Numeric Keypad ( 0 9,+ , - , Enter, Insert, Delete )6 0 9 , A Z Caps lock, Tab , Shift , Ctrl, Alt, Enter , Backspace,

    Special keys like $, %, #, & ( , ) , , ., : etc.7 Arrow keys8 Editing Keys ( Page up, Page down , Home , End, Insert, delete)

  • 7/28/2019 unit3_QA

    13/31

    Q26. Write a brief note on floppy disk.Ans:- FLOPPY DISK:1. It consists of circular magnetic disk, made from thins flexible mylar plastic sheet

    coated with magnetic oxide. This disk is closed in antistatic cover for protection

    dust.2. The diskette that is prepared for storing the information consists of 2 sides, topand bottom.Each side consists of number of tracks and each track is divided into number of sectors, each stores number of bytes of information. 1 byte is required to store onecharacter, say alphabetic or number. Data and programs are stored in a disk asfiles. A filename is nothing but a collection of related data. The filenames of different files stored in a diskette are stored in one area of disk, called asDIRECTORY.

    3. Based on the amount of data it can store, floppy disks are available in followingcapacities.a. 360 KB (360X1024 bytes(, where K=1024

    (2 sides X 40 tracks X 9 sectors X 512 bytes = 360 Kbytes) b. 1.2 Mb (1.2 X 1024 X 1024)

    (2 sides X 80 tracks x 18 sectors X 512 bytes = 1.2Mbytes)c. 1.44 MB (1.44 X 1024 X 1024)

    (2 sides X 80 tracks X 18 sectors X 512 bytes = 1.44Mbytes)4. Structure of floppy disk:

    Write Protectnotch

    Spindle Hole

    Index Hole

    Read/Write notch

    CompanyLabel

    User Label

    Track s

    Sector of track

  • 7/28/2019 unit3_QA

    14/31

  • 7/28/2019 unit3_QA

    15/31

    Q28. Explain in brief operation of LAN

    Ans:- LAN (Local Area Network)LAN refers to many interconnected computers located generally in thesame building; LAN has generally 3 characteristic features,A diameter of not more than a few KilometersA total data rate of at least several Mbps.Complete ownership by a single organization,There are various ways for interconnecting computers such as star,common bus and ring type LAN and so on, LAN technologies provide thehighest speed connections among computers, but sacrifice the ability tospan large distances.Ex: Departmental computers on a campus,

    Q29. Explain in brief operation of WAN Ans:- WAN (Wide area network)

    WAN , sometimes called long haul networks, allow end pints to bearbitrarily far apart and are intended for use over large distance. Typically,WANs span entire countries have data rates below 1 Mbps and are owned

    by multiple organizations, they can be connected through public or privatecommunication systems.

    Q30. What is E-mail explain?Ans:- E-mail (Electronic mail):

    Allows a user to compose memos and send them to the individual or groups. E-mail also provides the facility of reading memos that they havereceived. E-mail is one of the most popular and widespread Internetapplication services. E-mail uses the client-server paradigm. They useTCP/IP protocol, TCP/IP mail delivery system operates by having thesenders machine contact the receivers machine directly.

    ---------------xxxxxxxxxxxxxx-----------------------

  • 7/28/2019 unit3_QA

    16/31

    EXAMINATION QUESTIONS AND ANSWERS

    Q1. Write C-program to read N integer numbers interactively and to printbiggest and smallest numbers.

    Ans : - #includemain ( ){int a[20], big, small, I, N

    printf(\n enter the member of integer nos);scanf(%d, &N);

    printf(\n enter the elements of array);for (i=1; i

  • 7/28/2019 unit3_QA

    17/31

    int prime(n)

    { int i,p=0;

    for(i=2;i

  • 7/28/2019 unit3_QA

    18/31

    Q4. Write a C program to find the standard deviation of n valuesAns :-

    #include#include

    main() {int i,n;float a[10],mean,sd,var,sum1=0,sum2=0;

    printf("\n Enter the no.of elements in the array ");scanf("%d",&n);

    printf("\n Enter the elements of the array \n");for(i=0;i

  • 7/28/2019 unit3_QA

    19/31

    {trace=0;for(i=0;i

  • 7/28/2019 unit3_QA

    20/31

  • 7/28/2019 unit3_QA

    21/31

    }

    in the above example the value of the argument of sqr ( ), 10 iscopied into the parameter x, when the assignment x= x*x takes place only thelocal variable x is modified, the variable t used to call sqr ( ) still has the value 10.

    Hence the output is 100 10.

    Call by reference:In this method the address of an argument is copied into the parameter.

    Inside the function the address is used to access the actual argument used in thecall. This means the changes made to the parameter affect the variable used tocall the routine. We can create a call by reference by passing a pointer to theargument. The parameter has to be declared as pointer type.

    Ex:- main( ){ int x,y;

    x=10;y=10;swap ( &x , &y )

    }

    void swap ( int *x, int *y)

    { int temp;temp = *x;x = * y;*y = temp;

    }

    In this the contents of the variables used to call the function are swapped.

    Q8. Write a function using pointers to determine the length of a character string(5marks)

    Ans:- #includemain(){ char a[10];

    void fun(char *ptr); printf(" \n enter the string ");gets(a);fun(a);}

    void fun( char *b){ int i=0;while(b[i]!='\0')i++;

  • 7/28/2019 unit3_QA

    22/31

    printf("the length of the string is %d",i);}

    Q 9. Write a short notes on(i) Scope and lifetime of a variable

    (ii) Pointers in C (2*5)Ans:-i) Scope and lifetime of a variable:

    A variable in C can have any one of the four storage class1. Automatic variables

    Which are declared inside a function in which they are to beutilized. They are created when the function is called anddestroyed automatically when the function is exited.So they are termed as local or internal variables

    2. External Variables

    Variables that are both alive and active throughout the entire program are known as external variables. They are also called asglobal variables. Which can be accessed by any function in the

    program. These are declared outside the function.3. Static variables

    The value of static variables persists until the end of the program.,we can have internal type of an external type, depending where wehave defined.

    4. Register variablesThis tells the compiler that the variable should be kept in one of the machines registers , instead of in the memory, variable s in theregister the execution time is faster The life of this is until the end of function or block.

    ii) Pointers in CThe pointer is a variable that represents the location of a data

    item, such as a variable or an array element, pointers are used because they reduce the length and complexity of the program andalso increase the execution speed. In particular pointers also

    provide a way to return multiple data items from a function viafunction arguments. Pointers provide a convenient way torepresent multidimensional arrays,

    Ex:- if int price = 50;&price gives the address of price

    if ptr is a pointer

  • 7/28/2019 unit3_QA

    23/31

    ptr=&price is a valid statement.

    Q10. Write a C program to read a sentence and count the number of vowelsand consonants in the given sentence. Output the results on two lines

    with suitable headings (10marks)Ans:-#include#includemain(){int i=0,vowel=0,cons=0;char str[50];

    printf("\n Enter a sentence ");gets(str);while(str[i]!='\0')

    {if(isalpha(str[i]))switch(str[i])

    {case 'A':case 'a':case 'E':case 'e':case 'I':case 'i':case 'O':case 'o':case 'U':case 'u':vowel++;

    break;default :cons++;}

    i++;}

    printf("\n The entered sentence is -"); puts(str); printf("\n The no.of vowels =%d",vowel); printf("\n The no.of consonants =%d",cons);getch();}

    Q11. Write a C program add or subtract two matrices A (M x N) and B (M x N)Output the given matrices, their sum OR difference (10marks)

    Ans:-

  • 7/28/2019 unit3_QA

    24/31

  • 7/28/2019 unit3_QA

    25/31

    for(i=0;i

  • 7/28/2019 unit3_QA

    26/31

    norm+=a[i][j]*a[i][j];printf("\n NORM=%f",norm);

    getch();

    }

    Q13. write aC program to read a string and convert lower case to upper case andvice-versa (8marks)

    Ans:-#include#include#includemain(){

    int i=0;char ch[20]; printf("\n enter a string ");gets(ch);

    printf(\n The entered string is ); puts(ch);for(i=0;ch[i]!='\0';i++)

    {if(ch[i]>='a'&&ch[i]

  • 7/28/2019 unit3_QA

    27/31

    while(str1[i]!='\0')i++;

    length=i; prinyf( the length = %d, length);

    getch();

    }

    Q15. Write a C program to read two strings and concatenate them (without usinglibrary functions). Output the concatenated string along with the givenstrings. (10marks)

    Ans:-#includemain(){int i=0,length1;char str1[50],str2[25];

    printf("\n Enter the first string ");gets(str1); printf("\n Enter the second string ");gets(str2);

    printf("\ The first string is - "); puts(str1); printf("\n The second string is - "); puts(str2);while(str1[i]!='\0')

    i++;length1=i;i=0;while(str2[i]!='\0')

    {str1[length1+i]=str2[i];i++;}

    str1[length1+i]='\0'; printf("\n The concatenated string is - "); puts(str1);getch();}

    Q16. Develop functionsa. To read a given matrix.b. To output a matrixc. To computer the product of two matrices.

  • 7/28/2019 unit3_QA

    28/31

    Use the above developed functions to read in two matrices A(M x N) andB(N x M), to compute the product of the input matrices, to output thegiven matrices and the computed product matrix in a main function. (12)

    Ans:-#include

    main(){int A[10][10],B[10][10],C[10][10];void readmat(int [][],int,int),writemat(int [][],int,int);void matprod(int [][],int [][],int [][],int,int,int);int m,n,p,q;

    printf("\n Enter the order of the matrix A ");scanf("%d%d",&m,&n);

    printf("\n Enter the order of the matrix B ");scanf("%d%d",&p,&q);

    if(n!=p){ printf("\n Matrix multiplication is not possible");getch();exit(0);}

    printf("\n Matrix multiplication is possible"); printf("\n Enter the elements of matrix A \n");readmat(A,m,n);

    printf("\n Enter the elements of matrix B \n");readmat(B,p,q);matprod(A,B,C,m,n,q);

    printf("\n The elements of matrix A are \n");writemat(A,m,n);

    printf("\n The elements of matrix B are \n");writemat(B,p,q);

    printf("\n The elements of product matrix are \n");writemat(C,m,q);getch();}void readmat(int x[10][10],int m,int n)

    {int i,j;for(i=0;i

  • 7/28/2019 unit3_QA

    29/31

    int i,j;for(i=0;i

  • 7/28/2019 unit3_QA

    30/31

    Q18. Write a C Program to input N integers. Write the following functions and usethem in sort by selection sort.

    a. To find maximum of elementsb. To swap 2 elements (10marks)

    Ans:-

    #include int i,j,temp,n,a[10];main(){int mpos,max(int,int);void exchange(int,int);clrscr();

    printf("Enter value of n\n");scanf("%d",&n);

    printf("Enter the elements one by one\n");for (i=0;i

  • 7/28/2019 unit3_QA

    31/31

    Q19. Write a C program to swap two elements using pointers? (5marks)Ans:-

    #include main()

    {int a,b,*pta,*ptb,temp;scanf(%d %d ,&a,&b);

    pta=&a; ptb=&b; printf( \n values of a& b before swap are a= %d b= %d, a,b);temp=*pta;*pta = * ptb;*ptb= temp;

    printf( \n values of a& b after swap are a= %d b= %d, a,b);}

    ******************