Thursday, December 25, 2008

C Programming LANGUAGE Problems WITH Answers

Q1.Find the output of the following:
main( )
{ int x=100;
if(!!x)
printf(“x=%d”,!x);
else printf(“x=%d”,x);
}

a. 0
b. 2
c. 1.5
d. 100

Q2. main( )
{
float a=0.5, b=0.9;
if(a&&b>0.9)
printf(“it is ur style”);
else
printf(“it is my style”);
}
a. it is ur style
b. it is our style
c. it is my style
d. no output

Q3. main( )
{
int x=10, y=20;
if(!(!x) && x)
printf(“x=%d”,x);
else
printf(“y=%d”,y);
}

a. 10
b. 20
c. 1
d. 0

Q4. main( )
{
char ch=291;
printf(“%d%d%c”,32770,ch,ch);
}

a. 291
b. -32766 35#
c. 32770chch
d. 32770

Q5.
main ( )
{
int a,b;
a = -3- -3;
b = -3 - - (-3 );
printf(“a=%d b= %d”,a,b);
}

a. a=0 b=-6
b. a=-3 b=+3
c. a=-6 b=+6
d. a=6 b=0

Q6.main( )
{
int x;
x= -3 + 4 – 7 * 8 / 5 % 10;
printf(“x = %d”,x);
}

a. 23
b. 6
c. 7
d. 0

Q7.main( )
{
int x=3, y=4, z=4;
printf(“ans = %d”, (z>=y>=x?100:200));
}
a. 100
b. 300
c. 200
d. No Output

Q8.main( )
{
int a=30, b=40, x;
x=(a!=10) && (b=50);
printf(“x= %d”,x);
}

a. 10
b. 50
c. 1
d. 0

Q9.main( )
{
float x=12.25, y=13.65;
if(x=y)
printf(“x and y are equal”);
else
printf(“x and y are not equal”);
}

a. x and y are not equal
b. x and y are equal
c. No output

Q10.main ( )
{
int i=1, j=1;
for(;j;printf(“%d%d\t”,i,j))
j=i++ <= 5; }
a. 21 31 41 51 61 70
b. 21 30 41 50 61 70
c. 20 30 40 50 60 70
d. 21 31 41 51 61 71

Q11.main( )
{
int i=3, j=2, k=1, d;
d = ij&k, printf(“d = %d\t”,d);
d=ij&~k;
printf(“d = %d”,d);
}
a. d = 3 d=3
b. d=2 d=2
c. d=3 d=2
d. none of the above

Q12.main( )
{
int i=2;
printf(“i-- = %d”, i--);
}
a. i-- = 2
b. i-- = 3
c. i-- = 1
d. none of the above.
Q13. main( )
{
float y=0.9;
long double z= 0.9;
if(y = = z)
printf(“hello world”);
else printf(“hai world”);
}
a. hello world
b. hai world
c. no output

Q14. main( )
{
static int c=5;
printf(“%d\t”,c--);
if(c)
main( );
}
a. 5 4 3 2 1
b. 1 2 3 4 5
c. 5 4 3 3 3
d. none of the above
Q15.
int i;
main( )
{
int j;
for(;;)
{
if(j=fun(i))
printf(“%d”,j);
else break;
}
}
fun(x) int x;
{
static int v=2;
v--;
return (v-x);
}
a. 1
b. 5
c. 0
d. None of the above

Q16. What will be the output of the following program in UNIX OS with CC compiler and TC compiler?
int main()
{
int i=5;
printf("\n%d",++i + ++i + ++i + ++i + ++i );
}
a. 41
b. 42
c. 51
d. 62
Q17.
main()
{
if (!(1&&0))
{
printf("OK I am done.");
} else {
printf("OK I am gone.");
} }
a. OK I am done
b. OK I am gone
c. None of the above
Q18. main()
{
char *a = "Hello ";
char *b = "World";
clrscr();
printf("%s", strcpy(a,b));
}
a. World
b. Hello
c. Hello World
d. World Hello
Q19.
int z,x=5,y=-10,a=4,b=2;
z = x++ - --y * b / a;
What number will z in the sample code above contain?
a. 5
b. 6
c. 10
d. 11
e. 12
Q 20. With every use of a memory allocation function, what function should be used to release allocated memory
which is no longer needed?
a. unalloc()
b. dropmem()
c. dealloc()
d. release()
e. free()
[Ans]
Q 21.
void *ptr;
myStruct myArray[10];
ptr = myArray;
Which of the following is the correct way to increment the variable "ptr"?
Choice 1 ptr = ptr + sizeof(myStruct); [answer]
Choice 2 ++(int*)ptr;
Choice 3 ptr = ptr + sizeof(myArray);
Choice 4 increment(ptr);
Choice 5 ptr = ptr + sizeof(ptr);
Q22. 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;
}

What will print when the sample code above is executed?
Choice 1 y = HELLO
Choice 2 y = ELLO
Choice 3 y = LLO
Choice 4 y = LO [Ans]
Choice 5 x = O

Q23.struct node *nPtr, *sPtr; /* pointers for a linked list. */ for (nPtr=sPtr; nPtr; nPtr=nPtr->next)
{
free(nPtr);
}

The sample code above releases memory from a linked list.
Which of the choices below accurately describes how it will work?
Choice 1
It will work correctly since the for loop covers the entire list.
Choice 2
It may fail since each node "nPtr" is freed before its next address can be accessed.
Choice 3
In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next".
Choice 4
This is invalid syntax for freeing memory.
Choice 5
The loop will never end.

Q24. What function will read a specified number of elements from a file?
Choice 1
fileread()
Choice 2
getline()
Choice 3
readfile()
Choice 4
fread()
Choice 5
gets()


Q25. "My salary was increased by 15%!"
Select the statement which will EXACTLY reproduce the line of text above.
Choice 1
printf("\"My salary was increased by 15/%\!\"\n");
Choice 2
printf("My salary was increased by 15%!\n");
Choice 3
printf("My salary was increased by 15'%'!\n");
Choice 4
printf("\"My salary was increased by 15%%!\"\n");[Ans]
Choice 5
printf("\"My salary was increased by 15'%'!\"\n");


Q26. What is a difference between a declaration and a definition of a variable?
Choice 1
Both can occur multiple times, but a declaration must occur first.
Choice 2
There is no difference between them.
Choice 3
A definition occurs once, but a declaration may occur many times.
Choice 4
A declaration occurs once, but a definition may occur many times. [Ans]
Choice 5
Both can occur multiple times, but a definition must occur first.


Q27. int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
What value does testarray[2][1][0] in the sample code above contain?
Choice 1
3
Choice 2
5
Choice 3
7
Choice 4
9
Choice 5
11[Ans]


Q28. int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);

what will be the output when following code is executed
Choice 1
12,10,11,13
Choice 2
22,10,11,13
Choice 3
22,11,11,11
Choice 4
12,11,11,11
Choice 5
22,13,13,13[Ans]


Q29. int x[] = { 1, 4, 8, 5, 1, 4 };
int *ptr, y;
ptr = x + 4;
y = ptr - x;

What does y in the sample code above equal?
Choice 1
-3
Choice 2
0
Choice 3
4[Ans]
Choice 4
4 + sizeof( int )
Choice 5
4 * sizeof( int


Q30. void myFunc (int x)
{
if (x > 0)
myFunc(--x);
printf("%d, ", x);
}
int main()
{
myFunc(5);
return 0;
}

What will the above sample code produce when executed?
Choice 1
1, 2, 3, 4, 5, 5,
Choice 2
4, 3, 2, 1, 0, 0,
Choice 3
5, 4, 3, 2, 1, 0,
Choice 4
0, 0, 1, 2, 3, 4, [Ans]
Choice 5
0, 1, 2, 3, 4, 5,


Q31. 11 ^ 5
What does the operation shown above produce?
Choice 1
1
Choice 2
6
Choice 3
8
Choice 4
14 [Ans]
Choice 5
15


Q32. #define MAX_NUM 15
Referring to the sample above, what is MAX_NUM?
Choice 1
MAX_NUM is an integer variable.
Choice 2
MAX_NUM is a linker constant.
Choice 3
MAX_NUM is a precompiler constant.
Choice 4
MAX_NUM is a preprocessor macro. [Ans]
Choice 5
MAX_NUM is an integer constant.


Q33. Which one of the following will turn off buffering for stdout?
Choice 1
setbuf( stdout, FALSE );
Choice 2
setvbuf( stdout, NULL );
Choice 3
setbuf( stdout, NULL );
Choice 4
setvbuf( stdout, _IONBF );
Choice 5
setbuf( stdout, _IONBF );


Q34. What is a proper method of opening a file for writing as binary file?
Choice 1
FILE *f = fwrite( "test.bin", "b" );
Choice 2
FILE *f = fopenb( "test.bin", "w" );
Choice 3
FILE *f = fopen( "test.bin", "wb" );
Choice 4
FILE *f = fwriteb( "test.bin" );
Choice 5
FILE *f = fopen( "test.bin", "bw" );


Q35. Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory?
Choice 1
memcpy()
Choice 2
memset()
Choice 3
strncpy()
Choice 4
strcpy()
Choice 5
memmove()[Ans]


Q36. int x = 2 * 3 + 4 * 5;
What value will x contain in the sample code above?
Choice 1
22
Choice 2
26[Ans]
Choice 3
46
Choice 4
50
Choice 5
70


Q37. void * array_dup (a, number, size)
const void * a;
size_t number;
size_t size;
{
void * clone;
size_t bytes;
assert(a != NULL);
bytes = number * size;
clone = alloca(bytes);
if (!clone)
return clone;
memcpy(clone, a, bytes);
return clone;
}

The function array_dup(), defined above, contains an error. Which one of the following correctly analyzes it?
Choice 1
If the arguments to memcpy() refer to overlapping regions, the destination buffer will be subject to memory corruption.
Choice 2
array_dup() declares its first parameter to be a pointer, when the actual argument will be an array.
Choice 3
The memory obtained from alloca() is not valid in the context of the caller. Moreover, alloca() is nonstandard.
Choice 4
size_t is not a Standard C defined type, and may not be known to the compiler.
Choice 5
The definition of array_dup() is unusual. Functions cannot be defined using this syntax.


Q38. int var1;
If a variable has been declared with file scope, as above, can it safely be accessed globally from another file?
Choice 1
Yes; it can be referenced through the register specifier.
Choice 2
No; it would have to have been initially declared as a static variable.
Choice 3
No; it would need to have been initially declared using the global keyword.[Ans]
Choice 4
Yes; it can be referenced through the publish specifier.
Choice 5
Yes; it can be referenced through the extern specifier.


q39. time_t t;
Which one of the following statements will properly initialize the variable t with the current time from the sample above?
Choice 1
t = clock();[Ans]
Choice 2
time( &t );
Choice 3
t = ctime();
Choice 4
t = localtime();
Choice 5
None of the above


Q40. Which one of the following provides conceptual support for function calls?
Choice 1
The system stack[Ans]
Choice 2
The data segment
Choice 3
The processor's registers
Choice 4
The text segment
Choice 5
The heap

No comments:

Blog List