Programming Questions

Q:

Write a c program to find out sum of diagonal element of a matrix.

Answer

#include<stdio.h>

int main(){

  int a[10][10],i,j,sum=0,m,n;

  printf("\nEnter the row and column of matrix: ");
  scanf("%d %d",&m,&n);

  printf("\nEnter the elements of matrix: ");
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");

  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
      printf("%d\t",a[i][j]);
      }
 }
 for(i=0;i<m;i++){
     for(j=0;j<n;j++){
          if(i==j)
              sum=sum+a[i][j];
     }
 }
 printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);

 return 0;
}

Sample output:

Enter the row and column of matrix: 3 3
Enter the elements of matrix:
2
3
5
6
7
9
2
6
7
The matrix is
2       3       5
6       7       9
2       6       7
Sum of the diagonal elements of a matrix is: 16

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3583
Q:

Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?

Answer

Yes, using the realloc() function as shown below:


main()


{


        int *p;


        p = ( int *) malloc (20) ;


        t = p;


        t = (int *) realloc ( p, 40);


        if ( t == NULL )


        Printf (" Cannot reallocate, leaves previous allocated region unchanged ");


       else


       {


              if ( p ==t )


              ;  / * the array expanded at the same region */


             else


            { 


                 free ( p ); / * deallocate the original array */


                 p = t;  /* set p to newly allocated region */


             }


      }


}  

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3583
Q:

How would you free the memory allocated by the following program?

#include "alloc.h"

#define MAXROW 3

#define MAXCOL 4

main()

{

     int **p, i;

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

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

            p[i] = (int *) malloc (MAXCOL * sizeof (int ));

}

Answer

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


free (p[i]);


free (p);

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3558
Q:

If I use the following printf() to print a long int why I am not warned about the type mismatch?

printf ("%d",num );

Answer

When a function accepts a variable number of arguments , its prototype cannot provide any information about the number of arguments and type of those variable arguments. Hence the compiler cannot warn about the mismatches. The programmer must make sure that arguments match or must manually insert explicit typecast.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3422
Q:

Which of the following is true?

A) It is appropriate to use assertions to validate arguments to methods marked public B) It is appropriate to catch and handle assertion errors
C) It is NOT appropriate to use assertions to validate command-line arguments D) None
 
Answer & Explanation Answer: C) It is NOT appropriate to use assertions to validate command-line arguments

Explanation:

A is incorrect. It is acceptable to use assertions to test the arguments of private methods.

 

B is incorrect. While assertion errors can be caught, Sun discourages you from doing so.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

0 3398
Q:

How would you check whether the contents of two structure variables are same or not?

Answer

struct emp


{


     char n[20];


      int age;


};


main()


{


    struct emp e1 = {"Dravid", 23};


   struct emp e2;


   scanf ("%s %d",e2.n, & e2.age);


   if( structcmp (e1,e2) ==0)


        printf ("The structures are equal");


   else


         printf ("The structures are unequal");


}


structcmp ( struct emp x, struct emp y)


{


     if (strcmp (x.n,y.n) ==0)


           if (x.age == y.age)


            return (0);


            return (1);


}


In short, if you nee to compare two structures, you'll have to write your own function to do so which carries out the comparison field by field.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3304
Q:

Methods declared as what cannot be overriden?

A) Transcient B) Abstract
C) Final D) Super
 
Answer & Explanation Answer: C) Final

Explanation:

Once a method declared as Final cannot be  overriden

Report Error

View Answer Report Error Discuss

Filed Under: Programming

0 3277
Q:

How many times the following program would print 'Jamboree'?

main()

{

     printf ( "\nJamboree");

     main ();

}

Answer

Till the stack doesn't overflow

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

2 3265