Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need solutions to following multiple choice questions (http://www.go4expert.com/forums/solutions-following-multiple-choice-t6248/)

sudheer157 7Sep2007 20:04

Need solutions to following multiple choice questions
 
Question 1.
Code:

int **make_matrix(int *m,int *n,int mlen,int nlen)
{
int j,k;
int **matrix=malloc(mlen*nlen*sizeof(int));

for(k=0;k<nlen;k++)
for(j=0;j<mlen;j++)
matrix[k][j]=m[k]*n[j];

return matrix;
}

function make_matrix defined above contains a serious error.which of the following describes the error.

1.C does not allow creation of multidimwntiona arrays.
2.Tha call to malloc() allocates insufficient space to accommodate the correct number of elements.
3.Inside make_matrix dereferences random memory address,resulting in undefined behaviour.
4.C uses row-major indexing rather than column -major indexing.The logic used to compute indices is therefore incorrect.
5.internal algorithms of make_matrix() consistantly result in memory leak.

Question 2
Code:

#include<stdio.h>
size_t strlen(const char *s);
void main()
{
char *p;
printf("len %d\n",strlen(p));
}
size_t strlen(const char *s)
{
const char * const start =s;
while(*s != '\0')
s++;

return (s-start);
}

code above is a possible implementation of strlen() a standard library function,which one of the
following statements is true?

1.The proposed return type deviates from the standard.
2.By the standard C definition,the implementation miscalculates the length of the string begining
at s.
3.The suggested implementation is incorrect because a differrencing operation is performed on poi
nters of unlike type.
4.C does not support autoincrement for const qualified pointers.The body of the loop is therefore
invalid.
5.The behaviour of the implementation is undefined when s is the null pointer.

Question 3.
Code:

/*to eliminate white spaces from end of s */
void rtrim(char *s)
{
char * start,ws,text;
start=s,ws=text=s-1;

while(*s!='\0')
{
if(isspace(*s))
{
ws=s;
while(isspace(*s)) s++;
}
else {
text=s;
while(*s != '\0' && ! isspace(*s)) s++;
}
}
if(ws > text)
*ws='\0';
}

which one of the following describes the implementation error in the function rtrim(),define above?
1.The loop and its subordinate statements do not adequately detect end-of-string condition on s in all cases.
2.rtrim() does not correctly handle the case in which character of s is whitespace.
3.ws and text are not assignment-compatible with s or NULL.
4.The comma operator has a higher precedence than assignment,this produces an unintended affect.
5.rtrim() does not correctly handle the case in which no character of s is whitespace.

Question 4
Code:

void maemset(void *buf,int c,size_t len) ??<
unsigned char *b;
register int i;
assert(buf);
debug("assert(buf)=>TRUE??/n");
b=buf;
for(i=0;i<len;i++)??<
b??(i??)=c;
debug("b??(i??)<-c??/n");
??>
??>

Considering the function memset() defined above which one of the following is a copletely true st
atement about the code shown above?
1.source code file containing memset() has probably become corrupted.This code clearly does not compile.
2.providing the debug() is declared and correctly used,the code compiles correctly under a standard C compiler.
3.assuming that debug() prints its string argument,the string will run together on the same line.
4.some accesses performed on b are out-of-bounds.
5.The three-character sequences starting with ?? were inserted by Microsoft word.

Question 5
Code:

/* Acquire a unique ,nonnegative sequence number.If out of ids return -1  */
int get_next_id(void)
{
static int id;

if(id<0)
  return -1;

return ++id;
}

function get_next_id() defined above contains an error that prevents it from behaving as commented.
which one of the following describes the error?

1.The variable id is never initialized.
2.The value of id is not preserved between calls to get_next_id().The uniqueness of the sequences
numbers ,therefore,cannot be guaranteed.
3.since the condition of the if statement can never be true,the function is incapable of returning
the error code negative one(-1).this is a positive feture.and the code that checks for error contributes to code bloat.
4.get_next_id() checks for integer overflow before the integer is incrementd.
5.4.get_next_id() always returns negative one(-1);

Question 6

Code:

/*assume that each char takes 1 byte */
int strvisx(char *dst,const char *src,size_t len) {
int written=0;
for(;len>0;src++,len--)
if(isprint(*src))/*copy portable characters to dst */
*dst++=*src,written++;
else { /*convert unprintable characters */
sprintf(dst,'\\%03o",*src);
dst +=4,written+=4;
}
*dst='\0';
return written;
}

considering the function strvisx() defined above,how long must the destination buffer be to
guarantee that NO overflow condition will occur if src is 73 bytes in length?
1. 73 bytes
2. 74 bytes
3. 219 bytes
4. 292 bytes
5. 293 bytes

Question 7
Which one of the following is correct way to concatenate two strings s1,s2?

Code:

1.char *s3;
s3=malloc(strlen(s1)+strlen(s2)+1);
/*assume malloc() cucceded */
strncpy(s3,s1,strlen(s1));
strcat(s3,s2);

Code:

2.char *s3;
strcpy(s3,s1);
strcat(s3,s2);

Code:

3.String s3=null;
s3=s1+s2;

Code:

4.char *s3;
s3=s1+s2;

Code:

5.char *s3;
s3=malloc(strlen(s1)+strlen(s2)+1);
/*assume malloc() cucceded */
strcpy(s3,s1);
strncat(s3,s2,strlen(s2));

Question 8
void f(char *s,char c,size_t p)
/*
insert character c at position p of a buffer s containing a string existing characters from position p to the end of the string are shifted to the right before c is inserted.keeps all the existing characters of s there must be enough space remaining in the buffer for the added character.p must be less than the length of s.
*/
Reffering to the sample code above,which one of the following could be the body of the function "f" ?

Code:

1.s+=p;
while(*s) {
char t=*s;
*s++=c;
c=t;}

Code:

2.char *b=(char*)malloc(strlen(s)+2),*t=b;
  memcpy(t,s,p-1); t+=p-1;
*t++=c;
strcpy(t,s+p);
strcpy(s,b);
free(b);

Code:

3.memcpy(s+p+1,s+p,strlen(s+p)+1);
s[p]=c;

Code:

4.memmove(s+p+1,s+p,strlen(s+p)+1);
s[p]=c;

Code:

5.strcpy(s+p+1,s+p);
s[p]=c;

Question 9
Code:

/* Read an orbitary long string */
int read_long_string(conat char** const buf)
{
char *p=NULL;
const char *fwd=NULL;
size_t len=0;
assert(buf);
do
{
p=realloc(p,len+=256);
if(!p) return 0;
if(!fwd) fwd=p;
else fwd=strchr(p,'\0');
}while (fgets(fwd,256,stdin));
*buf=p;
return 1;
}

The function read_long_string(),defined above contains an error that may be particularly visible under heavy stress.which one of the following describes it?
1.if the call to realloc() fails during any iteration but the first,all memory previously allocated by the loop is leaked.
2.If the null pointer for char is not zero-valued on the host machine,the implicit comparisions to zero(0) may introduce undesired behaviour.moreover even if successful,it introduces machine dependent behaviour and harms portability.
3.the write to *buf is blocked by the const qualifications applied to its type.
4.the symbol stdin may not be defined in some ANSI C compliant systems.
5.else causes fwd to contain an errant address.

Question 10

Code:

int main(void){
struct x{
int:2;
};
goto int;
}

which one of the following describes the single error in the above sample code?
1.the identifier in the structure member declaration is invalid.
2.the target of the goto statement is invalid.
3.the structure cannot be declared inside a function definition.
4.a label cannot be put inside a structure declaration.
5.it is an error to jump into a structure.

Question 11.
Code:

/*obtain an integer from standard input */
int get_number(void)
{
char *buf;
int n;
do{
printf("entr a positive  num");
fflush (stdout);
scanf("%s",&buf); 
n=atoi(&buf);
}while(n<1);
return n;
}

The function above contains a very basic error that is often seen in code writte by beginning C programmers.which one of the following describes the error demonstarted above?
1.No storage is associated with the pointer buf;
2.the call to scanf() does not NUL-terminate buf.
3.the second argument to scanf() should be &buf.
4.the value of n may be read prior to initialization.
5.the function does not correctly handle an input of zero(0).

DaWei 7Sep2007 20:29

Re: Need solutions to following multiple choice questions
 
This is not a free homework agency, it is a help forum.
Before you make a query

sudheer157 7Sep2007 20:55

Re: Need solutions to following multiple choice questions
 
i really doesnt mean that, i tried to implement the codes for above and i have some solution, but i dont know whether they are correct or not,so i need help your help to conform that solution what i m thinking is correct or not.and here im giving my solution,can some one please tell me whether they are correct or not.
Thanks in advance.

SOLS.
Q1. 1
Q2. 4
Q3. -
Q4. 2
Q5. 4
Q6. -
Q7. 1
Q8. 4
Q9. 3
Q10. 2
Q11. 3

please tell me whether thease sols are correct or not.

shabbir 7Sep2007 22:30

Re: Need solutions to following multiple choice questions
 
Use code block in posts if you ahve code snippets.

sudheer157 8Sep2007 16:29

Re: Need solutions to following multiple choice questions
 
Quote:

Originally Posted by sudheer157
i really doesnt mean that, i tried to implement the codes for above and i have some solution, but i dont know whether they are correct or not,so i need help your help to conform that solution what i m thinking is correct or not.and here im giving my solution,can some one please tell me whether they are correct or not.
Thanks in advance.

SOLS.
Q1. 1
Q2. 4
Q3. -
Q4. 2
Q5. 4
Q6. -
Q7. 1
Q8. 4
Q9. 3
Q10. 2
Q11. 3

please tell me whether thease sols are correct or not.

if some one has different set of solutions please let me know....


All times are GMT +5.5. The time now is 18:01.