A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE NOTE: 1. There are TWO PARTS in this Module/paper. PART ONE contains FOUR questions and PART TWO contains FOUR questions. 2.
PART ONE is to be answered in the TEAR-OFFANSWER SHEET only, attached to the question paper, as per the instructions contained therein. PART ONE is NOT to be answered in the answer book.
3.
Maximum time allotted for PART ONE is ONE HOUR. Answer book for PART TWO will be supplied at the table when the answer sheet for PART ONE is returned. However, candidates who complete PART ONE earlier than one hour, can collect the answer book for PART TWO immediately after handing over the answer sheet for PART ONE. TOTAL TIME: 3 HOURS TOTAL MARKS: 100 (PART ONE-40; PART TWO – 60) PART ONE (Answer all the questions) 1.
Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10)
1.1
Assume that variable x resides at memory location 100, y at 200 and ip at 1000. int x=1; y=2; int *ip; ip=&x y=*ip What will be the value of y after execution of above code? A) 1 B) 2 C) 100 D) 1000
1.2
What will be the output of the following program? main() { int i=1,j,k; if(i>=2) j=2; k=3; printf(“\n%d%d”,j,k); } A) 2,3 B) 0,0 C) 0,3 D) 3,2
1.3
Which of the following keyword is used to jump out of a loop without waiting to get back to the conditional test? A) break B) continue C) while D) exit
A3-R3
Page 1 of 5
JANUARY, 2004
1.4
Pick up the odd one out from the following. A) a=a+1; B) a+=1; C) a++; D) a=+1;
1.5
Which of the following is the correct way of declaring an array of integer pointers? A) int *arr[10]; B) int arr[10]; C) *int arr[10]; D) int *arr;
1.6
The expression, i=30 * 10+27; evaluates to A) 327 B) -327 C) 810 D) 0
1.7
Which of the following is returned by the function ‘strcmp’ if the strings that are compared are identical? A) 0 B) 1 C) 2 D) true
1.8
The statement that correctly defines a character called letter is A) letter :=char; B) char letter; C) letter : char; D) character letter;
1.9
The statement that defines an input file handle called input_file, which is a pointer to type FILE, is: A) FILE*input_file; B) type input _file as FILE; C) input_file FILE; D) *FILE input_file;
1.10
Close the file associated with input_file A) close input_file; B) fclose(input_files); C) fcloseall(); D) input_file(fclose);
A3-R3
Page 2 of 5
JANUARY, 2004
2.
Each statement below is either TRUE or FALSE. Choose the most appropriate one and ENTER in the “tear-off” sheet attached to the question paper, following instructions therein. (1 x 10)
2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.10
Operation between integer and real(float) always yields a real result. The while and for loops cannot be nested loops the way if statements can be nested. The advantage of ‘switch’ statement over ‘if’ is that it leads to more structured program. Strings are arrays of characters terminated by the null character. A pointer is a variable that points to another variable. The variables commonly used in ’C’ functions are available to all functions in a program. fopen() function returns a pointer to the open file. Character data types cannot be declared as unsigned. The statement which prints out the value of the integer variable sum, is printf(“%s”, sum); The char variable is used to store the largest number less than 128.
3.
Match words and phrases in column X with the closest related meaning/ word(s)/phrases in column Y. Enter your selection in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10)
3.2
X Several built in functions in 'C' that can be called A. at any time A collection of values of the same type B.
3.3
A decision control structure
C.
The if-else statement
3.4
Logical AND operator
D.
&&
3.5
Finding length of a string
E.
strlen()
3.6
Passing address of variables to a function
F.
Call by reference
3.7
Reading formatted data from the standard input G. stream Preprocessor statement H.
3.1
3.8 3.9
A data type suitable for grouping data elements together 3.10 Dynamic memory allocation
A3-R3
Page 3 of 5
Y Library An Array
scanf() #define
I.
A Structure
J.
calloc, sizeof, free
K.
functions
L.
gets()
JANUARY, 2004
4.
A. D. G. J. M.
Each statement below has blank space to fit one of the word(s) or phrases in the list below. Enter your choice in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10) switch-case-default & main() for getchar()
4.1
B. E. H. K. N.
while linked list puts 20 fopen()
C. F. I. L. O.
5 #include<stdio.h> sizeof null
The statement within the _______ loop would keep on getting executed till the condition being tested remains true.
4.2
The control statement, which allows us to make a decision from the number of choice, is called a(n) _______.
4.3
A string is terminated by a(n) _______ character.
4.4
‘C’ provides a special operator _______ which gives the size of basic data types in bytes.
4.5
A(n) _______ is comprised of a series of nodes, each node containing a data element and a pointer to the next node.
4.6
The statement printf(“%20.5s\n”s) means that the first _______ characters are printed of an arrays.
4.7
The _______ accepts a single character from the keyboard.
4.8
The _______ function, accepts the pathname for the file and the access mode.
4.9
The _______ refers to the address of the variable.
4.10
The _______ acts as a check to make sure you are using the input and output commands properly.
A3-R3
Page 4 of 5
JANUARY, 2004
PART TWO (Answer any four questions) 5. a) b)
What does the scope of variable refer in ‘C’ language? Write a program to sort a given list of number using Bubble sort algorithm. (5+10)
6. a) b)
What is a Pointer? How can you use pointers with Array if you want to copy elements of array a into array b. We have a Single linked list as following: 5
Head
9
1
Write the ‘C’ link list program to sum up this list. ([3+5]+7) 7. a) b)
8. a) b)
Create a program that prints a Fahrenheit-to-Celsius conversion with a for loop or a while loop; where Celsius=((f-32) * 5/9); and Fahrenheit is denoted by f. While purchasing certain items a discount of 10% is offered if the quantity purchased is more than 2000. Write a program to calculate the total expenses, if quantity and price per item are entered through keyboard. (7+8) Write a program to determine whether a number is prime or not. Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In Fibonacci sequence the sum of two successive terms gives the third term. The following are few terms of Fibonacci sequence: 1
1
2
3
5
8
13
21
.... (7+8)
9.
A3-R3
Create a program that reads in a string containing a first name followed by a blank followed by a last name. Write functions to remove any leading or trailing blanks. Write another function that return the last name. (15)
Page 5 of 5
JANUARY, 2004
A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE NOTE: 1. There are TWO PARTS in this Module/paper. PART ONE contains FOUR questions and PART TWO contains FOUR questions. 2.
PART ONE is to be answered in the TEAR-OFFANSWER SHEET only, attached to the question paper, as per the instructions contained therein. PART ONE is NOT to be answered in the answer book.
3.
Maximum time allotted for PART ONE is ONE HOUR. Answer book for PART TWO will be supplied at the table when the answer sheet for PART ONE is returned. However, candidates who complete PART ONE earlier than one hour, can collect the answer book for PART TWO immediately after handing over the answer sheet for PART ONE. TOTAL TIME: 3 HOURS TOTAL MARKS: 100 (PART ONE-40; PART TWO – 60) PART ONE (Answer all the questions) 1.
Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10)
1.1
Consider the following code segment int a[10], *p1, *p2; p1 = &a[4]; p2 = &a[6[; Which of the following is incorrect w.r.t pointers?
A) B) C) D)
p1 +2 p2 - 2 p2 - p1 p2 +p1
1.2
The second expression (j – k), in the following expression will be evaluated (i + 5) || (j - k)
A) B) C) D)
if the expression (i + 5) is true. if the expression (i + 5) is false. irrespective of whether (i + 5) is true or false. will not be evaluated in any case.
1.3
In the for statement : for (exp1; exp2; exp3 ) { ……..} where exp1, exp2 and exp3 are expressions. What is/are optional?
A) B) C)
None of the expressions are optional. Only exp1 and exp3 are optional. Only exp1 is optional
A3-R3
Page 6 of 5
JANUARY, 2004
D)
All the expressions are optional.
1.4
Which of the following statement is not true for register variable?
A) B) C) D)
Only a few register variables may be defined in a function. It is not possible to take the address of a register variable. A struct variable can be stored in registers. If a register declaration is not honored, the register variables are treated as auto variables.
1.5
What will be the output of the following code? #include <stdio.h> myfunc ( struct test t) { strcpy(t.s, “world”); } main() { struct test { char s[10];}t; strcpy(t.s,”Hello “) printf(“%s”,t.s); myfunc(t); printf(“%s”,t.s); }
A) B) C) D)
Hello Hello Hello world world world the program will not compile
1.6
Which of the following is false for goto statement?
A) B) C) D)
Use of the goto statement should generally be avoided. A goto statement transfers the control to a labeled statement. No two statements in a function can have same label. With a goto statement, the control can be transferred to any statement of other program.
1.7
The output of the following code segment will be char x = ‘B’; switch ( x ) { case ‘A’ : printf(“a”); case ‘B’ : printf(“b”); case ‘C’ : printf(“c”); }
A) B) C)
B b BC
A3-R3
Page 7 of 5
JANUARY, 2004
D)
bc
1.8
How many values at the most can be returned to the calling function through a single return statement?
A) B) C) D)
0 1 2 any number of values
1.9
A constant cannot be used except
A) B) C) D)
for assigning initial value to a variable. with ++ operator. as a formal argument. on LHS of an assignment operator.
1.10
Which of the following preprocessor directives is used to create marcos
A) B) C) D)
#include #ifdef #define #undef
2.
Each statement below is either TRUE or FALSE. Choose the most appropriate one and ENTER in the “tear-off” sheet attached to the question paper, following instructions therein. (1 x 10)
2.1
The size of a pointer to an integer is same as the size of a pointer to a struct variable having four integer data members. The contents of a file opened in “r+” cannot be changed. The mod operator (%) can operate on float data. Every if statement can be converted into an equivalent switch statement. The only property of a recursive function is that it calls itself again and again. All members of a union are allocated contiguous memory locations. The main() function can call itself recursively. No two constants in an enum can have same value. (i + j)++ is illegal.
2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 A3-R3
Page 8 of 5
JANUARY, 2004
2.10
The statement struct s { int j; float f;}; tells the compiler to reserve the space for its data members j and f.
3.
Match words and phrases in column X with the closest related meaning/ word(s)/phrases in column Y. Enter your selection in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10)
3.1
X A homogeneous data structure
A.
declaration
Y
3.2
A non-linear data structure
B.
register
3.3
a return in main( )
C.
int *p[10]
3.4
Logical AND
D.
gets
3.5
Left to right precedence operator
E.
static variable
3.6
Space allocation
F.
&
3.7
Retains value across the function calls
G.
?:
3.8
Pointer to a function
H.
structure
3.9
Returns a pointer to an integer
I.
array
J.
getline
K.
terminate program
L.
int (*p)[10];
M.
linked llist
N.
int *p(void);
O.
auto
P.
definition
Q.
int (*p)(void);
R.
&&
3.10 To read a line from standard input
A3-R3
Page 9 of 5
JANUARY, 2004
4.
A. D. G. J. M. P. S.
Each statement below has blank space to fit one of the word(s) or phrases in the list below. Enter your choice in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1 x 10) 2.5 variable a[1][2] &a[1][2] P 2 struct s
p;
B. E. H. K. N. Q. T.
extern *p 3 all except first char (*s)[10]; 5 &p
C. F. I. L. O. R. U.
value char *s[10]; struct s *p reference expression first static
4.1
The arrays are always passed by __________.
4.2
&(*p) is equivalent to __________.
4.3
*(*(a+1)+2) is equivalent to __________.
4.4
__________ defines an array of pointers to strings.
4.5
A(n) __________ cannot be a part of formal argument list.
4.6
Within a multifile program, a(n) __________ variable can be defined in one file and accessed in another.
4.7
A3-R3
A self referential structure, s, has __________ as a member.
Page 10 of 5
JANUARY, 2004
4.8
Let f be a float variable, then after execution of the statement f = 5 / 2; the value of f will be __________.
4.9
Let str be a string of 5 characters, then printf(“%3s”, str); will display __________ characters.
4.10
The subscript positions(s) __________ need not be included in the formal argument declaration of an array.
PART TWO (Answer any FOUR questions) 5.
6. a) b)
7. a) b) 8.
A3-R3
Write a C program to copy only those lines from one file to another that have more than 80 characters. Also print the line numbers in the new file. The line numbers must be with respect to the original file. Assume there are at the most 100 characters in a line. (15) Write a C function, ‘str_search’, that takes two strings, as arguments and returns a pointer to the first occurrence of 1st string in 2nd string, or NULL if it is not present. Write a recursive C function, power (, b) to compute ab. Assume a and b are nonnegative integers. (9+6) Write a C function to remove duplicates from an ordered array. For example, if input is – 1,1,1,3,4,4,5,8,8,10 then output should be 1,3,4,5,8,10. Write a recursive C function to print the reverse of a string. (9+6) Write a C program to generate a triangle with fibonacci numbers (series given below) as elements of every row. For example, for m = 5, the output should be 1 1 1
Page 11 of 5
JANUARY, 2004
1 1 2 1 1 2 1 1 2 Given, fibo(n) = =
3 3 5. fibo (n – 1) + fibo (n – 2), 1 for n= 1, 2
for n >= 3 (15)
9. a) b)
Define a structure of anode of a linked list having an integer data member x. Use the above structure in (a) and write the functions for the following parts; i) a function which takes a pointer to the head of linked list, which is in ascending order and an integer, x to be inserted in the linked list, as arguments. The node must be inserted in such a way that the linked list remains in ascending order after insertion. ii) a function which takes a pointer to the head of a linked list and an integer, x to be removed from the linked list, as arguments. If x is not found in the linked list, then it should display an appropriate message. (3+ [6 + 6])
A3-R3
Page 12 of 5
JANUARY, 2004