Sunday 9 November 2014

Top Android Common Interview Question and Answer

Written test :

1. Sorting of array
2. Swapping two no without using third variable.
3. Prime Numbers
4. Armstrong Number
5. Fibonacci Series
6. Add two shorted array in one shorted array.
7. Reverse number
8. Palindrome number
9. Print Pattern
 1.
    *
   ***
  *****
 *******
*********
2. 
 *
 **
 ***
 ****
 ***** 
 
10.Find 2nd Largest elements of the array

Answer:

I will Only explain the logic of the program.
 
1. Sorting of Array 


 int array[n], n, c, d, swap;
 
  printf("Enter number of elements\n");
  scanf("%d", &n);
 
  printf("Enter %d integers\n", n);
 
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
 
  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }
 
  printf("Sorted list in ascending order:\n");
 
  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);


2.Swapping of two numbers without third variable

 int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
 

3.  Prime Numbers

int n, c = 2;
 
   printf("Enter a number to check if it is prime\n");
   scanf("%d",&n);
 
   for ( c = 2 ; c <= n - 1 ; c++ )
   {
      if ( n%c == 0 )
      {
         printf("%d is not prime.\n", n);
  break;
      }
   }
   if ( c == n )
      printf("%d is prime.\n", n);
 
4.Armstrong Number
 
  int number, sum = 0, temp, remainder;
 
   printf("Enter an integer\n");
   scanf("%d",&number);
 
   temp = number;
 
   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }
 
   if ( number == sum )
      printf("Entered number is an armstrong number.\n");
   else
      printf("Entered number is not an armstrong number.\n") 

5. Fibonacci Series
 
int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
  
 
6. Add two shorted array in one shorted array.

 Java Example:

public static int[] merge(int[] a, int[] b) {

    int[] answer = new int[a.length() + b.length()];
    int i = 0, j = 0, k = 0;
    while (i < a.length() && j < b.length())
    {
        if (a[i] < b[j])
        {
            answer[k] = a[i];
            i++;
        }
        else
        {
            answer[k] = b[j];
            j++;
        }
        k++;
    }

    while (i < a.length())
    {
        answer[k] = a[i];
        i++;
        k++;
    }

    while (j < b.length())
    {
        answer[k] = b[j];
        j++;
        k++;
    }

    return answer;
}

C Example :

  1.     int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
  2.  
  3.     printf("\n Enter size of array Array 1: ");
  4.     scanf("%d", &m);
  5.     printf("\n Enter sorted elements of array 1: \n");
  6.     for (i = 0; i < m; i++)
  7.     {
  8.         scanf("%d", &array1[i]);
  9.     }
  10.     printf("\n Enter size of array 2: ");
  11.     scanf("%d", &n);
  12.     printf("\n Enter sorted elements of array 2: \n");
  13.     for (i = 0; i < n; i++)
  14.     {
  15.         scanf("%d", &array2[i]);
  16.     }
  17.     i = 0;
  18.     j = 0;
  19.     while (i < m && j < n)
  20.     {
  21.         if (array1[i] < array2[j])
  22.         {
  23.             array3[k] = array1[i];
  24.             i++;
  25.         }
  26.         else
  27.         {
  28.             array3[k] = array2[j];
  29.             j++;
  30.         }
  31.         k++;
  32.     }
  33.     if (i >= m)
  34.     {
  35.         while (j < n)
  36.         {
  37.             array3[k] = array2[j];
  38.             j++;
  39.             k++;
  40.         }
  41.     }
  42.     if (j >= n)
  43.     {
  44.         while (i < m)
  45.         {
  46.             array3[k] = array1[i];
  47.             i++;
  48.             k++;
  49.         }
  50.     }
  51.     printf("\n After merging: \n");
  52.     for (i = 0; i < m + n; i++)
  53.     {
  54.         printf("\n%d", array3[i]);
  55.     }

7. Reverse number 


int n, reverse = 0;
 
   printf("Enter a number to reverse\n");
   scanf("%d",&n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);

8. Palindrome number

int n, reverse = 0, temp;
 
   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);
 
   temp = n;
 
   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }
 
   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);
 


9. Print Pattern 
1.
int row, c, n, temp;
 
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
 
   temp = n;
 
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      printf("\n");
   }
 2.
int n, c, k;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");
 
        printf("\n");
    }
 

10.Find 2nd Largest elements of the array
 
int array[]={1,8,5,7,10,9,2};

int firstmax=array[0];

int secondmax=array[1];

int temp;

if(firstmax<secondmax)

{

temp=secondmax;

secondmax=firstmax;

firstmax=temp;

}



for(int i=0;i<7;i++)

{

if(firstmax<array)

{

secondmax=firstmax;

firstmax=array;

}

if((firstmax>array)&&(array>secondmax))

{

secondmax=array;

}

}

printf("second max:%dn",secondmax);

system("pause"); 
 
 
Android face to face Interview Question :
 
1. Difference between Async & thread .
 

For long-running or CPU-intensive tasks, there are basically two ways to do this: Java threads, and Android's native AsyncTask.
Neither one is necessarily better than the other, but knowing when to use each call is essential to leveraging the system's performance to your benefit.
Use AsyncTask for:
  1. Simple network operations which do not require downloading a lot of data
  2. Disk-bound tasks that might take more than a few milliseconds
Use Java threads for:
  1. Network operations which involve moderate to large amounts of data (either uploading or downloading)
  2. High-CPU tasks which need to be run in the background
  3. Any task where you want to control the CPU usage relative to the GUI thread
And there are lot of good resources over internet which may help you
 
2. Difference String Buffer & String Builder Classes
 
Ans: 
The StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.

Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again with out leaving behind a lot of new unused objects.

The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe(not Synchronised).

It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.
  
3. What is Android?

It is an open-sourced operating system that is used primarily on mobile devices, such as cell phones and tablets. It is a Linux kernel-based system that’s been equipped with rich components that allows developers to create and run apps that can perform both basic and advanced functions.
 
4. What is the Android Architecture?

Android Architecture is made up of 4 key components:
- Linux Kernel
- Libraries
- Android Framework
- Android Applications
 
5. What is ANR?
 
 is short for Application Not Responding. This is actually a dialog that
 appears to the user whenever an application have been unresponsive for a
 long period of time. 

6.
OnCreate()
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().
onRestart():
Called after your activity has been stopped, prior to it being started again. Always followed by onStart()
onStart():
Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
onResume():
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it. Always followed by onPause().
onPause ():
Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume(). When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
onStop():
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.
onDestroy():
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
When the Activity first time loads the events are called as below:
onCreate() onStart() onResume() When you click on Phone button the Activity goes to the background and the below events are called:
onPause() onStop() Exit the phone dialer and the below events will be called:
onRestart() onStart() onResume() When you click the back button OR try to finish() the activity the events are called as below:
onPause() onStop() onDestroy()

Download the sample code from here

What is a Fragment?
 A fragment is a part or portion of an activity. It is modular in a sense that you can move around or combine with other fragments in a single activity. Fragments are also reusable.

To be Continue... 

No comments:

Post a Comment