Tuesday, December 15, 2009

Ternary Operator

What is the output.

int main()
{
int i=10,j=20;
j = i, j?(i,j)?i:j:j;
printf("%d %d",i,j);
}
Choice:
a)10 10
b)20 20
c)10 11
d)11 10
e)10 20

Monday, June 8, 2009

What is the output of following programme ?

main()
{
int a=2,*f1,*f2;
f1=f2=&a;
*f2+=*f2+=a+=2.5;
*f1+=*f1+=a+=2.5;
printf("\n%d %d %d",a,*f1,*f2);
}

Choice
a) 18,18,18
b) 10,10,10
c) 72,72,72
d) 64,64,64
e) 26,26,26

Monday, June 1, 2009

Whatz the output

void main()
{
int i = abc(10);
printf("%d\n",--i);
}

int abc(int i)
{
return(i++);
}

Choice
a) 10
b) 11
c) 9
d) 0

Wednesday, May 27, 2009

What is the output of following programme ?

int main()
{
int x=10;
printf("%d",++x+x++<x+++x++);
return 0;
}

a. Error
b. 1
c. 24
d. 0
e. 46

Tuesday, May 26, 2009

State the output

#include
int a = 20;
void main()
{
fun1();
printf(" ");
fun1();
printf(" ");
fun1();
printf(" ");
}

fun1()
{
static int b = 10;
b++;
printf("%d", b++);
b=12;
printf(" ");
printf("%d", b++);
}

A 10 12 10 12 10 12
B 11 12 11 12 11 12
C 11 12 14 12 14 12
D Error "static variable re-assignment"

Tuesday, May 19, 2009

You have a program containing the following statements:

#define ARRAYSIZE 20
char str[ARRAYSIZE]="Test string";
strcat(str, 'z');

When you run the program, it produces a segmentation fault.

Referring to the above scenario, how do you resolve the problem?
Choice a : Use malloc to dynamically allocate memory, rather than using a fixed array.
Choice b : Change 'z' to 'z\0'
Choice c : Change ARRAYSIZE to 21.
Choice d : Change 'z' to "z"
Choice e : Change strcat to strncat and specify a size of 1.

You are looping through a set of results from an SQL database query,

Looking for a specific sequence of entries. Once the sequence of entries is found, you want to stop the loop and continue the program at the statement following the loop

Given the scenario described above, which line of code do you use?

Choice a : if (strcmp (DBSequence, "SEQUENCE")==0) continue;
Choice b : if (strcmp (DBSequence, "SEQUENCE")=0) break;
Choice c : if (strcmp (DBSequence, "SEQUENCE")==0) break;
Choice d : if (strcmp (DBSequence, "SEQUENCE")==0) return 0;
Choice e : if (strcmp (DBSequence, "SEQUENCE")==0) exit;

If the size of the array is less than the number of initializers then,

a. Extra values are being ignored
b. Generates an error message
c. Size of Array is increased
d. Size is neglected when values are given

Tuesday, May 12, 2009

Which one of the following is printed when this code is executed

int x;
int z=0;
for( x=0; x<5; x++) { z++; if( x <3 ) continue; if( x>3 ) break;
z++;
}
printf(“%d\n”, z);

a Z=1
b. Z=6
c. Z=7
d. Z=9
e. Z=10

1. When the function below is called with a pointer to an open file that contains only three characters A, B and C which one of the following is printed

void listFile( FILE * fp )

{
int c;

while( c = fgetc(fp) != EOF )

{

printf(“%d”, c);

}

printf(“\n”);

}


a. ABC1

b. 1111

c. The characters AB followed by an infinite number of C characters

d. 6566671

e. 0001

Thursday, April 9, 2009

The first line of a source file contains the following line.The compiler warns about "struct x declared inside parameter list". What is the compiler worried about?

extern int f(struct x *);

For two structures to be compatible, they must not only have the same tag name but be defined in the same scope. A function prototype, however, introduces a new, nested scope for its parameters. Therefore, the structure tag x is defined in this narrow scope, which almost immediately disappears. No other struct x pointer in this translation unit can therefore be compatible with f's first parameter, so it will be impossible to call f correctly (at least, without drawing more warnings). The warning alluded to in the question is trying to tell you that you shouldn't mention struct tags for the first time in function prototypes.

(The warning message in the question is actually produced by gcc, and the message runs on for two more lines, explaining that the scope of the structure declared "is only this definition or declaration, which is probably not what you want.")

What is output of following recusive main function call ?

main()
{
static int s=5;
s--;
printf("%d",s);
(s>3)?main():0;
}

Output: 43

What is the ANSI Standard definition of a null pointer constant?

Answer: "An integral constant expression with the value 0, or such an expression cast to type (void *)".

Are the parentheses in a return statement mandatory?

Answer: No. The formal syntax of a return statement is

return expression ;

But it's legal to put parentheses around any expression, of course, whether they're needed or not.

How can %f work for type double in printf and %lf is required in scanf?

Answer: In variable-length argument lists such as printf's, the old "default argument promotions" apply, and type float is implicitly converted to double. So printf always receives doubles, and defines %f to be the sequence that works whether you had passed a float or a double.


(Strictly speaking, %lf is *not* a valid printf format specifier, although most versions of printf quietly excepts it.)

scanf, on the other hand, always accepts pointers, and the types pointer-to-float and pointer-to-double are very different (especially when you're using them for storing values). No implicit promotions apply.

Why doesn't \% print a literal % with printf?

Answer: Backslash sequences are interpreted by the compiler (\n, \", \0, etc.), and \% is not one of the recognized backslash sequences. It's not clear what the compiler would do with a \% sequence -- it might delete it, or replace it with a single %, or perhaps pass it through as \ %. But it's printf's behavior we're trying to change, and printf's special character is %. So it's a %-sequence we should be looking for to print a literal %, and printf defines the one we want as %%.

How can you print a literal % with printf?

Answer: %%

Sunday, March 29, 2009

Are the parentheses in a return statement mandatory?

Answer: No. The formal syntax of a return statement is

return expression ;

But it's legal to put parentheses around any expression, of course, whether they're needed or not.

Thursday, March 19, 2009

‘C’ allows arrays of greater than two dimensions, who will determined this

a. Programmer
b. Compiler
c. Parameter
d. None of these

Converting Hex Value To Decimal & Vice Versa - Simple Logic

Theory works like this:

* Consider a number (say 15482) in decimal system
* This is equivalent to (1 * 10000) + (5 * 1000) + (4 * 100) + (8 * 10) + 2
* This is equivalent to (1 * 10^4) + (5 * 10^3) + (4 * 10^2) + (8 * 10^1) + (2 * 10^0)
where ^ is the power-of operator.

* Now consider a number (say 12F3B) in hexadecimal system
* Using similar logic, this is equivalent to (1 * 16^4) + (2 * 16^3) + (F * 16^2) + (3 * 16^1) + (B * 16^0)

Now all you have to do is compute 16^4, 16^3 etc. and also take F=15 and B=11 and there you have it... the answer in decimal

Note that you can use this logic to convert a number from *ANY* base into decimal.

Sunday, March 15, 2009

How can you swap two integer variables without using a temporary variable?

A: The reason that this question is poor is that the answer ceased to be interesting when we came down out of the trees and stopped using assembly language.

The "classic" solution, expressed in C, is

a ^= b;
b ^= a;
a ^= b;

Due to the marvels of the exclusive-OR operator, after these three operations, a's and b's values will be swapped.

However, it is exactly as many lines, and (if we can spare one measly word on the stack) is likely to be more efficient, to write the obvious

int t = a;
a = b;
b = t;

No, this doesn't meet the stipulation of not using a temporary. But the whole reason we're using C and not assembly language (well, one reason, anyway) is that we're not interested in keeping track of how many registers we have.

If the processor happens to have an EXCH instruction, the compiler is more likely to recognize the possibility of using it if we use the three-assignment idiom, rather than the three-XOR.

By the way, the even more seductively concise rendition of the "classic" trick in C, namely

a ^= b ^= a ^= b

is, strictly speaking, undefined, because it modifies a twice between sequence points. Also, if an attempt is made to use the idiom (in any form) in a function which is supposed to swap the locations pointed to by two pointers, as in

swap(int *p1, *p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}

then the function will fail if it is ever asked to swap a value with itself, as in

swap(&a, &a);

or

swap(&a[i], &a[j]);

when i == j. (The latter case is not uncommon in sorting algorithms. The effect when p1 == p2 is that the pointed- to value is set to 0.)

Monday, March 9, 2009

Declaring structure pointer

Suppose that you declare

struct x *xp;

without any definition of struct x. Is this legal? Under what circumstances would it be useful?

Answer: It is perfectly legal to refer to a structure which has not been "fleshed out," as long as the compiler is never asked to compute the size of the structure or generate offsets to any members. Passing around pointers to otherwise undefined structures is quite acceptable, and is a good way of implementing "opaque" data types in C.

What's the difference between these three declarations?

char *a = "abc";
char b[] = "abc";
char c[3] = "abc";

Answer:The first declares a pointer-to-char, initialized to point to a four-character array somewhere in (possibly read-only) memory containing the four characters a b c \0. The second declares an array (a writable array) of 4 characters, initially containing the characters a b c \0. The third declares an array of 3 characters, initially containing a b c. (The third array is therefore not an immediately valid string.)

Thursday, March 5, 2009

The following code should add a new element to the beginning of a linked list

The pointer head should point to the start of the list.
It is called as: Push(head, 192);

1: void Push(struct node* head, int data) {
2: struct node* newNode = malloc(sizeof(struct node));
3: newNode->data = data;
4: newNode->next = head;
5: head = newNode;
6: }

Given the scenario described above, what changes need to be made to the code in order for it to work as intended?
Choice a: Line 2 should be: struct node* newNode = malloc(sizeof(struct *node)); and change head in lines 4 and 5 to *head.
Choice b: Line 1 should be: void Push(struct node** head, int data) and change head in lines 4 and 5 to *head.
Choice c: The node struct needs to be created within the Push function prior to being used.
Choice d: Line 1 should be: void Push(struct node** head, int data).
Choice e: When calling the function, the value needs to be 287 or less due to the type specified in the function definition.

Wednesday, February 25, 2009

Which of the following is evaluated first:

a) &&
b) ||
c) !
d) *

Tuesday, February 24, 2009

Thursday, February 19, 2009

A multidimensional array can be expressed in terms of

a. Array of pointers rather than as pointers to a group of contiguous array
b. Array without the group of contiguous array
c. Data type arrays
d. None of these

Wednesday, February 11, 2009

Why does a pointer definition need a type to be specified?

Choice a : To specify the size of the pointer
Choice b : To clear the memory being pointed to before it is assigned
Choice c : To set the endianness of the pointer
Choice d : To set the address space definition
Choice e : To allow pointer math to work properly

If a machine uses some nonzero internal bit pattern for null pointers, how should the NULL macro be defined?

Answer: As 0 (or (char *)0), as usual. The *compiler* is responsible for translating null pointer constants into internal null pointer representations, not the
preprocessor.

Monday, February 9, 2009

What is sizeof('A') ?

The same as sizeof(int). Character constants have type int in C. (This is one area in which C++ differs.)

Thursday, February 5, 2009

Which one of the following operators has right to left associativity?

Choice a: Function call operator ()
Choice b: Logical not operator !
Choice c: Array index operator []
Choice d: Multiplication operator *
Choice e: Pointer operator ->

Thursday, January 29, 2009

You have the following line in a C program:

#include
and you compile the program.
Given the scenario described above, how is the line parsed?

Choice a : The linker resolves the symbols in stdio.h and links it with the overall program.

Choice b : The preprocessor creates pointers to stdio.h.

Choice c : The compiler creates lexically local variables definitions stored in stdio.h.

Choice d : The compiler allocates a name space for the symbols stored in stdio.h.

Choice e : The preprocessor replaces the line with the contents of stdio.h.

Monday, January 26, 2009

State o/p

#include
int main()
{
printf("%d",-16["WHAT A PLAYER WHAT A PLAYER SLUMDOG!!!"]);
return 0;
}
A. Error
B. GARBAGE
C. 65
D. none of the above

Wednesday, January 21, 2009

Find the correct output

void main()
{
char a[]=”rama”;
char b[]=a;
printf (“%d %s”, sizeof (b),b);
}

(a) 5 rama

(b) 4 rama

(c) 5 r

(d) None of these, some other reason

Monday, January 12, 2009

Which one of the following is an advantage of dynamically linked object files?

Choice a : Programs that use dynamic linking do not require recompiling when a new processor type is used.

Choice b : Dynamically linked programs contain all machine code required to run them.

Choice c : Dynamically linked programs use extended memory, but statically linked programs only have access to conventional memory.

Choice d : Programs that use dynamic linking can benefit from library updates without relinking.

Choice e : Dynamically linked programs run faster because debugging symbols are not included.

Saturday, January 10, 2009

Which one of the following is NOT a valid identifier?

Choice a: bigNumber
Choice b: peaceful_in_space
Choice c : auto
Choice d : __ident
Choice e : g42277

Thursday, January 8, 2009

Find the correct output

void main()
{

int a[]={‘a’,’b’,’c’};
printf(“%d”, sizeof(a));

}

(a) 3
(b) 4
(c) can’t be initialized
(d) None of these

Thursday, January 1, 2009

An expression contains relational, assignment and arithmetic operators. If Parenthesis are not present, the order will be

a. Assignment, arithmetic, relational
b. Relational, arithmetic, assignment
c. Assignment, relational, arithmetic
d. Arithmetic, relational, assignment