Wednesday, 4 September 2013

SCAN IT Online Test Answer - Day 4 ( 03 / 9 / 2013 )

                         
    Day 4 Students & Day 5 Students had the same Question

                                             Multiple Choice Question

1. What will be the output for the following C code ?
#define TRUE 0
int main()
{
while(TRUE)
{
printf("Colosseum 2013 was a great Success !");
}
}

 Solution :

     Prints Nothing ! . In this case , it will be assumed as while(0) and exit the loop.



2.Find the output for the following question.
int main()
{
int x=5;
printf("%d  %d",x<<2,x>>2);
}

Solution:
 The binary value of 5 is 0101

 Now 5<<2 will shift the bit to left two times, so it results 010100 == 20.
And 5>>2 will results in 0001 == 1

The output is 20 1



3. Find error in following c snippet?
int x;
int x=0;
int x;
int main()
{
   printf("%d",x); 
   return 0;
}

Solution :

* No Error. Prints 0. Multiple declaration of global variables doesn't results in error.!  

( Tested in both TURBO C and LINUX ENVIRONMENT)

4. What is the output of code given below?
int main()
{
printf("3rd Year");
goto asl;
printf("IT");
asl :printf("C Section");
}

Solution :
   Output will be 3rd Year C Section.. (Note that 'IT' will not be printed ).




5.The output of the code below is

int main()
{
int i=0, k;
label:printf("'faccha' is SASTRA colloquial for a freshman");
if(i==0)
goto label;
}

// The above context taken from Sastra Imprint XII Edition

Solution:
          Will be printed infinite times.




6.What would be the output of the following code?

int main(void)
{
int a = 10, b = 20, c = 30;
printf("%d..%d..%d", a+b+c, (b = b*2), (c = c*2));
return 0;
}

Solution:
    The precedence is from right to left.

     c = 30 * 2  == 60.
     b = 20 * 2  == 40.
     a+b+c = 10 + 40 + 60 == 110.

     Output will be 110..40..60




7. What will the output for the following snippet ?

if((strcmp("OS", "DBMS")) == 0)
    {
flag = 1;
    }

if(flag)
    {
printf("DBMS is great");
    }
else
    {
printf("OS is great");
    }

Solution :

    In case of strcmp() , if both strings are equal it will return zero otherwise it will print some other value. 

  Hence  " if((strcmp("OS", "DBMS")) == 0) " will return false.

   So 'if' will not be executed , only 'else' will be executed.

   The output will be "OS is great"



8. What will be the output for the following code ?

if((printf("Online Test is ") > 0 ? 1 : 0))
printf("Boring !");
else
printf("Interesting !");


Solution:

  First it will print "Online Test is" and it will return '1' . (Always a successful statement will return '1') . So 1>0 will also return true and replaced by 1 (Conditional Operator). And if(1) prints "Boring !".

Hence the output is "Online Test is Boring !".





9.What will be the output for the following program ?

void main()
{
  if(((printf("SCANIT ")),(printf("is going ")))&&((printf("to conduct "))||(printf("web designing class"))))
                printf(5+"from next week");

   else
            printf(3 + "from tomorrow");
}

Solution :

Some Tips :

*  Normally printf(4+"SASTRA")  will displays "RA" (display data from 4th character i.e 5th and other character)

* if(1,1) returns 1
   if(0,0) returns 0
   if(1,0) returns 0
   if(0,1) returns 1

* A Successful statement always returns 1. (printf(), scanf())

Hence printf() statement will be executed one by one. if((1,1)&&(1 || 1)) returns true and execute the statement inside if. And 'from' will be skipped as it starts from 5th character. 

 "web designing class" will not be executed. The reason is ( 1 || printf("web designing class")) will returns true without considering the printf statement.

The output will be "SCANIT is going to conduct next week"




10. In C , modulus operator(%) can be applied to ___ Variables.

(i) int
(ii) char
(iii) double
(iv) float

Solution :  int and char.




11.What will be the output for the following code ?

int main()
{
            float me = 1.1;
            double you = 1.1;
            if(me==you)
                  printf("Hi !");
            else
                  printf("Hello !");
}

Solution :
The output will be "Hello !".  In this case , if() will returns false.

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value  represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb: 
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) . 



12.What will be the output for the following code ?

             int main()
            {
            static int var = 5;
            printf("%d ",var--);
            if(var)
                        main();
            }


Solution :

The output is 5 4 3 2 1
 When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively. 




13.What will be the output for the following snippet ?

int main()
{
            extern int i;
            i=20;
            printf("%d",i);
}

Solution:
Linker Error : Undefined symbol '_i'
Explanation: 
                        extern storage class in the following declaration,
                                    extern int i;
specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .






14. What will be the output for the following code?

int main()
{
            int i=-1,j=-1,k=0,l=2,m;
            m=i++&&j++&&k++||l++;
            printf("%d %d %d %d %d",i,j,k,l,m);
}

Solution:
The output is 0 0 1 3 1

Variables i,j,k,l are incremented and the m value results one.




15.Predict output/error in the following snippet.
int main()
{
struct xx
{
      int x=3;
      char name[]="hello";
 };
struct xx *s;
printf("%d",s->x);
printf("%s",s->name);
}

Solution:
Compiler Error. In structures ,you should not initialize variables in declaration.


                               Program Type Question



1.Write a C Program / Algorithm for the following problem statement :

SCANIT circular regarding ‘Online Test’ is passed to all the second and third year classes.  The circular says “Today you have Online Test  at IT LAB between 5pm and 6pm”. Unfortunately the test is only for third year students. In IT LAB , only 85 students are allowed to attend the test. One of our SCAN IT member will be there to guide the students. He is having the list of all second and third year students. He should allow only third year students.

Sample Test Case:

Your Register No  : 115015104
Result : You are allowed Inside.

Your Register No : 116015048
Result : The test is only for third year students.

Your Register No: 172678183
Result : Sorry . You are not an IT student.

Your Register No: 115015048
Result : Only 85 students are allowed at a time.


Logic :
               while(1)
                {
                      * get student reg no.

                      * compare the given reg no. with the record.

                                 if( reg > 115015001 && reg <115015150 && count <86)
                                {
                                     print "You are Allowed"
                                     increment count value;
                                 }
                                 
                                 else if( reg > 116015001 && reg <116015150)
                                  {
                                      print "The test is only for third year students"; 
                                    }

                                    else if( reg > 115015001 && reg <115015150 && count >85)
                                {
                                     print "Already 85 students entered the LAB";
                                 }
                           
                               else 
                                {
                                     print "You are not an IT student !";
                                 }
                 }




2. Write a C Program to print a semicolon without using semicolon in the code.

Solution:

#include <stdio.h>
int main() 
{
   if (printf("%c ", 59)) 
    { 
    }

}




3. Write a  C program to print some numbers without using any numbers in the program .

  The program should satisfies the following condition :
* Don’t get user input (Command Line Argument / Scanf())
* Datatype conversion is not allowed.
* You should use only variable in the program that should be of int type and while printing in printf also you should use only ‘%d’. 


Solution :

** You can use any other idea **

Simple Logic :

int a;
a= printf("hiiiiiiii");
printf("%d",a);





4. Write a C program/algorithm for the following problem statement :

               Assume you have one secured file in your laptop. We need password to open that file. ( the password is chithvihar). Now the laptop is passed to all the 60 students in our class. But no one knows the password. The students entered the password randomly . The file is opened for eight students but none of the 60 students typed ‘chithvihar’. How is it possible ? How to overcome from this loophole ?

Solution:

#include<stdio.h>

int main(int argc, char *argv[])
{
    int flag = 0;
    char passwd[10];

    memset(passwd,0,sizeof(passwd));

    strcpy(passwd, argv[1]);

    if(0 == strcmp("chithvihar", passwd))
    {
        flag = 1;
    }

    if(flag)
    {
        printf("\n Password cracked \n");
    }
    else
    {
        printf("\n Incorrect password \n");

    }
    return 0;
}

Output Screen :

                              Click on the Image to ZOOM






The authentication logic in above password protector code can be compromised by exploiting the loophole of strcpy() function. This function copies the password supplied by user to the ‘passwd’ buffer without checking whether the length of password supplied can be accommodated by the ‘passwd’ buffer or not. So if a user supplies a random password of such a length that causes buffer overflow and overwrites the memory location containing the default value ’0′ of the ‘flag’ variable then even if the password matching condition fails, the check of flag being non-zero becomes true and hence the password protection is breached.




5. Write a program in C that returns 3 numbers from a function.

Logic :
               Using structures , you can return.

            ** Can use any New Idea **




           

                            Aptitude Question

1.You are trapped in a room with two doors...  One door lead to certain freedom and the other leads to a ton of worms that you'll have to eat.  You don't know which door goes to the worms and which door gets you out.

There are two guards in the room.  One guard always tells the truth and the other guard always lies.  You don't know which one is honest and which one is the liar.

If you just guessed, you'd have a 50-50 shot.

To figure out which door to choose, you get to ask one guard one question

What is your question and which guard will you ask?


Hint:
+ x - = -
- x + = -

Solution :

This is very tricky and one of the world's most famous logic problems.

Here's the question you should ask... and it doesn't matter which guard you ask:

"If I want freedom, what door will HE say I should go through?"

Think long and hard about it...

Let's say that the red door leads to freedom.

If you ask the honest guard, he will honestly tell you that the other guard will lie and tell you the blue door.

If you ask the lying guard, he will lie and tell you the honest guard will say the blue door.

They've both said the blue door -- the one that leads to the worms.

So, you ask your question... and, then, go through the OTHER door!






2. Use FIVE 0's and any math operators and get 120.

Solution:  
     (0! + 0! + 0! + 0! + 0!)!
  ==> (1+1+1+1+1)!
  ==>   5!
  ==>  5 * 4 * 3 * 2 * 1
  ==>  120.





3.Bridge Question

Solution:

               Question is wrong..



4. There are three boxes. One is labeled "APPLES" another is labeled "ORANGES". The last one is labeled "APPLES AND ORANGES". You know that each is labeled incorrectly. You may ask me to pick one fruit from one box which you choose.

How can you label the boxes correctly?

Solution:

Pick from the one labeled "Apples & Oranges". This box must contain either only apples or only oranges.
E.g. if you find an Orange, label the box Orange, then change the Oranges box to Apples, and the Apples box to "Apples & Oranges."



5. Mary's mum has four children.
The first child is called April.
The second May.
The third June.
What is the name of the fourth child?

Solution : Mary

Saturday, 31 August 2013

Program for using Ping command.

/*
Exercise : Ex 1(b)
Title : Program for using ping command..
Author :Aslam Jainul
*/

import java.net.*;
import java.io.*;
import java.net.InetAddress.*;
public class EX_1_B_Ping
{
public static void main(String args[])
{
try
{
DataInputStream obj= new DataInputStream(System.in);

System.out.println("Enter the IP/Name Address.....");

String user_str = obj.readLine();

String mystring ="ping " + user_str ;

Runtime r= Runtime.getRuntime();
Process p=r.exec(mystring);

DataInputStream obj1= new DataInputStream(p.getInputStream());

String  data=obj1.readLine();
while(data !=null )
{
System.out.println(data);
data=obj1.readLine();
}
}

catch(Exception E)
{
System.out.print("SOME ERROR OCCURED");
}
}
}

Output :



Program to find IP Address of a computer

/*
Exercise : Ex 1(a)
Title : Program to find IP Address of a computer..
Author :Aslam Jainul
*/

import java.net.*;
import java.io.*;
import java.net.InetAddress.*;
public class EX_1_A_IP
{
public static void main(String args[])
{
try
{
DataInputStream obj= new DataInputStream(System.in);

System.out.println("Enter the IP Address.....");

String user_str = obj.readLine();

System.out.println();
InetAddress ip =InetAddress.getByName(user_str);
if(ip.isReachable(3000))
{

System.out.print(" The given IP address is ACTIVE");
System.out.print(" \n\n");
System.out.print(" Host Name : ");
System.out.print(ip.getHostName());
System.out.print(" \n\n");
System.out.print(" Host Address : ");
System.out.print(ip.getHostAddress());
System.out.print(" \n\n");

}
else
{
System.out.println("The given IP address is INACTIVE");
}
}

catch(Exception E)
{
System.out.print("SOME ERROR OCCURED");
}
}
}

Output :




Wednesday, 28 August 2013

SCAN IT Online Test Answer - Day 2 ( 28 / 8 /2013 )



                               SCAN IT Online Test - Answers

                            Day 2 – 28/8/2013 (Wednesday)


                              Multiple Choice Question


1. 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.


2. 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.


3. 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.



4. What will be output when you will execute following c code?
void main()
{
int check=2;
switch(check){
case 1: printf("Microsoft");
case 2: printf("Amazon");
case 3: printf("Ebay");
case 4: printf("Zoho");
default: printf("Wipro");
}
}

Solution:
The output is ‘Amazon Ebay Zoho Wipro’ . There is no break statement.



5. What will be output if you will compile and execute the following c code?
#define x 5+2
void main()
{
 int i;
i=x*x*x;
printf("%d",i);
}

Solution:
   The output is 5+2*5+2*5+2 รจ 27 .. “7*7*7” is a wrong approach.



6. 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.


7. Which of the following structure declaration doesn’t require pass-by-reference?

a) struct{int a;}s;
main(){}

b) struct temp{int a;};
main(){
struct temp s;
}

c) struct temp{int a;};
main(){}
struct temp s;

d) None of the mentioned

Solution:
The answer is None of the mentioned.


8. 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

In the above program y value is 1 or 0.


9. What is the output for the following code ?

void main()
{
int a=10,b=20;
char x=1,y=0;
if(a,b,x,y)
{
printf("EXAM");
}
}

Solution: Nothing Will be printed.



10. What will be output when you will execute following c code?
void main()
{
int x=1;

switch(x<<2+x)
{
default:printf("Only");

case 4: printf("6 IT Students");

case 5: printf("got");

case 8: printf("Placed");
}
}

Solution:
 ” Placed”… It’s doing operation of left shift. And then checking the switch condition.


11. 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 the correct one.
Solution:: All Four are correct.


12. 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 . In this problem , auto and register values will not change but in case of static variable , k is initialized only once across three calls of fun(). So the first time k outputs 1 then 2 then 3.


13. What will be output if you will compile and execute the following c code?
void main()
{
char *str="Hello world";
printf("%d",printf("%s",str));
}

Solution: Hello world11 .. Hello world for string and 11 for the length of that string.


14. What will be the output for the following snippet ?
void main()
{
int i=0;
for(;i<=2;)
printf(" %d",++i);
}

Solution: 1 2 3 . Each time ‘i’ value is incremented and it is printed.


15. 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: 10


16. What is the output of this C code?
void main()
{
int b = 5 & 4 | 6;
printf("%d", b);
}

Solution: 6 .



17. What will be output if you will compile and execute the following c code?
void main()
{
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}


Solution:

In Linux environment , it will print 19.
In Turbo C , it will print 21.
In JAVA , C# , it will print 18.



18. What is the output of the following code?
void main()
{
int s=0;
while(s++<10)
{
if(s<4 && s<9)
continue;
printf("%d",s);
}
}

Solution: 4 5 6 7 8 9 10


19. Find the output for the following.
int main()
{
int i = 1;
if (i++ && (i == 1))
printf("Tanjore");
else
printf("Trichy");
}
Solution: Trichy


20. 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.


                 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 subtract two numbers without using subtraction operator.

Solution:

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



3. Write a c program which takes password from users.

Sample output:
Enter the password:******
You have entered: scanit

Solution:

printf("Enter the password:");

while((p=getch())!= 13)
{
   password[i++] = p;
   printf("*");
}
password[i] = '\0';
printf("\nYou have entered: %s",password);


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. Arun 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);



                  Aptitude Questions.

1. You just bought TEN trees...
You want to plant them in FIVE rows with FOUR trees in each row.
How are you going to do it?

Solution:



2. Can you find the numbers A, B, C and D so that the following works?
A B C D x 4 = D C B A

Solution: A = 2 ; B = 1 ; C = 7 ; D =8. 2178 * 4 = 8712


3. 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..




4. How many times per day do a clock hands overlap ?
Solution:
22 Times Per day . The times are 12:00 1:05 , 2:11 , 3:16 , 4:22 , 5:27, 6:33, 7:38 ,8:44 , 9:49, 10:55.. 11 th hour not having any pair.



5.A is two years older than B who is twice as old as C. If the total of the ages of A, B and C be 27, the how old is B?A is two years older than B who is twice as old as C. If the total of the ages of A, B and C be 27, the how old is B?

Solution:
From the problem, A = B +2, B = 2 x C and A+B+C = 27
--> A + B + C = 27
--> (B + 2) + B + (B/2) = 27
--> 2B + 2 + (B/2) = 27
--> 2B + (B/2) = 25
--> 4B + B = 25 * 2
--> B = 10