C Bank

1. Predict the output:
main()
{
   int a[2]={2,3} ;
   printf("%d%d",a[0],a[1] );
}
Answer: 23 

2. Debug the code: ( Write the corrected code )
main)
{
   int a = 10
   printf ( “%d, a” ) ;
}
Answer:
main(
  int a = 10 ; 
  printf ( “%d, a ) ; 
}
3. Answer this:
Which one of the following is the exit check loop ?
Answer: do..while

4. Predict the output:
main()
{
   int a = 2.5 ;
   if ( a == 2.5 )
      printf ( “Equal” ) ;
   else
      printf ( “Not equal” ) ;
}
Answer: Not equal
Explanation: 
Since variable 'a' is declared as int and 2.5 is assigned, implicit type conversion takes place and hence 2 is stored in 'a'.

5. Predict the output:
main()
{
   char A = 'a' ;
   int B = 'a' ;
   if ( A == B )
     printf ( “True” ) ;
   else
     printf ( “False” ) ;
}
Answer: True 
Explanation: 
Due to implicit type conversions.

6. Predict the output:
main() 
  int a[]={1, 5, 12} ; 
  printf("%d %d",a[0+1],a[1]+1 ); 
}
Answer: 5 6

7. Debug the code: ( Write the correct code – Output should be 5,3,2 )
main() 
{
  float a[2] = { 5, 3, 2 } ;
  printf ( “%d,%d”,a[0],a[1],a[2] ) ;
}
Answer:
main()
{
  int a[3] = { 5, 3, 2 };
  printf ( "%d,%d,%d", a[0], a[1], a[2] );
}

8. Debug the code: ( Output : SPC )
main() 
  char s[] = “SProC” ; 
  printf ( “%c%c%c”, 0[s], s[1], s[strlen(s)] ) ; 
}
Answer:
main() 
  char s[] = “SProC” ; 
  printf ( “%c%c%c”, 0[s], s[1], s[strlen(s)-1] ) ; 
}
Explanation:
Only s[strlen(s)] should be changed to s[strlen(s) – 1]
s[0] is internally interpreted as *(s + 0) [which means take the value at the location after moving 0 positions from the base address s]. By the commutative property of addition, s + 0 = 0 + s. Therefore *(s+0) = *(0+s) which is also equal to 0[s].
Hence, s[0] = *(s+0) = *(0+s) = 0[s].

9. Predict the output: 
main() 
{
  int a[5] ;
  printf ( “%d”, a[3] ) ; 
}
Answer:
Garbage value

10. Predict the output:
main() 
{
  char a[8] = “Welcome” ; 
  printf ( “%c,H”, a[8] ) ; 
}
Answer:
,H
Explanation:
Null character is stored at the 8th position. 

11. Output of the program will be: 
main() 
  int a = 40 ;
  printf("%d %d", a ); 
}
Answer: 40, <garbage>

12. What is the Output of the code:  
main() 
  int i; 
  printf("%d", &i)+1; 
}
Answer: Address of i
Explanation: +1 will be ignored; no error message

13. Output of the code will be:  
main() 
  int i = 100, j = 50 ; 
  printf("%d", i, j ); 
}
Answer: 100
Explanation: Format specifier takes the value of the first variable

14. Output of the program will be: 
main() 
{
  int i=0, j=0;
  scanf("%d"+scanf("%d %d", &i, &j));
  printf("%d %d", i, j); 
}
Answer: Input values

15. How will you print % character?
Answer: printf ( “\%” ); in Turbo C and printf ( "%%" ); in gcc

16. 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".

17. 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].

18. Predict the output
main()
{
   float me = 1.1;
   double you = 1.1;
   if(me==you)
      printf("C");
   else
      printf("C++");
}
Answer:
C++
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.

19. 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.

20. 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.
 

21. What will be the output ?
main() 
   int i=2, j=3, k=0 ; 
   int p=(i, j, k) ; 
   printf ( "%d\n", p ) ; 
}
Answer: 0
Explanation: The last value ( value of 'k' ) is assigned

22. What will be the output ?
main() 
  printf ( "%d ", printf ( "Computer" ) ) ; 
}
Answer: Computer 8
Explanation: The inner printf is executed first and returns the number of characters displayed.

23. What will be the output ?
main() 
   int v1=1, v2=4, v3=4 ; 
   v1=v2==v3 ; 
   printf ( "%d", v1 ) ; 
}
Answer: 1
Explanation: The '==' operator has higher precedence than '='

24. What will be the output ?
main() 
  int val= -- 3 ; 
  printf ( "%d", val ) ; 
}
Answer: 3
Explanation: '-' here acts as unary minus operator. Hence, - ( -3 ) = +3.
The decrement operator works only on variables.

25. What will be the output ?
main() 
   int ans=5 ; 
   printf ( "%d", ans=++ans==6 ) ; 
}
Answer: 1
Explanation: The precedence of the operators used in this code decreases as follows.
++, ==, =

26. What will be the output ? 
main() 
   int i=-1; 
   +i; 
   printf("+i = %d", +i) ; 
} Answer: -1
Explanation: It is just given as +i and it is not ++i

27. What will be the output ?
main() 
   switch ( printf ( "AB" ) ) 
   { 
       case 1 : 
          printf ( "AB" ) ; 
       case 2 : 
          printf ( "CD" ) ; 
          break ; 
       default: 
          printf ( "EF" ) ; 
    } 
}
Answer: ABCD
Explanation: The printf ( "AB" ) will display "AB" and returns 2 ( since 2 characters are printed ). So, case 2: is executed which displays "CD".
 
28. What will be the the value of 'i' after execution of switch ? 
main() 
   int i ; 
   for ( i = 0 ; i <= 3 ; i ++ ) 
   { 
      switch ( i ) 
      { 
          case 0 : 
              i += 1 ; 
          case 1 : 
              i += 2 ; 
          case 3 : 
              i += 3 ; 
          default: 
              i += 4 ; 
      } 
   } 
}
Answer: 10
Explanation: Since there is no break, all the cases execute and after the execution i becomes 10 and the loop condition becomes false.

29. What will be the output ?  
main() 
   int i,j; 
   i=j=2, 3; 
   while ( --i && j++ ) 
       printf("%d %d", i, j ) ; 
}
Answer: 1 3
Explanation: The statement i=j=2,3 assigns 2 ( first value ) to both i and j. 

30. What will be the output ?
main() 
   int a = 5, b = 10 ,c = 1; 
   if ( a && b > c ) 
       printf("Sona SProC" ) ; 
   else 
       break; 
}
Answer: Compilation error
Explanation: break; statement should be given within a loop or switch statement.

31. Which of the following functions read one character at a time? Answer: ifstream::get()
32. Which of the following functions support binary input/output? Answer: fstream::write()

33. Predict the output 
main() 
   float a=5, b=3.14, c=7.00 ;
   printf ( "%g %g %g", a, b, c ) ; 
}
Answer: 5 3.14 7
Explanation: %g format specifier displays only the significant digits.


34. What is the output of the following code [2 marks]
#define SQR(x) x * x 
main() 
   printf("%d", 225 / SQR(15)); 
Answer: 225
Explanation: 
           define just substitutes 15 * 15 in place of SQR(15). So, the statement is 225 / 15 * 15. Since both / and * have same order of precedence and possess left to right associativity, the expression is evaluated as 225 / 15 * 15 = 15 * 15 = 225.
35. What is the output of the following code
main() 
   int n2 = 5, n1 = 0 ; 
   n2 = n2 || n1 && printf ( "SProC" ) ; 
   printf ( "%d", n2 ) ; 
} Answer: 1
Explanation: 
              || and && operators return either 1 ( true ) or 0 ( false ). Since, n2 is 5, which is a non-zero number, it is true. If the first input is true, || will not evaluate the next expressions. Hence the statement terminates after assigning 1 ( true ) to n2.

36. What does the following statement do ?
if ( n & ( n – 1 ) )
Answer: Checks whether n is a power of 2
Explanation: 
            If the statement has been given as if ( n && ( n - 1 ) ), it will check if n and it's previous number are non-zero. But, '&' is the bitwise operator whereas '&&' is the logical operator.

37. How to obtain the remainder of division of a float by another float.Answer: Use the fmod function available in math.h
Explanation: 
         % operator cannot be used for floating numbers. Instead the fmod function has to be used.
38. Predict the output
main() 
   int i = 107, x = 5 ; 
   printf ( ( x > 7 ) ? "%d":"%c", i ) ; 
}
Answer: k
Explanation: The ternary operator can also be used within printf
39. What is the output of the following code
main() 
   int i ; 
   for ( i = 5 ; i < 2 ; i ++ ) ; 
      printf ( "%d", i ) ; 
}
Answer: 5
Explanation: The loop condition fails in the first time itself.


40. What is the output of the following code 
main() 
   int a = ( 10 > 7 ) ? 15 : 10 ; 
   printf ( "%d", a ) ; 
}
Answer: 15
Explanation: When the condition is true, the first value is assigned to the variable.



41. Predict the output
main() 

   char *str1 = "abcd" ; 
   char str2[] = "abcd" ; 
   printf("%d %d %d", sizeof ( str1 ), sizeof ( str2 ), sizeof ( "abcd") ); 
}
Answer: If you have used gcc compiler, the answer would be 4 5 5. If Turbo C complier, is used, the answer will be 2 5 5.
Explanation: 

           The sizeof some data types will vary from compiler to compiler. Each character takes 1 byte in both the compilers. So, the size if 5 including the NULL.

42. Predict the output

main() 
{
   int k=1; 
   printf("%d==1 is " "%s", k, k==1 ? "TRUE": "FALSE"); 

Answer: 1==1 is TRUE.
Explanation: The conditional operator is used.


43. Predict the output

main() 

    char a[]="12345\n\0"; 
    int i=strlen(a); 
    printf("%d", i); 

Answer: 6
Explanation: The '\n' is taken as a single character and NULL is not counted


44.
Predict the output

main() 

    while (strcmp(“some”,”some\0”)) 
        printf(“Strings are not equal\n”); 

Answer: No output
Explanation: Both strings are equal and hence 0 is returned by strcmp. 0 is false condition and hence the statement printf is not executed.


45.
Predict the output

main() 

   char a[ ]="GOOD"; 
   printf ( "%d %d", sizeof(a), strlen(a) ); 
}
Answer: 5 4
Explanation: sizeof returns ( number of bytes occupied ) value including NULL and strlen returns ( number of characters ) value excluding NULL.



46. Which of the following declaration(s) is(are) correct ? 
(i) enum cricket {Gambhir,Smith,Sehwag}c;
(ii) enum cricket {Gambhir,Smith,Sehwag};
(iii) enum {Gambhir,Smith=-5,Sehwag}c;
(iv) enum c {Gambhir,Smith,Sehwag};
Answer: All the above

47. Which of the following does not modify data type in C?
(i) extern
(ii) interrupt
(iii) huge
(iv) register
(v) All the above

Answer: interrupt

48. Which of the following is integral data type in C?

(i) float
(ii) void
(iii) char
(iv) none
Answer: char
49. Predict the output
main()


    int a=-5; 
    unsigned int b = -5u; 
    if(a==b) 
       printf("Equal"); 
    else  
       printf("Empty"); 
}
Answer: Equal
Explanation: The comparison is made bitwise. ( So, both will be equal ).


50. Which of the following is not derived data type?
(i) Function
(ii) Pointer
(iii) Enumeration

Answer: Function


51. Predict the output
main() 


   char str[] = "Sona\0Programming\0Club" ; 
   printf ( "%s %s %s\n", str + 17, str + 5, str ) ; 
}
Answer: Club Programming Sona
Explanation:
               The values 17 and 5 are added to the base address of the string 'str' and the behaviour of printf is it prints till a null character is encountered. Though only "Sona" is stored in str, "Programming" and "Club" are stored in subsequent memory locations.


52. What is the output? 

main() 

   int i = 0 ; 
   ! i && printf ( "Text" ) ; 
}
Answer: Text
Explanation:
The '!' operator converts 0 to 1, which is true and the printf is evaluated as the behavior of '&&' is to check the second instruction only if the first one is true.


53. What would be the output?
main () 


   printf ( "What\0are you doing ? " ) ; 
}
Answer: What
Explanation: The behaviour of printf is to print characters only till it reaches a \0 ( null ) character.


54. Predict the output
main() 


   printf ( "Information" ) || printf ( "Technology" ) ; 
}
Answer: Information
Explanation:
             Since the first printf returns 11 ( number of characters printed ), the first expression evaluates to true and since the '||' operator is used, it does not check the second expression ( second printf ).


55. Find the output 

 main() 
{
    printf ( "%d", printf ( "2+" ) + printf ( "2=" ) ) ; 
}
Answer:
2+2=4
Explanation:
Because of the execution order of the printf statements.



56. Predict the output 
main() 
{
   int m = 21000 ; 
   float n = 1.2e100 ;
   printf ( "%d %d", m, n ) ; 
}
Answer: 21000 0

Explanation: A float value is given to the '%d' format specifier
 

57. What is the output?
main() 


   int j ; 
   for ( j = 0 ; j ++ <= 4 ; printf ( "%d", j ) ) ; 
}
Answer: 12345

Explanation: 'j' is incremented as soon as it's compared with 4.

58. What would be the output?
main () 


   int j ;
   for ( j = 1 ; 10 ; j ++ )
      printf ( "%d", j ) ; 
}
Answer: Infinite loop
Explanation: 10 is a non-zero value, which is always evaluated as true.

59. Predict the output
main() 


   if ( 'A' < 'a' )
      printf ( "Yes" ) ;
   else 
      printf ( "No" ) ; 
}
Answer: Yes
Explanation: The ASCII value of 'A' is 65 and that of 'a' is 97.


60. Find the output 

main() 

    int x = 1 ; 
    if ( "%d=hello", x ) ; 
}
Answer: No output
Explanation:
The if condition is treated as true since there is a string. The ', x' doesn't affect the program.


61. Predict the output
main()
{
   int a = '1', b = '11' ;
   printf ( "%d %d", a, b ) ;
}
Answer: 49 12593
Explanation: 

            The value is stored as binary in consequent memory. '1' is stored as 0011 0001 whose integer equivalent is 49. Similarly, '11' is stored as 0011 0001 0011 0001 whose integer value is 12593.

62. Predict the output
main() 


    int y = 7 ; 
    if ( !!y ) 
        printf ( "%d", !y ) ; 
    else 
        printf ( "%d", y ) ; 
}
Answer: 0
Explanation: !y makes non-zero ( 7 ) value as 0 and !!y makes zero value as 1 ( True )

63. Predict the output:
main () 

{
    int i = 1 ; j = -1 ; 
    if ( printf ( "%d", i ) > printf ( "%d", j ) ) 
       printf ( "%d", i ) ; 
    else printf ( "%d", j ) ; 
}
Answer: 1-1-1
Explanation: printf returns the number of characters displayed

64. Predict the output:
main() 


    int i = -1 ; 
    if ( i ++ ) 
       printf ( "True" ) ; 
    else 
       printf ( "False" ) ; 
}
Answer: True
Explanation: i ++ is post increment and -1 is also true

65. Predict the output:
main() 


   int a = 12 ; 
   printf ( "%p", a ) ; 
}
Answers: 0xc for gcc compiler ; 000C for Turbo C compiler
Explanation: The hexadecimal equivalent is printed for %p.



66. Predict the output:
#define MAX 65
main ( )
{
    printf ( "%c", MAX ) ;
}
Answer: A
Explanation: ASCII value of A is 6567. Predict the output:
#define SHOW( a ) printf ( #a " = %d", a ) ;
main()
{
    int x = 20 ;
    SHOW ( x ) ;
}
Answer: x = 20
Explanation: Whenever # is used inside a macro, it substitutes the variable name.

68. Predict the output:
#define STR(s,d) ( s ## d )
main ()
{
    char a[] = "Sona", b[] = "SProC", ab[] = "How?" ;
    printf ( "%s", STR ( a, b ) ) ;
}
Answer: How?
Explanation: 
               When we give ## in a macro, string concatenation ( of variables ) take place. This concept is called Token pasting. In this case a and b are concatenated as ab and hence "How?" is displayed.
69. Predict the output:
#define INC x++ ; y ++ ; 
main() 
     int x = 20, y = 10 ; 
     if ( 0 ) 
         INC ; 
     printf ( "%d %d", x, y ) ; 
}
Answer: 20 11
Explanation: Always remember that a macro substitutes before compilation. Hence, the only statement within if is x++ ;

70. Predict the output:
#define define if
main ()
{
    define ( 1 )
        printf ( "You can!" ) ;
}
Answer: You can!
Explanation: It is just macro definition. You can substitute even keywords using macro.

71. Predict the output
main()
{
    int i=5;
    printf("%d%d%d%d%d", i++, i--, ++i, --i, i) ;
}
Answer: The answer is compiler dependent
gcc        : 45555
Turbo C: 45545
Explanation: printf statement takes the values in reverse order ( LIFO - Last In First Out )

72. Predict the output
#define clrscr() 100
main()
{
    clrscr();
    printf("%d\n", clrscr() );
}
Answer: 100
Explanation: If a function name is defined, it overrides the function definition.

73. What are the file pointers which are automatically opened when a C file is executed?
Answer: All the above ( stdin, stdout, stderr )

74. Predict the output
main()
{
   static int var = 5;
   printf("%d ",var--);
   if(var)
      main();
}
Answer: 5 4 3 2 1
Explanation: The value of a static variable is maintained globally, though a static variable is accessible only within it's block

75. What happens if a C program has 2 main functions controlled by #if and #else
Answer: None of these ( Neither compiler, linker or run-time error )

76. Which of the following is evaluated first ?
Answer: !

  •  
  • 77. What does 7/9*9 equal (in C)? 
  • Answer: 0
    Explanation: Both '/' and '*' have same order of precedence. But, since both have left to right associativity, '/' is evaluated first. 7/9 equals 0 ( since both are integers, the result is also the same ) and then 0 * 9 = 0. To avoid this, typecasting can be used or the expression may be changed as 7.0/9*9 or 7/9.0*9 or 7.0/9.0 * 9.
    78. EvaluateAnswer: True
    79. What does 22 % 5 result to ?
    Answer: 2
    Explanation: Modulus returns the reminder after division.
    80. What is the output ?
    int v()
    {
       int m=0;
       return m++;
    }
    int main()
    {
        printf("%d", v());
    }
    Answer: 0
    Explanation: The value of 'm' is returned and only then, it's incremented.


    81. What is (void*)0?
    Answer: Representation of void pointer

    82. How can you combine the following two statements into one?
    • char *p; 
    • p = (char*) malloc(100);
    • Answer: char *p = (char*) malloc(100);
    • Explanation: The void pointer returned by malloc function is typecasted to char pointer.

    • 83. How many bytes are occupied by near, far and huge pointers in Turbo C?
    • Answer: near=2 far=4 huge=4
    • Explanation: These are special type of pointers

    • 84. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
    • *(*(*(*(a+i)+j)+k)+l)
    • Explanation: a[i] = *(a + i)

    • 85. What does fp point to in the program ?
    • main() 
    •    FILE *fp; 
    •    fp=fopen("trial", "r"); 
    • }
    • Answer: A structure which contains a char pointer which points to the first character of a file.




    • 87. Which among functions, inline functions and macros is the fastest ?
    • Answer: Macros
    • Explanation: Though inline functions are faster, macros are the fastest.

    Answer: Checks for the operator associativity.

    Answer: Compilation error
    Explanation: We can't access the address of a register variable


    Answer: Base address of the array

    Answer: During preprocessing

    Answer:  y = (int)(x + 0.5)


    Answer: b

     
    Answer: 12

    }
    Answer: 2, 15, 6, 8, 10
    Explanation: Every time, the value of b[1] is changed and hence the last updation remains.

     
    Answer: 48
    Explanation: Takes the ASCII equivalent since 0 is given as a character (within single quotes).

    Answer:  Using base address and offset concept and also using pointers





    • 104. Mentioning the prototype of a function is a
    • Answer: Declaration

3 comments:

  1. These questions will surely help us to improve our programming skill

    ReplyDelete
  2. Replies
    1. A garbage value is nothing, but a random value taken by variables, when they are declared but, not initialized.

      Delete