1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Saturday, February 28, 2009

Hexadecimal To Decimal Conversion Program

#include
#include
#include
#include
#include
#define MAX_STR 80
void reverse ( char * str )
{
char *beg, *end;
beg = str;
end = str + strlen ( str ) - 1;
while ( beg < end )
{
*beg ^= *end ^= *beg ^= *end;
beg++; end--;
}
return;
}
int to_num ( char dig )
{
if ( !isxdigit ( dig ) )
{
fprintf ( stderr, "Not a hexadecimal number" );
exit ( EXIT_FAILURE );
}
if ( isalpha ( dig ) )
return tolower ( dig ) - 'a' + 10;
else return dig - '0';
}
intmain ( void )
{
char str[MAX_STR];
long dec = 0, exp = 0;
char *ptr;
printf ( "Enter a hexadecimal number: " );
scanf ( "%s", str );
/* discard "0x" or "0X" */
if ( str[0] == '0' )
if ( tolower ( str[1] ) == 'x' )
memmove ( str, str+2, 3 );
puts ( str );
/* use function reverse(s) of exercise 1-19.c */
reverse ( str );
ptr = str;
while ( *ptr )
{
dec = dec + to_num ( *ptr ) * pow ( 16, exp );
exp++;
ptr++;
}
printf ( "Decimal: %ld\n", dec );
return EXIT_SUCCESS;
}

Friday, February 27, 2009

How can I declare an array with only one element and still access elements beyond the first element (in a valid fashion)?

Discuss it!
There is a way to do this. Using structures.
struct mystruct
{
int value;
int length;
char string[1];
};
Now, when allocating memory to the structure using malloc(), allocate more memory than what the structure would normally require!. This way, you can access beyond string[0] (till the extra amount of memory you have allocated, ofcourse). But remember, compilers which check for array bounds carefully might throw warnings. Also, you need to have a length field in the structure to keep a count of how big your one element array really is :). A cleaner way of doing this is to have a pointer instead of the one element array and allocate memory for it seperately after allocating memory for the structure.
struct mystruct
{
int value;
char *string; // Need to allocate memory using malloc() after allocating memory for the strucure.
};

Is char a[3] = "abc"; legal?

Discuss it!
It declares an array of size three, initialized with the three characters 'a', 'b', and 'c', without the usual terminating '\0' character. The array is therefore not a true C string and cannot be used with strcpy, printf %s, etc. But its legal

What is the difference between enumeration variables and the preprocessor #defines?

Discuss it!
Functionality Enumerations Numeric values assigned automatically?
Enumerations -YES
#defines -NO
Can the debugger display the symbolic values?
Enumerations -YES
#defines - NO
Obey block scope?
Enumerations -YES
#defines -NO
Control over the size of the variables?
Enumerations -NO
#defines -NO

What is the difference between char *a and char a[]?

There is a lot of difference!
char a[] = "string"; char *a = "string";
The declaration char a[] asks for space for 7 characters and see that its known by the name "a". In contrast, the declaration char *a, asks for a place that holds a pointer, to be known by the name "a".
This pointer "a" can point anywhere. In this case its pointing to an anonymous array of 7 characters, which does have any name in particular. Its just present in memory with a pointer keeping track of its location.
char a[] = "string";
+----+----+----+----+----+----+------+
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
a: s t r i n g '\0' +----+----+----+----+----+----+------+
char *a = "string";
+-----+ +---+---+---+---+---+---+------+
a: *======> s t r i n g '\0' +-----+ +---+---+---+---+---+---+------+
Pointer Anonymous array It is curcial to know that a[3] generates different code depending on whether a is an array or a pointer.
When the compiler sees the expression a[3] and if a is an array, it starts at the location "a", goes three elements past it, and returns the character there.
When it sees the expression a[3] and if a is a pointer, it starts at the location "a", gets the pointer value there, adds 3 to the pointer value, and gets the character pointed to by that value. If a is an array, a[3] is three places past a. If a is a pointer, then a[3] is three places past the memory location pointed to by a.
In the example above, both a[3] and a[3] return the same character, but the way they do it is different! Doing something like this would be illegal.
char *p = "hello, world!";
p[0] = 'H';

General Questions on Pointers In C Language Part-1

1. An element of structure can be a Pointer; Which Points to a structure of the same type is called ___.
referential pointer
structure pointer
pointer structure
Self-Referential Structure
Ans. : d
2.Consider the following statements:

int *p;
int i;
int k;
i = 42;
k = i;
p = &i;
After these statements, which of the following statements will change the value of i to75?
K = 75
*k = 75;
p = 75;
*p = 75;
Ans. : d, Indirection operator will yield the value of the variable i.
3.Consider the following statements:

int i = 42;
int j = 80;
int *p1;
int *p2;
p1 = &i;
p2 = &j;
*p1 = *p2;
printf(“%d%d",i,j);
What numbers are printed by the output statement?
a. 42 and then another 42
b. 42 and then 80
c. 80 and then 42
d. 80 and then another 80
Ans. : d, since the last statement will change the values of i, as the *p2 which is actually j value will be copied to *p1 i.e. i.
4. . void main()
{
int *p1,*p2;
p1=(int *)malloc(sizeof(int));
p2=(int *)malloc(sizeof(int));
*p1=17;
*p2=153;
1st O/p printf("\n%d.......%d..",*p1,*p2);
free( p1);
p1=p2;
2nd O/p printf("\n%d.......%d..",*p1,*p2);
*p1=253;
3rd O/p printf("\n%d.......%d..",*p1,*p2);
*p2=355;
4th O/p printf("\n%d.......%d..",*p1,*p2);
}
What will be the O/p at O/p 4.
a. 17….153
b. 253….253
c. 253….353
d. 353….353
Ans. : d, Since both p1 and p2 point to the same location, so when indirection of p2 is modified, the actual value gets changed, and both the pointers indirection will yield the same value.
5.What is printed by these statements?

int i = 1;
int k = 2;
int *p1;
int *p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
printf(“%d”,i);
a. 1
b. 2
c. 3
d. 4
Ans. : a, Since the line p1 = p2, makes both of the pointers to point at k, so the next two lines changes the value of k.
6.What is printed by these statements?
int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
p1 = p2;
*p1 = 3;
*p2 = 4;
printf(“%d”,k);
a. 1
b. 2
c. 3
d. 4
Ans. : d, Since the line p1 = p2, makes both of the pointers to point at k, so the next two lines changes the value of k and the last changed value is 4.
7.What is printed by these statements?
int i = 1;
int k = 2;
int* p1;
int* p2;
p1 = &i;
p2 = &k;
*p1 = *p2;
*p1 = 3;
*p2 = 4;
printf(“%d”,i);
a. 1
b. 2
c. 3
d. 4
Ans. : c, Last but one line changes the value stored in i to 3.
8.In which location do dynamic variables reside?
a. The code segment.
b. The data segment.
c. The heap.
d. The run-time stack.
Ans. : c, All the dynamic memory allocation is done from the heap of the program.
9.Suppose you have the following function prototype and variable declaration:
void goop(int z[ ]); int x[10];
Which is the correct way to call the goop function with x as the argument:
a. goop(x);
b. goop(x[ ]);
c. goop(x[10]);
d. goop(&x);
e. goop(&x[ ]);
Ans. : a,
10. What's wrong with this code?

char *p;
*p = malloc(10);
a. No Error
b. Logical Error
c. Syntax Error
d. All of the above
Ans.: c, Memory is allocated to the pointer and not to indirection of the pointer.
11. Is there a problem with this code…if yes then what
typedef struct {
char *item;
NODEPTR next;
} *NODEPTR;
a. No Problem
b. The struct ptr cannot be defined inside the same struct
c. Problem with typedef
d. The struct has to be defined first
Ans. : c, the problem is with typedef, struct node{ char *item; struct node *next; } *NODEPTR; would be correct 12. Does *p++ increment p, or what it points toa. equivalent to *(p++)b. equivalent to (*p)++c. Gives Errord. Equivalent to *(++p)Ans. : a
13.Suppose that the goop function from the previous question changes the value of z[1]. Does this change effect the value of the actual argument?
Yes
No
Ans. : Yes, it will change.
14.Here is a function declaration:

void goo(int* x) {
*x = 1;
}
Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you print *a after the function call goo(a)?
a. 0
b. 1
c. address of a
d. address of x
e. None of the above
Ans. : b, The value is changed to 1 in the function by the indirection operator.
15.A variable b if represents its value, what does &b represent

a. Another way to access value of b
b. Address of b
c. Pointer to b
d. Pointer on b
Ans. : b, The operator ‘&’ as such says at the address of, it is not a separate variable to be a pointer.
16. What is the problem if any…
int array[5], i, *ip;
for(i = 0; i < 5; i++)
array[i] = i;
ip = array;
printf("%d\n", *(ip + 3 * sizeof(int)));
a. No Error
b. Prints 3
c. Prints Garbage(Logical Error)
d. Syntax Error
Ans. : c, When ip+3 is calculated, since it is an int pointer it automatically traverses to the 3rd element in the array, size of is not required.
17. What will go wrong if any,
……….
void *p1, *p2;
*p1++ …
*p2--…..
……….
a. No error
b. Pointers Cannot have Additions and subtractions
c. Void Pointers cannot undergo any arithmetic operation
d. Syntax Logic is correct But syntax is a problem
Ans. : c, Void pointers cannot undergo ANY because the size of the pointing object is unknown.
18. In the statement

Char * p1, p2;
a. Both are pointers
b. Only p1 is a char pointer but p2 is not
c. Neither p1 nor p2 are pointers
d. Syntax Error
Ans. : b.
19. What is NULL…
a. Practically = 0
b. A Macro defined in stdio.h
c. Some Garbage Value
d. A pointer Pointing to Nothing
Ans. : b.
Fig Pointers1.1
2006
2008
4
i j k

2004 2006 2008

int *i, *j, k;
k = 4;
j = &k;
i = &j;
20. printf(“%d”,j);
a. 4
b. 2006
c. 2008
d. Error
Ans. : c
21. printf(“%d”,*j);
a. 4
b. 2006
c. 2008
d. Error
Ans. : a
22 . printf(“%d”,*i);
a. 4
b. 2006
c. 2008
d. Error
Ans. : c
23. printf(“%d”,**i);
a. 4
b. 2006
c. 2008
d. Error
Ans. : a
24. printf(“%d”,&k);

a. 4
b. 2006
c. 2008
d. Error
Ans. : c
25. printf(“%d”,&j);
a. 4
b. 2006
c. 2008
d. Error
Ans. : b
26. printf(“%d”,&i);
a. 2004
b. 2006
c. 2008
d. Error
Ans. : a
27. The pointer that may point to a structure of same type as the structure is called as______
a. Referential pointer
b. Self-Referential Pointer
c. Structure pointer
d. Pointer structure
Ans)B
28. Assume Memory location = 100 (decimal). What happens when we write:
ptr + 1;(ptr is pointing to an integer variable)
a. ptr points 101
b. ptr point to 102
c. ptr point to 104
d. Gives an error
Ans. : b, when a pointer variable is incremented it’s incremented by the size of the data type it is pointing to, in this case int.
29. What will *++p yield:
a. 1
b. 23
c. Garbage
d. Error
Ans. : b, Since it is ++p, it will be de-referencing to the second place directly.
30. What will *p+1 yield:
a. 1
b. 23
c. 2
d. Error
Ans. : c, Since it is p+1, first p will be de- referenced then the value got will be incremented, so 2.
31. What will *(p+1) yield:
a. 1
b. 23
c. Garbage
d. Error
Ans. : b, The pointer value will be incremented first then it will be de – referenced so to the second location yields 23.
32. What’s wrong if any :
struct abc{
int a =40;
struct abc *n;
};
a. Nothings Wrong
b. Error because of initialization
c. Error because of the pointer
d. Syntax Error
Ans. : b, initialization is not allowed inside a structure
33. What is true for calloc:
a. calloc initializes memory after allocating it.
b. calloc does not initialize after allocating it
c. calloc is not used to allocate memory
d. calloc takes in only one argument.
Ans. : a, One of the difference between calloc and malloc is that calloc initializes the allocated memory.
34. What is False for calloc:
a. Calloc takes in two arguments
b. Calloc returns a pointer to the reserved memory
c. Calloc is a function defined in alloc.c
d. Calloc always initializes to 0.
Ans. : d, The second argument in calloc is the one which determines what the allocated space is supposed to be initialized to.
35. What is true for malloc:

a. Malloc initializes memory after allocating it.
b. Malloc does not initialize after allocating it
c. Malloc is not used to allocate memory
d. Malloc takes in two arguments.
Ans. : b, One of the difference between calloc and malloc is that malloc does not initialize the allocated memory.
36. What is False for Malloc:
a. Malloc takes in one argument.
b. Malloc returns a pointer to the reserved memory
c. Malloc is a function defined in alloc.c
d. Malloc always initializes memory to some value specified in the call.
Ans. : d, Malloc never initializes the memory in the first place.
37. The command to free the memory reserved during the running of the program is:
a. realloc()
b. calloc()
c. malloc()
d. free()
Ans. : d, free actually frees the memory or returns the memory back to the heap.
38. The ‘&’ is known as __________.
a. Referencing Operator
b. Dereferencing Operator
c. Memory Allocation Operator
d. Memory freeing operator
Ans. : a, Referencing Operator or at the address of operator
39. The ‘*’ is known as ___________.

a. Referencing Operator
b. Dereferencing Operator
c. Memory Allocation Operator
d. Memory freeing operator
Ans. : b, Dereferencing Operator or value at operator
40. Consider the code
int A;
int * ptr = &A;
int** ptp = &ptr;
ptp will yield:
a. The same value as in ptr
b. Value of A
c. The address of ptr
d. Will give an error, a pointer cannot be pointed
Ans. : c, &ptr will return the address of variable ptr.
41. int a = 77;
int *d,*f,*i,*g,*t;
d = &a;
f = &a;
i = &a;
g = &a;
t = &a;
a. Error not more than one pointer can point to a single location.
b. d,f,i,g,t will have the same address of 77
c. d,f,i,g,t will have the value 77
d. Error due to assignment operator
Ans. : b, It is absolutely fine with as many number of links to the same address.
42. int a = 77;
int *g,*u;
g = &a;
u = g;
a. Error due to copying to two pointers
b. g will have the address of a and u will have the value of a
c. g and u will have the same address
d. u will have the address of g
Ans. : b, pointers can be copied the contents that is the address will be copied
43. int a = 60, b = 70;
int *i,*j,*k;
*k = i * j / 10;
a. K will have the value 4200
b. Error, multiplication is not possible on pointers
c. Syntax error
d. K will have the product of the addresses held in i and j
Ans. : b, pointers can only undergo addition and subtraction not multiplication and division.
44. void *i;
int k = 55;
i = &k;
a. Creates an error
b. No problem with this
c. Logical error
d. It is ok, but pointer arithmetic cannot be performed
Ans. : a, void pointer is a generic pointer, it cannot be directly pointed to any variable, since in a void pointer the size of the datatype it will be pointing to is unknown.
45. void *i;
int k = 55;
(int *) i = &k;
a. Creates an error
b. No problem with this
c. It will create a logical error since void pointer
.d. The address will be stored but the pointer will anyways be of one byte.
Ans. : b, No problem with this because although a generic pointer but is type casted.
46. int func(int a, float b); T
his is a prototype of which argument passing type,
a. Pass by Value
b. Pass by Reference
c. Call by Reference
d. It generates an Error.
Ans. : a, This is an example of pass by value
47. int func(int *a, float *b);
This is a prototype of which argument passing type,
a. Pass by Value
b. Pass by Reference
c. Call by Reference
d. It generates an Error.
Ans. : b, This is an example of pass by value
48. The term pointee in pointer assignment is used to indicate __________
a. The pointed location
b. The location of the pointer
c. There is no term as pointee
d. The term does not deal with pointers.
Ans. : a, pointee is basically the location which the pointer is pointing to.Refn. : http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
49. What are two pointers pointing to the same location called.
a. Multiple Pointers
b. Sharing Pointers
c. Sharing Pointees
d. Multiple Pointees
Ans. : b, two pointers sharing the same pointee are known as sharing pointers.Refn. : http://cslibrary.stanford.edu/102/PointersAndMemory.pdf
50. What is the solution called if two functions share the same copy of memory without actually copying the memory into their respective space allocated.(In ‘C’)
a. Pass by reference
b. Pass by value
c. Deep Copying
d. Shallow Copying
Ans. : d, Shallow copying is a technique in which more than one function refer to the same location by using a pointer each, both of which point to the same pointee. Refn. :
51. What is the technique known as in which the complete memory location is passed to the functions, where each function has its own copy of the memory it wants to access and is even free to edit, which was not available with shallow copying.
a. Pass by reference
b. Pass by value
c. Deep Copying
d. Shallow Copying
Ans. : c, Deep Copying, Refn. Is the same pdf as above.
52. What is a bad pointer?
a. A pointer which do not have any pointee, initially.
b. It’s similar to dangling pointer
c. No term as bad pointer exists
d. A pointer pointing to a corrupt location
Ans. : a, Every pointer when initialized is actually a bad pointer, because it does not point to any location and bad pointers differ from dangling pointers by, the fact that dangling pointers first points to a location and then without dereferencing the location if deleted, the pointer becomes dangling.
53. What is a dangling pointer?
a. A pointer which do not have any pointee, initially.
b. It’s similar to bad pointer
c. No term as dangling pointer exists
d. A pointer pointing to a location, which does not exist anymore.
Ans. : d, Explanation is same as of the bad pointer.
54. Consider the code snippet,
void eg( )
{
int *p;
*p = 45;
printf(“%d”,*p);
}
a. Program has a syntax error.
b. It generates an error on runtime.
c. It works perfectly fine with 45 being printed on the screen.
d. The pointer does not get dereference in the printf statement
Ans. : b, it generates a runtime error, pointer cannot point to data as such, it can only point to locations.
55. What will be the o/p
void main(){
int *x,*y;
x = (int *)malloc(sizeof(int));
*x = 42;
y = x; *y = 24;
printf("%d",*x);
}
a. Generates a syntax error
b. Generates a runtime error
c. Prints 42 on console
d. Prints 24 on console
Ans. : d, prints 24 on console.
56. Consider the code..
void main() {
int *x,*y; x = (int *)malloc(sizeof(int));
*x = 42; y = x;
printf("%d",*y);
}
a. Generates a syntax error
b. Generates a runtime error
c. Prints 42 on console
d. Prints 24 on console
Ans. d,Prints 42 on console.
57. Consider the code..

void main(){
int *x,*y;
x = malloc(sizeof(int));
*x = 42;
y = x;
*y = 24;
printf("%d",*y);
}
a. Generates a syntax error
b. Generates a runtime error
c. Prints 42 on console
d. Prints 24 on console
Ans. : a, Generates a syntax error, malloc will return a void pointer, it has to be typecasted.
58. Consider the code…
void main()
{
int *x,*y;
x = (int *)malloc(sizeof(int));
*x = 42;
y = x;
*y = 24;
printf("%d,%d”,x,y);
}
a. 42, 24
b. 24 , 24
c. 409863, 409863
d. 409863, 409865
Ans. : c, both will print the same address on the console, that in this case is 409863.
59. What is wrong with the code if any .
int * tab()
{
int temp;
temp = 24;
return (&temp);
}
void victim() {
int *ptr;
ptr = tab();
printf(“%d”,*ptr);
}
a. Generates a syntax Error
b. Generates a Runtime Error
c. Prints 24 on the console.
d. Prints Garbage.
Ans. : b, Generates a runtime Error, since temp is only a local pointee.
60. What happens when a memory is allocated in a pointer and then without de-allocating the function exits…
a. Nothing Happens
b. A runtime Error occurs
c. Memory Leak
d. Automatic de allocation happens
Ans. : c, memory leak will occur, i.e. the function will exit for that time but the memory will not be allocated for any further requirements.
61. Memory to the pointer is allocated from
a. Program Heap allocated to the program
b. Borrowed from secondary Memory
c. Borrowed from primary Memory
d. No memory is allocated, since it does not store any value
Ans. : a, memory newly allocated is always allocated from the program heap.
Consider the piece of code :
void main()
{
int *p1,*p2;
p1=(int *)malloc(sizeof(int));
p2=(int *)malloc(sizeof(int));
*p1=17;
*p2=153;
1st O/p printf("\n%d.......%d..",*p1,*p2);
free( p1);
p1=p2;
2nd O/p printf("\n%d.......%d..",*p1,*p2);
*p1=253;
3rd O/p printf("\n%d.......%d..",*p1,*p2);
*p2=355;
4th O/p printf("\n%d.......%d..",*p1,*p2);
}
62. What will be the output at O/p 1.
a. 17 ……153
b. 17…….
c. ….153
d. …….
Ans. : a, It at no time on the 1st output give garbage value, both the pointers have been assigned proper values.
63. What will be the O/p at O/p 2.
a. 17……153
b. 153……153
c. Results into a runtime error, because p1 has been freed
d. 17…….17
Ans. : b, there wont be any error because the pointer has been freed but has not been eliminated from the program, so on p1, p2’s address is copied, resulting in p1 and p2 both pointing to the same location.
64. What will be the O/p at O/p 3.
a. 17…..153
b. 153…..153
c. 253…..253
d. 253…..153
Ans. : c, Since both p1 and p2 point to the same location, so when indirection of p1 is modified, the actual value gets changed, and both the pointers indirection will yield the same value.
65. Assume Memory location = 2022 (decimal). What happens when we write:
ptr + 1;(ptr is pointing to an double variable)
a. ptr points 2023
b. ptr point to 2024
c. ptr point to 2030
d. ptr points to some garbage location
Ans. : c, when a pointer variable is incremented it’s incremented by the size of the data type it is pointing to, in this case double, so 8.
66. Consider this code
int a[] = {1,23,17,4,-5,100};
int *p = a;
What will *p++ yield :
a. 1
b. 23
c. Garbage
d. Error
Ans. : a, first de-referencing will take place then increments will happen. Since it is p++, the value of the pointer will be incremented in the next stmt.

ARRAYS (DATA STRUCTURES…ARRAYS) In C - Language Questions

1. An array of n elements will be declared in c as:
a. array[n]
b. array[n-1]
c. array[n+1]
d. array
Ans : a. array[n]
2. The first element of a 0 based array can be accessed by :
a. array
b. array[0]
c. array[n-1]
d. array[n]
Ans. : a. & b. array[0] is obvious.. and array is actually pointing toward the first element of the array.
3. What will the piece of code reveal…
int a[] = {1,2,3,4,5,6,7,8,9,0};
printf(“%d”,a[2]);
printf(“%d”,2[a]);
** Regarding the fact that there are no syntax errors..
a. 3 8
b. Logical error
c. 3 3
d. 3
Ans. c. both will be resolved as (address of a + 2), so both will yield the same address.
4. Memory is allocated in an array in :
a. Non Continues Way
b. Continues fashion
c. It may be any way, does not matter
d. Random but in some logical manner according to some algorithm.
Ans. : b.. Memory is indeed allocated in a continues manner, that is the reason why arrays can offer random access while linked lists cant.
5. A ‘n’ sized 1 based array indexed from:
a. [1] – [n]
b. [0] – [n]
c. [1] – [n-1]
d. [0] – [n-1]
Ans. : a An array may be 0 based, 1 based or n based. Now whichever it is based on that number decides its starting and ending index.
6. How is this resolved.. Arr[2];
a. *(&Arr + 2)
b. Arr[0] + 2
c. The Second position from Arr[0]
d. *(Arr + 2)
Ans. : a. Arr always refers to the first element of the array.. and the index is added to the base address of the array to get the result.
7. Lower bound and Upper bound of an Array represent the following element..
a. Largest and Smallest Resp.
b. Smallest and Largest + 1
c. Smallest and Largest – 1
d. Smallest and Largest Resp.
Ans.: d., Lower bound represents the first element of the array and upper bound represents the last element of the array.
8. In an Array the range specifies…
a. Scope of the Array
b. Nos. of Elements in the Array
c. The Group of the Array
d. Size – 1 of the Array
Ans. : b., Range Specifies the total nos of elements in an Array.
9.If Array’s upper bound is specified by ‘U’ and lower bound is specified by ‘L’ then the range of the array will be.
a. U – L +1
b. L + U – 1
c. U – L – 1
d. L – U + 1
Ans. :a The range of the array will be Upper - Lower + 1
10 . If Upper bound is represented by 99 and lower bound by 0 in a o based array, then the range of the array is..

a. 0
b. 99
c. 100
d. 101
Ans. : c., Range is given by Upper – Lower + 1
11. A string is ended by…
a. The Last Character
b. The Null Character
c. The Dot Character
d. The First Character
Ans. : b, A string in C is Always Ended by a Null Character.
12. Suppose that you want to declare an array of characters to hold a C++ string with exactly 9 letters. Which declaration is best?
a. char s[8];
b. char s[9];
c. char s[10];
d. char s[11];
e. char s[12];
Ans : c, you will need 10 cells, 9 to hold the 9 characters and the last one cell to hold the end of the string
13. Which of the following can be used to describe arrays as well as hard disks?
a. static
b. dynamic
c. direct access
d. linked access
e. linked
Ans. : c.
14. Of the following, which would access an element of an array of records?
a. Data[i]
b. Data(i)
c. Data(i,j)
d. Data.i
e. Data[i].f
Ans. : a
15. Arrays are also called as
1) Dense lists2) Predefined variables3) Static data structures
a. 1 and 2
b. 2 and 3
c. 1 and 3 d)All
Ans. : c
16. If a is an array (which actually is a pointer as such) and a pointer p;
The statement a = p, yields
a. Address of p copied on a
b. Address remains unchanged
c. Leads to an error
d. Address of a is copied on p instead.
Ans. : c, Leads to an error, since array a, is a pointer but a constant pointer.

General Questions on Data Structures In C -1.

1. What is Data Structure?
a. Addresses of the variables
b. Subset of all variables
c. The memory representation of data
d. The type of the variables
Ans. : c
2. A tool for specifying logical properties of a data type is…
a. Abstract Data Type
b. Logical Data Type
c. Non Abstract Data Type
d. Linear Data Type
Ans . : a, Abstract Data Type… Logical Properties are defined by ADT.
3. The Separation of data structures and their operations from the implementation of the data structures in memory and functions is called

______________.
a. Data Abstraction
b. Data hiding
c. Data Extraction
d. Data insertion
Ans. : a
4. A set of instances or values is called a ___________________.
a. variable
b. Data Object
c. Data type
d. Class
Ans. : b
5. The individual instances of a Data Object are called ___________.
a. class
b. object
c. Primitive
d. Data object
Ans. : c
6. Formulae based representation uses ______________to represent the instances of object.
a. tree
b. link list
c. Array
d. Graph
Ans. : c
7. An example of a dynamic data structure is:
1) array 2) record 3) list 4) index
a. 1 or 3
b. 2 and 4
c. Only 2
d. Only 4
Ans. : a, Array can also be dynamically allocated using malloc
8.
Which Of the following is a collection of heterogeneous elements?
a. Array
b. Structure
c. Stack
d. Queue
Ans)B
9.Which Data Structure is used to manage Printer Buffer?

a. Stack
b. Queue
c. Linked List
d. Tree
Ans)B
10. Which One of the following is a hierarchical data structure?
1. Linked list
2. Stack
3. Tree
4. None
a. 1 or 3
b. 2 or 4
c. Only 2
d. Only 4
Ans) a, Both are hierarchical structure
11. Complexity in terms of machine cycles of a data structure is measured in terms of

A. Time Complexity
B. Space Complexity
c. Mean Complexity
d. A and B
Ans)a
12. Which data structure is used in evaluating mathematical expressions with parentheses?
a. Stack
b. Queue
c. Tree
d. Graph
Ans)A
13. Which Of the following occupies more memory with same number of elements? Array
a. Single Linked list
b. Doubly Linked List
c. Queue
Ans) c
14. Recursive function implements which mechanism?
1) Queue 2) Lifo 3) Filo 4) Fifo
a. Only 1
b. 2 or 3
c. 1 or 4
d. All
Ans) b
15. Which of the following abstract data types cannot be used to represent a many to many relation?
1) Binary Tree 2) Graph 3) Stack
a. 1 and 2
b. 2 and 3
c. 1 and 3
d. All
Ans)C
16. An Example of linear Data structures ?
1) Array 2) Stack 3) Linked List
a. 1 and 2
b. 2 and 3
c. 1 and 3
d. All
Ans)D
17. The logical picture of data type plus the specification of operations required to create & manipulate objects of this type is known ___
a. Structure
b. Pointer
c. ADT
d. Class
Ans)C
18. In Quick sort & Radix sort except array ________data structure is used?
a. Pointer
b. Link list
c. Double link list
d. Tree
Ans)A
19. In Counter index generation ________ data structure is used
a. Deque
b. Priority Queue
c. Circular Queue
d. Linear Queue
Ans)C
20. Simulations are implemented using _______ data structure.
a. Stack
b. Queue
c. Linked list
d. Tree
Ans)B
21. The data structures you will use if you want to go to first record from the last and vice versa______________
a. Tree
b. Linked list
c. Stack
d. Doubly linked circular list
Ans)D
22. In RDBMS, the efficient data structure used in the Internal storage representation is
a. Binary tree
b. Stack
c. B+Tree
d. Graph
Ans)C
23. Pick the appropriate statement for the data structures from the following options.
a. May be helpful to develop efficient algorithms in different phases of data processing
b. Need not give relationship between data items
c. Is programming language dependent
d. None
Ans)A
24. Suppose you opened a notepad, a music player, an excel sheet, and also you are doing your data structure programming simultaneously. Your OS implements which data structure for it.
a. Stack
b. Queue
c. Tree
d. Linked list
Ans)B
25. Which of the following abstract data types can’t be used to represent a many to many

1)Tree 2)Stack 3)Graph
a. 2 and 3
b. 1 and 3
c. 1 and 2
d. None
Ans)C
26. The Linear properties of data structure are estimated on the bases of ?
1. adjecency 2. Linearity 3. Levels
a. 2 and 3
b. 1 and 2
c . 1 and 3
d. ALL
Ans)B
27. The following are the examples of Linear Data Structures?
1. Polynomial 2. Graph 3. Trees
a. 1 and 2
b. 2 and 3
c. 1 and 3
d. None
Ans)D
28. List out the areas in which data structures are applied extensively?1)Numerical Analysis2)Graphics3)Artificial Intelligence
a. 1 and 2
b. 1 and 3
c. 2 and 3
d. 1,2 and 3
Ans)D
29. Which of the following combination of data structures used in the areas : RDBMS, Network data model and Hierarchical data model are correct
1)RDBMS– Array (i.e. Array of structures)
2)Network data model – Graph
3)Hierarchical data model – Trees
a. 1 and 2
b. 1 and 3
c. 2 and 3
d. None
Ans)C
30. Polynomials in memory may not be maintained through

1)Linked list with header node
2)One dimensional array
3)Stack
a. 2 and 3
b. 1 and 3
c. 1 and 2
d. None
Ans)C

C and Data Structures Question Bank Part-4

1. Which of the following is not a character of ‘C’ ?
a) ~
b) X
c) $
d) ^
Ans :- c
2. The associativity of assignment operator is
a) Left to right
b) Right to left
c) Either right to left or left to right
d) None
Ans :- b
3. Header files in C contains
a) Compiler commands
b) Header file information
c) Library functions
d) Operators
Ans :- c
4. The scanf () function returns
a) The actual values read for each argument
b) The number of successfully read inputs
c) No values
d) ASCII values
Ans :- b
5. What is the output of the following code
main()
{
printf(“%c”,’A’);
}
a) 65
b) a
c) A
d) Error
Ans :- c
6. If x=2,y=6,z=6 then what will be the output of
x = y = = z;
printf(“%d”,x);
a) 0
b) 1
c) 2
d) 6
Ans :- b
7. Quantities that appear as fixed values in the program code are
a) Variables
b) Constants
c) Operators
d) Identifiers
Ans :- b
8. For how many times the following loop is executed?
while(i=10)
{
}
a) 0
b) 1
c) 10
d) infinite
Ans :- d
9. Which of the following statements is false?
a) Each new C statement has to be written on a separate line
b) Usually all C statements are entered in lower case letters
c) Blank spaces may be inserted between two words in a C statement
d) C statement always ends with a semicolon
Ans :- d
10. A C program is a collection of
a) Subroutines
b) Functions
c) Procedures
d) Structures
Ans :- b
11. Which symbol is used to represent processing statements in a flow chart
a) Parallelogram
b) Rectangle with rounded sides
c) Rectangle
d) Rectangle with double ended sides
Ans :- c
12. An identifier in C
a) is a name of an entity such as variable and function
b) is made up of alphabets, numerals and underscore character
c) contains both uppercase and lowercase alphabets
d) all the above
Ans :- d
13. The maximum width of a C variable name can be
a) 6 characters
b) 8 characters
c) 10 characters
d) 31 characters
Ans :- b
14. Which of the following statement is wrong?
a) value1=123.45;
b) value2=’T’*’A’;
c) value3=’X’*20;
d) 5+x=y;
Ans :- d
15. Given
float a=12.6578;
printf(“%4.2f”,a);
What will be the output of the printf statement?
a) 12.56
b) 0012.56
c) 12.5678
d) 12.57
Ans :- d
16. What is the conversion character for hexadecimal integer?
a) h
b) i
c) x
d) u
Ans :- c
17. What will be the output of the following program?
main()
{
int x=10,y,z;
z=y=x;
y-=x--;
z-=--x;
x=--x – x--;
printf(“%d%d%d”,x,y,z);
}
a) 8 9 10
b) –1 0 2
c) 1 0 0
d) –2 0 2
Ans :- b
18.What would be the output of the following program?
main()
{
int a=250;
printf(“%1d”,a);
}
a) 2
b) 0
c) 250
d) 25
Ans :- c
19. Left shifting a number by 1 is equivalent to
a) Dividing by 2
b) Multiplying by 2
c) Adding by 2
d) Subtracting by 2.
Ans :- b
20. To print out a and b given below, which printf() statement would you use?
float a=3.14;
double b=3.14;
a) printf(“%f %f”,a,b);
b) printf(“%Lf %f”,a,b);
c) printf(“%Lf %Lf”,a,b);
d) printf(“%f %Lf”,a,b);
Ans :- a

C and Data Structures Question Bank Part-1 Answers

1. c
2. b
3. a
4. c
5. b
6. a
7. b
8. d
9. d
10. a
11. b
12. c
13. a
14. c
15. a

C and Data Structures Question Bank Part-3

31.
#define sqr(x) x*x
main( )
{
int y;
y= sqr(3+2);
printf(“%d”,y);
}
The output is
a. 25
b. 11
c. 10
d. none
Ans :- b
32.The output of the program given below is:
main()
{
int x=0,y=1;
if (x=y)
printf(“\n hello”);
else
printf(“\n bye”);
}
a) hello
b) bye
c) error
d) none
Ans :- a
33. #define cube(x) x* x*x
main( )
{
int x=3,y;
y=cube(x++);
printf(“%d%d”,x,y);
}
a. 4,27
b. 6,27
c. 6,60
d. 6,120
Ans :- b
34. #define cube(x) x* x*x
main( )
{
int x=3,y;
y=cube(++x);
printf(“%d%d”,x,y);
}
a. 6,216
b. 6,60
c. 6,120
d. 4,120
Ans :- a
35. What is the output ?
int i;
main()
{printf(“%d”,i);
}
a. 0
b. 1
c. error
d. cannot say
Ans :- a
36.
main(){
int i;
printf(“%d”,i);
}
a) error
b) cannot say
c) 0
d) 65535
Ans :- b
37.
main(){
int i=10;
printf(“%d…”,++i++);
printf(“%d”,++i);
}
a) 11…13
b) 12…13
c) error
d) none of the above
Ans :- c
38.
main()
{
int I=10;
I=I / ++I;
printf(“%d”,I);
}
a) 1
b) 10
c) error
d) cannot say
Ans :- a
39.
main()
{
int I=10,a;
a=I++/++I;
printf(“%d…%d”,a,I);
}
a) 1…10
b) 10…10
c) error
d) none of the above
Ans :- d
40.
main(){
printf(“%d”,3.0);
}
a) 3
b) 3.0
c) error
d) cannot say
Ans :- d
41.
main(){
printf();
}
a. error
b. 0
c. –1
d. cannot say
Ans :- d
42.
main(){
int j=100,k=200;
printf(“%d…%d”);
}
a) 200…100
b) 100…200
c) error
d) cannot say
Ans :- a
43.
main()
{int j;
for(j=0;j<=10;j++,printf(“%d”,j));
}
a) 0 to 10
b) 1 to 11
c) infinite loop
d) error
Ans :- b
44.
main()
{int j=5;
printf(“%d”,j = ++j == 6);
}
a) 0
b) 1
c) error
d) none of the above
Ans :- b
45.
main()
{int j;
printf(“%d”,j^j);
}
a. 0
b. 1
c. error
d. none of the above
Ans :- a
46.
main()
{printf(“%d”,3^2);
}
a) 0
b) 1
c) error
d) none of above
Ans :- b
47.
main()
{printf(“%d”,3>>2?100:200);
}
a) 100
b) 200
c) error
d) 300
Ans :- b
48.
main()
{printf(“\nH\ne\nll\ro”);
}
a) H
e
ol
b) H
e
ll
o
c) error
d) none of the above
Ans :- a
49.
main()
{int j;
for(j=0;j=3;j++)
printf(“%d”,j);
}
a. 4
b. 3
c. 0
d. infinite
e. error
Ans :- d
50.
main()
{printf(“\nHe%cllo”,13);
}
a) He
llo
b) He
llo
c) llo
d) none of above
Ans :- c
51.
main( )
{
long j;
j=65536+300;
printf(“%d…%d”,j);
}
a. 300…1
b. 300…..1
c. 1…300
d. 65836…65836
Ans :- a
52.
#define i j
main( )
{
int i= 10;
printf(“%d”,j);
}
a) garbage value
b) 5
c) 10
d) error
Ans :- c

C and Data Structures Question Bank Part-2

16. What is the output of the following program?
main()
{
unsigned int m=32;
printf(“%x”,~m);
}
a) ffff
b) 0000
c) ffdf
d) ddfd
Ans :- c
17. What does the following program do?
main()
{
unsigned int num;
int c=0;
scanf(“%u”,&num);
for(;num;num>>=1)
{
if(num&1)
c++;
}
printf(“%d”,c);
}
a) It counts the number of bits which are on (1s) in the number num.
b) It sets all bits in the number num to 1
c) It sets all the bits in the number num to 0
d) None of the above
Ans :- a
18. What does the following program do ?
main()
{
unsigned int num;
int I;
scanf(“%u”,&num);
for(I=0;I<16;I++)
printf(“%d”,(num << I & 1 << 15) ? 1 : 0);
}
a) It prints all even bits from num
b) It prints all odd bits from num
c) It prints binary equivalent of num
d) None of the above
Ans :-c
19. Which bit wise operator is suitable for checking whether a particular bit is on or off?
a) &
b)
c) ~
d) ^
Ans :- a
20. What is the output of the following program?
#define SQR(x) (x*x)
main()
{
int a,b=3;
a=SQR(b+2);
printf(“\n%d”,a);
}
a) 25 b) 11 c) Error d) Garbage value
Ans :- b
21. What would be the output of the following program?
main()
{
printf(“%d%d%d”,sizeof(3.14f),sizeof(3.14),sizeof(3.14l));
}
a) 4 4 4
b) 4 garbage value
c) 4 8 10
d) Error
Ans:- c
22. What is the binary equivalent of 5.375
a) 101.101110111
b) 101.011
c) 101011
d) None of the above
Ans :- b
23. What would be the output of the following program?
main()
{
int i=-3,j=2,k=0,m;
m=++i && ++j ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
a) –2 3 1 1
b) –2 3 0 1
c) –2 3 1 1
d) –3 2 0 1
Ans :- b
24. What would be the output of the following program?
main()
{
int i=-3,j=2,k=0,m;
m=++i ++j && ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
a) –2 2 0 1
b) –2 3 1 1
c) –2 3 0 1
d) –3 3 1 1
Ans :- a
25. What would be the output of the following program?
main()
{
int i= - 3,j=2,k=0,m;
m=++i && ++j && ++k;
printf(“\n%d%d%d%d”,i,j,k,m);
}
a) –2 2 0 1
b) –2 2 1 1
c) –2 3 1 1
d) –2 3 0 1
Ans :- c
26. Which code produces the following output?
1
22
333
4444
55555

a) a = 1;
while( a <= 5 ) {
while( b <= a ) {
printf("%d\n", a);
b = b + 1;
}
a = a + 1;
}

b) a = 1;
while( a <= 5 ) {
b = 1;
while( b <= a ) {
printf("%d", a);
b = b + 1;
}
printf("\n");
a = a + 1;
}
c) a = 1;
while( a <= 5 ) {
while( b <= 5 ) {
printf("%d", a);
b = b + 1;
}
a = a + 1;
printf("\n");
}
d) a = 1;
while( a <= 5 ) {
printf("\n");
b = 1;
while( a <= b ) {
printf("%d", a);
b = b + 1;
}
a = a + 1;
}
Ans :- b
27. If abc is the input, then the following program fragment results in
char x,y,z;
printf(“%d”,scanf(“%c%c%c”,&x,&y,&z));
a) A syntax error
b) A fatal error
c) Segmentation violation
d) Printing of 3
Ans :-d
28. what is the output of the following
main( )
{
int x=10,y=10,z=10;
if(++x ++y z++)
printf(“%d%d%d”,x,y,z);
}
a. 11 11 11
b. 11 11 10
c. 11 10 10
d. 10 10 10
Ans :- c
29. main( )
{
int x=1,y=2,z=3;
if(--x && --y ++z)
printf(“%d%d%d”,x,y,z);
}
the output is---------------
a. 0 2 4
b. 1 2 4
c. 0 1 4
d. 1 1 4
Ans :- a
30. 11%3, -11%3, -11%-3, 11%-3
the output of the above operations is
a. 2 -2 -2 2
b. 2 2 2 2
c. 2 -2 -2 -2
d. 2 –2 2 -2
Ans :- a

C and Data Structures Question Bank Part-1

1. Which operator has the least priority?
a) Semicolon
b) Assignment
c) Comma
d) Conditional
2. What is the value of j in the following expression?
i=1;
j=i<<1%2;
a) –2
b) 2
c) 10
d) 0
3. What is the value of ‘ i ‘ in the following expression?
i=1;
i=(i<<=1%2)
a) 2
b) 1
c) syntax error
d) 0
4. From the following code
char a,b;
scanf(“%c %s”,&a,&b);
What are the values assigned to a and b, if x and y are given as inputs?
a) a=x and b=blank
b) a=x and b is not assigned
c) a=x and b=y
d) a=x and b=x
5. From the following code:
int i=20;
printf(“%x”,i);
What will be the output of the above printf statement?
a) x14
b) 14
c) 20
d) none of the above
6.
main()
{
int i=0;
for(; ++i;)
printf(“%d”,i);
}
How many times the for loop is executed
a) 0
b) 32767
c) infinite
d) The maximum unsigned integer value
7. The minimum number of inputs and outputs for a program are
a) 0,0
b) 0,1
c) 1,0
d) 1,1
8. The purpose of main () function in a C program is to
a) Initialize all variables
b) Accommodate all input/output statements
c) Initialize necessary variable and open/close files
d) Initialize necessary variables, open/close files and maintain the overall control structures
9. Which of the following are the valid character constants?
a) ‘\n’
b) ‘\\’
c) ‘\0\’
d) All the above
10. What would be the value of x after executing the following program segment?
int x,y=10; char t=’b’;
x=y+t;
a) 108
b) 10
c) 12
d) Error
11. Given a=5, b=6
What is the value of c, if C=a++ + ++b;
a) 11
b) 12
c) 10
d) 13
12. What is the output of the following statements ?
int n=64;
printf(“%04d”,n);
a) 64
b) 6400
c) 0064
d) none of the above
13. What will be the values of a, b and c printed out to the display from the following statements ?
scanf(“%2d %*d %d”,&a,&b,&c);
printf(“a=%d,b=%d,c=%d”,a,b,c);
when the values entered are 1004, 25, 64.
a) a=10, b=25, c= garbage value
b) a=10, b=4, c=25
c) a=1004, b=25, c=64
d) a=1004, b=64, c=garbage value
14. What is the output of the following program ?
int a=5;
printf(“%d, %d, %d”,a++,a,++a);
a) 5,6,7
b) 5,5,5
c) 6,6,6
d) 7,6,6
15. What would be the output of the following program?
main()
{
int i=32,j=0x20,k,l,m;
k=i j;
l=i & j;
m=k^l;
printf(“%d %d %d %d %d”,i,j,k,l,m);
}
a) 32 32 32 32 0
b) 0 0 0 0 0
c) 0 32 32 32 32
d) 32 32 32 32 32

Operating -System Structures

Refer Operating-System Structures Chapter-3 Below Link

http://raghuu1234.googlepages.com/operating-systemstructures

Operating System- Computer System Structures

Refer Operating System-Computer System Structures Chapter-2

http://raghuu1234.googlepages.com/operatingsystem-computersystemstructures2

Operating System Introduction Chapter-1

Refer Operating System Introduction Chapter-1 Below link :-

http://raghuu1234.googlepages.com/operatingsystem

Volaile Keyword in C Language

Refer Volatile keyword or Variable in C Langauge :-

http://raghuu1234.googlepages.com/volatilekeywordinclanguage

Bit Opearations in C Langauge

Refer Bit Operations Document in C Lanaguge
http://raghuu1234.googlepages.com/bitoperationsinclanguage

 
# #