Sunday, 13 October 2013

TCS C programming Questions ...

1.  What is the error message/output in the following program?
        void main( )
         {
                clrscr( )
                    printf(“Welcome”);

a)   I don’t know
b)  Semicolon missing 
c)  Welcome
d)  Statement missing

2. Check the below program
main( )
                {
                     printf(“%d”,a/0));
                }
a)       Division by Zero (Warning)
b)       Undefined Symbol ‘a’ (Error)
c)       0 (Output)
d)       None of the above

3. Input function in ‘C’
a)       printf() & scanf( )                  
b)  scanf( ) & stdio.h                            
c)  scanf( )              
d)  All the Above

4. Display the output
                main( )
                { 
                printf(“Welcoem to ‘C’ language”);
                }
a)       Welcome to ‘C’ language
b)       Welcome to “C” language
c)       Welcoem to ‘C’ language
d)       Welcoem to ‘C’ language.

5. “stdio.h” and “conio.h” are often called as 
a)    Pre-defined functions          
 b)  Library functions             
c)  Library files      
d)  Only C compiler knows 


6. Which of the following is used to clear the output screen? 
a)   clearscr()           
b) clearscreen()             
c) clrscr()           
d) clrscreen()

7. Division by 0 gives 
a)  Compiler error   
b) Runtime error          
c) Assembler error     
d)None of the Above

8. Which of the following denote special operators in C? 
a)      ?:                       
b)  ,                               
c)  >                              
d)  None of the Above

9. Which of the following denote invalid character constants in C? 
a)      's'          
b). '$'                 
c). 'E'                
d). 'wer'

10. The long integer is denoted as
a)      l                        
b)  ld                  
c)  Any of A or B                       
d) None of the Above

11. Relational operator in C compares two 
a)   Operands           
b) operators       
c). Both A and B            
d). None of the Above

12. Which of the following functions can be used to enter an integer in C program? 
a)    getche               
b). gets              
c). scanf            
d). None of the Above

13. What is the Boolean value of a false statement resulting in a C program? 
a)     1                       
b)  0                 
 c)  -1                 
d)  None of the Above

14. What is the output of the following program?
main() 
     int a=10,b=25; 
     a=b++ + a++; 
     b= ++b + ++a; 
     printf(“%d%dn”,a,b); 
a)    35 65                 
b)  36 64                        
c)  37 64                        
d)  34 64

15. What is the output of the following program?
main()
     char t1[]=“Exforsys”; 
     char t2[]= “Training”; 
     printf(“%s”,t1); 
a)     E                     
b)  Exforsys                   
c). NULL                      
d). Exforsys Training

16. The modifier used for horizontal tab is 
a)  h           
b) ht                 
c) t                   
d) th

17. What is the output of the statement?
printf("Exforsys Training \nis \n Very Good"); 

a)      Exforsys 
            Training 
             is Very Good
b)      Exforsys Training 
             is Very Good
c)      Exforsys Training is Very Good
d)      Exforsys Training 
             is
            Very Good
  
18. The code used for denoting unsigned decimal integer in scanf is 
a)      %d                    
b)  %u                           
c)  %ud                                     
d)  None of the Above

19. Which takes the first precedence in evaluation among the following operators? 
a)    ||                        
b)  !                               
c)  &&            
d)  All the Above has equal precedence

20. The unsigned char have maximum value of 
a)     128                    
b)  300                           
c)  255               
d)  240


Answers:::::::::::


  1. d
  2. b
  3. c
  4. c
  5. c
  6. c
  7. b
  8. d
  9. a
  10. c
  11. a
  12. a
  13. c
  14. b
  15. c
  16. b
  17. c
  18. d
  19. b
  20. a

HCL Question Paper

1) Given the following statement

enum day = { jan = 1 ,feb=4, april, may}
What is the value of may?

2) Find the output for the following C program

main( )
{
int x, j, k;
j=k=6; x=2;
x=j*k;
printf("%d", x);
}

3) main( )
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}

4) Find the output for the following C program
main( )
{
intx=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n", x,y);
}

5) Find the output for the following C program
main( )
{
int x=5;
printf("%d %d %d\n",x, x<<2,x>>2);
}

6)  Find the output for the following C program

#define  swap1(a,b)  a=a+b;b=a-b;a=a-b;
main( )
{
int x=5,y=10;
swap1(x,y);
printf("%d %d\n",x,y);
swap2(x,y);
printf("%d %d\n",x,y);
}
//------------------------------
int swap2(int a,int b)
{
int temp;
temp=a;
b=a;
a=temp;
return;
}

7) Find the output for the following C program

main()
{
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}

8) Find the output for the following C program

#include< stdio.h>
main()
{
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}

9) Find the output for the following C program

#include< stdio.h>
main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}

10) The following variable is available in file1.c

static int average_float;

11) Find the output for the following C program

 # define TRUE 0
some code
while(TRUE)
{
some code
}

12) Find the output for the following C program

main( )
{
int x=10,y=15;
x=x++;
y=++y;
printf("%d %d\n" ,x ,y );
}

13) Find the output for the following C program

main( )
{
int a=0;
if(a=0) printf("Ramco Systems\n");
printf("Ramco Systems\n");
}

14) Display the Output
main( )
{
int x=2;
if(!x)
printf("Hello");
else
printf("Welcome");
}

15) Find the output for the below program
main( )
{
int x=10;
if(x==10);
printf("C programing");
printf("C tutor");
}

Answers::::::::::::::::::::::::

1)  6
2)  36
3)  An empty string
4)  57 ,  94
5)  5 ,  20,  1
6)  10,  5
7)  Samco Systems
8)  Compilation error giving it cannot be an modifiable 'Lvalue'
9)  RamcoSystems
10) All the functions in the file1.c can access the variable
11) This won't go into the loop as TRUE is defined as 0
12) 11,  16
13) Ony one time "Ramco Systems" will be printed
14) Welcome
15)  C programming, C tutor

ADD 2 no. without using semicolon in addition statement

#include<conio.h>
#include<stdio.h>
#define a 30;
void main()
{
int b=10;
int c;
c=b+a
printf("%d",c);
getch();
}

Check ur AbILIty

C  Questions

Note : All the programs are tested under Turbo C/C++ compilers. It is assumed that, Programs run under DOS environment, The underlying machine is an x86 system, Program is compiled using Turbo C/C++ compiler. The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).

Predict the output or error(s) for the following:
1. Look at the sample program and predict the output
void main()
{
            int  const * p=5;
            printf("%d",++(*p));
}
Answer:
                        Compiler error: Cannot modify a constant value.

Explanation:   
                       p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

2. Look at the sample program and predict the output
main()
{
            char s[ ]="man";
            int i;
            for(i=0;s[ i ];i++)
            printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer:
                        mmmm
                       aaaa
                       nnnn

Explanation:
                   s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally  array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the  case of  C  it is same as s[i].

3. Look at the sample program and predict the output
 main()
{
            float me = 1.1;
            double you = 1.1;
            if(me==you)
printf("I love U");
else
                        printf("I hate U");
}
Answer:
               I hate U

Explanation:
                   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(== , >, <, <=, >=,!= ) . 

4. Look at the sample program and predict the output
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. 

5. Look at the sample program and predict the output
main()
{
             int c[ ]={2.8,3.4,4,6.7,5};
             int j,*p=c,*q=c;
             for(j=0;j<5;j++) {
                        printf(" %d ",*c);
                        ++q;     }
             for(j=0;j<5;j++){
printf(" %d ",*p);
++p;     }
}

Answer:
                        2 2 2 2 2 2 3 4 6 5

Explanation:
                   Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

6. Look at the sample program and predict the output

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

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

 7. Look at the sample program and predict the output
 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);
}
Answer:
                        0 0 1 3 1

Explanation :
                    Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR (||) operator. So the expression  i++ && j++ && k++’ is executed first. The result of this expression is 0    (-1 && -1 && 0 = 0). Now the expression is 0 || 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 || 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

8. Look at the sample program and predict the output
main()
{
            char *p;
            printf("%d %d ",sizeof(*p),sizeof(p));
}

Answer:
                        1 2

Explanation:
                    The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

9. Look at the sample program and predict the output
main()
{
            int i=3;
            switch(i)
             {
                default:printf("zero");
                case 1: printf("one");
                           break;
               case 2:printf("two");
                          break;
              case 3: printf("three");
                          break;
              } 
}
Answer :
               three

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

10. Look at the sample program and predict the output
 main()
{
              printf("%x",-1<<4);
}
Answer:
              fff0

Explanation :
                   -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

11. Look at the sample program and predict the output
main()
{
            char string[]="Hello World";
            display(string);
}
void display(char *string)
{
            printf("%s",string);
}
Answer:
             Compiler Error : Type mismatch in redeclaration of function display

Explanation :
                      In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.