Programming Questions

Q:

In programming, repeating some statements is usually called  ?

A) Running B) Structure
C) Looping D) Control structure
 
Answer & Explanation Answer: C) Looping

Explanation:
Report Error

View Answer Report Error Discuss

Filed Under: Programming
Job Role: Analyst , IT Trainer

6 3267
Q:

How would you use the function memmove()?

Answer

#include "mem.h"


#include "alloc.h"


main()


{


      int area;


      char *dest;


      char src[] = "Life is the camera and you are the target"


                                  "so keep smiling always";


      area = sizeof (src);


      dest = malloc (area);


      memmove (dest, src, area);


      printf("\n%s", dest);


      printf("\n%s",src);


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3229
Q:

How would you dynamically allocate a 2-D array of integers?

Answer

#include "alloc.h"


#define MAXROW 3


#define MAXcol 4


main()


{


        int *p, i, J;


        p = (int *) malloc (MAXROW * MAXCOL * sizeof (int));


         for ( i=0; i < MaxROW ; i++)


         {


                for (j=0; j < MAXCOL ; j++)


                { 


                      p [ i * MAXCOL + j] = i;


                       printf ( "%d", p [i * MAXCOL + j] );


                 }


                  printf ("\n");


          }


}

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3154
Q:

What will be output of following c code?

void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;

}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
clrscr();
printf("%d",*p);
getch();
}

Answer

Output: 12

Explanation:
Binary value of a=1 is 00001 (in 5 bit)
Binary value of b=3 is 00011 (in 5 bit)
Binary value of c=3 is 000011 (in 6 bit)

In memory it is represented as:
Let address of bit1 is 500 which initialize to char pointer p. Since can is one byte data type so p++ will be 501. *p means content of memory location 501 which is (00001100) and its binary equivalent is 12. Hence output is 12.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2996
Q:

Point out the error, if any, in the following program.

#include "stdio.h"

main()

{

      unsigned char;

       FILE *fp;

       fp = fopen ("trail", "r");

       while (( ch = getc (fp)) ! = EOF)

               printf ("%c", ch);

       fclose (fp);

}  

Answer

EOF has been defined as #define EOF -1 n the file "stdio.h" and an unsigned char ranges from 0 to 255 hence when EOF is read from the file it cannot be accommodated in ch. Solution is to declare ch as an int.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 2869
Q:

Point out the error, if any, in the following program.

# include "stdio.h"

main()

{

     FILE *fp;

     char str[80];

     fp = fopen ("trail", "r");

     while (!feof (fp))

    {

           fgets (str, 80, fp);

           puts (str);

     }

     fclose (fp);

}

Answer

The last line from the file "trial" would be read twice. To avoid this, ues:


       While ( fgets (str, 80, fp) ! = NULL)


               puts (str);

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2774
Q:

In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No>

typedef struct employee *ptr;

struct employee

{

       char name[20];

        int age;

        ptr next;

};

Answer

Yes

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 2767
Q:

What value does read() return when it has reached the end of a file?

A) 1 B) -1
C) 0 D) None
 
Answer & Explanation Answer: B) -1

Explanation:

It returns -1

Report Error

View Answer Report Error Discuss

Filed Under: Programming

1 2762