Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Thursday, 6 June 2019

Aptitude Hack#103(6-6-19)

 Question:

If the ratio of the areas of two squares is 1:4, the ratio of their perimeters is

A. 1:2
B. 1:4
C. 1:6
D. 1:8


Wednesday, 5 June 2019

Aptitude Hack#102(5-6-19)

Question:

Ram and Hari started from A and B, towards B and A at 6.00 am and 7.00 am respectively. They meet each other at 9.00 am and continued towards their respective destinations. Ram reaching B turns back and catches up with Hari before Hari reaches A at 11.00 am. At what time will Hari reach A.

 (a) 7.00 pm
 (b) 4.00 pm
 (c) 5.00 pm
 (d) 6.00 pm

Aptitude Trick#4(Square of two digit number)

Square of two digit number using Formule 1

Consider An Example

Example: Square of 26

  • Step 1:

           we separate the number into
            two parts(a & b)



  • Step 2: Now using formulae

                 a2| 2ab | b2

                we get 2*2 | 2*2*6 | 6*6



Answer = 676

Aptitude Trick#3 (Square of three digit number)

Square of three digit number

Consider An Example

Example: Square of 206

  • Step 1:

           we separate the number into
            two parts(a & b)



  • Step 2: Now using formulae

                 a2| 2ab | b2

                we get 20*20 | 2*20*6 | 6*6

Answer = 42436

Tuesday, 4 June 2019

Aptitude Hack#101(4-6-19)

Question:

A tin of oil was 4/5 full.when 6 bottles of oil were taken out and four bottles of oil were poured into it, it was ¾ full. how many bottles of oil can the tin contain?
  
 A )   35
 B )   50
 C )   40
 D )   45

Monday, 3 June 2019

Aptitude Hack#100(3-6-19)

Question:

A person has the capability of thinking 100 lines of code in five minutes and can type 100 lines of code in 10 minutes. He takes a break for five minutes after every ten minutes. How many lines of codes will he complete typing after an hour? 

(a) 100
(b) 250
(c) 350
(d) 600

Sunday, 2 June 2019

Aptitude Hack#99 (2-6-19)

Question:

If 1/3rd of a number subtracted from. Of that number, then the difference is 10 more Than 1/7th of the same number. How much is that number?

   A )   420
   B )   400
   C )   405
   D )   415



Program for Shell Sort in C


Shell short is an improved and efficient version of the insertion sort.

In this algorithm, we sort the pair of elements that are far apart by gap h.
 The process is repeated by reducing h until it becomes 1.


Shell Sort

Algorithm

Following is the algorithm for shell sort.

Step 1 − Initialize the value of h

Step 2 − Divide the list into smaller 
               sub-list of equal interval h

Step 3 − Sort these sub-lists using insertion sort


Step 4 − Repeat until the complete list is sorted


Program for Shell Sort in C

#include<stdio.h>

void sort(int a[],int n)
{
int gap,i,j,temp;

for(gap=n/2;gap>0;gap/=2)
{
for(i=gap;i<n;i+=1)
{
temp=a[i];

for(j=i;j>=gap&&a[j-gap]>temp;j-=gap)

     a[j]=a[j-gap];

  a[j]=temp;

}

}

}

int main()
{
int a[20],i,n;

printf("Enter number of elements:");

scanf("%d",&n);

printf("Enter array elements:\n");

for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
} sort(a,n);

printf("\nArray after shell sort:\n");

for(i=0;i<n;++i)
printf("%d ",a[i]);

    return 0;
}


Output

Enter number of elements:5
Enter array elements:
56 7 2 9 12

Array after shell sort:
2 7 9 12 56

C program to print the truth table for XY+Z


C program to print the truth table for XY+Z  

#include<stdio.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf(“XtYtZtXY+Z”);

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf(“nn%dt%dt%dt1”,x,y,z);
else
printf(“nn%dt%dt%dt%d”,x,y,z,x*y+z);
}
}

OUTPUT:


Print “javaabhigyan” with empty main() in C


Write a program that prints “javaabhigyan” with empty main() function.You are not allowed to write anything in main().


1.) One way of doing this is to apply GCC constructor attribute to a function so that it executes before main()

#include <stdio.h> 
  
/* Apply the constructor attribute to myStartupFun()  
   so that it is executed before main() */
void myStartupFun(void) __attribute__((constructor)); 
  
/* implementation of myStartupFun */
void myStartupFun(void) 
{ 
    printf("javaabhigyan"); 
} 
  
int main() 
{ 
} 

Output:
javaabhigyan


2.) In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more.

#include <stdio.h> 
#include <stdlib.h> 
  
int main(void) 
{ 
} 
  
// _start() function 
void _start(void) 
{ 
    printf("javaabhigyan"); 
  
    // Call main() function 
    int var = main(); 
    exit(var); 
} 

Now compile this by following command

gcc -nostartfiles -o file file.c

Output:
javaabhigyan

Compute average of two numbers without overflow


Given two numbers, a and b. Compute the average of the two numbers.

The well know formula (a + b) / 2 may fail at the following case :
If, a = b = (2^31) – 1; i.e. INT_MAX.
Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work

Improved Formula that does not cause overflow :

Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)


Below is the implementation :

// C code to compute average of two numbers 
#include <stdio.h>
#define INT_MAX 2147483647
// Function to compute average of two numbers 
int compute_average(int a, int b) 
{ 
    return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); 
} 
  
  int main() 
{ 
    // Assigning maximum integer value 
    int a = INT_MAX, b = INT_MAX; 
  
    // Average of two equal numbers is the same number 
    printf("Actual average : %d\n",INT_MAX); 
  
    // Function to get the average of 2 numbers 
    printf("Computed average : %d",compute_average(a, b)); 
  
    return 0; 
} 

Output:
Actual average: 2147483647
Computed average: 2147483647