PPS QB SOL UNIT-2-converted PDF
Document Details
Uploaded by EntrancedClavichord7491
Gujarat Technological University, Ahmedabad
2016
PPS
Rahul Subramanyam
Tags
Summary
This document is a past paper for a computer science course. It contains questions and answers on strings, arrays, pointers, and enumerated data types in C programming. The questions appear to be from the year 2016 and were likely asked in a programming exam.
Full Transcript
UNIT-2 PART-A 1. Distinguish between string and character. (Dec 2016[R16] ) The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a c...
UNIT-2 PART-A 1. Distinguish between string and character. (Dec 2016[R16] ) The main difference between Character and String is that Character refers to a single letter, number, space, punctuation mark or a symbol that can be represented using a computer while String refers to a set of characters. In C programming, we can use char data type to store both character and string values. 2. Write the syntax for enumerated data types. Give example. (May 17[R16] ) An enumeration type (also called enum) is a data type that consists of integral constants. To define enum, the enum keyword is used. Syntax: enum flag {const1, const2,..., constN}; By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary). Eg: enum color{red,green,blue} 3. Write the difference between array and pointer? (May /Jun 2017[R13], May 17[R15] ) An array is a collection of elements of similar data type whereas the pointer is a variable that stores the address of another variable. An array size decides the number of elements it can store where as, a pointer variable can store the address of only one variable in it. 4. Give example to Declare one, two and multi dimensional arrays. An array is a collection of similar data items, A one-dimensional array is like a list; Eg: int array; A two dimensional array is like a table Eg: int array; Multi dimensional array is like a cube Eg: int array; 5. List the input/output functions for strings. (Aug 2017[R16] ) String input functions: scanf(), gets() Output functions: printf(), puts() There is a little difference between scanf() and gets(), while reading string from keyboard, the scanf() accepts character by character from keyboard until either a new line (‘\n’) or blank space is found, which ever comes earlier. Whereas “gets()” accepts until a newline is found. It accepts white spaces & tab also, these input functions append a null character at end of the string. The printf() and puts() is work in similar way. 6. Differentiate between gets() and scanf().(Dec 2017[R16] ) There difference between scanf() and gets(), while reading string from keyboard, the scanf() accepts character by character from keyboard until either a new line (‘\n’) or blank space is found, which ever comes earlier. Whereas “gets()” accepts until a newline is found. It accepts white spaces & tab also, these input functions append a null character at end of the string. 7. List the sub strings that can be formed from the string “ABCD”. Dec 2017[R16] ) The substrings are “a”, “b”, “c”, “d”, “ab”, “bc”, “cd”, “abc”, “bcd” and “abcd” 8. Distinguish between structure and union. (May /Jun 2019[R16] ) Structure Union You can use a struct keyword to define a You can use a union keyword to define a union. structure. Every member within structure is assigned a In union, a memory location is shared by all the unique memory location. data members. The total size of the structure is the sum of the The total size of the union is the size of the largest size of every data member. data member. It occupies space for each and every member It occupies space for a member having the highest written in inner parameters. size written in inner parameters. 9. Explain about the typedef with an example. (Dec 2017[R16] ) typedef is a keyword used in C language to assign alternative names to existing datatypes. It is mostly used with user defined datatypes, when names of the datatypes become slightly complicated to use in programs. syntax for using typedef: typedef example: typedef unsigned long ulong; The above statement define a term ulong for an unsigned long datatype. Now this ulong identifier can be used to define unsigned long type variables. 10. What is an array? Explain. (Dec 2018[R13] ) Array is defined as a collection of homogeneous (similar)data items and sharing with a common name. Array elements are stored at contiguous memory locations. Ex: int array; 11. Define Union. (Dec 2018[R13] ) A union is a collection of similar and dissimilar variables referred under a single name. It allows us to store different datatypes of values in the same memory location. We can define a union with many members, but only one member can contain a value at any given time. Syntax: Union tag_name { Type1 member 1; Type1 member 2; ….. ….. }; 12.What is string? (Dec 2018[R13] ) A string in C is merely an array of characters. The length of a string is determined by a terminating null character: '\0'. So, a string with the contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the terminating null ( '\0' ) character. 13. Define structure with example. (Apr 2018[R16] ) Structure is a user-defined datatype in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. But structure on the other hand, can store data of any type. Example: Struct student { Char name; Int age; Char branch; }; 14. Write the array applications. (May 2018[R13]) Applications: Array Used for maintaining multiple variable names using single name. Array can be used for sorting elements.... Array can perform Matrix Operation.... Array can be used in CPU Scheduling.... Array can be used in Recursive Function. 15. Can an assignment operator copy one array to another? Justify your answer. (May 2018[R16] ) It is not possible in C. The problem with C is that this language do not provide an array type. Instead, arrays are simply an area of contiguous allocated memory and the name of the array is actually a pointer to the first element of the array. 16. Discuss about strlen() function with an example.(Dec 2018[R18]. strlen() function is used to find the length of the string. #include #include void main () { char str=”welcome”; int length; length = strlen(str); printf("Length of string is: %d", length); } 17. What is it necessary to give the size of an array in array Declaration? (Dec 2019[R18] ) Array is a variable which store more than one data element of similar type. So, if we doesn't give the size of array at declaration time then compiler is unable to allocate memory for that variable and then error will occur in run time declaration. So it is necessary to give the size of an array to allocate the memory. 18. Mention the advantages and disadvantages of an array. (Dec 2019[R18] ) Arrays are basically a collection of similar type of data in a contiguous memory location under one name. Advantages: collection of similar types of data. if we want to store the marks of all students it will easy to store in array otherwise we have to store marks in different location, which is not easy to memorise. we have to remember the first index of array. 2 Dimensional array is used to represent a matrix. Disadvantages: Time complexity increase in insertion and deletion operation. wastage of memory because arrays are fixed in size. if there is enough space present in the memory but not in contiguous form , in this case not able initialize array. it is not possible to increase the size of the array, once declared the array. 19. Mention the advantages and disadvantages of pointers. (May /Jun 2019[R18] ) Advantages: Pointers provide direct access to memory Pointers provide a way to return more than one value to the functions Reduces the storage space and complexity of the program Reduces the execution time of the program Disadvantages: Pointers are slower than normal variables Un initialized pointer might cause segmentation fault 20. Explain about self referential structure. (May 2019[R16] ) Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature. 21. What is pointer? Explain how the pointer variable Declared and initialized? (May 2019[R16] A pointer is a variable that stores memory address. Syntax to declare pointer variable data-type * pointer-variable-name; data-type is a valid C data type. * symbol specifies it is a pointer variable. You must prefix * before variable name to declare it as a pointer. Example to declare pointer variable int * ptr; 22. How to declare, define and initialize a pointer. (Jan-2020[R18]) Pointer is a variable which holds the address of another variable Declaration: datatype *pintervariable; Ex: int *ptr; Initialization: Int n=100; Int *ptr; Ptr=&n; PART-B 1. What are the advantages and drawbacks of pointers? (Dec 2016[R16], May 17[R16] ) Advantages: Pointers provide direct access to memory Pointers provide a way to return more than one value to the functions Reduces the storage space and complexity of the program Reduces the execution time of the program Provides an alternative way to access array elements Pointers allows us to perform dynamic memory allocation and de-allocation Address of objects can be extracted using pointers Disadvantages: Un initialized pointers might cause segmentation fault Dynamically allocated block needs to be freed explicitly. Otherwise it would lead to memory leak Pointers are slower than normal variables Pointer bugs are difficult to debug 2. Explain about String manipulation functions. (Dec 2018[R16], May 2018[R13] ) C programming language provides a set of pre-defined functions called string handling functions to work with string values. The string handling functions are defined in a header file called string.h. Whenever we want to use any string handling function we must include the header file called string.h. most commonly used string handling function and their use... Function Syntax (or) Example Description strcpy() strcpy(string1, string2) Copies string2 value into string1 strncpy(string1, string2, strncpy() Copies first 5 characters string2 into string1 5) strlen() strlen(string1) returns total number of characters in string1 strcat() strcat(string1,string2) Appends string2 to string1 strncat() strncpy(string1, string2, 4) Appends first 4 characters of string2 to string1 strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same; less than 0 if string1string2 strncmp() strncmp(string1, string2, 4) Compares first 4 characters of both string1 and string2 strlwr() strlwr(string1) Converts all the characters of string1 to lower case. strupr() strupr(string1) Converts all the characters of string1 to upper case. 3. Write a C program to check whether given elements in an array are distinct or not? (Jun2017[R13], May 17[R15] ) #include void distict_elements(int a[], int n); void main() { int size_array, i, arr; scanf(“%d”, &size_array); for(i=0; im1,&ptr->m2,&ptr->m3); Printf(“ Entered details are:”); Printf(“\nName:%s\nrollnumber=%d\nm1=&d\nm2=%d\nm3=%d”, ptr->name, ptr->roll_no, ptr->m1,ptr->m2,ptr->m3); } 9. Write a program to display the Location of a character ‘T’ in a given string. (Sep 2017[R16] ) Program: #include #include #include void main() { char s, t; char *found; clrscr(); puts("Enter the first string: "); gets(s); puts("Enter the string to be searched: "); gets(t); found = strstr(s, t); if(found) { printf("Second String is found in the First String at %d position.\n", found - s); } else { printf("-1"); } getch(); } 10. Give the signature fgets(), Puts() function. (Sep 2017[R16] ) For reading a string value with spaces, we can use either gets() or fgets() in C programming language. fgets(): It reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached. Syntax : char *fgets(char *str, int n, FILE *stream) // fgets() #include #define MAX 15 void main() { char buf[MAX]; fgets(buf, MAX, stdin); printf("string is: %s\n", buf); } puts() function is a file handling function in C programming language which is used to write a line to the output screen. puts(string); Program: #include #include Void main() { char string; strcpy(string, "This is a test string"); puts(string); } 11. Write a program using structure to search a name in a record of ten mobile subscribers and print the name, address, bill number and amount of the searched record. (Sep 2017[R16] ) # include # include # include struct teledir{ long teleno ; char name; char address; }; Void main() { struct teledir t; FILE *fp; long tno; char sname,c; int ch; while(1) { clrscr(); printf("\t\t\t\tMENU\n"); printf("1. TO ADD THE RECORD IN THE FILE.\n"); printf("2. TO SEARCH THE RECORD BY NAME.\n"); printf("3. TO SERACH THE RECORD BY TELEPHONE NUMBER.\n"); printf("4. TO SEE ALL THE RECORD."); printf("5. TO EXIT.\n"); printf("ENTER YOUR CHOICE:->"); scanf("%d",&ch); switch(ch) { case 1: fp=fopen("telephon.txt","ab"); while (1) { clrscr(); printf("ENTER THE TELEPHONE NUMBER:->"); scanf("%ld",&t.teleno); fflush(stdin); printf("\nENTER THE NAME :->"); scanf("%s",t.name); fflush(stdin); printf("\nENTER THE ADDRESS :->"); scanf("%s",t.address); fwrite(&t,sizeof(t),1,fp); fflush(stdin); printf("\n\nWISH TO CONTINUE?(Y/N)"); scanf("%c",&c); if(c=='n' || c=='N') break; } fclose(fp); break; case 2: fp=fopen("telephon.txt","rb"); clrscr(); printf("ENTER THE NAME :->"); scanf("%s",sname); while(fread(&t,sizeof(t),1,fp)) { if(strcmp(sname,t.name)==0) { printf("\n\t%ld\t %s\t %s",t.teleno,t.name,t.address); getch(); break; } } fclose(fp); getch(); break; case 4: exit(1); } } getch(); } 12. Write a program to count the number of times a given character appears in the string (Dec 2017[R16] ) #include #include Void main(){ Char str,ch; Int I,count=0; Printf(“\nEnter a string”); Gets(str); Printf(“\n Enter the character that you want to search for:”); Scanf(“%c”,&ch); For(i=0,iid); printf(" Name is: %s \n", ptr->name); printf(" Percentage is: %f \n\n", ptr->percentage); } 14.Write a program to define a structure for hotel that has members- name, address, grade, number of rooms and room charges. Write a function to print the names of a hotel in a particular grade. Also write a function to print names of a hotel that have room charges Less than the specified value. (Dec 2017[R16] ) #include Void main() { Struct hotel { Char name; Char city; Char grade; Int rc,nr; }; Struct hotel ht,t; Int I,n,j,c; Char gr; Printf(“Enter no.of hotels”); Scanf(“%d”,&n); For(i=0;i