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

0 comments:

Post a Comment