Saturday 16 March 2013

not=!2;


main()
    {
    char not;
    not=!2;
    printf("%d",not);
    }
Answer:
0
Explanation:
! is a logical operator. In C the value 0 is considered to be the boolean value FALSE, and any non-zero value is considered to be the boolean value TRUE. Here 2 is a non-zero value so TRUE. !TRUE is FALSE (0) so it prints 0.

Blog Author: Vijay Kumar

Go to: Java Aptitude


printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));


main()
    {
    char *str1="abcd";
    char str2[]="abcd";
    printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
    }
Answer:
2 5 5
Explanation:
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the '\0' termination character). The third sizeof is similar to the second one.

Blog Author: Vijay Kumar

Go to: Java Aptitude


calling of main() only


main()
    {
    main();
    }
Answer:
 Runtime error : Stack overflow.
Explanation:
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.


Blog Author: Vijay Kumar

Go to: Java Aptitude


files which are automatically opened


What are the files which are automatically opened when a C file is executed?
Answer: stdin, stdout, stderr (standard input,standard output,standard error).

Blog Author: Vijay Kumar

Go to: Java Aptitude


Unary + operator


main()
    {
    int i=-1;
    +i;
    printf("i = %d, +i = %d \n",i,+i);
    }
Answer:
 i = -1, +i = -1
Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it just because it has no effect in the expressions (hence the name dummy operator).

Blog Author: Vijay Kumar

Go to: Java Aptitude


assert a debugging tool


int i,j;
    for(i=0;i<=10;i++)
    {
    j+=5;
    assert(i<5);
    }
Answer:
Runtime error: Abnormal program termination.
             assert failed (i<5), <file name>,<line number>
Explanation:
asserts are used during debugging to make sure that certain conditions are satisfied. If assertion fails, the program will terminate reporting the same. After debugging use,
    #undef NDEBUG
and this will disable all the assertions from the source code. Assertion
is a good debugging tool to make use of. 

Blog Author: Vijay Kumar

Go to: Java Aptitude

Array subscript error


main( )
{
  int a[ ] = {10,20,30,40,50},j,*p;
  for(j=0; j<5; j++)
    {
printf(“%d” ,*a);
a++;
    }
    p = a;
   for(j=0; j<5; j++)
      {
printf(“%d ” ,*p);
p++;
      }
 }
Answer:
Compiler error: lvalue required.
        
Explanation:
Error is in line with statement a++. The operand must be an lvalue and may be of any of scalar type for the any operator, array name only when subscripted is an lvalue. Simply array name is a non-modifiable lvalue.

Blog Author: Vijay Kumar

Go to: Java Aptitude


Function declaration solutions


main()
{
 show();
}
void show()
{
 printf("I'm the greatest");
}
Answer:
Compier error: Type mismatch in redeclaration of show.
Explanation:
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().

Blog Author: Vijay Kumar

Go to: Java Aptitude


Correct Global Variable


main()
{
 extern out;
 printf("%d", out);
}
 int out=100;
Answer:
100
    Explanation:
when compiler reaches to extern out then it will find it outside main and it will put out=100 as the effect of int out=100. This is correct example of previous program code.

Blog Author: Vijay Kumar

Go to: Java Aptitude

global variable error


main()
{
printf("%d", out);
}
int out=100;
Answer:
Compiler error: undefined symbol out in function main.
Explanation:
The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error.


Blog Author: Vijay Kumar

Go to: Java Aptitude


printf("%d",++*p + ++*str1-32);


#include<stdio.h>
main()
{
  char s[]={'a','b','c','\n','c','\0'};
  char *p,*str,*str1;
  p=&s[3];
  str=p;
  str1=s;
  printf("%d",++*p + ++*str1-32);
}
Answer:
M
Explanation:
p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is incremented to 11. the value of ++*p is 11. ++*str1 meAnswer:"str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is subtracted from 32.
i.e. (11+98-32)=77("M");


Blog Author: Vijay Kumar

Go to: Java Aptitude


for(;i++;printf("%d",i)) ;


main()
{
int i=0;

for(;i++;printf("%d",i)) ;
printf("%d",i);
}
Answer:
    1
Explanation:
before entering into the for loop the checking condition is "evaluated". Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).


Blog Author: Vijay Kumar

Go to: Java Aptitude


#define f(g,g2) g##g2


#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
    }
Answer:
100 

Explanation## is the preprocessor "command" for concatenating what comes before and after so becomes var12.



Blog Author: Vijay Kumar

Go to: Java Aptitude

printf("%d",scanf("%d",&i));


main()
{
int i;
printf("%d",scanf("%d",&i));  // value 10 is given as input here
}
Answer:
1
Explanation:
Scanf returns number of items successfully read and not 10.  Here 10 is given as input which should have been scanned successfully. So number of items read is 1.


Blog Author: Vijay Kumar

Go to: Java Aptitude

switch error


#include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
 {
 case 1:  printf("GOOD");
        break;
 case j:  printf("BAD");
       break;
 }
}
Answer:
Compiler Error: Constant expression required in function main.
Explanation:
The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error).
    Note:
Enumerated types can be used in case statements. 


Blog Author: Vijay Kumar

Go to: Java Aptitude


printf("%d",i+++++i);


void main()
{
    int i=5;
    printf("%d",i+++++i);
}
Answer:
Compiler Error
Explanation:
The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. 



Blog Author: Vijay Kumar

Go to: Java Aptitude

static char names[5][20]={"pascal","ada","cobol","fortran","perl"};


 main()
{
   static char names[5][20]={"pascal","ada","cobol","fortran","perl"};
    int i;
    char *t;
    t=names[3];
    names[3]=names[4];
    names[4]=t; 
    for (i=0;i<=4;i++)
    printf("%s",names[i]);
}
Answer:
Compiler error: Lvalue required in function main
Explanation:
Array names are pointer constants. So it cannot be modified.



Blog Author: Vijay Kumar

Go to: Java Aptitude

goto error example


main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
      goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}
Answer:
Compiler error: Undefined label 'here' in function main
Explanation:
Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main.



Blog Author: Vijay Kumar

Go to: Java Aptitude


i=i++ * ++i;


void main()
{
clrscr();
int i=5,j=5,k=5;
i=i++ * ++i;
j=++j*++j;
k=k++*k++;

printf("%d\n",i);
printf("%d\n",j);
printf("%d\n",k);

getch();
}
Ans: i=37, j=49, k=27. Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.



Blog Author: Vijay Kumar

Go to: Java Aptitude


int c= --2;


main()
   {
               int c= --2;
               printf("c=%d",c);
   }

it will generate an error LValue Required... Because -- operator can only be applied to variables as a decrement operator (eg., --i, i--). 2 is a constant and not a variable.



Blog Author: Vijay Kumar

Go to: Java Aptitude