Wednesday 4 September 2013

SCANIT Online Test Answer - Day 1( 27/08/2013)

                                     
                                         Multiple Choice Question


1. The control automatically passes to the first statement after the loop in ______________.

Answer : break statement


2. Predict the output of following C program.
main()
{
      int x=20,y=35;
      x = y++ + x++;
      y = ++y + ++x;
      printf("%d",y);
}

Solution:

x = 20 + 35 = 55
y = 36 + 1 + 56 + 1 = 94

The value of y is 94.


3. What will be the result for the following statement?

                      printf (“%d”, printf (“tifaccore”));

Solution:
The output is ‘tifaccore9’ , ‘tifaccore’ due to inner printf statement and 9 as length due to outer printf statement.


4.Initially the value of n is -54 , what will be the output generated for this program ?

if (n < 0)
{
        printf ("-");
        n = -n;
}

if (n % 10)
     printf ("%d", n);
else
     printf ("%d", n/10);

printf ("%d", n);

Solution:
      The output is -5454 . Here n%10 doesn’t affect n value.



5.How many times ‘SCAN IT’ will be printed in the following C program ?
main()
{
int a=0,b=5;
if(a=0) printf("SCAN IT ");
if(b=5) printf("SCAN IT ");
if((a>b)==1) printf("SCAN IT ");
printf("SCAN IT ");
}

Solution:
       2 times it will be printed. “ If(a=0)” result false so it won’t print . The reason is assigning a variable to zero is always returns 0 but in case of assigning a variable to other values , it will return 1.


6. What will happen if we execute the following code ?
int main(void)
{
      http://www.sastra.edu
     printf("SASTRA UNIVERSITY");

       return 0;
}

Solution:
The output is ‘SASTRA UNIVERSITY’ . The code will not affect because of http://www.sastra.edu . ‘http:’ will be considered as goto statement. ‘//www.sastra.edu’ will be assumed as comments and it will be ignored.


7. What will be output when you will execute following c code?
void main()
{
int check=2;
switch(check){
case 1: printf("Vijay");
case 2: printf("Ajith");
case 3: printf("Surya");
case 4: printf("Rajini");
default: printf("Kamal");
}
}

Solution:
The output is ‘Ajith Surya Rajini Kamal’ . There is no break statement.


8.Which of the following operation is illegal in structures?
(a)Typecasting of structure
(b)Pointer to a variable of same structure
(c)Dynamic allocation of memory for structure
(d)All of the mentioned

Answer : (a) Typecasting of structure.


9.What will be the output of the program?
int main()
{
int i=3;
switch(i)
{
case 1:
printf("Online Test");

case 2:
printf(“is");

case 3:
continue;

case 4:
printf("Boring");

default:
printf("Very Interesting");
}
return 0;
}

Solution:
It results in Compiler Error . We can use continue statement only for loop .. If we use loop inside switch that time we can use continue statement.


10.What will be the output of the program?
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d", x, y))
    {
        y = x++ <= 5;
    }
    return 0;
}

Solution:
2 1 3 1 4 1 5 1 6 1 7 0


11. The operation between float and int would give the result as 

Answer : int


12. What will be output when you will execute following c code?
void main(){
     int movie=1;
     switch(movie<<2+movie){
        default:printf("Thalaiva");
        case 4: printf("Billa 2");
        case 5: printf("Singam 2");
        case 8: printf("Vishwaroopam");
     } 
}


Solution : 
1 << 3  ==> 8 ( Shifting the bit to left  00001  to 001000 )
So only case 8 will be executed.

The output is Vishwaroopam.


13.Find the output for the following.
    int main()
    {
        int i = 1;
        if (i++ && (i == 1))
            printf("SCANIT");
        else
            printf("ACE");
    }


Solution : The output is "ACE"..  if statement returns false because while comparing i value becomes 2 ( because of increment operator )


14.Consider the following declaration of enum.

(i)enum  cricket {Sachin,Dhoni,Dravid}c;
(ii)enum  cricket { Sachin,Dhoni,Dravid };
(iii)enum   { Sachin,Dhoni=-5,Dravid }c;
(iv)enum  c {Sachin,Dhoni,Dravid };

Choose correct one:
Solution : All the four statements are correct .

15.What will be the output of following code?  
      int main()    
    {        
             fun();  
             fun();
             fun();    
             return 0;    
     }

    void fun()    
    {      
             auto int i=0;        
             register int j=0;      
             static int k=0;      
              i++; j++; k++;        
              printf(“%d %d %d”,i,j,k);  
    }

Solution :
   1 1 1 1 1 2 1 1 3
    Everytime while printing 'i' and 'j' value will be 1. But k value will change ' 1 2 3 ' because it is static so it will get last defined value.

16. Comment on the output of this C code?      
        struct temp      
       {          
              int a;      
       } s;      

       void change(struct temp);      

        main()      
       {          
                s.a = 10;          
                change(s);          
                printf("%d", s.a);      
        }      

       void change(struct temp s)      
      {          
                  s.a = 1;      
      }

Solution : The output will be 10.


17.What is the output of following code?    
       void main()   
       {       
             int x = 0;       
             if (x = 0)           
                printf("Vijay");       
            else           
                printf("Ajith");   
       }

Solution :  The output will be "Ajith" .. If(x=0) returns false so it won't execute if statement so else should be executed..


                  Program Type Question

1. A number of “Cats” got together and decided to kill 999919 mice. Every cat killed equal number of “mice”. Write a program to find different possible number of cats.

Solution:
Logic : (999919 % x ==0) The value of x is from 0 to 999919. There may be different possible solution. For example the number of cats may be one , in that case 1 cat killed 999919 mice .. Assume there are 999919 cats , in that each cats killed 1 mice.. Using loop and the above if statement , you can write the code.


2. Write a C program to add two numbers without using '+' operator.

Solution:

Logic 1 : sum = a - (-b) ;

Logic 2 : sum = a -( ~b ) -1; (~b is equivalent to –b-1)


  3.Replace X by some expression in such a manner you should get output as “SASTRA UNIVERSITY”.   
        
                   if(X)            
                          printf(“SASTRA”);       
         
                 else            
                          printf(“UNIVERSITY”);

Solution:
X should be replaced by "(printf("SASTRA") == 0)"

So first SASTRA will be printed and returns 1 , then the condition "1==0" becomes false so else will be executed.

4. There are some goats and ducks in a farm. There are 60 eyes and 86 foot in total. Write a program to find number of goats and ducks in the farm.

Solution:

Logic : 2(x+y) = 60 so that x + y = 30 == > 1st Equation – eyes.
            4x + 2y = 86 . Solve the equation and get the value of x and y (i.e. Number of goats and ducks.)



5. Pragadeesh has a money pouch containing Rs.700. There are equal number of 25 paise coins, 50 paise and one rupee coins. Write a C program to find how many of each are there?

Solution:

             Logic x = 700 / (0.25+0.50+1);



6.  Write a program to print in the following pattern :
1
3 5
7 9 11

Solution:

int main()
{
    int i,j,v,t;

    t=0;

    for(i=0;i<3;i++)
    {
        for(j=0;j<=i;j++)
        {
            v=((2*t)+1) ;
            printf("%d \t",v);
            t=t+1;   
                
        }
            printf("\n");

    }

    return 0;

}


7.Write a C program to find the smallest of three integers without using any of the comparison operators.

Solution:

int smallest(int x,int y,int z)
{
  int c = 0;
  while ( x && y && z )
  {
      x--;  y--; z--; c++;
  }
  return c;
}

int main()
{
   int x = 12, y = 15, z = 5;
   printf("Minimum of 3 numbers is %d", smallest(x, y, z));
   return 0;

}


8. Write a C program to print numbers from 1 to 100 without using conditional operators.


Solution :

int main()
{
    int num = 1;

    print(num);
    return 0;
}


int print(num)

{
       if(num<=100)

      {
         printf("%d ",num);
         print(num+1);
       }
}



                  Aptitude Questions.

1. An analog clock reads 3:15. What is the angle between the minute hand and hour hand ?
( The answer is not zero !)

Solution: At 3:15 , the hour hand will be ¼ of between 3 and 4 & the minute hand will be at 3.. The angle is ¼ of between 3 and 4. The total clock angle is 360. Half ( 12 to 6) is 180.. 180/6 will return angle between each number ( 2 and 3 , 3 and 4). SO the angle between 3 and 4 is 30 . ¼ of between 3 and 4 is 7.5..


2. Arun works in cryptology department.One fine morning when Arun entered his room in office he saw that his system is already logged in and all his important files are gone. Arun then went to his boss cabin and said "I need my system to be secure for that i need new password.My old password is hacked and all my important files are gone".Boss told him "I know your important files are gone and for that i issued you new password.Your new password is unbreakable,trust me".

Arun said "But what is my new password?"

Boss said "This is important.If you will listen carefully you will get your new password." 

* Your new password is in someway opposite to your old password and you already heard that. 

* Your new password and old password has 3 letters in common.

 * Your new password length is one less then the double of your old password length.  

* Always remember more the length of password more it is difficult to crack.

Arun then went to his cabin and logged in with new password.How he must have guessed the new password?What is his new password?What was his old?

Solution:

Old Password - hacked
New Password - unbreakable

3. There is a room with a door (closed) and three light bulbs. Outside the room there are three switches, connected to the bulbs. You may manipulate the switches as you wish, but once you open the door you can't change them. Identify each switch with its bulb.

Solution :
Switch the first one on for a couple of minutes then switch it off, switch the second one on, and keep the third turned off. Get into the room, the first should be hot and not lit, the second is lit, and the third is cold and not lit.


4. Find out the wrong number in the given sequence of numbers 582, 605, 588, 611, 634, 617, 600
Solution :
634. Alternatively 23 is added and 17 is subtracted from the terms. So, 634 is wrong.


5. Yesterday in a party, I asked Mr. Shah his birthday. With a mischievous glint in his eyes he replied. "The day before yesterday I was 83 years old and next year I will be 86." Can you figure out what is the Date of Birth of Mr. Shah? Assume that the current year is 2000.

Solution:
Mr. Shah's date of birth is 31 December, 1915

Today is 1 January, 2000. The day before yesterday was 30 December, 1999 and Mr. Shah was 83 on that day. Today i.e. 1 January, 2000 - he is 84. On 31 December 2000, he will be 85 and next year i.e. 31 December, 2001 - he will be 86. Hence, the date of birth is 31 December, 1915. Many people do think of Leap year and date of birth as 29th February as 2000 is the Leap year and there is difference of 3 years in Mr. Shah's age. But that is not the answer.


0 comments:

Post a Comment