Programming Questions

Q:

We want to round off x, a Float to an Int value. The correct way to do so would be

A) Y = ( int ) ( x + 0.5 ) ; B) Y = int ( x + 0.5) ;
C) Y = ( int ) x + 0.5; D) Y = ( int ) ( ( int ) x + 0.5 )
 
Answer & Explanation Answer: A) Y = ( int ) ( x + 0.5 ) ;

Explanation:

Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.

 

y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)

 

Example:

 

#include

 

int main ()

 

{

 

  float x = 2.6;

 

  int y = (int)(x + 0.5);

 

  printf ("Result = %d\n", y );

 

  return 0;

 

}

 

Result : 3

Report Error

View Answer Report Error Discuss

Filed Under: Programming

1 4614
Q:

Write a quick script for launching a new activity within your application.

Answer

An explicit intent explicitly defines the activity the developer wishes to start. 


Script code :


Intent myIntent = new Intent(this, MyNewActivity.class);


startActivity(myIntent);

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

15 4580
Q:

What's the difference between the functions rand(), random(), srand() and randomize()?

Answer

rand()  returns a random number


random()  returns a random number in a specified range


srand()  initialise a random number generator with a given seed value


randomize()   initializes a random number generator with a random value based o time.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

2 4292
Q:

What is the difference between malloc() and calloc() functions?

Answer

As against malloc(), calloc() needs two arguments, the number of elements  to be allocated and the size of each element. For example,


 p = (int *) calloc (10, sizeof (int));


would allocate space for a 10- integer array. Additionally, calloc() would also set each of this element with a value 0.


Thus the above call to calloc() is equivalent to:


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


memset (p, 0, 10 * sizeof( int ));

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3960
Q:

Rewrite the following set of statements using conditional operators.

int a =1, b ;

if ( a > 10 )

b = 20; 

Answer

int a = 1, b , dummy;


a > 10 ? b = 20 : dummy =1;


 


Note that the following would not have worked:


a > 10 ? b = 20 : ;;


 

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

3 3925
Q:

The coding or scrambling of data so that humans cannot read them, is known as _____.

A) Compression B) Encryption
C) Ergonomics D) Biometrics
 
Answer & Explanation Answer: B) Encryption

Explanation:

The coding or scrambling of data so that humans cannot read them is known as encryption.

Report Error

View Answer Report Error Discuss

Filed Under: Programming

9 3801
Q:

How would you use qsort() function to sort an array of structures?

Answer

#include "string.h"


#include "stdlib.h"


struct stud


{


       int rollno;


       int marks;


       char name[30];


};


int sort_m (struct stud *, struct stud *);


int sort_name (struct stud *, struct stud *);


int sort_marks (struct stud *, struct stud *);


 


main()


{


static struct stud ss[] = {


                                            { 15, 96, "Akshay" },


                                            { 2, 97, "Madhuri" },


                                            { 8, 85, "Aishvarya" },


                                            { 10, 80, "Sushmita" }


                                   };


int x,w;


clrscr();


w = sizeof (struct stud);


 


printf ('\nIn order of roll numbers:");


qsort (ss, 4, w, sort_rn);


for(x=0; x<4;x++)


     printf ("\n%d%s%d", ss[x].rollno, ss[x].name,ss[x].marks);


 


printf("\n\nIn order of names:");


qsort(ss, 4, sort_name);


 


for (x=0; x<4;x++)


      printf("\n%d%s%d",ss[x].rollno, ss[x].name,ss[x].marks);


printf("\n\nIn order of marks:");


qsort(ss,4,w,sort_marks);


 


for (x=0;x<4;x++)


      printf ("\n%d%s%d",ss[x].rollno,ss[x].name,ss[x].marks);


}


int sort_rn (struct stud *t1, struct stud *t2)


{


     return (t1->rollno-t2->rollno);


}


 


int sort_name (struct stud *t1, struct stud *t2)


{


     return (strcmp(t1->name,t2->name));


}


int sort_marks (struct stud *t1, struct stud *t2)


{


     return (t2->marks-t1->marks);


}


 


 


 

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 3771
Q:

What would be the output of the following program?

main()

{

        char huge * near * far *ptr1;

        char near * far * huge *ptr2;

        char far * huge * near *ptr3;

         printf ("%d%d%d", sizeof (ptr1), sizeof (ptr2), sizeof (ptr3));

}

Answer

4  4  2

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

1 3654