Menu

Friday, November 15, 2013

C & C++- question & ans

Q 1) What does this code do?
 #include<stdio.h>
 void func(char s[],int c)
 {
     int i,j;
     for(i=j=0;s[i]!='\0';i++)
         if(s[i]!=c)
             s[j++]= s[i] + s[i]==c ;
     s[j]='\0';
 }
 int main()
 {
     char s[]=“Hello world";
     func(s,'k');
     printf("%s",s);
     return(0);
 }
Output:  empty string or nothing
Explanation: The subroutine takes a string and a character, if the sum of 2 present character ASCII value is equal to the ASCII value of the character C then a 1 is added to the string s (s[i]+s[i]==c) and since the sum will never be equal to the ACSII value of C the string will remain empty
 
Q2)    Predict the output of the following piece of code. Assume that bit position 0 is at the right end and that i and j are sensible positive values.
 #include<stdio.h>
 unsigned getbits(unsigned a,int i,int j)
 {
     return(a>>(i+1-j)) & (~0<<j);
 }
 int main()
 {
     unsigned num=128;
     printf("%d\n",getbits(num,4,1));
     return(0);
 }
Output: 16
Explanation:

a>>(i+1-j) equals 8 ie binary value 00001000. (~1<<j) equals 15 i.e. binary value 00001111.Binary & of these two values gives us 00001000 which is equal to 16
 
Q 3)  Will this code work properly? If yes why? If not why not?
 #include<stdio.h>
 void swap(int *a,int *b)
 {
     *a=*a+*b;
     *b=*a-*b;
     *a=*a-*b;
 }
 int main()
 {
     int a=3617283450;
     int b=3617283449;
     swap(&a,&b);
     printf("%d %d\n",a,b);
     return(0);
 }
Explanation : For some systems it will return a value but it is garbage value. For some systems it won't work because of overflow in the statement *a=*a+*b;

Q 4) What will be the output of the following code
void swap(int,int);
void main()
{ int a=10,b=15;
clrscr();
printf("Befor swap a=%d ,b=%d",a,b);
swap(a,b);
printf("\nAfter swap a=%d ,b=%d",a,b);
getch(); }
void swap(int a, int b)
{ int c;
c=a;
a=b;
b=c; }

Output: Before swap a=10,b=15 After swap a=10,b=15
Explanation: a and b are auto variable (default storage class is auto) . Here its scope is only within main function. After the main function both a and b will die. We are swapping outside the main function so value of a and b is not changing.

Q 5) What will be the output of the following code
char* myFunc (char *ptr)
{
ptr += 3;
return (ptr);
}
int main()
{
char *x, *y;
x = "HELLO";
y = myFunc (x);
printf ("y = %s \n", y);
return 0;
}
Output: LO
Explanation :The subroutine will increment the pointer to the current position of string by 3 and return the new position to calling program So when the printf statement is executed it prints characters after third position.

Q 6) What will be the output of following Code:
void myFunc (int x)
{
if (x > 0)
myFunc(--x);
printf("%d, ", x);
}
int main()
{
myFunc(5);
return 0;
}
Output: 0, 0, 1, 2, 3, 4,
Explanation : myFunc is calling itself recursively the call stack will be like this
myFunc(0)
myFunc(0)
myFunc(1)
myFunc(2)
myFunc(3)
myFunc(4)

Q 7) what will be the output of the following Code:
#include <stdio.h>
int i;
void increment( int i )
{
i++;
}
int main()
{
for( i = 0; i < 10; increment( i ) )
{
}
printf("i=%d\n", i);
return 0;
}
Output: It will loop indefinitely.
Explanation : Because the increment function is returning void and it will not increment the “i” needed in the for loop of main function as the for loop will expect “i” to be incremented in the loop itself
 
Q 8) What is the output of the following Code:
#include <stdio.h>
void func()
{
int x = 0;
static int y = 0;
x++; y++;
printf( "%d -- %d\n", x, y );
}
int main()
{
func();
func();
return 0;
}
Output :1 -- 1 1 -- 2
Explanation : the variable y is static for the subroutine thus it will maintain its value even after the control exits from the present call.

Q 9) What will be the output of following code
#include <stdio.h>
void inc_count(int *count_ptr)
{
(*count_ptr)++;
}
int main()
{ int count = 0;
while (count < 10)
inc_count(&count);
Printf(“%d “, Count); 
return (0);
}
Output:10
Explanation :The address of the count variable is passed to the inc_count function which increments and return the incremented value. This is also called pass by reference.

Q 10) What will be the output of the following code
#include <stdio.h>
#include <stdlib.h>
void func(int);
main()
{
void (*fp)(int);
fp = func;
(*fp)(1);
fp(2);
exit(EXIT_SUCCESS);
}

void func(int arg)
{ printf("%d\n", arg);
}
 
Output: 1 2
Explanation: the first call (*fp)(1); will call the function through its pointer and return the value passed as argument and the second call fp(2); will invoke the function normally and print the parameter value to the screen

No comments:

Post a Comment