Programming Assignment 2 Hhhh

45
pa F-2,Block, Amity Campus Sec-125, Nodia (UP) India 201303 ASSIGNMENTS PROGRAM: SEMESTER-I Subject Name Study COUNTRY : Permanent Enrollment Number (PEN) : Roll Number : Student Name : INSTRUCTIONS a) Students are required to submit all three assignment sets. ASSIGNMENT DETAILS MARKS Assignment A Five Subjective Questions 10 Assignment B Subjective Questions + Case Study 10 Assignment C Objective Questions 10

Transcript of Programming Assignment 2 Hhhh

Page 1: Programming Assignment 2 Hhhh

pa

F-2,Block, Amity CampusSec-125, Nodia (UP)

India 201303

ASSIGNMENTSPROGRAM:SEMESTER-I

Subject Name Study COUNTRY : Permanent Enrollment Number (PEN) :Roll Number : Student Name :

INSTRUCTIONSa) Students are required to submit all three assignment sets.

ASSIGNMENT DETAILS MARKSAssignment A Five Subjective Questions 10Assignment B Subjective Questions + Case Study 10Assignment C Objective Questions 10

b) Total weightage given to these assignments is 30%. OR 30 Marks

c) All assignments are to be completed as typed in word/pdf.d) All questions are required to be attempted.e) All the three assignments are to be completed by due dates

(specified from time to time) and need to be submitted for evaluation by Amity University.

f) The evaluated assignment marks will be made available within six weeks. Thereafter, these will be destroyed at the end of each semester.

g) The students have to attached a scan signature in the form.

Page 2: Programming Assignment 2 Hhhh

Signature : ---------------------- Date : _______

( √ ) Tick mark in front of the assignments submittedAssignment

‘A’Assignment ‘B’

Assignment ‘C’

Programming & Problem Solving through C Language

Assignment-A

1. Evaluate the expression 2+3*3-7+10-5/4+3 Using operator precedence.

1. 2+ (3*3) -7+10- 5/4 +3

• = 2+9-7+10- (5/4) +3

• = ( 2 +9) - (7+10) - (1.25+3)

• = 11-17-4.25

• = 11 (- -) 17 (- -) 4.25

• = (11+17+4.25)

• = 32.25 ANS.

2. What are the storage classes available in c language?

Auto

Page 3: Programming Assignment 2 Hhhh

static

registers

3. Compare while and Do – While loop with examples?

While statement; is a C looping statement that allow repeated execution of statements or block of statement as long as the condition remains true ( non zero). For example in the following program While A do-while statement is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. It consists of blocks and conditions;

4. Compare break, continue, and goto statements.

Break is a statement used to stop the interaction to occur in the loop. It provides an early exit from the while or for loop.where as Continue is a statement that forces interaction to occur in the loop even if part of the body of the loop has not been executed yet. It does the opposite of break, it immediately loops again skipping the rest of the code. Where as Go to statement in c is used to transfer particular control (jump) a particular position of the program marked with the label.

5. What is pointer? Explain with example

A pointer is a way of informing a compiler that what want to store is an address. It is a memory address of a variable specifically declared to hold a pointer. For example in declaring a point we write ;

Int *k;

Int y= *k

Assignment-B

Page 4: Programming Assignment 2 Hhhh

1. a) What is structured programming? Explain and give examples of relevant constructs using pseudo-code. Highlight the advantages and disadvantages of structured programming.Structured programming is a programming that links control blocks (a function, an if statement block and others) to the scope variables.Pseudo code is basically a mnemonics language. The c program is quite different from it. Pseudo code is an artificial and informal language. It is the text based tool to write the algorithms. The algorithms of programs are mentioned in simple English.For example:

if total is greater than 75print passelseprint fail

.Advantages of pseudo code

• Easy to convert into a corresponding program language

• Consists only of executable statements• Similar to English language• Helps us “think out” a program before writing

it Disadvantages

Not visual No accepted standard, varies from company to company

b) What is an execution error? Differentiate it from syntactic error. Give examples.

An execution error is any error normally logical error that can cause the program to crash.

Page 5: Programming Assignment 2 Hhhh

It is different from syntactic error because a syntactic error occurs when the code is not formatted or typed correctly like typing If instead of if, because it recognizes lowercase.

c) It is said that ‘C’ is a middle level assembly language. Mention those features of ‘C’which enables this description.I ) C uses symbols ,letters and numbers.ii) C requires a translator such as a compiler.

2. a) Write and explain the action of WHILE statement. Develop a program in ‘C’ language to compute the average of every third integer number lying between 1 and 100. Include appropriate documentation.

The action of WHILE statement is shown where if the test condition is checked and it is found out that it is true, the statement gets executed. If the test condition is checked again and it is true, statement is executed, the cycle repeats until the test condition becomes false.

For example; while (total<=10); { Print f (“ Enter another test mark:\n”); Scan f ( “ %d ”, & test ); Print f ( “ Total of %d and %d is %d”, test, exams , Total);

Develop a program in ‘C’ language to compute the average of every third integer number lying between 1 and 100. Include appropriate documentation }/*a c program that computes the average of every third integer number lying between 1 and 100*/#include<stdio.h>main(){int x=3;while(x<=100);x =3;printf(“%d is %d”,x);

Page 6: Programming Assignment 2 Hhhh

return 0}

b) Develop a function to calculate sum of n even integers starting from a given even integer./* c program that calculates the sum of n even integers starting from a given even integer*/#include<stdio.h>main(){int n, even=0,k=4;while(k=0,k>n,k++)even=n+even;n= n+4;printf(“the sum of %d and %d is %d”,n+2,sum);return 0; }c) Identify all the compound statements which appear in the following program segment: {sum=0;do {scanf(“%d”, &i);if (i < 0){i=-i;++flag;}sum += i;} while (i != 0);}The compound statements are;

I) if II) while III) do

Page 7: Programming Assignment 2 Hhhh

3. a) Define an array. How are arrays processed in ‘C’? Illustrate by taking two-dimensional arrays as examples.

Arrays in c are processed in two dimensional arrays by using rows and columns where by the source code is made clearer by use of braces like square brackets for subscripts and braces for initialization values. For example the initialization of array k;

Int k[5][4]={1,3,4,5,6,2,1,4,5}; An array is a set of ordered data items. It is also a collection

of homogeneous elements.

b) What are subscripts? How are they specified? What restrictions apply to the values that can be assigned to subscripts in ‘C’ language?

Subscripts are numbers/variables which represent the entire set of data items; where by each element of a set can be referenced by means of a number. For example a grade represents not a single value but the entire set of grades.

They are specified by writing/typing the number of the data items in the square brackets after declaring a variable like k[4].

The restriction is that the values run from 0 up to n-1 whereby n stands for array size or subscript.c) Write a ‘C’ program that will enter a line of text, store in an array and then display backwards. The length of the line should be undefined, (being terminated by ENTER key), but less than 80 characters.

4. a) What is a pointer in ‘C’? How is a pointer variable declared? Give examples and explain.Enumerate the utility of pointer variables.

A pointer is a memory address of a variable specifically declared to hold a pointer.

Page 8: Programming Assignment 2 Hhhh

A pointer is declared by introducing an asterisk sign (*) between the type of data and a variable, for example we can write int *p.

The * sign informs the compiler that we want a pointer variable so that it keeps aside many byte to store this address in memory.

Below are utilities of pointer variables; Pointer variables provide fast means of referencing array

elements. They allow c functions to modify their calling parameters.

b) A program in ‘C’ language contains the following declaration:static int x[8] = {1,2,3,4,5,6,7,8};i) What is the meaning of x? x is declared as an array of values with total subscript or index 8.ii) What is the meaning of (x + 2)?The second value in the array x and add it to x.iii) What is the meaning of *x?Variable x is a pointer variable.iv) What is the meaning of (*x + 2)?Add the second value contained in the x array to the memory address to which pointer x is pointing to.v) What is the meaning of *(x + 2)?It means that pointer arithmetic has been used.c) What is a structure? How does a structure differ from a union? Give examples. For what kind of applications, union data structure is useful? How are arrays different from structure?

Structure is a method of packing data items of different type.

In a union, members share the same storage within the computer memory.

Where as A structure differs by having each of its member assigned its own unique storage area. For example, in declaration of

Page 9: Programming Assignment 2 Hhhh

structure we specify the total size of all elements as shown below; Strut class_ tables;{ Char type [30];Char state[20];Float title; } While in union we write;Union books;{Int b;Float title;Char c; }Code;

Union data structure is useful for applications involving multiple members.

In arrays, elements stored are to be of the same data type. Where as a structure differ by requiring using a collection of different data items of different data types.

5 a) How can a procedure be defined in ‘C’ ? Bring the difference between function and procedure.

A procedure is a block of code that performs a single task. Its a building block of a computer program .

A function always returns a value using the return statement while a procedure may return one or more values through parameter or may not run at all.

Page 10: Programming Assignment 2 Hhhh

b) Draw a flowchart and then develop an interactive ‘C’ program which finds whether a given integer number is prime or not. Make

Page 11: Programming Assignment 2 Hhhh

Below is the flow chart required to find out whether a given integer number is a prime or not.

No

Yes

Is p = 7 ?

Start

Prime = 0

P = p+5

Print prime

End

Page 12: Programming Assignment 2 Hhhh

Assignment-C

Multiple choice questions in C

/*1*/Code:

int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;

What number will z in the sample code above contain? Choice 15 Choice 26 Choice 310 [Ans] Corrected by buddy by running the programChoice 411 Choice 512 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*2*/With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? Choice 1unalloc() Choice 2dropmem() Choice 3dealloc() Choice 4release() Choice 5

Page 13: Programming Assignment 2 Hhhh

free()- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*3*/Code:

void *ptr;myStruct myArray[10];ptr = myArray;

Which of the following is the correct way to increment the variable "ptr"? Choice 1ptr = ptr + sizeof(myStruct); Choice 2++(int*)ptr; Choice 3ptr = ptr + sizeof(myArray); Choice 4increment(ptr); Choice 5ptr = ptr + sizeof(ptr); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*4*/Code:

char* myFunc (char *ptr){ ptr += 3; return (ptr);} int main(){ char *x, *y; x = "HELLO"; y = myFunc (x); printf ("y = %s \n", y); return 0;

Page 14: Programming Assignment 2 Hhhh

}

What will print when the sample code above is executed? Choice 1y = HELLO Choice 2y = ELLO Choice 3y = LLO Choice 4y = LO Choice 5x = O - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*5*/Code:

struct node *nPtr, *sPtr; /* pointers for a linked list. */ for (nPtr=sPtr; nPtr; nPtr=nPtr->next){ free(nPtr);}

The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work? Choice 1It will work correctly since the for loop covers the entire list. Choice 2It may fail since each node "nPtr" is freed before its next address can be accessed. Choice 3In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next". Choice 4This is invalid syntax for freeing memory. Choice 5The loop will never end.

Page 15: Programming Assignment 2 Hhhh

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*6*/What function will read a specified number of elements from a file? Choice 1fileread() Choice 2getline() Choice 3readfile() Choice 4fread() Choice 5gets()- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*7*/"My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. Choice 1printf("\"My salary was increased by 15/%\!\"\n"); Choice 2printf("My salary was increased by 15%!\n"); Choice 3printf("My salary was increased by 15'%'!\n"); Choice 4printf("\"My salary was increased by 15%%!\"\n");[Ans] Choice 5printf("\"My salary was increased by 15'%'!\"\n"); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*8*/What is a difference between a declaration and a definition of a variable? Choice 1

Page 16: Programming Assignment 2 Hhhh

Both can occur multiple times, but a declaration must occur first. Choice 2There is no difference between them. Choice 3A definition occurs once, but a declaration may occur many times. Choice 4A declaration occurs once, but a definition may occur many times. Choice 5Both can occur multiple times, but a definition must occur first. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*9*/ int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain? Choice 13 Choice 25 Choice 37 Choice 49 Choice 511- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*10*/Code:

int a=10,b;b=a++ + ++a;printf("%d,%d,%d,%d",b,a++,a,++a);

what will be the output when following code is executedChoice 112,10,11,13Choice 2

Page 17: Programming Assignment 2 Hhhh

22,10,11,13Choice 322,11,11,11Choice 412,11,11,11Choice 522,13,13,13- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*11*/Code:

int x[] = { 1, 4, 8, 5, 1, 4 }; int *ptr, y; ptr = x + 4; y = ptr - x;

What does y in the sample code above equal? Choice 1-3 Choice 20 Choice 34Choice 44 + sizeof( int ) Choice 54 * sizeof( int - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*12*/Code:

void myFunc (int x) { if (x > 0) myFunc(--x); printf("%d, ", x);

Page 18: Programming Assignment 2 Hhhh

} int main() { myFunc(5); return 0; }

What will the above sample code produce when executed? Choice 11, 2, 3, 4, 5, 5, Choice 24, 3, 2, 1, 0, 0, Choice 35, 4, 3, 2, 1, 0, Choice 40, 0, 1, 2, 3, 4, Choice 50, 1, 2, 3, 4, 5, - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*13*/11 ^ 5 What does the operation shown above produce? Choice 11 Choice 26 Choice 38 Choice 414 Choice 515 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*14*/#define MAX_NUM 15

Page 19: Programming Assignment 2 Hhhh

Referring to the sample above, what is MAX_NUM? Choice 1MAX_NUM is an integer variable. Choice 2MAX_NUM is a linker constant. Choice 3MAX_NUM is a precompiler constant. Choice 4MAX_NUM is a preprocessor macroChoice 5MAX_NUM is an integer constant. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*15*/Which one of the following will turn off buffering for stdout? Choice 1setbuf( stdout, FALSE ); Choice 2setvbuf( stdout, NULL ); Choice 3setbuf( stdout, NULL ); Choice 4setvbuf( stdout, _IONBF ); Choice 5setbuf( stdout, _IONBF ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*16*/What is a proper method of opening a file for writing as binary file? Choice 1FILE *f = fwrite( "test.bin", "b" ); Choice 2FILE *f = fopenb( "test.bin", "w" ); Choice 3FILE *f = fopen( "test.bin", "wb" ); Choice 4

Page 20: Programming Assignment 2 Hhhh

FILE *f = fwriteb( "test.bin" ); Choice 5FILE *f = fopen( "test.bin", "bw" ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*17*/Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory? Choice 1memcpy() Choice 2memset() Choice 3strncpy() Choice 4strcpy() Choice 5memmove()- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*18*/int x = 2 * 3 + 4 * 5; What value will x contain in the sample code above? Choice 122 Choice 226Choice 346 Choice 450 Choice 570 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*19*/

Page 21: Programming Assignment 2 Hhhh

Code:

void * array_dup (a, number, size) const void * a; size_t number; size_t size; { void * clone; size_t bytes; assert(a != NULL); bytes = number * size; clone = alloca(bytes); if (!clone) return clone; memcpy(clone, a, bytes); return clone; }

The function array_dup(), defined above, contains an error. Which one of the following correctly analyzes it? Choice 1If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption. Choice 2array_dup() declares its first parameter to be a pointer, when the actual argument will be an array. Choice 3The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard. Choice 4size_t is not a Standard C defined type, and may not be known to the compiler. Choice 5The definition of array_dup() is unusual. Functions cannot be defined using this syntax. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*20*/

Page 22: Programming Assignment 2 Hhhh

int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from another file? Choice 1Yes; it can be referenced through the register specifier. Choice 2No; it would have to have been initially declared as a static variable. Choice 3No; it would need to have been initially declared using the global keyword.[Ans] Choice 4Yes; it can be referenced through the publish specifier. Choice 5Yes; it can be referenced through the extern specifier. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*21*/time_t t; Which one of the following statements will properly initialize the variable t with the current time from the sample above? Choice 1t = clock();Choice 2time( &t ); Choice 3t = ctime(); Choice 4t = localtime(); Choice 5None of the above - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*22*/Which one of the following provides conceptual support for function calls? Choice 1

Page 23: Programming Assignment 2 Hhhh

The system stack[Choice 2The data segment Choice 3The processor's registers Choice 4The text segment Choice 5The heap - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -//*23*/C is which kind of language? Choice 1Machine Choice 2ProceduralChoice 3Assembly Choice 4Object-oriented Choice 5Strictly-typed - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*24*/Code:

int i,j; int ctr = 0; int myArray[2][3]; for (i=0; i<3; i++) for (j=0; j<2; j++) { myArray[j][i] = ctr; ++ctr; }

Page 24: Programming Assignment 2 Hhhh

What is the value of myArray[1][2]; in the sample code above? Choice 11 Choice 22 Choice 33 Choice 44 Choice 55 00,10,01,11,12 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*25*/Code:s

int x = 0; for (x=1; x<4; x++); printf("x=%d\n", x);

What will be printed when the sample code above is executed? Choice 1x=0 Choice 2x=1 Choice 3x=3 Choice 4x=4Choice 5x=5 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*26*/Code:

int x = 3; if( x == 2 );

Page 25: Programming Assignment 2 Hhhh

x = 0; if( x == 3 ) x++; else x += 2;

What value will x contain when the sample code above is executed? Choice 11 Choice 22Choice 33 Choice 44 Choice 55 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*27*/Code:

char *ptr; char myString[] = "abcdefg"; ptr = myString; ptr += 5;

What string does ptr point to in the sample code above? Choice 1fg /*because string*/Choice 2efg Choice 3defg Choice 4cdefg Choice 5None of the above

Page 26: Programming Assignment 2 Hhhh

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*28*/Code:

double x = -3.5, y = 3.5; printf( "%.0f : %.0f\n", ceil( x ), ceil( y ) ); printf( "%.0f : %.0f\n", floor( x ), floor( y ) );

What will the code above print when executed?ceil =>rounds up 3.2=4 floor =>rounds down 3.2=3 Choice 1-3 : 4 -4 : 3Choice 2-4 : 4 -3 : 3 Choice 3-4 : 3 -4 : 3 Choice 4-4 : 3 -3 : 4 Choice 5-3 : 3 -4 : 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*29*/Which one of the following will declare a pointer to an integer at address 0x200 in memory? Choice 1int *x; *x = 0x200;[Ans] Choice 2int *x = &0x200; Choice 3int *x = *0x200;

Page 27: Programming Assignment 2 Hhhh

Choice 4int *x = 0x200; Choice 5int *x( &0x200 ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*30*/Code:

int x = 5; int y = 2; char op = '*'; switch (op) { default : x += 1; case '+' : x += y; /*It will go to all the cases*/ case '-' : x -= y; }

After the sample code above has been executed, what value will the variable x contain? Choice 14 Choice 25 Choice 36Choice 47 Choice 58 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*31*/Code:

x = 3, counter = 0; while ((x-1))

Page 28: Programming Assignment 2 Hhhh

{ ++counter; x--; }

Referring to the sample code above, what value will the variable counter have when completed? Choice 10 Choice 21 Choice 32Choice 43 Choice 54 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*32*/char ** array [12][12][12]; Consider array, defined above. Which one of the following definitions and initializations of p is valid? Choice 1char ** (* p) [12][12] = array; Choice 2char ***** p = array; Choice 3char * (* p) [12][12][12] = array; Choice 4const char ** p [12][12][12] = array; Choice 5char (** p) [12][12] = array; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*33*/void (*signal(int sig, void (*handler) (int))) (int);

Page 29: Programming Assignment 2 Hhhh

Which one of the following definitions of sighandler_t allows the above declaration to be rewritten as follows: sighandler_t signal (int sig, sighandler_t handler); Choice 1typedef void (*sighandler_t) (intChoice 2typedef sighandler_t void (*) (int); Choice 3typedef void *sighandler_t (int); Choice 4#define sighandler_t(x) void (*x) (int) Choice 5#define sighandler_t void (*) (int) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*34*/All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C? Choice 1Code:

int count_digits (const char * buf) { assert(buf != NULL); int cnt = 0, i; for (i = 0; buf[i] != '\0'; i++) if (isdigit(buf[i])) cnt++;

return cnt; }

Choice 2Code:

int count_digits (const char * buf) { int cnt = 0; assert(buf != NULL);

Page 30: Programming Assignment 2 Hhhh

for (int i = 0; buf[i] != '\0'; i++) if (isdigit(buf[i])) cnt++; return cnt; }

Choice 3

Code:

int count_digits (const char * buf) { int cnt = 0, i; assert(buf != NULL); for (i = 0; buf[i] != '\0'; i++) if (isdigit(buf[i])) cnt++; return cnt; }

Choice 4

Code:

int count_digits (const char * buf) { assert(buf != NULL); for (i = 0; buf[i] != '\0'; i++) if (isdigit(buf[i])) cnt++; return cnt; }

Choice 5

Code:

int count_digits (const char * buf) { assert(buf != NULL); int cnt = 0; for (int i = 0; buf[i] != '\0'; i++) if (isdigit(buf[i])) cnt++;

Page 31: Programming Assignment 2 Hhhh

return cnt; }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*35*/struct customer *ptr = malloc( sizeof( struct customer ) ); Given the sample allocation for the pointer "ptr" found above, which one of the following statements is used to reallocate ptr to be an array of 10 elements? Choice 1ptr = realloc( ptr, 10 * sizeof( struct customer)); [Ans] Choice 2realloc( ptr, 9 * sizeof( struct customer ) ); Choice 3ptr += malloc( 9 * sizeof( struct customer ) ); Choice 4ptr = realloc( ptr, 9 * sizeof( struct customer ) ); Choice 5realloc( ptr, 10 * sizeof( struct customer ) ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*36*/Which one of the following is a true statement about pointers? Choice 1Pointer arithmetic is permitted on pointers of any type. Choice 2A pointer of type void * can be used to directly examine or modify an object of any type. Choice 3Standard C mandates a minimum of four levels of indirection accessible through a pointer. Choice 4A C program knows the types of its pointers and indirectly referenced data items at runtime. Choice 5Pointers may be used to simulate call-by-reference.

Page 32: Programming Assignment 2 Hhhh

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*37*/Which one of the following functions returns the string representation from a pointer to a time_t value? Choice 1localtime Choice 2gmtime Choice 3strtime Choice 4asctime Choice 5ctime - - - - - - - - - - - - - - - - -

//*38*/Code:

short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} }; printf( "%d\n", sizeof( testarray ) );

Assuming a short is two bytes long, what will be printed by the above code? Choice 1It will not compile because not enough initializers are given. Choice 26 Choice 37 Choice 412 Choice 524 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*39*/

Page 33: Programming Assignment 2 Hhhh

char buf [] = "Hello world!";

char * buf = "Hello world!"; In terms of code generation, how do the two definitions of buf, both presented above, differ? Choice 1The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not. Choice 2The first definition is not suitable for usage as an argument to a function call; the second definition is. Choice 3The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal. Choice 4They do not differ -- they are functionally equivalent. [Ans]Choice 5The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//*40*/In a C expression, how is a logical AND represented? Choice 1@@ Choice 2|| Choice 3.AND. Choice 4&&Choice 5.AND - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Page 34: Programming Assignment 2 Hhhh