0% found this document useful (0 votes)
12 views

CDS S13

The document provides solutions to 18 questions related to data structures, algorithms, and C programming. Some key points addressed include: - The time complexity of searching a linked list is O(n) - Macro definitions can reduce execution time when used in loops or where a function is called in many places - Constant expressions in C are evaluated at compile time

Uploaded by

Jaydeep S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CDS S13

The document provides solutions to 18 questions related to data structures, algorithms, and C programming. Some key points addressed include: - The time complexity of searching a linked list is O(n) - Macro definitions can reduce execution time when used in loops or where a function is called in many places - Constant expressions in C are evaluated at compile time

Uploaded by

Jaydeep S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DATA STRUCTURE & ALGORITHM

C- PROGRAMMING LANGUAGE

SOLUTIONS

1. The following program

main ( )
{
int i = 5;
if (i = = 5) return;
else printf “i is not five”);
printf (“over”);
}

results in

(a) a syntax error (b) an execution error


(c) printing of over (d) printing anything

Solution: Option (d)

2. The following statements

for (i = 3; i < 15; i +=3)


{
printf (“%d”, i);
++i;
}

will result in the printing of

(a) 36912 (b) 3691215


(c) 3711 (d) 371115

Solution: Option (c)

3. Consider the following flow chart


1
Which of the following does not correctly implements the above flow chart?

(a) if (a>b) (b) if (a<=b)


if (b>c) if (b>c)
a=1 a=1
else if (c>d) else if (c<=d)
b=2 b=2

(c) if (a>b) (d) if (a <=b)


; ;
else if (b>c) else if (b>c)
a=1 a = 1;
else if (c<=d) else if (c>d)
b=2 ;
else b = 2

Solution: Option (a)

4. Consider the following program

main ( )
{
int x = 2, y = 2;
if (x<y) return (x = x+y);
else printf (“z1”);
printf (“z1”);
printf (“z2”);
}

Choose the correct statements

(a) The output is z2 (b) The output is z1z2


2
(c) This will result in compilation error (d) None of the above

Solution: Option (b)

5. Choose the False statements:

(a) The scope of a macro definition need not be the entire program
(b) The scope of a macro definition extends from the point of definition to the end of the file
(c) A macro definition may go beyond a line
(d) None of the above

Solution: Option (d)

6. Consider the following program fragment

if (a>b)
printf (“a>b”)
else
printf (“else part”);
printf (“a<=b”);
then a <= b

will be printed if

(a) a > b (b) a < b


(c) a = = b (d) All of the above

Solution: Option (d)

7. Consider the two declarations

void *voidPtr;
char *charPtr;

Which of the following assignments are syntactically Correct?

(a) charPtr = voidPtr (b) voidPtr = charPtr


(c) *charPtr = voidPtr (d) *voidPtr = *charPtr

3
Solution: Option (b)

8. The output of the following program is

main ( )
{
static int x[ ] = {1, 2, 3, 4, 5, 6, 7, 8};
inti ;
for (i = 2; i<6; ++i)
x [x[i]] = x [i];
for (i = 0; i<8; ++i)
printf (“%d”, x [i]);
}

(a) 12335578 (b) 12345678


(c) 12354678 (d) 87654321

Solution: Option (a)

9. The following program

main ( )
{
static char [3] [4] = {“abcd”, “mnop”, “fghi”};
putchar (**a);
}

(a) will not compile successfully (b) results in run-time error


(c) prints garbage (d) none of these

Solution: Option (d)

10. The following program

#include<stdio.h>

4
main ( )
{
int abc ( );
abc ( );
(*abc) ( );
}

int abc ( )
{ printf (“come”);}

(a) results in a compilation error (b) prints come come


(c) results in a run-time error (d) prints come

Solution: Option (b)

11. The time required to search an element in a linked list of length n is

(a) O(log2 n) (b) O(1)


(c) O(n) (d) O(n2)

Solution: Option (c)

12. Consider the declaration

char x [ ] = “SUCCESS”;
char *y = “SUCCESS”;

Pick the correct answers.

(a) The output of puts (x) and puts (y) will be different
(b) The output of puts (x) and puts (y) will be same
(c) The output of puts (y) is implementation dependent
(d) None of the above comments are true

Solution: Option (b)

13. Use of macro instead of function is recommended.

(a) when one wants to reduce the execution time


5
(b) when there is a loop with a function call inside
(c) when a function is called in many places in a program
(d) In (a) and (b) above

Solution: Option (d)

14. For loop in a C-program, if the condition is missing

(a) it is assumed to be present and taken to be false


(b) it is assumed to be present and taken to be true
(c) it result in a syntax error
(d) execution will be terminated abruptly

Solution: Option (b)

15. Consider the following statements.

#define hypotenuse (a, b) sqrt (a* a+b *b);


The macro-call hypotenuse (a+2, b+3);

(a) Finds the hypotenuse of a triangle with sides a+2 and b+3
(b) Finds the square root of (a+2)2 + (b+3)2
(c) Finds the square root of 3*a + 4*b + 5
(d) Is invalid

Solution: Option ( c )

Explanation:

After macro expansion it becomes sqrt(a+2*a+2+a+3*b+3) = sqrt(a+2a+2+3b+3)=


sqrt(3a+4b+5)

16. For ‘C’ programming language

(a) constant expressions are evaluated at compile time


(b) size of array should be known at compile time
(c) strings constants can be concatenated at compile time
(d) all of these

6
Solution: Option (d)

17. Consider the declarations:

char first (int(*) (char, float));


int second (char, float);

Which of the following function invocation is valid?

(a) first (*second) (b) first (&second);


(c) first (second) (d) none of these

Solution: Option (c)

18. The output of the following program

main ( )
{
int a = 1, b = 2, c = 3;
printf (“%d”, a+ = (a+ = 3, 5, a));
}

will be

(a) 12 (b) 6
(c) 9 (d) 8

Solution: Option (d)

You might also like