You are on page 1of 3

The following C declarations

struct node
{
int i;
float j;
};
struct node *s[10] ;
define s to be
(A) An array, each element of which is a pointer to a structure of type node
(B) A structure of 2 fields, each field being a pointer to an array of 10 elements
(C) A structure of 3 fields: an integer, a float, and an array of 10 elements
(D) An array, each element of which is a structure of type node.

Answer: (A)

Explanation:
// The following code declares a structure
struct node
{
int i;
float j;
};

// The following code declares and defines an array s[] each


// element of which is a pointer to a structure of type node
struct node *s[10] ;
Consider the following C declaration
struct {
short s [5]
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes,
respectively. The memory requirement for variable t, ignoring alignment
considerations, is (GATE CS 2000)
(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes
Answer: (c)
Explanation: Short array s[5] will take 10 bytes as size of short is 2 bytes. Since u is a union, memory
allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
The value of j at the end of the execution of the following C program. (GATE CS 2000)
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main ()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
(a) 10
(b) 4
(c) 6
(d) 7
Answer (a)
Eplaination: count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in
first call. Other calls to this function will take the old values of count.
Count will become 0 after the call incr(0)
Count will become 1 after the call incr(1)
Count will become 3 after the call incr(2)
Count will become 6 after the call incr(3)
Count will become 10 after the call incr(4)
Consider the following pseudo-code
x:=1;
i:=1;
while (x <= 1000)
begin
x:=2^x;
i:=i+1;
end;
What is the value of i at the end of the pseudo-code?
(A) 4
(B) 5
(C) 6
(D) 7

Answer: (B)

Explanation: Initialisation: x = 1, i = 1;
Loop: x i
21 2
22 3
24 4
216 5
After this condition becomes false.
So, i = 5, Option (B) is correct.
Study the following program:
//precondition: x>=0
public void demo(int x)
{
System.out.print(x % 10);
if (x % 10 != 0)
{
demo(x/10);
}
System.out.print(x%10);
}
Which of the following is printed as a result of the call demo(1234)?
(A) 1441
(B) 3443
(C) 12344321
(D) 43211234

Answer: (D)

Explanation: In the above code, first print statement is executed and prints the value obtained after
performing modulus of 10 and the recursively another function is called with the value divide by 10. And
after the return of the function, it prints the values again.
demo(1234)
prints 4 call demo(123)
prints 3 call demo(12)
prints 2 call demo(1)
prints 1 call demo (0)
prints 1 prints 2 prints 3 prints 4.
So, option (D) is correct.

You might also like