c and c++ techniqual interview questions

25
C-Dac Sample quesion paper - pattern 1 Fundamentals of Programming 1.The programming language that was designed for specifying algorithm Address ASCII ALGOL None of these options 2. _____ contains the addresses of all the records according to the contents of the field designed as the record key. Index<------ans Subscript Array File 3. _________ symbol is used for Processing of data. Oval Parallelogram<------ans Rectangle Diamond 4. __________ is the analysis tool used for planning program logic Protocol None of these options PROLOG Pseudocode 5. Machine language has two part format the first part is__________ and the second part is __________ OPCODE,OPERAND<------ans OPERAND,OPCODE DATA CODE,OPERAND OPERAND,CODEOP 6. Language Primarily used for internet-based applications ADA C++ JAVA;------ans FORTRAN 7. _________ is a point at which the debugger stops during program execution and awaits a further command.

Upload: bindur87

Post on 07-Mar-2015

53 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: C and C++ Techniqual Interview Questions

C-Dac Sample quesion paper - pattern 1

Fundamentals of Programming 1.The programming language that was designed for specifying algorithmAddressASCIIALGOLNone of these options 

2. _____ contains the addresses of all the records according to the contents of the field designed as the record key.Index<------ansSubscriptArrayFile 

3. _________ symbol is used for Processing of data.OvalParallelogram<------ansRectangleDiamond 

4. __________ is the analysis tool used for planning program logicProtocolNone of these optionsPROLOGPseudocode 

5. Machine language has two part format the first part is__________ and the second part is __________OPCODE,OPERAND<------ansOPERAND,OPCODEDATA CODE,OPERANDOPERAND,CODEOP 

6. Language Primarily used for internet-based applicationsADAC++JAVA;------ansFORTRAN 

7. _________ is a point at which the debugger stops during program execution and awaits a further command.Memory DumpWatch point<------ansBreak pointNone of these options 8. ________do not contain any program logic and are ignored by the language ProcessorProtocolVirusCommentNone of these options 

Page 2: C and C++ Techniqual Interview Questions

9. The component of data base management system is ________Data definition LanguageData manipulation LanguageData definition Language and Data manipulation LanguageNone of these options 10. The quality of Algorithm is judged on the basis of_________Time requirementMemory RequirementAccuracy of solutionAll of these options<------ans 

11. Advantages of using flow charts isEffective AnalysisEfficient CodingTime consumingEffective Analysis and Efficient Coding<-----ans 

Programming in C 12. The Real constants in C can be expressed in which of the following formsFractional form onlyExponential form onlyASCII form onlyBoth Fractional and Exponential forms<------ans 

13. The program, which translates high-level program into its equivalent machine language program, is calledTransformerLanguage processorConverterNone of these options<------ans<!--[if !supportEmptyParas]--> 

14. Consider the following statements. i.Multiplication associates left to right ii.Division associates left to rightiii.Unary Minus associates right to leftiv.subtraction associates left to right All are true <------ansonly i and ii are trueall are falseonly iii and iv are true 15. What will be the value of variable a in the following code?unsigned char a;a = 0xFF + 1;printf("%d", a);0xFF0x1000 <------ans0x0 

16. What is the output of the following program?#include <stdio.h>void main(){printf("\n10!=9 : %5d",10!=9);

Page 3: C and C++ Techniqual Interview Questions

}1<------ans0ErrorNone of these options 17. #include<stdio.h>void main(){int x=10;(x<0)?(int a =100):(int a =1000);printf(" %d",a);}Error<------ans1000100None of these options 18. Which of the following shows the correct hierarchy of arithmetic operations in C(), **, * or /, + or -(), **, *, /, +, -(), **, /, *, +, -(), / or *, - or + <-----Ans

19. What is the output of the following code?#include<stdio.h>void main(){int a=14;a += 7;a -= 5;a *= 7;printf("\n%d",a);}112<------ans9889None of these options 

20. What is the output of the following code? #include<stdio.h>#define T tvoid main(){char T = `T`;printf("\n%c\t%c\n",T,t);}ErrorT tT Tt t 

21. The statement that prints out the character set from A-Z, isfor( a = `z`; a < `a`; a = a - 1)printf("%c", &a);for( a = `a`; a <= `z`; a = a + 1

Page 4: C and C++ Techniqual Interview Questions

printf("%c", &a);for( a = `A`; a <= `Z`; a = a + 1)<----Ans printf("%c", a);for( a = `Z`; a <= `A`; a = a + 1)printf("%c", a); 22. The statement which prints out the values 1 to 10 on separate lines, isfor( count = 1; count <= 10; count = count + 1) printf("%d\n",count);for( count = 1; count < 10; count = count + 1) printf("%d\n",count);<------ansfor( count = 0; count <= 9; count = count + 1) printf("%d ",count);for( count = 1; count <> 10; count = count + 1) printf("%d\n",count); 23. What does the term `call-by-reference` refer to?Passing a copy of a variable into a function. Passing a pointer to a variable into a function. <------ansChoosing a random value for a variable.A function that does not return any values. 

24. What is the output of the following code? #include<stdio.h>void swap(int&, int&);void main(){int a = 10,b=20;swap (a++,b++);printf("\n%d\t%d\t",a, b);}void swap(int& x, int& y){x+=2;y+=3;}14, 2411, 21 <------ans10, 20Error 25. What is the output of the following program code #include<stdio.h>void abc(int a[]){a++;a[1]=612;}main(){char a[5];abc(a);printf("%d",a[4]);}100612Error<------ansNone of these options 26. which of the following is true about recursive functioni. it is also called circular definitionii. it occurs when a function calls another function more than onceiii. it occurs when a statement within the function calls the function itself iv. a recursive function cannot have a return statement within it"

Page 5: C and C++ Techniqual Interview Questions

i and iii<------ansi and iiii and ivi, iii and iv 27.What will happen if you assign a value to an element of an array whose subscript exceeds the size of the array?The element will be set to 0Nothing, its done all the timeOther data may be overwrittenError message from the compiler 

28. What is the output of the following code? #include<stdio.h>void main(){int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }}; printf("\n%d",**(*arr+1)+2+7);}16 <------ans711Error 29. If int s[5] is a one dimensional array of integers, which of the following refers to the third element in the array?*( s + 2 ) <------ans*( s + 3 )s + 3s + 2 

30. #include"stdio.h"main(){int *p1,i=25;void *p2;p1=&i;p2=&i;p1=p2;p2=p1;printf("%d",i);}The output of the above code is :Program will not compile <------ans25Garbage valueAddress of I 31. What is the output of the following code? void main(){int i = 100, j = 200;const int *p=&i;p = &j;printf("%d",*p);}100200 <------ans300

Page 6: C and C++ Techniqual Interview Questions

None of the above 

32. void main(){int i=3;int *j=&i;clrscr();printf("%d%d",++*j,*(&i));}What is the output of this program?3 34 3 <------ans4,address of i printedError:Lvalue required 33. What is the output of the following code? #include<stdio.h>void main(){int arr[] = {10,20,30,40,50};int *ptr = arr;printf("\n %d\t%d\t",*ptr++,*ptr);}10 2010 10<------ans20 2020 10 

34. Which of these are reasons for using pointers?1.To manipulate parts of an array2.To refer to keywords such as for and if3.To return more than one value from a function 4.To refer to particular programs more conveniently1 & 3 <------ansOnly 1Only 3All of the above 

35. struct num{int no;char name[25];};void main(){struct num n1[]={{25,"rose"},{20,"gulmohar"}, {8,"geranium"},{11,"dahalia"}};printf("%d%d" ,n1[2].no,(*&n1+2)->no+1);}What is the output of this program?8 88 9 <------ans9 88 , unpredictable 36. During initializing a union

Only one member can be initialised.

Page 7: C and C++ Techniqual Interview Questions

All the members will be initialised. Initialisation of a union is not possible.<------ansNone of these options 37. Self referential structure is onea. Consisting the structure in the parent structureb. Consisting the pointer of the structure in the parent structureOnly aOnly bBoth a and bNeither a nor b 38. Individual structure member can be initialized in the structure itselfTrueFalseCompiler dependentNone of these options 

39. Which of the following is the feature of stack?All operations are at one endIt cannot reuse its memoryAll elements are of different data typesAny element can be accessed from it directly<------ans 40. When stacks are createdAre initially empty<------ansAre initialized to zeroAre considered fullNone of these options 

41. What is time required to insert an element in a stack with linked implementation?(1)(log2n)<------ans(n)(n log2n) 

42. Which of the following is the feature of stack?All operations are at one endIt cannot reuse its memoryAll elements are of different data typesAny element can be accessed from it directly<------ans 43. Time taken for addition of element in queue is(1)(n)(log n)<------ansNone of these options 44. When is linear queue said to be empty ? Front==rear Front=rear-1Front=rear+1Front=rear<------ans 

45. When queues are createdAre initially empty<------ansAre initialized to zeroAre considered fullNone of the above 

Page 8: C and C++ Techniqual Interview Questions

46. What would be the output of the following program?#include <stdio.h>main(){printf("\n%c", "abcdefgh"[4]);}abcdefghde <------anserror

47. Select the correct C code which will read a line of characters(terminated by a \n) from input_file into a character array called buffer. NULL terminate the buffer upon reading a \n.

int ch, loop = 0; ch = fgetc( input_file ); while( (ch != `\n`)&& (ch != EOF) ){buffer[loop] = ch; loop++; ch = fgetc(input_file );} buffer[loop] = NULL; 

int ch, loop = 0; ch = fgetc( input_file ); while( (ch = "\n")&& (ch = EOF)) { buffer[loop] = ch; loop--; ch = fgetc(]input_file ); } buffer[loop]= NULL; int ch, loop = 0; ch = fgetc( input_file ); while( (ch <> "\n")&& (ch != EOF) ) { buffer[loop] = ch; loop++; ch = fgetc(input_file ); } buffer[loop] = -1; None of the above 48. What is the output of the following code ?void main(){int a=0;int b=0;++a == 0 || ++b == 11;printf("\n%d,%d",a,b);}0, 11, 1 <------ans0, 01, 0 

49. What is the output of the following program? #define str(x)#x#define Xstr(x)str(x)#define oper multiplyvoid main(){char *opername=Xstr(oper);printf("%s",opername);}opernameXstrmultiply <------ansXstr 

50. What is the output of the following code ? #include<stdio.h>#include<string.h>void main(){char *a = "C-DAC\0\0ACTS\0\n"; printf("%s\n",a); }

Page 9: C and C++ Techniqual Interview Questions

C-DAC ACTSACTSC-DAC <------ansNone of these 

51. #include<stdio.h>void main(){while (1){if (printf("%d",printf("%d")))break;elsecontinue;}}The output isCompile time errorGoes into an infinite loopGarbage values <------ansNone of these options 52. Select the correct C statements which tests to see if input_file has opened the data file successfully.If not, print an error message and exit the program.if( input_file == NULL ) { printf("Unable to open file.\n");exit(1); }

if( input_file != NULL ) { printf("Unable to open file.\n");exit(1); }while( input_file = NULL ) { printf("Unable to open file.\n");exit(1);}None of these options 53.The codeint i = 7;printf("%d\n", i++ * i++);prints 49prints 56 <------ansis compiler dependentexpression i++ * i++ is undefined 54. Recursive procedure are implemented byLinear listQueueTreeStack<------ans

55. Which of these are reasons for using pointers?1. To manipulate parts of an array2. To refer to keywords such as for and if3. To return more than one value from a function 4. To refer to particular programs more conveniently1 & 3<------ansonly 1only 3None of these options 

56. The expression x = 4 + 2 % -8 evaluates to -664

Page 10: C and C++ Techniqual Interview Questions

None of these options

57. What is the output of the following code? #include<stdio.h>main(){register int a=2;printf("\nAddress of a = %d,", &a); printf("\tValue of a = %d",a);Address of a,2 <------ansLinker errorCompile time errorNone of these options 

58. What is the output of the following code? #include<stdio.h>void main(){int arr[]={0,1,2,3,4,5,6};int i,*ptr;for(ptr=arr+4,i =0; i<=4; i++) printf("\n%d",ptr[-i]);(as the 0=4,for -1 it becomes =3)}Error6 5 4 3 20 garbage garbage garbage garbage4 3 2 1 0 <------ans

59. Which of the following is the correct way of declaring a float pointer:float ptr;float *ptr; <------ans*float ptr;None of the above 60.If the following program (newprog) is run from the command line as:newprog 1 2 3 What would be the output of the following?void main (int argc, char*argv[]){int I,j=0;for (I=0;I<argc;I++)j=j + atoi(argv[I]);printf("%d",j);}1236123Compilation error<------ans 

Prodex Paper1.x=3    function(++x)...value 4 is passed to the function 

2 x=3   function(x++)...value 3 is passed to the function 

Page 11: C and C++ Techniqual Interview Questions

3.some ques on file opening...    if(name)..(exixts)     {      ...      }   the file can be opened 

4.if(!name)...(not exixts)    {     ...     }   the file cant b opened 

5. a for loop ques  does not print array...condition not satisfied     a[10]={10,14,18,20} 

6.another for loop ques prints correctly...condition satisfied 

7.main()    {      function(x,y);     }    void function(int *x,int *y)     {      .....     } the function does not work. 

8.A d();    a j;    it works well 

Sample Technical Paper        ( C and C++)

1. Point out error, if any, in the following program

main()

 {

  int i=1;

  switch(i)

  {

    case 1:

           printf("\nRadioactive cats have 18 half-lives");

           break;

   case 1*2+4:

          printf("\nBottle for rent -inquire within");

          break;

}

}

    Ans. No error. Constant expression like   1*2+4 are acceptable in cases of a switch. 

2. Point out the error, if any, in the following

program                                                         

Page 12: C and C++ Techniqual Interview Questions

main()

{

    int a=10,b;

    a>= 5 ? b=100 : b=200; 

    printf("\n%d",b);

}

Ans. lvalue required in function main(). The second assignment should be written in

parenthesis as follows:

 a>= 5 ? b=100 : (b=200);

3. In the following code, in which order the functions would be called?

    a= f1(23,14)*f2(12/4)+f3();

     a) f1, f2, f3 b) f3, f2, f1

    c) The order may vary from compiler to compiler d) None of the above

4. What would be the output of the following program?

main()

{

   int i=4;

   switch(i)

   {

    default:

          printf("\n A mouse is an elephant built by the Japanese");                                         

    case 1:

             printf(" Breeding rabbits is a hair raising experience");

             break;

case 2:

           printf("\n Friction is a drag");

           break;

case 3:

          printf("\n If practice make perfect, then nobody's perfect");

}

}

a) A mouse is an elephant built by the Japanese b) Breeding rabbits is a hare raising

experience

c) All of the above d) None of the above

5. What is the output of the following program?

#define SQR(x) (x*x)

main()

   {

     int a,b=3;

     a= SQR(b+2);

     printf("%d",a);

   }

   a) 25    b) 11 c) error d) garbage value

6. In which line of the following, an error would be reported?                                      

1. #define CIRCUM(R) (3.14*R*R);

2. main()

3. {

4. float r=1.0,c;

Page 13: C and C++ Techniqual Interview Questions

5. c= CIRCUM(r);

6. printf("\n%f",c);

7. if(CIRCUM(r))==6.28)

8. printf("\nGobbledygook");

9. }

a) line 1 b) line 5 c) line 6    d) line 7

7. What is the type of the variable b in the following declaration?

#define FLOATPTR float*

FLOATPTR a,b;

    a) float b) float pointer c) int d) int pointer

8. In the following code;

#include<stdio.h>

main()

{

  FILE *fp;

  fp= fopen("trial","r");

 }     

fp points to:

a) The first character in the file. 

    b) A structure which contains a "char" pointer which points to the first character in the

file.

c) The name of the file. d) None of the above.                                                     

9. We should not read after a write to a file without an intervening call to fflush(),

fseek() or rewind() < TRUE/FALSE>

Ans. True

10.  If the program (myprog) is run from the command line as myprog 1 2 3 , What would

be the output?

main(int argc, char *argv[])

{

   int i;

   for(i=0;i<argc;i++)

   printf("%s",argv[i]);

  }

  a) 1 2 3    b) C:\MYPROG.EXE 1 2 3

c) MYP d) None of the above

11. If the following program (myprog) is run from the command line as myprog 1 2 3,

What would be the output?

main(int argc, char *argv[])

   {

   int i,j=0;

   for(i=0;i<argc;i++)

   j=j+ atoi(argv[i]);

   printf("%d",j);

  } 

a) 1 2 3     b) 6 c) error d) "123"

12. If the following program (myprog) is run from the command line as myprog monday

tuesday wednesday thursday,

What would be the output?

Page 14: C and C++ Techniqual Interview Questions

main(int argc, char *argv[])

   { 

   while(--argc >0)

   printf("%s",*++argv);

  }

  a) myprog monday tuesday wednesday thursday     b) monday tuesday wednesday

thursday

c) myprog tuesday thursday d) None of the above                                                   

13. In the following code, is p2 an integer or an integer pointer?

 typedef int* ptr

 ptr p1,p2;

 Ans. Integer pointer

14. Point out the error in the following program

main()

   {

   const int x;

   x=128;

   printf("%d",x);

  }

    Ans. x should have been initialized where it is declared.

15. What would be the output of the following program?

 main()

  {

   int y=128;

   const int x=y;

   printf("%d",x);

 }

    a) 128 b) Garbage value c) Error d) 0

16.  What is the difference between the following declarations?                                          

   const char *s;

   char const *s;

  Ans. No difference

17. What would be the output of the following program?

  main()

    {

    char near * near *ptr1;

    char near * far *ptr2;

    char near * huge *ptr3;

    printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3));

   }

     a) 1 1 1         b) 1 2 4       c) 2 4 4    d) 4 4 4

18. If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?

main(int argc, char*argv[])

  {

   printf("%c",**++argv);

Page 15: C and C++ Techniqual Interview Questions

  }

  a) m     b) f c) myprog d) friday

19. If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?                                                                                      

 main(int argc, char *argv[])

   {

    printf("%c",*++argv[1]);

     }

    a) r b) f c) m d) y

20.  If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?

main(int argc, char *argv[])

   {

  while(sizeofargv)

  printf("%s",argv[--sizeofargv]);

  }

a) myprog friday tuesday sunday b) myprog friday tuesday

c) sunday tuesday friday myprog d) sunday tuesday friday                               

21.  Point out the error in the following program

main()

  {

   int a=10;

   void f();

   a=f();

   printf("\n%d",a);

   }

   void f()

    {

    printf("\nHi");

    }

    Ans. The program is trying to collect the value of a "void" function into an integer

variable.

22.  In the following program how would you print 50 using p?

main()

   {

    int a[]={10, 20, 30, 40, 50};

    char *p;

    p= (char*) a;

   }

    Ans. printf("\n%d",*((int*)p+4));

23.  Would the following program compile?

main()

  {

   int a=10,*j;

   void *k;

   j=k=&a;

Page 16: C and C++ Techniqual Interview Questions

   j++;

   k++;

   printf("\n%u%u",j,k);

}

 a) Yes b) No, the format is incorrect

 c) No, the arithmetic operation is not permitted on void pointers                       

d) No, the arithmetic operation is not permitted on pointers

24. According to ANSI specifications which is the correct way of declaring main() when it

receives command line arguments?

a) main(int argc, char *argv[]) b) main(argc,argv) int argc; char *argv[];

c) main() {int argc; char *argv[]; } d) None of the above

25. What error would the following function give on compilation? 

  f(int a, int b)

      {

     int a;

     a=20;

     return a;

      }

a) missing parenthesis in the return statement b) The function should be declared as int

f(int a, int b)

    c) redeclaration of a    d) None of the above                                            

26. Point out the error in the following program

main()

    {

     const char *fun();

     *fun()='A';

    }

   const char *fun()

    {

     return "Hello";

   }

    Ans. fun() returns to a "const char" pointer which cannot be modified

27. What would be the output of the following program?

main()

   {

    const int x=5;

    int *ptrx;

    ptrx=&x;

    *ptrx=10;

    printf("%d",x);

 }

   a) 5      b) 10 c) Error d) Garbage value

28. A switch statement cannot include

    a) constants as arguments b) constant expression as arguments                                

    c) string as an argument d) None of the above

29. How long the following program will run?

main()

{

Page 17: C and C++ Techniqual Interview Questions

printf("\nSonata Software");

main();

}

a) infinite loop     b) until the stack overflows

c) All of the above d) None of the above

30. On combining the following statements, you will get char*p; p=malloc(100);

    a) char *p= malloc(100) b) p= (char*)malloc(100)

c) All of the above d) None of the above

31. What is the output of the following program?

main()

  {

   int n=5;

   printf("\nn=%*d",n,n);

  }

a) n=5   b) n=5   c) n= 5 d) error

Sonata Software Limited

Sample Technical Paper        

1. Point out error, if any, in the following program

main()

 {

  int i=1;

  switch(i)

  {

    case 1:

           printf("\nRadioactive cats have 18 half-lives");

           break;

   case 1*2+4:

          printf("\nBottle for rent -inquire within");

          break;

}

}

    Ans. No error. Constant expression like   1*2+4 are acceptable in cases of a switch. 

2. Point out the error, if any, in the following

program                                                         

main()

{

    int a=10,b;

    a>= 5 ? b=100 : b=200; 

    printf("\n%d",b);

}

Ans. lvalue required in function main(). The second assignment should be written in

parenthesis as follows:

 a>= 5 ? b=100 : (b=200);

Page 18: C and C++ Techniqual Interview Questions

3. In the following code, in which order the functions would be called?

    a= f1(23,14)*f2(12/4)+f3();

     a) f1, f2, f3 b) f3, f2, f1

    c) The order may vary from compiler to compiler d) None of the above

4. What would be the output of the following program?

main()

{

   int i=4;

   switch(i)

   {

    default:

          printf("\n A mouse is an elephant built by the Japanese");                                         

    case 1:

             printf(" Breeding rabbits is a hair raising experience");

             break;

case 2:

           printf("\n Friction is a drag");

           break;

case 3:

          printf("\n If practice make perfect, then nobody's perfect");

}

}

a) A mouse is an elephant built by the Japanese b) Breeding rabbits is a hare raising

experience

c) All of the above d) None of the above

5. What is the output of the following program?

#define SQR(x) (x*x)

main()

   {

     int a,b=3;

     a= SQR(b+2);

     printf("%d",a);

   }

   a) 25    b) 11 c) error d) garbage value

6. In which line of the following, an error would be reported?                                      

1. #define CIRCUM(R) (3.14*R*R);

2. main()

3. {

4. float r=1.0,c;

5. c= CIRCUM(r);

6. printf("\n%f",c);

7. if(CIRCUM(r))==6.28)

8. printf("\nGobbledygook");

9. }

a) line 1 b) line 5 c) line 6    d) line 7

7. What is the type of the variable b in the following declaration?

#define FLOATPTR float*

Page 19: C and C++ Techniqual Interview Questions

FLOATPTR a,b;

    a) float b) float pointer c) int d) int pointer

8. In the following code;

#include<stdio.h>

main()

{

  FILE *fp;

  fp= fopen("trial","r");

 }     

fp points to:

a) The first character in the file. 

    b) A structure which contains a "char" pointer which points to the first character in the

file.

c) The name of the file. d) None of the above.                                                     

9. We should not read after a write to a file without an intervening call to fflush(),

fseek() or rewind() < TRUE/FALSE>

Ans. True

10.  If the program (myprog) is run from the command line as myprog 1 2 3 , What would

be the output?

main(int argc, char *argv[])

{

   int i;

   for(i=0;i<argc;i++)

   printf("%s",argv[i]);

  }

  a) 1 2 3    b) C:\MYPROG.EXE 1 2 3

c) MYP d) None of the above

11. If the following program (myprog) is run from the command line as myprog 1 2 3,

What would be the output?

main(int argc, char *argv[])

   {

   int i,j=0;

   for(i=0;i<argc;i++)

   j=j+ atoi(argv[i]);

   printf("%d",j);

  } 

a) 1 2 3     b) 6 c) error d) "123"

12. If the following program (myprog) is run from the command line as myprog monday

tuesday wednesday thursday,

What would be the output?

main(int argc, char *argv[])

   { 

   while(--argc >0)

   printf("%s",*++argv);

  }

  a) myprog monday tuesday wednesday thursday     b) monday tuesday wednesday

thursday

c) myprog tuesday thursday d) None of the above                                                   

Page 20: C and C++ Techniqual Interview Questions

13. In the following code, is p2 an integer or an integer pointer?

 typedef int* ptr

 ptr p1,p2;

 Ans. Integer pointer

14. Point out the error in the following program

main()

   {

   const int x;

   x=128;

   printf("%d",x);

  }

    Ans. x should have been initialized where it is declared.

15. What would be the output of the following program?

 main()

  {

   int y=128;

   const int x=y;

   printf("%d",x);

 }

    a) 128 b) Garbage value c) Error d) 0

16.  What is the difference between the following declarations?                                          

   const char *s;

   char const *s;

  Ans. No difference

17. What would be the output of the following program?

  main()

    {

    char near * near *ptr1;

    char near * far *ptr2;

    char near * huge *ptr3;

    printf("%d %d %d",sizeof(ptr1),sizeof(ptr2),sizeof(ptr3));

   }

     a) 1 1 1         b) 1 2 4       c) 2 4 4    d) 4 4 4

18. If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?

main(int argc, char*argv[])

  {

   printf("%c",**++argv);

  }

  a) m     b) f c) myprog d) friday

19. If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?                                                                                      

 main(int argc, char *argv[])

   {

    printf("%c",*++argv[1]);

Page 21: C and C++ Techniqual Interview Questions

     }

    a) r b) f c) m d) y

20.  If the following program (myprog) is run from the command line as myprog friday

tuesday sunday,

What would be the output?

main(int argc, char *argv[])

   {

  while(sizeofargv)

  printf("%s",argv[--sizeofargv]);

  }

a) myprog friday tuesday sunday b) myprog friday tuesday

c) sunday tuesday friday myprog d) sunday tuesday friday                               

21.  Point out the error in the following program

main()

  {

   int a=10;

   void f();

   a=f();

   printf("\n%d",a);

   }

   void f()

    {

    printf("\nHi");

    }

    Ans. The program is trying to collect the value of a "void" function into an integer

variable.

22.  In the following program how would you print 50 using p?

main()

   {

    int a[]={10, 20, 30, 40, 50};

    char *p;

    p= (char*) a;

   }

    Ans. printf("\n%d",*((int*)p+4));

23.  Would the following program compile?

main()

  {

   int a=10,*j;

   void *k;

   j=k=&a;

   j++;

   k++;

   printf("\n%u%u",j,k);

}

 a) Yes b) No, the format is incorrect

 c) No, the arithmetic operation is not permitted on void pointers                       

d) No, the arithmetic operation is not permitted on pointers

Page 22: C and C++ Techniqual Interview Questions

24. According to ANSI specifications which is the correct way of declaring main() when it

receives command line arguments?

a) main(int argc, char *argv[]) b) main(argc,argv) int argc; char *argv[];

c) main() {int argc; char *argv[]; } d) None of the above

25. What error would the following function give on compilation? 

  f(int a, int b)

      {

     int a;

     a=20;

     return a;

      }

a) missing parenthesis in the return statement b) The function should be declared as int

f(int a, int b)

    c) redeclaration of a    d) None of the above                                            

26. Point out the error in the following program

main()

    {

     const char *fun();

     *fun()='A';

    }

   const char *fun()

    {

     return "Hello";

   }

    Ans. fun() returns to a "const char" pointer which cannot be modified

27. What would be the output of the following program?

main()

   {

    const int x=5;

    int *ptrx;

    ptrx=&x;

    *ptrx=10;

    printf("%d",x);

 }

   a) 5      b) 10 c) Error d) Garbage value

28. A switch statement cannot include

    a) constants as arguments b) constant expression as arguments                                

    c) string as an argument d) None of the above

29. How long the following program will run?

main()

{

printf("\nSonata Software");

main();

}

a) infinite loop     b) until the stack overflows

c) All of the above d) None of the above

Page 23: C and C++ Techniqual Interview Questions

30. On combining the following statements, you will get char*p; p=malloc(100);

    a) char *p= malloc(100) b) p= (char*)malloc(100)

c) All of the above d) None of the above

31. What is the output of the following program?

main()

  {

   int n=5;

   printf("\nn=%*d",n,n);

  }

a) n=5   b) n=5   c) n= 5 d) error

 ---------------------------------------------------------------------------------------------------------------