Saturday 31 August 2013

C - Aptitude - 1



Some C Aptitude questions that are often questioned in interviews are listed below.
You can find the answers at the bottom of the page.



Questions :
1. What can be said of the following program  ?
          main()
             {
                enum Months {JAN =1,FEB,MAR,APR};
                Months X = JAN;
                if(X==1)
                   {
                     printf("Jan is the first month");                                                              
                  }
             }
          a) Does not print anything
          b) Prints : Jan is the first month
          c) Generates compilation error
          d) Results in runtime error



2.main()
{
            extern int i;
            i=20;
printf("%d",i);
}
a) 20
        b) Garbage Value
        c) Compilation error
        d) Linker error


                       

3.         enum colors {BLACK,BLUE,GREEN}
 main()
{
 
 printf("%d..%d..%d",BLACK,BLUE,GREEN);
  
 return(1);
}
a) 5..4..5
b) Garbage Value
c) 0..1..2
d) Error





  4. Given the following statement enum day = { jan = 1 ,feb=4, april, may} What is the value of may?
      (a) 4
      (b) 5
      (c) 6
      (d) 11
      (e) None of the above




5. What is the output of the following program?
                 main()
                        {
                          int l=6;
                          switch(l)
                          { default : l+=2;
                            case 4: l=4;                                                                           
                            case 5: l++;
                            break;
                            }
                            printf("%d",l);                                                                                      
                              }
                    a)8 b)6 c)5 d)4 e)none


6.       main()
{
            int i=3;
            switch(i)
             {
                default:printf("zero");
                case 1: printf("one");
                           break;
               case 2:printf("two");
                          break;
              case 3: printf("three");
                          break;
              } 
}
a) zero
b) three
c) Compiler error
d) Runtime error





7.         #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
                break;
 case j:  printf("BAD");
               break;
 }
}
a) GOOD
b) BAD
c) Compiler error
d) Runtime error





8.         main()
{
                        float i=1.5;
            switch(i)
                        {
                        case 1: printf("1");
                                    case 2: printf("2");
                                    default : printf("0");
            }
}
a) 1
b) 0
c) Compiler error
d) Runtime error



                      

9. Output of the following program is
main()
{
int i=0;
for(i=0;i<20;i++)
{
switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;
}
printf("%d,",i);
}
}

a) 0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error







10. main()
 {
 int i;
 for(i=0;i<3;i++)
 switch(i)
 {
 case 1: printf("%d",i);
 case 2 : printf("%d",i);
 default: printf("%d"i);
 }
 }
a) 0111222
b) Error
c)012
d)011122




11.  what is printed when the following program is compiled and executed?
           int    func (int x)
              {
                if (x<=0)
            return(1);
            return func(x -1) +x;                                                                                                 
              }
              main()
                    {
                       printf("%d\n",func(5));
                     }
            a) 12
            b) 16
            c) 15
            d) 11


 

12. What is the output of the following program?
            main()
                   {
                    int x=20;
                    int y=10;
                    swap(x,y);
                    printf("%d %d",y,x+2);
                  }
                     swap(int x,int y)
                             {
                               int temp;
                               temp =x;
                               x=y;
                                y=temp;
                             }
                
a)       10,20 b) 20,12 c) 22,10 d)10,22 e)none




13. Which of the following about the following two declaration is true
       i ) int *F()
       ii) int (*F)()
 Choice :
      a) Both are identical
      b) The first is a correct declaration and the second is wrong
      c) The first declaraion is a function returning a pointer to an integer and the second is a pointer to            function returning int
      d) Both are different ways of declarin pointer to a function   




 14. main()
{
        printf("%p",main);
}
a) Compiler error
b) Stack Overflow
c) Some address will be printed
d) Garbage value printed recursively



          

15.        main()
{
clrscr();
}
clrscr();

a) Syntax error
b) No output/error
c) Stack  Overflow
d) Linker error
           




16.       main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}
a) Error
b) 1
c) 2
d) 4


 


17.        main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}
a) prints I'm the greatest
b) Compiler error
c) Runtime error
d) prints 1







18.        main()
            {
            main();
            }
a) Syntax error
b) No output/error
c) Stack  Overflow
d) Linker error







19.        What are the following notations of defining functions known as?
i.      int abc(int a,float b)
                        {
                        /* some code */
 }
ii.    int abc(a,b)
        int a; float b;
                        {
                        /* some code*/
                        }





             
20.    What is printed when this program is executed
         main()
                {
                   printf ("%d\n",f(7)); 
                }
                  f(X)
                      {
                       
 if ( x<= 4)
                         return x;

                         return f(--x);
                      }
              a) 4
              b)5                                                                               
              c) 6
              d) 7
               



Answers : 
1 . b

2. Linker Error : Undefined symbol '_i'
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 .

3. 0..1..2
 enum assigns numbers starting from 0, if not explicitly defined.

4. c

5. c 

6. three
The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match. 

7. Compiler Error: Constant expression required in function main.
The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Enumerated types can be used in case statements. 

8. Compiler Error: switch expression not integral
Switch statements can be applied only to integral types. 

9. d 

10. 011122

11. b

12. d

13. c

14. Some address will be printed.
Function names are just addresses (just like array names are addresses).
main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers.

15.  No output/error 
The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).

16.  1
Scanf returns number of items successfully read and not 1/0.  Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

17. Compier error: Type mismatch in redeclaration of show.
When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.
The solutions are as follows:
1. declare void show() in main() .
2. define show() before main().
3. declare extern void show() before the use of show().

18.  Runtime error : Stack overflow.
main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.

19. i. ANSI C notation
ii. Kernighan & Ritche notation

20. a




Hope this is useful :) 






 

Saturday 3 August 2013

World's Top 10 Companies



Here is the list of World's Top 10 Companies...
This list is based on sales, profits, assets and market value of the companies.



1.ICBC



Info

Country : China
Industry : Banking


China’s Industrial & Commercial Bank of China (ICBC) takes the top spot on this list for the first time. Sales were up 24% and profits 17%.

2.China Construction Bank


 

Info

Country : China
Industry : Banking

The China Construction banks leapt eleven places to become the second largest company in the world. China Construction Bank is expected to increase its presence outside of China this year.


3.JP Morgan Chase


 

Info

Country : U.S.
Industry : Banking

JP Morgan Chase took the third spot this year despite having to write off at least $6 billion in trading losses from one trader, known as the London Whale.



4.General Electric


 

Info

Country : U.S.
Industry : Conglomerate

General Electric, the largest conglomerate in the U.S. plans to stay focused on its industrial businesses. Earlier this year, GE announced that it would sell its remaining stake in NBC Universal to Comcast.





5.Exxon Mobil


 

Info

Country : U.S.
Industry : Oil & Gas Operations

Despite being the most profitable company in the world, Exxon Mobil, last year’s number one, drops down to the fifth spot in 2013. 


6.HSBC Holdings


 

Info

Country : U.K.
Industry : Banking

London-based HSBC continues to focus on growth, especially in the European market. The bank recently sold its stake in China’s Ping An Insurance for $7.4 billion. 

7.Royal Dutch Shell


 

Info

Country : Netherlands
Industry : Oil & Gas Operations
Royal Dutch Shell slips back to the number 2 spot in sales and to 7th overall. The company owns 44,000 service stations around the world, operates over 30 refineries and produced 3.3 million barrels of oil per day in 2012. 

8.Agricultural Bank of China


 

Info

Country : China
Industry : Banking

Another of China’s high performing banks. The Agricultural Bank of China jumped eleven spots to number 8 on the list, helped by double-digit growth in sales, profits and assets.

9.Berkshire Hathaway


 

Info

Country : U.S.
Industry : Financial
Profits at Berkshire Hathaway were up 45% in 2012. The company announced in February that it would partner with billionaire Jorge Paulo Lemann’s 3G Capital to buy H.J. Heinz for a reported $28 billion, including debt.


9.PetroChina


 

Info

Country : China
Industry : Oil & Gas Operations
PetroChina, the largest producer of oil and gas in China, saw profits fall in 2012 due to the economic slowdown and state-controlled fuel prices.





This list is based on Forbes List. 



To see the list of Top 10 Billionaires, visit the link below

Top 10 Billionaires