Monday, February 28, 2011

Puzzles and Logical Reasoning Questions

Q:- There is a man lives on the 10th floor of a very tall building. Everyday he gets the elevator down to the ground floor to leave the building to go to work. Upon returning from work though, he can only travel half way up in the lift and has to walk the rest of the way unless it's raining! Why?


Ans:- The man is very, very short and can only reach halfway up the elevator buttons. However, if it is raining then he will have his umbrella with him and can press the higher buttons with it.



Q:- A man is wearing black. Black shoes, socks, trousers, coat, gloves and ski mask. He is walking down a back street with all the street lamps off. A black car is coming towards him with its light off but somehow manages to stop in time. How did the driver see the man?

Ans:- It is daytime

Q:- A pole in a lake one half of the pole is in the ground another one third of it is covered by water and 12 feet is out of water what is the total length of the pole in feet?

Ans:- 
        Assume height of pole as x.
        Then x/2 is in the ground and x/3 in the water. 
        So the total invisible portion of the pole is x/2 + x/3 which is 5x/6. 
        So, the visible portion of the pole is x- 5x/6 = x/6.
        So if x/6 = 12, x = 12 * 6 = 72

Q:- 500 men are arranged in an array of 10 rows and 50 columns according to their heights. Tallest among each row of all are asked to come out. And the shortest among them is A. Similarly after resuming them to their original positions, the shortest among each column are asked to come out. And the tallest among them is B. Now who is taller A or B ?

Ans:-
         Lets arrange this in matrix form
         x1 x2 x3.......................x50
         x51 .............................x100
         ....................................
         ....................................
         x451............................x500

        Condition-1:-  x50,x100,x150....x500
        among these shortest is x50->A
        Condition-2:- x1,x2,..........x50
        among these tallest is x50->B
        So Both Are Same




Thursday, February 17, 2011

DBMS Concepts Book

Recursive Main Function Calling

 #include <stdio.h>  
 main()  
 {  
         static int var=5;  
         printf("%d", var--);  
         if(var)  
                 main();  
 }  
 Answer: 5 4 3 2 1  

 Explanation:  
 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.  

RDBMS Infosys Book

Wednesday, February 16, 2011

Sorting Long Array which contains 0's and 1's by Traversing through the array only once


 class Sorting_Array_By_Traversing_Only_Once   
 {   
         public static void main(String[] args)   
         {   
                 int a[] = {0,1,0,1,0,0,0,1,1,1,1,0,0,0,1,0,1,1,0,1,0,1,0};   
                 int start=0;   
                 int end=a.length-1;   
                 while (start<end)   
                 {   
                         if (a[start]==0&&a[end]==0)   
                         {   
                                 start++;   
                         }   
                         else if (a[start]==1&&a[end]==0)   
                         {   
                                 a[start]=0;   
                                 a[end]=1;   
                                 start++;   
                                 end--;   
                         }   
                         else if (a[start]==1&&a[end]==1)   
                         {   
                                 end--;   
                         }   
                         else   
                         {   
                                 start++;   
                                 end--;   
                         }   
                 }   
                 for (int i=0; i<a.length; i++)   
                 {   
                         System.out.print(a[i]+" ");   
                 }   
         }   
 }  

DBMS Concepts

Database Concepts

Tuesday, February 15, 2011

Monday, February 14, 2011

Interpreters


Interpreters

An interpreter is another way of implementing a programming language. Interpretation shares many aspects with compiling. Lexing, parsing and type-checking are in an interpreter done just as in a compiler. But instead of generating code from the syntax tree, the syntax tree is processed directly to evaluate expressions and execute statements, and so on. An interpreter may need to process the same piece of the syntax tree (for example, the body of a loop) many times and, hence; interpretation is typically slower than executing a compiled program.



Compilation Phases


Compilation Phases

Lexical analysis:

            This is the initial part of reading and analyzing the program text: the text is read and divided into tokens, each of which corresponds to a symbol in the programming language, e.g., variable name, keyword or number.

Syntax analysis:

                This phase takes the list of tokens produced by the lexical analysis and arranges these in a tree-structure (called the syntax tree) that reflects the structure of the program. This phase is often called parsing.

Type checking:

This phase analyses the syntax tree to determine if the program violates certain consistency requirements, e.g., if a variable is used but not declared or if it is used in a context that doesn’t make sense given the type of a variable, such as trying to use a Boolean value as a function pointer.

Intermediate code generation

            The program is translated to a simple machine-independent intermediate language.

Register allocation

            The symbolic variable names used in the intermediate code are translated to number, each of which corresponds to a register in the target machine code.

Machine code generation

            The intermediate language is translated to assembly language (a textual representation of machine code) for specific machine architecture.

Assembly and linking

            The assembly language code is translated into binary representation and addresses of variables, functions, etc., are determined.
            The first three phases are collectively called the frontend of the compiler and the last three phases are collectively called the backend.  The middle part of the compiler is in this context only the intermediate code generation, but this often includes various optimizations and transformations on the intermediate code.