Detailed Solutions DC-05 June 2003
Q1 (a) (C) y = 14
(b) (B) displays number from 0 to 10
(c) (A) terminate to the other loops.
(d) (A) the same datatype throughout the program both in main and function.
(e) (B) 5x1.5
(f) (B) 1027
(g) (C) math.h
(h) (B) 10
Q2 (a)
|
Format Conversion |
Qualifying Input |
Argument Required |
|
C |
Single character: Reads the next character (whitespace characters included). |
char * |
|
D |
Decimal integer: Number optionally preceeded with a sign. |
int * |
|
e,E,f,g,G |
Floating point: Decimal number containing a decimal point, optionally preceeded by a sign and optionally folowed by the e or E character and a decimal number. Valid entries are -732.103 or 7.12e4 |
float * |
|
O |
Octal integer. |
int * |
|
S |
String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are blank, newline and tab). |
char * |
|
U |
Unsigned decimal integer. |
unsigned int * |
|
X |
Hexadecimal integer. |
int * |
Example.
/* scanf example */#include <stdio.h> int main (){ char str [80]; int i; printf ("Enter your surname: "); scanf ("%s",str); printf ("Enter your age: "); scanf ("%d",&i); printf ("Mr. %s , %d years old.\n",str,i); printf ("Enter a hexadecimal number: "); scanf ("%x",&i); printf ("You have entered %#x (%d).\n",i,i); return 0;}
Q2 (b) A compound statement (also called a block) typically appears as the body of another
statement, such as the if statement.
if(a[i] == target)
{
printf(“found target at %d”, i);
exit(0);
}
An arithmetic expression like:
x = y + z;
Can be included in a compound statement as:
{ .........x = y + z;
.........}
Q2 (c) The increment operator provides a short hand way to add one to an object. The
operator may be used either prefix or postfix. See examples.
int count;
int i = 5;
// Prefix use
count = ++i;
The above expression first adds one to i, so i = 6. Then the value of i is assigned to count. After the expression has executed, count = 6 and i = 6.
//Postfix use
count = i++;
The above expression first assigns the value of i to count. So, count = 5. Then i is incremented. After the expression has executed, count = 5 and i = 6.
The decrement operator provides a short hand way to subtract one from an object. The operator may be used either prefix or postfix. See examples.
int count;
int i = 5;
// Prefix use
count = --i;
The above expression first subtract one to i, so i = 4. Then the value of i is assigned to count. count = 4. After the expression has executed, count = 4 and i = 4.
//Postfix use
count = i--;
The above expression first assigns the value of i to count. So, count = 5. Then i is decremented. After the expression has executed, count = 5 and i = 4.
Q3 (a) Any function in a C program can be called recursively; that is, it can call itself. The
number of recursive calls is limited to the size of the stack. Each time the function is called, new storage is allocated for the parameters and for the auto and register variables so that their values in previous, unfinished calls are not overwritten. Parameters are only directly accessible to the instance of the function in which they are created. Previous parameters are not directly accessible to ensuing instances of the function.
Note that variables declared with static storage do not require new storage with each recursive call. Their storage exists for the lifetime of the program. Each reference to such a variable accesses the same storage area.
Difference
The problems solved by recursive solutions can also be solved iteratively. Generally, an iterative solution is preferred as it will be more efficient. With recursive solutions, there is overhead associated with each function call and new variables must be created on the stack.
Q3(b) Program to generate fibonacci numbers to n
#include "stdio.h"
#include "conio.h"
int main()
{
int a=1,b=1,y=1,l,x;
printf("enter the limit of the list");
scanf("%d",&l);
printf("%d\n",a);
for(x=0;x<=l;x++)
{
printf("%d\n",y);
y=a+b;
b=a;
a=y;
}
getch();
return 0;
}
Q4 (a) (i) We can use the break statement in switch statements and while, for, or do...while loops.
Executing the break statement exits from the current loop or statement, and begins execution with the statement following the loop or switch statement. The following example illustrates the use of the break statement:
for(I=1;I<100;I++) { if(I==50) { break; } printf(“%d \n”, I); } The output of the above code is 1,2 ..... 49.
Q4 (a) (ii)
Executing the continue statement stops the current iteration of the loop and continues program flow with the beginning of the loop. This has the following effects on the different types of loops:
· while and do...while loops test their condition, and if true, execute the loop again.
· for loops execute their increment expression, and if the test expression is true, execute the loop again.
The following example illustrates the use of the continue statement:
i = 1;while(i<100){ i++; if(i<50) continue; printf(“%d \n”, i);}
The output of the above code is :
50
51
......
......
......
99
Q4 (a) (iii)
goto name;
. . .
name: statement
The goto keyword transfers control directly to the statement specified by the label name.
Example
In this example, a goto
statement transfers control to the point labeled stop when i equals 5.
// Example of the goto statement for ( i = 0; i < 10; i++ ) { printf( "Outer loop executing. i = %d\n", i ); for ( j = 0; j < 3; j++ ) { printf( " Inner loop executing. j = %d\n", j ); if ( i == 5 ) goto stop; } } /* This message does not print: */ printf( "Loop exited. i = %d\n", i ); stop: printf( "Jumped to stop. i = %d\n", i );
Q4(b) In addition to automatic and static variables, C has a third type of local variable called
a register variable. Register variables have the same lifespan as automatic variables.
They live for only the duration of a function call. But rather than being created on the
stack like automatic variables, the keyword register instructs the compiler to try to
store these variables in hardware registers that are part of the CPU. These registers
have faster access times than the stack, which resides in memory. Frequently used
variables would be stored in registers to speed program execution. The compiler
automatically converts register variable into automatic type if register is not available.
Q4 (c) If n=1 x = 2
Y = 0
If n=0 x = 1
Y = 0
Q 5 (a) Array indexing.
Individual elements of an array may be referenced by giving the name of the array followed by the subscripts in square brackets. Each subscript must be expressed as a non-negative integer. The subscript is also known as array indexe. The value of index can be expressed as an integer constant, an integer variable or a more complex integer expression, e.g. a[i], b[0][1] etc. where a and b are 1-D and 2-D arrays respectively.
Rules: - To pass an array to a function, the array name must appear by itself, without brackets or subscripts, as an actual argument within the function call. The corresponding formal argument is writen in the same manner, though it must be declared as an array with the formal argument declarations. When declaring a one-dimensional array as a formal argument, the array name is written with a pair of empty square brackets. The size of the array is not specified within the formal argument declaration. However, for multi-dimensional arrays, in the formal argument declarations, the size of all the subscripts except the first has to be specified.
Q 5 (b)
#include "stdio.h"
#include "conio.h"
int main()
{
int a,b,c,d,m,n,x1,x2;
printf("Enter the values of a,b,c,d,m,n");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
scanf("%d",&m);
scanf("%d",&n);
if((a*d-b*c)==0)
{
printf ("The values of a,b,c,d are invalid.Enter other values after running again.");
getch();
return 0;
}
x1=(m*d-b*n)/(a*d-b*c);
x2=(n*a-m*c)/(a*d-c*b);
printf("The solutin is x1 = %d and x2 = %d",x1,x2);
getch();
return 0;
}
Q 6 (a)
The output will be as:
_6.70,_22.4,22.445
where _ represents a blank.
Q 6 (b)
#include "conio.h"
#include "stdio.h"
int main()
{
int choice;
printf("Enter choice 1 for perimeter of circle\n");
printf("Enter choice 2 for perimeter of rectangle\n");
printf("Enter choice 3 for perimeter of triangle\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
float rad, per;
printf("\nEnter the radius of circle");
scanf("%f",&rad);
per=(2*22*rad)/7;
printf("\nThe perimeter of circle is: %f",per);
break;
}
case 2:
{
int b,l,per;
printf("\nEnter the breadth of rectangle:\t");
scanf("%d",&b);
printf("\nEnter the length of rectangle:\t");
scanf("%d",&l);
per = 2*(l+b);
printf("\nThe perimeter of rectangle is: %d",per);
break;
}
case 3:
{
int a,b,c,per;
printf("\nEnter the sides of the triangle\n");
printf("\nEnter the first side:\t");
scanf("%d",&a);
printf("\nEnter the second side:\t");
scanf("%d",&b);
printf("\nEnter the third side:\t");
scanf("%d",&c);
per=a+b+c;
printf("\nThe perimeter of triangle is: %d",per);
break;
}
default:
{
printf("Wrong choice");
}
}
getch();
return 0;
}
Q 6 (c) Function Declarations and Definitions
Function declarations establish the name of the function, its return type, and the type and number of its formal parameters. A function definition includes the function body.
Function declarations can appear inside or outside a function definition. Any declaration within a function definition is said to appear at the “internal” or “local” level. A declaration outside all function definitions is said to appear at the “external,” “global,” or “file scope” level. Function definitions always occur at the external level
A function declaration specifies the name of the function, the types and number of parameters it expects to receive, and its return type. It can be written anywhere in the program but should be before the function call. If definition appears before function call in the program then it is not required to declare the function.
Q 7 (a) (i) Nested Structures
Structures can also be nested so that a valid element of a structure can also be another structure.
struct movies_t { char title [50]; int year;} struct friends_t { char name [50]; char email [50]; movies_t favourite_movie; } charlie, maria;
Q 7 (a) (ii) Arrays of Structures
The data structures needed to solve some problems are best represented as an array of structures. Consider, for instance, the problem of counting the occurrence of each letter in a string. One possible approach would be to declare two separate arrays. One would hold the letters to compare against. The second would hold a count of the occurrences of each letter. Their declarations would be:
|
#define
NUMLETTERS 26 |
This approach would work, but each letter and its count are closely related. It would be convenient and less error prone to keep this closely linked data together in a structure.
|
typedef
struct lettertype { |
Q 7 (b) #include <stdio.h>
#include <string.h> typedef struct{ int age; float salary; char fName[15]; char lName[20];}employee; void PrintInfo ( employee ); int main(){ employee Jill; Jill.age = 24; Jill.salary = 10000.50; strcpy(Jill.fName, "Jill"); strcpy(Jill.lName, "Smith"); PrintInfo( Jill ); return 0;} void PrintInfo( employee emp ){ printf("%s %s is %d years old.", emp.fName, emp.lName, emp.age); printf("\n%s's salary is %.2f.\n", emp.lName, emp.salary);}
Q 8 (a)
· In the call-by-value mechanism, if the called function modifies a parameter, it merely modifies its own copy. Therefore, such modifications are not "visible" to the caller function, in the sense that the caller's copy of that datum is unaffected.
void
swap(int x, int y);
int main()
{
int x = 4;
int y = 2;
printf("Before swap, x is %d, y is
%d\n",x,y);
swap(x,y);
printf("After swap, x is %d, y is %d\n",x,y);
}
void swap(int first, int second)
{
int temp;
temp = second;
second = first;
first = temp;
}
The output will be Before Swap, x is 4 y is 2
After Swap x is 4 y is 2
· In the call-by-reference mechanism, the caller function passes a reference to each parameter. This means the caller and the called function are both manipulating the same datum, so when such a subprogram modifies one of its parameters, the modification is visible in caller function as well.
void
swap(int *x, int *y);
int main()
{
int x = 4;
int y = 2;
printf("Before swap, x is %d, y is
%d\n",x,y);
swap(&x,&y);
printf("After swap, x is %d, y is %d\n",x,y);
return 0;
}
void swap(int *first, int *second)
{
int temp;
temp = *second;
*second = *first;
*first = temp;
}
The output will be Before Swap, x is 4 y is 2
After Swap x is 2 y is 4
Q 8 (b) void RemoveChar( char str[], char ch )
{
int j, i;
char * p1;
p1 = (char * ) malloc(strlen(str));
for( i=0, j=0; i < strlen(str); i++)
{
if( str[i] != ch)
p1[j++] = str[i];
}
p1[j] = '\0';
strcpy(str, p1);
free(p1);
}
Q 9 (a) (i) *ptr = (int *) malloc (m , sizeof ( int j ) );
· ptr should not be prefixed with * as memory address will be returned by the malloc and not the value.
· malloc() function does take only one parameter rather two i.e. number of bytes to be allocated.
(ii) table = (float *) calloc (100);
· calloc() takes two parameters 1) Number of elements, and 2) Length in bytes of each element.
(iii) Node = free ( ptr ) ;
· free() does not return anything.
Q 9 (b)
#include "stdio.h"
#include "conio.h"
#include "math.h"
void reverseArr(int[]);
int reverseNum(int);
int len;
int main(int argc, char* argv[])
{
int arr[30],x,n;
printf("enter the size of the array");
scanf("%d",&len);
printf("Enter the numbers in the array.");
for(x=0;x<len;x++)
{
scanf("%d",&n);
arr[x]=n;
}
reverseArr(arr);
getch();
return 0;
}
void reverseArr(int passedArr[])
{
int a[30],i,j,k;
for(i=0;i<len;i++)
{
int j=reverseNum(passedArr[len-i-1]);
a[i]=j;
}
for(k=0;k<len;k++)
{
printf("\n%d",a[k]);
}
}
int reverseNum(int num)
{
int n1,x=0,l=0,y=0,result=0,a;
int res[5];
while(num!=0)
{
n1=num%10;
//res=res+(10^x)*n1;
res[x]=n1;
x++;
num=num/10;
}
y=x-1;
for (a=0;a<x;a++)
{
result=result+((pow(10,y)) * (res[a]));
y--;
}
return result;
}
Q 10 (a) The array implementation has one serious drawback that we must know the maximum number of items in our collection when we create it. This presents problems in programs in which this maximum number cannot be predicted accurately
when the program starts up. Fortunately, we can use a structure called a linked list to overcome this limitation.
The linked list is a very flexible dynamic data structure as items may be added to it or deleted from it at will. A programmer need not worry about how many items a program will have to accommodate: this allows us to write robust programs which require much less maintenance. A very common source of problems in program maintenance is the need to increase the capacity of a program to handle larger collections
Q 10 (b)
/* reverse the list while traversing and then print the list*/
void reverseandprint(struct node *head)
{
struct node *p,*q,*t,;
q = head;
p = head->next;
if(q!=NULL)
q->next = NULL;
while(p!=NULL)
{
t = p->next;
p->next = q;
q = p;
p = t;
}
while (q!=NULL)
{
printf(“%d”, q->num);
q = q->next;
}
return;
}
Q 11 (a) (i) Synthetic errors: These are the errors that are induced knowingly into the program code while testing or debugging to study effect of these errors over the program flow. These errors generally induced while doing stress-testing techniques.
Semantic errors: Semantic errors arise when program code is formulated in full accordance with the syntax rules, but do not reflect the programmer's intentions correctly. Some semantic errors, e.g. incorrect references to objects can be detected early but other semantic errors will not become apparent until run-time.
Q 11 (a) (ii) Run-Time Errors are those that appear only after you compile and run your code. These involve code that may appear to be correct in that it has no syntax errors, but that will not execute. For example, you might correctly write a line of code to open a file. But if the file is corrupted, the application cannot carry out the Open function, and it stops running. You can fix most run-time errors by rewriting the faulty code, and then recompiling and rerunning it.
Q 11 (a) (iii) Run-Time Errors are those that appear only after you compile and run your
code. These involve code that may appear to be correct in that it has no syntax errors, but that will not execute. For example, you might correctly write a line of code to open a file. But if the file is corrupted, the application cannot carry out the Open function, and it stops running. You can fix most run-time errors by rewriting the faulty code, and then recompiling and rerunning it.
Latent errors: Once a program passes all the tests in its test suite, testing no longer leads programmers to errors. However, the program is still likely to contain latent errors i.e. those errors that are could not be revealed even after the execution of possibly all test cases and it may be difficult to generate new test cases that reveal additional faults. These errors are remain alive at a corner and get activated at a particular condition that is not fired during testing.
Q 11 (a) (iv) Debugging is a methodical process of finding and reducing the number of
bugs, or defects, in a computer program or a piece of electronic hardware thus making it behave as expected. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge in another
The basic steps in debugging are:
Software testing is a process used to help identify the correctness, completeness and quality of developed computer software. With that in mind, testing can never completely establish the correctness of computer software. Only the process of formal verification can prove that there are no defects.
One definition of testing is "the process of questioning a product in order to evaluate it," where the "questions" are things the tester tries to do with the product, and the product answers with its behavior in reaction to the probing of the tester. Although most of the intellectual processes of testing are nearly identical to that of review or inspection, the word testing is connoted to mean the dynamic analysis of the product-- putting the product through its paces.
Q 11 (a) (v) Compiler testing and runtime testing when a new compiler is designed for a high level language (HLL), it is tested to see whether it translates the HLL program properly or not, is compiler testing.
When a program is executed and checked for errors at runtime is runtime testing.
Q 11 (b) The character string mode specifies the type of access requested for the file, as
follows:
"r" Opens a file for reading. If the file does not exist or cannot be found, the fopen returns NULL.
"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a" Opens a file for writing at the end of the file (appending); creates the file first if it doesn’t exist.
"r+" Opens for both reading and writing.
"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+" Opens for reading and appending; creates the file first if it doesn’t exist.
Q1 (a) (A)
0
(b) (C) b
(c) (A) 2
(d) (C) Dennis Ritchie
(e) (B) a compiler
(f) (A) multi-word strings
(g) (C) to terminate the execution of the program
(h) (A) 0
Q 2 (a) A ternary operation is any operation of arity three, that is, that takes three arguments.
In the C programming language, the "?:" operator is a ternary operator.
It takes in a boolean value, and two statements, and returns the return value of the first statement if the boolean value is true, and the return value of the second statement if the boolean value is false.
For example, the statement z = (x > y) ? x : y; assigns x to z if the value x is greater than y, and otherwise assigns y to z.
Q 2 (b) Variable
A variable is used to hold data within the program. A variable represents a location in the computer's memory. Every variable has two parts, a name and a data type.
Example:
int
idnumber;
int transaction_number;
Keyword
Keywords are the
words whose meanings have been fixed and these meanings cannot be changed.
They serve as basic building blocks for program statements. E.g.: if, else,
int, long, etc.
Constant
A constant is similar to a variable in the sense that it represents a memory location. It differs in that it cannot be reassigned a new value after initialization. There are three techniques used to define constants in C.
First, constants may be defined using the preprocessor directive #define. #if/#endif,
#define pi 3.1415 |
The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415; |
The third technique to declare constants is called enumeration. An enumeration defines a set of constants. In C an enumerator is of type int.
|
#include <stdio.h> |
Q 2 (c)
void main()
{
int j = 10;
if ( (j % 2) == 0 )
{
printf(“\nThe given integer is even.”);
}
else
{
printf(“\nThe given integer is odd.”);
}
}
Q 3 (a)
void main()
{
int x, y;
x = 10;
y = 20;
x = x + y;
y = x - y;
x = x - y;
}
Q 3 (b)
#include "stdio.h"
#include "conio.h"
int main(int argc, char* argv[])
{
int a=1,b=1,y=1,l,x;
printf("enter the limit of the list");
scanf("%d",&l);
printf("%d\n",a);
for(x=0;x<=l;x++)
{
printf("%d\n",y);
y=a+b;
b=a;
a=y;
}
getch();
return 0;
}
Q 4 (a) Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type, for
example float, char, int, pointer to float, pointer to int, a structure or function.
The elements of an array are stored sequentially in memory. This allows convenient and powerful manipulation of array elements using pointers.
Defining Arrays
An array is defined with this syntax.
|
datatype arrayName[size]; |
· In the call-by-value mechanism, if the called function modifies a parameter, it merely modifies its own copy. Therefore, such modifications are not "visible" to the caller function, in the sense that the caller's copy of that datum is unaffected.
void
swap(int x, int y);
int main()
{
int x = 4;
int y = 2;
printf("Before swap, x is %d, y is
%d\n",x,y);
swap(x,y);
printf("After swap, x is %d, y is %d\n",x,y);
}
void swap(int first, int second)
{
int temp;
temp = second;
second = first;
first = temp;
}
The output will be Before Swap, x is 4 y is 2
After Swap x is 4 y is 2
· In the call-by-reference mechanism, the caller passes a reference to each parameter. This means the caller and the called function are both manipulating the same datum, so when such a subprogram modifies one of its parameters, the modification is visible in caller function as well.
void
swap(int *x, int *y);
int main()
{
int x = 4;
int y = 2;
printf("Before swap, x is %d, y is
%d\n",x,y);
swap(&x,&y);
printf("After swap, x is %d, y is %d\n",x,y);
return 0;
}
void swap(int *first, int *second)
{
int temp;
temp = *second;
*second = *first;
*first = temp;
}
The output will be Before Swap, x is 4 y is 2
After Swap x is 2 y is 4
Q 4 (b) (i) In the Top-Down Model an overview of the system is formulated, without going
into detail for any part of it. Each part of the system is then refined by designing it in more detail. Each new part may then be refined again, defining it in yet more detail until the entire specification is detailed enough to begin development.
Top-down approaches emphasise planning and a complete understanding of the system. It is inherent that no coding can begin until a sufficient level of detail has been reached in the design of at least some part of the system. This, however, delays testing of the ultimate functional units of a system until significant design is complete.
Q 4 (b) (ii) In bottom-up design individual parts of the system are specified in detail, and
may even be coded. The parts are then linked together to form larger components, which are in turn linked until a complete system is formed.
Bottom-up emphasises coding and early testing, which can begin as soon as the first module has been specified. This approach, however, runs the risk that modules may be coded without having a clear idea of how they link to other parts of the system, and that such linking may not be as easy as first thought.
Q 5 (a)
#include "stdio.h"
#include "conio.h"
void main()
{
int j,i;
printf(“Enter the number for which table is required”);
scanf(“%d”m, &i);
for ( int j = 1 ; j<= 10 ; j++ )
{
printf("\n %d * %d = %d ", i, j, i*j );
}
getch();
}
Q 5 (b)
int
factorial(int n)
{
if (n > 1) {
return n * factorial(n - 1);
}
else {
return 1;
}
}
int main()
{
int n, fact;
printf(“Enter
a number”);
scanf(“%d”, &n);
fact = factorial(n);
printf(“%d! = %d”,n, fact);
return 0;
}
Q 6 (a) Pointers are variables that hold addresses in C using ‘&’ operator, the address of a variable is assigned to a pointer. The ‘*’ operator is used both to create a pointer and
to access data pointed to by a pointer e.g. the statements:
int
x;
int *ptr = &x;
can be visualized a below:
![]()
varible prt x
value 1010 20
address 1000 1010
int
add(int *m, int *n);
int main()
{
int result, x=3, y=5;
int *ptx = x;
int *pty = y;
result
= add (ptx,pty);
return 0;
}
int add(int *x, int *y)
{
return *x + *y;
}
Q 6 (b) Arrays
Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type, for example float, char, int, pointer to float, pointer to int, a structure or function.
The elements of an array are stored sequentially in memory. This allows convenient and powerful manipulation of array elements using pointers.
Defining Arrays
An array is defined with this syntax.
|
datatype arrayName[size]; |
Structures
A structure provides a means of grouping variables of different datatypes under a single name for easier handling and identification. Complex hierarchies can be created by nesting structures. Structures may be copied to and assigned. They are also useful in passing groups of logically related data into functions.
Structure definition for a student information
|
struct
point { |
Q 7 (i) Basis path testing is a hybrid between path testing and branch testing:
Path Testing: Testing designed to execute all or selected paths through a computer program
Branch Testing: Testing designed to execute each outcome of each decision point in a computer program
Basis Path Testing: Testing that fulfills the requirements of branch testing & also tests all of the independent paths that could be used to construct any arbitrary path through the computer program
1: Draw a control flow graph
Any procedural design can be translated into a control flow graph:
2: Calculate Cyclomatic complexity
The second step in basis path testing is to calculate the Cyclomatic Complexity complexity from the control flow graph. McCabe’s Cyclomatic Complexity is used to determine the minimum number of tests that must be executed for complete basis path coverage.
Cyclomatic Complexity is calculated from a control flow graph by subtracting the number of nodes from the number of edges and adding 2 times the number of unconnected parts of the graph.
3: Choose a “basis set” of paths
Choose a set of basis paths – Determining the predicate nodes can help identify a set of basis paths. If test cases can be designed to cover the basis path set, it will result in complete decision (and statement) coverage of the code. Each basis path that you select must in effect test at least one new untested edge, in other words it must traverse at least one new edge. Otherwise it is considered a redundant path and does not belong in the basis set. This aids in eliminating redundant testing and ensures validity of each test case. For each subsequent basis path selected try to keep the number of new edges added as low as possible – progressing on until you have covered all possible basis paths.
4: Generate test cases to exercise each path
Generate test cases that will force execution of each path in the basis set
Q 7 (ii) Black box testing
Black box testing, concrete box or functional testing is used in computer programming, software engineering and software testing to check that the outputs of a program, given certain inputs, conform to the functional specification of the program.
The term black box indicates that the internal implementation of the program being executed is not examined by the tester. For this reason black box testing is not normally carried out by the programmer. In most real-world engineering firms, one group does design work while a separate group does the testing.
Various types of black box testing techniques are
A technique in black box testing is equivalence partitioning. Equivalence partitioning is designed to minimize the number of test cases by dividing tests in such a way that the system is expected to act the same way for all tests of each equivalence partition. Test inputs would be selected from each partition.
Equivalence partitions are designed so that every possible input belongs to one and only one equivalence partition.
Boundary value analysis is a technique of Black box testing in which input values at the boundaries of the input domain are tested. It has been widely recognized that input values at the extreme ends of, and just outside of, input domains tend to cause errors in system functionality.
Fuzz testing uses software tools to generate random data for the inputs of software. It's quite useful because it requires very little test design, and can locate unexpected errors, especially in input-handling.
A sub-set of the black box test is the smoke test. A smoke test is a cursory examination of all of the basic components of a software system to ensure that they work. Typically, smoke testing is conducted immediately after a software build is made. The term comes from electrical engineering, where in order to test electronic equipment, power is applied and the tester ensures that the product does not spark or smoke.
Q 8 (a) It is the problem of taking an arbitrary permutation of n items and rearranging them
into the total order,
Xi >= Xj <=====> i >= j Descending order
or
Xi <= Xj <=====> i <= j Ascending order
Sorting is, without doubt, the most fundamental algorithmic problem
/* Bubble sort for integers */#define SWAP(a,b) { int t; t=a; a=b; b=t; } void bubble( int a[], int n )/* Pre-condition: a contains n items to be sorted */ { int i, j; /* Make n passes through the array */ for(i=0;i<n;i++) { /* From the first element to the end of the unsorted section */ for(j=1;j<(n-i);j++) { /* If adjacent items are out of order, swap them */ if( a[j-1]>a[j] ) SWAP(a[j-1],a[j]); } } }
Q 8 (b) int max( int a[], int n )
{
int i, j;
j = a[0];
for(i=1;i<n;i++)
{
if( a[i] > j ) j = a[i];
}
return j;
}
Q 9 (a) void stringcopy( char sr[], char d[] )
{
int i,j;
for ( i=0 ; sr[i] != '\0' ; i++)
{
d[i] = sr[i];
}
d[i] = '\0';
}
Q 9 (b) int wordcount( char sr[])
{
int i,j = 0;
for ( i=1; sr[i] != '\0' ; i++)
{
if (sr[i] == ' ' && sr[i-1] != ' ') j++;
}
return j;
}
Q 10 (a) int nodecount( list *head)
{
int j = 0;
list *temp;
if (head == NULL )
{
printf("List does not exist");
return 0;
}
temp = head;
for ( j = 1 ; temp->next != NULL ; j++ )
{
temp = temp->next;
}
return j;
}
Q 10 (b) void reverseArray(char a[])
{
int i,j;
char c;
for(i=0, j=strlen(a)-1; i<j; i++,j--)
{
c = a[i];
a[i] = a[j];
a[j] = c;
}
return;
}
Q 11 (a) void RemoveBlankLines( )
{
FILE *fpread, *fpwrite;
int len, blankline = 0;
char c[100];
char *p = 0;
fpread = fopen("C:\\MyFile.txt", "r+");
fpwrite = fopen("c:\\temp.txt", "w+");
if(fpread == NULL || fpwrite == NULL)
{
printf(“Not able to access data”);
exit(1);
}
while ( (p = fgets( c, 100, fpread )) != NULL )
{
len = strlen(c);
if ( len > 1 )
{
fputs(c, fpwrite);
}
}
fclose(fpread);
fclose(fpwrite);
remove("C:\\MyFile.txt");
rename("C:\\temp.txt", "C:\\MyFile.txt");
}
Q 11 (b) void RemovePunctuations( )
{
FILE *fpread, *fpwrite;
char p;
fpread = fopen("C:\\FirstFile.txt", "r+");
fpwrite = fopen("c:\\SecondFile.txt", "w+");
if(fpread == NULL || fpwrite == NULL)
{
printf(“Not able to access data”);
exit(1);
}
while ( (p = fgetc( fpread )) != -1 )
{
if ( (p != ‘,’) && (p != ‘.’) && (p != ‘;’) )
fputc(p, fpwrite);
}
fclose(fpread);
fclose(fpwrite);
}
Q1 (a) (B) yields a Boolean result
(b) (C) in each statement with in the loop body
(c) (D) None of these
(d) (C) a structure variable
(e) (D) a header file
(f) (C) Contents of zzz.c
(g) All are valid variable names – Answer is not in objectives
(h) (C) 10
Q 2 (a) #include "stdio.h"
#include "conio.h"
int main(int argc, char* argv[])
{
float i,r;
printf("Enter the value in inches:");
scanf("%f",&i);
r=i*(100/39.37);
printf("The number of centimeters in %f inches is %f",i,r);
getch();
return 0;
}
Q 2 (b) void CountVowels( )
{
FILE *fpread;
char p;
int count = 0;
fpread = fopen("C:\\FirstFile.txt", "r+");
if(fpread == NULL)
{
printf(“Not able to open input file.”);
exit(1);
}
while ( (p = fgetc( fpread )) != -1 )
{
if ( (p == ‘A’)||(p == ‘E’ )||(p == ‘I’ )||(p == ‘O’)||(p == ‘U’)|| \
(p == ‘a’)||(p == ‘e’)||(p == ‘i’) || (p == ‘o’)||(p == ‘u’) )
{
count++;
}
}
printf(“No of vowels = “, count);
fclose(fpread);
}
Q 3 (a) while
Execution of a while loop proceeds as follows:
If expression is true (nonzero), the body of the statement is executed and the process is repeated beginning at step 1.
do – while
The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.
Execution of a do-while loop proceeds as follows:
For example:
i = 10;
while(i<10)
printf(“in the while loop”);
do
{
printf(“in do-while loop”);
}
while (i<10);
The output of this code will be:
in do-while loop
Q 3 (b) void main()
{
int i;
printf("\n");
for ( i = 1 ; i <= 100 ; i++ )
{
if ( (i%2) == 0 )
printf("%d,\t", i );
}
getch();
}
Q 3 (c) The output of the program will be : 0
Q 4 (a) #include"stdio.h"
#include"conio.h"
#include"math.h"
int main(int argc, char* argv[])
{
int a,b,c;
float x1, x2;
printf("Enter the values of a,b and c");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
x1=( -b + pow( ( pow( b, 2 ) - (float) 4*a*c ) , ( (float) 1/2 ) ) ) / ( (float) 2*a );
x2=( -b - pow( ( pow( b, 2 ) - (float) 4*a*c ) , ( (float) 1/2 ) ) ) / ( (float) 2*a );
printf("The roots of quadratic equation %dx2+%dx+%d are:\n",a,b,c);
printf("%f\t%f",x1,x2);
getch();
return 0;
}
Q 4 (b) A function (subroutine, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more
statement blocks; such code is sometimes collected into software libraries. Subroutines can be "called", thus allowing programs to access the subroutine repeatedly without the subroutine's code having been written more than once. There are numerous motivations for the use of subprograms:
Generally, to make use of a subprogram, a programmer places some form of call instruction--which constitutes a call site--into an instruction sequence. When the call site is encountered, the instruction sequence is temporarily suspended, and the subprogram itself executes until it completes, at which time the original instruction sequence resumes.
Q 5 (a) #include "stdio.h"
#include "conio.h"
int series(int n)
{
int a=0, b=0, x,y;
for(x=1; x<= n, x++)
{
for(y=1; y<=x; y++)
{
a=a+y;
}
b = b+a;
a=0;
}
return b;
}
int main()
{
int n;
printf("Enter the no. of characters you want in the series");
scanf("%d",&n);
n = series(n)
printf("The sum of the series is :\t%d",n);
getch();
return 0;
}
Q 5 (b)
· In the call-by-value mechanism, if the called function modifies a parameter, it merely modifies its own copy. Therefore, such modifications are not "visible" to the caller function, in the sense that the caller's copy of that datum is unaffected.
· In the call-by-reference mechanism, the caller function passes a reference to each parameter. This means the caller and the called function are both manipulating the same datum, so when such a subprogram modifies one of its parameters, the modification is visible in caller's function as well.
Q 6 (a) Top-down programming is a programming style characterized by the general flow of the project. The project starts with an often complex description of the goals of the
project, and then starts breaking it down into smaller tasks.
Advantages :- Programming team stays focused on the goal, and everyone knows his or her job. By the time the programming starts, there are no questions. Code is easier to follow, since it is written methodically and with purpose.
Q 6(b) In C when we define a pointer variable we do so by preceding its name with an
asterisk. For example, consider the variable declaration:
int *ptr;
The '*' informs the compiler to set aside space required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer.
Suppose now that we want to store in ptr the address of our integer variable k. To do this we use the unary & operator and write: ptr = &k. Now, ptr is said to "point to" k.
The "dereferencing operator" is the asterisk and it is used as follows:
*ptr = 7;
will copy 7 to the address pointed to by ptr. When we use the '*' this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.
Q 6 (c)Logical Operators
|
Operator |
Description |
|
&& |
AND |
Returns true if and only if both the operands are True else it returns false. |
|
|| |
OR |
Returns true if either of the operands is true else it returns false. |
|
! |
NOT |
Returns true if the operand is false and false if the operand is true |
Q 6 (d) In the design phase the architecture is established. This phase starts with the
requirement document delivered by the requirement phase and maps the requirements into an architecture. The architecture defines the components, their interfaces and behaviors. The deliverable design document is the architecture. The design document describes a plan to implement the requirements. This phase represents the ``how'' phase. Details on computer programming languages and environments, machines, packages, application architecture, distributed architecture layering, memory size, platform, algorithms, data structures, global type definitions, interfaces, and many other engineering details are established.
Software testing is a process used to help identify the correctness, completeness and quality of developed computer software.
In many software engineering methodologies, the testing phase is a separate phase which is performed by a different team after the implementation is completed. There is merit in this approach; it is hard to see one's own mistakes, and a fresh eye can discover obvious errors much faster than the person who has read and re-read the material many times.
Q 7 (a) A union is a data structure that can hold objects of different types and sizes at different times. The compiler allocates sufficient space to hold the largest object. It is necessary to keep track of which data type is currently stored in order to manipulate it properly.
Example:
union config_value {
int *ival;
float *fval;
double *dval;
char *sval;
}
Any of these data types could be stored in the same location in memory, that of the union. The space required for the above union is 2 bytes.
In a structure, the space is allocated to every member. Thus the space occupied by a structure is sum of the space requirement of every member. For example, structure point defined below required 4 bytes of storage space.
struct
point {
int x;
int y;
};
Q 7 (b) struct book
{
char bookTitle[30];
char author[30];
float price;
int edition;
int yearOfPublication;
};
Q 7 (c) Definition of own types (typedef).
C allows us to define our own types based on other existing data types. In order to do that we shall use keyword typedef, whose form is:
typedef existing_type new_type_name ;
where existing_type is a C fundamental or any other defined type and new_type_name is the name that the new type will receive. For example:
typedef char C;
typedef unsigned int WORD;
typedef char * string_t;
typedef char field [50];
The user defined data types improve readability of the program.
Q 8 (a) int gcdr(int m, int n)
{
if (m == 0)
return(n);
if (n == 0)
return(m);
return(gcdr1(n, m % n));
}
int gcdr1(int m, int n)
{
int rem;
if (m == 0 && n == 0)
return(-1);
if (m < 0) m = -m;
if (n < 0) n = -n;
return(gcdr(m, n));
}
Q 8 (b) void mat_mul(int a[][10], int b[][10], int n, int m, int p, int c[][10])
{
int i,j,k;
for(i=0; i<n;i++)
{
for(j=0;j<p;j++)
{
c[i][j] = 0;
for(k=0;k<m;k++)
{
c[i][j] += a[i][k]*b[k][j];
}
}
}
return;
}
Q 9 Insertion of node in ascending order:
void insertNode(XYZ *newNode)
{
XYZ *temp, *back;
temp = NULL;
if (head == NULL)
{
head = newNode;
return;
}
if( head->num >= newNode->num)
{
newNode->next =head;
head = newNode;
return;
}
back = temp = head;
for(; temp!=NULL && temp->num < newNode->num ; back=temp,temp=temp->next );
newNode->next = temp;
back->next = newNode;
}
Deletion of Node:
void deleteNode(int num)
{
XYZ *temp,*back;
temp = NULL;
if (head == NULL)
{
printf("\nList does not exist");
return;
}
if( head->num == num)
{
temp = head;
head = temp->next;
free(temp);
return;
}
back = temp = head;
while(temp != NULL && temp->num < num )
{
back = temp;
temp = temp->next;
}
if(temp == NULL)
printf(“Node not found”);
else if (temp-> num==num)
{
back->next = temp->next;
free(temp);
}
}
Q 10 (a) (i)
scanf()
int scanf ( const char * format [ , argument , ...] );
Read formatted data from
stdin.
Reads data from the standard input (stdin) and stores it into the locations
given by argument(s). Locations pointed by each argument are
filled with their corresponding type of value requested in the format
string.
There must be the same number of type specifiers in format string and arguments
passed.
Q 10 (a) (ii)
fgets()
char * fgets (char * string , int num , FILE * stream);
Get a string from a
stream.
Reads characters from stream and stores them in string until (num
-1) characters have been read or a newline or EOF character is reached,
whichever comes first.
A newline character ends reading but is considered a valid character and
included in the new string.
A null character is always appended at the end of the resulting string.
Q 10 (a) (ii)
getch()
The getch function reads a single character from the console without echoing. There is no need to press enter key after pressing the character key.
Q 10 (a) (iv)
fopen()
FILE * fopen (const char * filename, const char * mode);
Open a file.
Opens the file which name is stored in the filename string and returns a
pointer to the file (stream). Operations allowed to the file returned are
defined by the mode parameter.
Q 10 (b) The character string mode specifies the type of access requested for the file, as follows:
"r" Opens a file for reading. If the file does not exist or cannot be found, the fopen returns NULL.
"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a" Opens a file for writing at the end of the file (appending); creates the file first if it doesn’t exist.
"r+" Opens for both reading and writing.
"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+" Opens for reading and appending; creates the file first if it doesn’t exist.
Q 10 (c) An interpreter is a computer program that converts a line of code into machine code and then executes it. This is repeated for every lime of a program
A compiler is a computer program that translates a computer program written in one computer language (called the source language) into an equivalent program written in another computer language (called the output, object, or target language) for later execution
Q 11 (a) void CountVowels( )
{
FILE *fpread;
char p;
int count = 0;
fpread = fopen("C:\\FirstFile.txt", "r+");
if(fpread == NULL)
{
printf(“File does not exist”);
exit(1);
}
while ( (p = fgetc( fpread )) != -1 )
{
if ( (p == ‘A’)||(p == ‘E’ )||(p == ‘I’ )||(p == ‘O’)||(p == ‘U’)|| \
(p == ‘a’)||(p == ‘e’)||(p == ‘i’) || (p == ‘o’)||(p == ‘u’) )
{
count++;
}
}
printf(“No of vowels = “, count);
fclose(fpread);
}
Q 11 (b) typedef struct
{
int rollno, marks;
char name[20]
};
void ReadFile()
{
ABC a, b;
FILE *fp = fopen("C:\\MyFile.dat", "r+b");
if (fp == NULL)
{
printf(“File does not exist”);
exit(1);
}
fread(&a,sizeof(ABC),1,fp);
b = a;
while ( fread(&a,sizeof(ABC),1,fp) )
{
if (b.marks < a.marks )
{
b = a;
}
}
printf("\nDetails of student whose marks are highest : ");
printf("\nRoll No. : %d”, b.rollno);
printf("\nStudent Name : %s", b.name);
printf("\nMarks : %d", b.marks);
fclose(fp);
}
Q1 (a) (C) anywhere, but starting on a new line
(b) (D) printing of 3
(c) (B) 215 - 1
(d) (D) 6, 6
(e) (B) 2
(f) (D) ‘xyz’
(g) (C) 5 Address of a
(h) (A) an entire array can be passed as argument to a function.
Q 2 (a) void LeftMostDigit()
{
int num, leftMostDigit;
printf("Enter the number");
scanf("%d",&num);
while(num!=0)
{
leftMostDigit = num%10;
num = num/10;
}
printf("\nThe left most digit is : %d", leftMostDigit);
}
Q 2 (b) void RotateValues()
{
int X; int Y; int Z, temp;
printf("Enter the number");
scanf("%d%d%d",&X, &Y, &Z);
temp = X;
X = Y;
Y = Z;
Z = temp;
printf("\nRotated values are \n %d, %d, %d", X, Y, Z);
}
Q 2 (c) (i)
_256.67
where _ is a blank character
Q 2 (c) (ii)
256.666992
Q 2 (c) (iii)
2.57e+02
Q 2 (c) (iv)
2.566670e+02
Q 3 (a) (i)
#define can be used to define constants for example:
#define name value
Its function is to define a macro called name that whenever it is found in some point of the code is replaced by value. For example:
#define MAX_WIDTH 100
char str1[MAX_WIDTH];
char str2[MAX_WIDTH];
It defines two strings to store up to 100 characters.
#define can also be used to generate macro functions:
#define getmax(a,b) a>b?a:b
int x=5, y;
y = getmax(x,2);
after the execution of this code y would contain 5.
Q 3 (a) (ii)
void main(void)
The main function serves a special purpose
in C programs. When they are executed, main() is the first function called. The
portion of the statement that reads void indicates that the return value—the
value to which the main function will evaluate—is an void i.e this function will
not return any value. The portion that reads (void) indicates that the main function will take no arguments
from whatever calls it.
Q 3 (a) (iii)
The getchar() returns
the next character of an input stream, as an int. If the end of file is
encountered, EOF is returned. It is part of the C standard library.
int getchar(FILE *stream);
The scanf function is part of the standard C
library and is used for formatted input. It reads from standard input, which is
usually the keyboard. It returns EOF on error or it the end of file is reached.
Otherwise, the number of items read is returned. Its prototype is:
int scanf(const char *format, arg1, arg2, .....);
Scanf reads from standard input and converts each argument as specified in the
format string. The arguments after the format string must be pointers.
Q 3 (b) void CountPersons()
{
int Age = 0, count = 0 ;
int maxPersons = 0;
while (maxPersons < 100)
{
printf("Enter the age of person number %d : ",maxPersons + 1);
scanf("%d", &Age);
if ( (Age > 40) && (Age < 60) )
{
count++;
}
maxPersons++;
}
printf("\nNumber of persons between the age 40 to 60 are : %d", count);
}
Q 3 (c) if(x == 0)
{
y = 20.5;
}
else if(x<10)
{
y = 4.8 * x – 3;
}
else
y = 3 * x + 5;
Q 4 (a) (i) In this statement closing double quotes (“) are not there. Then the statement is also not terminated.
Q 4 (a) (ii) In the scanf() function \n is not required.
Q 4 (a) (iii) Both the expressions of if clause must be enclosed as below:
if ( (p<0) || (q<0) )
Q 4 (a) (iv) There is not syntactical error but there is logical error. The while loop is infinite
as count is initialized in the loop and hence will never reach terminating condition.
Q 4 (b) void CountVowelsFromStr( )
{
char str[100], *p;
int count = 0;
printf("Enter a string : ");
gets(str);
p = str;
while ( *p != '\0' )
{
if ( (p == ‘A’)||(p == ‘E’ )||(p == ‘I’ )||(p == ‘O’)||(p == ‘U’)|| \
(p == ‘a’)||(p == ‘e’)||(p == ‘i’) || (p == ‘o’)||(p == ‘u’) )
{
count++;
}
p++;
}
printf("\nThe number of vowels in the entered string are : %d", count);
}
Q 5 (a) void LowerToUpper( )
{
char str[100], *p;
printf("Enter a string : ");
gets(str);
p = str;
while ( *p != '\0' )
{
if ( (*p >= ‘a’) && (*p <= ‘z’) )
{
*p-=32;
}
p++;
}
printf("\n");
puts(str);
}
Q 5 (b) Assuming printf() statement in the for loop have opening double quotes (“), the output will be:
1 2 3 4 5 6
x=6 k=64
Q 5 (c) void DoubleTheSpace( )
{
char str[100], pstr[200], *p, *q;
printf("Enter a string : ");
gets(str);
p = str;
q = pstr;
while ( *p != '\0' )
{
*q = *p;
q++;
*q = ' ';
q++;
p++;
}
printf("\n");
*q = 0;
puts(pstr);
}
Q 6 (a) (i) Assuming printf() statement is printf(“%d”, count); the output will be :
6543210
Q 6 (a) (ii) The loop runs infinitely.
Q 6 (a) (iii) x = 1
while ( x<10 )
{
x = x+1;
printf(“%d”, x);
}
Q 6 (a) (iv) The expression would evaluate to true.
Q 6 (b) void ExamineChar(char color)
{
switch (color)
{
case 'r':
case 'R':
printf("\nIt is Red colour");
break;
case 'g':
case 'G':
printf("\nIt is Green colour");
break;
case 'b':
case 'B':
printf("\nIt is Blue colour");
break;
default:
printf("\nIt is White colour");
break;
}
}
Q 7 (a) struct Complex
{
float real, imaginary;
};
Q 7 (b) sturct Complex cArray[100];
Q 7 (c) void ComplexArray()
{
int j;
float r,i;
printf("Enter the elements of the array.");
for(j=0;j<100;j++)
{
printf("Enter the value of x in x+iy");
scanf("%f",&cArray[j].real);
printf("Enter the value of y in x+iy");
scanf("%f",&cArray[j].imaginary);
}
for(j=0;j<100;j++)
{
sumx+=cArray[j].real;
sumy+=cArray[j].imaginary;
}
printf("The sum of all the complex numbers is :\t%f + i%f",sumx,sumy);
}
Q8 (a) (i) for
The for statement lets you repeat a statement or compound statement a specified number of times. The body of a for statement is executed zero or more times until an optional condition becomes false. You can use optional expressions within the for statement to initialize and change values during the for statement’s execution.
while
The while statement lets you repeat a statement until a specified expression becomes false.
Q8 (a) (ii) break
We can use the break statement in switch statements and while, for or do...while loops.
Executing the break statement exits from the current loop or statement, and begins script execution with the statement immediately following.
The goto keyword transfers control directly to the statement specified by the label name. It is not necessary to use goto statement within loops or switch statement only.
Q8 (a) (iii) break
We can use the break statement in switch statements and while, for or do...while loops.
Executing the break statement exits from the current loop or statement, and begins script execution with the statement immediately following.
continue
Executing the continue statement stops the current iteration of the loop and continues program flow with the beginning of the loop. This has the following effects on the different types of loops:
while and do...while loops test their condition, and if true, execute the loop again.
· for loops execute their increment expression, and if the test expression is true, execute the loop again.
Q8 (b) void main()
{
int x[30][30],i,j;
for(i=0;i<10;i++)
{
for(j=0;j<=i;j++)
{
if(j==0||j==i)
x[i][j] = 1;
else
x[i][j] = x[i-1][j-1] + x[i-1][j];
}
printf("\n");
}
for(i=0;i<10;i++)
{
for(j=0;j<=i;j++)
printf(“%d”, x[i][j]);
printf(“\n”,x[i][j];
}
getch();
}
Q 9 (a) void main()
{ int l, i, j; printf("Enter the number of rows and columns in square matrix\n"); scanf("%d",&l); printf("\n"); for(i=0;i<l;i++) { for (j=l-1;j>=0;j--) { if(j>=i) printf("1\t"); else printf("0\t"); } printf("\n"); } getch();}
Q 9 (b) (i) 21%(int)4.5 - Valid & value is 1
(5/3)*3+5%3 - Valid & value is 5
Q 9 (b) (ii) ! (5 + 5 >= 10) - evaluates to false
10 != 15 && !(10<20) || 15>30 - evaluates to false
Q 9 (b) (iii) A null statement is when a ‘;’ appears on a line without any executable statement before it. For example, consider the following for loop:
for(i=0; i<00; i++) ; // a null statement
This way a null statement can be used to cause a time delay.
Q 9 (b) (iv)
Arrays
Arrays are a data structure that is used to store a group of objects of the same type sequentially in memory. All the elements of an array must be the same data type, for example float, char, int, pointer to float, pointer to int, a structure.
Structures
A structure provides a means of grouping variables of different data types under a single name for easier handling and identification.
Q 10 (a) struct XYZ
{
char name[40];
struct stats
{
int i;
float f;
} s;
struct XYZ *next;
};
Q 10 (b)
XYZ *ptr;ptr = (XYZ*) malloc(sizeof(XYZ));
Q 10 (c) (i)
Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO statement (also known as "go to").
A technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure. Three types of control flow are used: sequential, test, and iteration.
Q 10 (c) (ii)
In the Top-Down Model an overview of the system is formulated, without going into detail for any part of it. Each part of the system is then refined by designing it in more detail. Each new part may then be refined again, defining it in yet more detail until the entire specification is detailed enough to begin development.
Top-down approaches emphasise planning and a complete understanding of the system. It is inherent that no coding can begin until a sufficient level of detail has been reached in the design of at least some part of the system. This, however, delays testing of the ultimate functional units of a system until significant design is complete.
Q 10 (c) (iii)
An interpreter is a computer program that executes other programs.
A compiler converts the program into object code in one go. This object code is later executed. An interpreter converts a line of code into object code, exectues it and repeats it for all the lines of program.
Q 11 (a) void ReadFile1()
{ int a = 20; float b = 23.23; char c = 'x'; int i; FILE *p = fopen("C:\\Sample.old", "r"); FILE *q = fopen("C:\\Sample.new", "w"); while ( (i = fscanf(p,"%d %f %c",&a,&b,&c)) > 0 ) { printf("\nValue of a : %d", a); printf("\nValue of b : %f", b); printf("\nValue of c : %c", c); fprintf(q,"%d %f %c",a,b,c); } fclose(p); fclose(q); }
Q 11 (b) (i)
Template:
When a structure definition is given as:
struct s{ int x,y;}
It simply describes a format called template as no space is allocated to members of x and y. The space is allocated only when a variable of type struct s is defined.
Q 11 (b) (ii)
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type int.
The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).
When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment. When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.
/* Example of the sizeof keyword */int i = sizeof( int );
Q 11 (b) (iii)
The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition.
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.
/* Example of the sizeof keyword */typedef unsigned long ulong;
Q 11 (b) (iv)
Float is a fundamental data type in C. Float variables are used to hold non-integer, floating point values. The floating-point type float represents the single-precision 32-bit format.