senthil


{ City } bangalore
< Country > india
* Profession * senior software engineer
User No # 45045
Total Questions Posted # 0
Total Answers Posted # 26

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 206
Users Marked my Answers as Wrong # 60
Answers / { senthil }

Question { 8397 }

Identify the operators that is not used with pointer

a. &&

b. #

c. *

d. >>


Answer

ANS. b. #

# is an operator used to convert a parameter to string
# should always be followed by a formal parameter (in below example parameter x)

Ex:
#define SAM(x) #x
printf(SAM(Hello)); // prints Hello

In other cases
char a = 0x10, b;
char *p = &b;

a. a&&*p is valid
b. *p is valid
c. a>>*p is valid

Is This Answer Correct ?    2 Yes 1 No

Question { IBM, 4606 }

#include
void main()
{
String s1[]={"swathi"};
string s2[]={"maddimsetti"};
s1[]=s[];
printf("%s",s1[]);
}


Answer

compilation error.

the following statement is wrong
s1[]=s[];

cannot change the value of array pointer s1

Is This Answer Correct ?    2 Yes 0 No


Question { 3735 }

how to write a cprogram yo get output in the form
*
***
*****
*******
*********
*******
*****
***
*


Answer

Generic solution:

n = 5
loop 1: i = 0 to 4
loop 2: i = 5 to 0

Loop variables => i j k j exp k exp
* 0 5 spaces, 1 stars (5-0) (2*0)+1
*** 1 4 spaces, 3 stars (5-1) (2*1)+1
***** 2 3 spaces, 5 stars (5-2) (2*2)+1
******* 3 2 spaces, 7 stars (5-3) (2*3)+1
********* 4 1 spaces, 9 stars (5-4) (2*4)+1
*********** 5 0 spaces, 11 stars(5-5) (2*5)+1
********* 4 1 spaces, 9 stars (5-4) (2*4)+1
******* 3 2 spaces, 7 stars (5-3) (2*3)+1
***** 2 3 spaces, 5 stars (5-2) (2*2)+1
*** 1 1 spaces, 3 stars (5-1) (2*1)+1
* 0 5 spaces, 1 stars (5-0) (2*0)+1

generalising expressions => (n-i) (2*i)+1

void printPattern(int n)
{
int i, j, k;

for(i=0; i {
for(j=0; j<=(n-i); j++)
printf(" ");

for(k=0; k<(2*i+1); k++)
printf("*");

printf("\n");
}

for(i=n; i>=0; i--)
{
for(j=0; j<=(n-i); j++)
printf(" ");

for(k=0; k<(2*i+1); k++)
printf("*");

printf("\n");
}
}

Is This Answer Correct ?    2 Yes 0 No

Question { 3735 }

how to write a cprogram yo get output in the form
*
***
*****
*******
*********
*******
*****
***
*


Answer

description for previous answer posted again

n = 5
loop 1: i = 0 to 4
loop 2: i = 5 to 0

Loop variables => i j k j exp k exp
.....*............0 5 spaces, 1 stars (5-0) (2*0)+1
....***...........1 4 spaces, 3 stars (5-1) (2*1)+1
...*****..........2 3 spaces, 5 stars (5-2) (2*2)+1
..*******.........3 2 spaces, 7 stars (5-3) (2*3)+1
.*********........4 1 spaces, 9 stars (5-4) (2*4)+1
***********.......5 0 spaces, 11 stars(5-5) (2*5)+1
.*********........4 1 spaces, 9 stars (5-4) (2*4)+1
..*******.........3 2 spaces, 7 stars (5-3) (2*3)+1
...*****..........2 3 spaces, 5 stars (5-2) (2*2)+1
....***...........1 1 spaces, 3 stars (5-1) (2*1)+1
.....*............0 5 spaces, 1 stars (5-0) (2*0)+1

..........generalising expressions => (n-i) (2*i)+1

Is This Answer Correct ?    0 Yes 0 No

Question { 2858 }

main()
{
int i;
for(i=0;i<5;i++)
printf("%d",1l< }
why doesn't 'l' affect the code??????


Answer

In 1l, 'l' converts number 1 from int to long int.
It is a valid expression

Is This Answer Correct ?    2 Yes 0 No

Question { 3755 }

how to find greatet of 10 numbers without using array?


Answer

int main()
{
int i, num;
int greatest=0;

for(i=0; i<10; i++)
{
scanf("%d", &num);
if(num > greatest)
greatest = num;
}
printf("greatest = %d", greatest);
return 0;
}

Is This Answer Correct ?    9 Yes 2 No

Question { 3711 }

what is the use of ~ in c lang?????


Answer

Agree with answer 2.
~ is used to find the 1's complement of any number
ex:
a = 0xCA (in hex) => 11001010 (in binary)
~a = 0x35 (in hex) => 00110101 (in binary)

Is This Answer Correct ?    5 Yes 0 No

Question { 3421 }

1) int main() {
unsigned char a = 0;
do {
printf("%d=%c\n",a,a);
a++;
}while(a!=0);
return 0;
}
can anyone please explain the explain the output


Answer

ANS: Prints all the ascii characters from 0 to 255 in the below format
0=
1=☺
2=☻
3=♥
4=♦
5=♣
6=♠
.
.
.
251=√
252=ⁿ
253=²
254=■
255= 

'a' is a unsigned char variable, size = 8 bits
so the maximum value 'a' can store is 255.
an increment from 'a' value 255 will cause overflow and
'a' value becomes 0, when the loop terminates.

Is This Answer Correct ?    4 Yes 0 No

Question { 3757 }

A MobileNumber is a VIP number if it satisfy the following
conditions.

The operator should be Vodafone.
Atleast one 0 (Zero) should be exist in mobile number.
The number should not end with 8.
The single digit sum of all the digits in the number should
be equal to 9. For example if the number is 9876543210, the
sum is 9+8+7+...+1+0 = 45. Sum of 4+5 = 9.
Write a method:

private boolean isVIPMobileNumber(String mobileNum, String
operator)

mobileNum phone number
operator mobile operator as bsnl, Vodafone


Answer

// The prototype in question is not exactly c language. but i tried to provide equivalent solution in c.

int SingleDigitSum(int num)
{
int hund, tens, ones;
int sum = 0;

sum = num;

do
{
hund = sum / 100;
sum = sum % 100;
tens = sum / 10;
ones = sum % 10;
sum = hund + tens + ones;
}
while(sum > 9);

return(sum);
}

boolean isVIPMobileNumber(String mobileNum, String operator)
{
int i, sum=0, zeroCnt=0;
int tens, ones;
boolean retValue = FALSE;

// check for Vodafone operator
if(strcmp("Vodafone", operator) == 0)
{
// validate length of mobile number including NULL
if(strlen(mobileNum) == 11)
{
// check for the number should not end with 8
if(mobileNum[9] != 8)
{
for(i=0; i<10; i++)
{
if(mobileNum[i] == '0') zeroCnt++;

sum += mobileNum[i];
}

// Atleast one 0 (Zero) should be exist in mobile number.
// The single digit sum of all the digits in the number should be equal to 9.

// maximum sum possible single digit sum = 9+9+9+9+9+9+9+9+9+9 = 90 = 9 + 0 = 9

if((zeroCnt != 0) && (SingleDigitSum(sum) == 9))
{
retValue = TRUE;
}
}
}
}
}

Is This Answer Correct ?    2 Yes 0 No

Question { 7176 }

main()
{
int *ptr=(int*)malloc(sizeof(int));
*ptr=4;
printf("%d",(*ptr)+++*ptr++);
}


Answer

Ans = 8

Explanation:
after brackets post increment has higher precedence,
hence expression can be viewed as
(*ptr)++ + *ptr++

printf("%d",(*ptr)+++*ptr++); can be expanded in 3 steps as

1. printf("%d",(*ptr)+*ptr); => displays 4+4
2. ptr++; => increments the pointer to next location
3. (*ptr)++; => increments the value in that location

This program can be better understood, with the below modification
main()
{
int *ptr=(int*)malloc(sizeof(int)*2);
*ptr=4; // current location value = 4
*(ptr+1) = 10; // next location value = 10
printf("%d\n",(*ptr)+++*ptr++); // display 8 (4+4)
printf("%d\n",*(ptr-1)); // current location value = 4
printf("%d\n",*ptr); // next location value = 10+1 = 11
}

Is This Answer Correct ?    3 Yes 3 No

Question { Vector, 26338 }

int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))
main()
{
if(-1<=SIZE) printf("1");
else printf("2");
}


Answer

program prints "2"

Here sizeof returns unsigned int value
so sizeof(array)/sizeof(int)
=> 32(unsigned int)/4(unsigned int)
=> 8 (unsigned int value)

During comparison, the datatypes are different on both sides of if condition
-1(signed int) <= 8(unsigned int)

so by rule of type conversion in c,
signed int gets converted to unsigned int

hence expression becomes
0xFFFFFFFF(unsigned int equivalent of -1) <= 8(unsigned int)

Hence overall condition becomes FALSE

Is This Answer Correct ?    72 Yes 6 No

Prev    1    [2]