Slideshow

Monday, 25 February 2013


Q.11 In the for statement: for(exp1; exp2; exp3){ … }
where exp1, exp2 and exp3 are expressions. What is optional?
(A) None of the expressions is optional.
(B) Only exp1 is optional.
(C) Only exp1 and exp3 are optional.
(D) All the expressions are optional.


Ans: D
All the expressions are optional. For (;;) is a valid statement in C.
Q.12 The output of the following code segment will be
char x = ‘B’;
switch (x) {
case ‘A’: printf(“a”);
case ‘B’: printf(“b”);
case ‘C’: printf(“c”);
}
(A) B (B) b
(C) BC (D) bc
Ans: D
Since there is no break statement, all the statement after case’B’ are executed.
Q.13 What will be the output of the following code segment?
main( ) {
char s[10];
strcpy(s, “abc”);
printf(“%d %d”, strlen(s), sizeof(s));
}
(A) 3 10 (B) 3 3
(C) 10 3 (D) 10 10
Ans: A
strlen(s) give the length of the string, that is 3 and sizeof(s) give the size of array s
that is 10.
Q.14 Which of the following is the odd one out?
(A) j = j + 1; (B) j =+ 1;
(C) j++; (D) j += 1;
AC05 Programming & Problem Solving Through ‘C’
AC06 Data Structures
4
Ans: B
j=+1 is odd one out as rest all means incrementing the value of variable by 1.
Q.15 Which of the following is true for the statement:
NurseryLand.Nursery.Students = 10;
(A) The structure Students is nested within the structure Nursery.
(B) The structure NurseryLand is nested within the structure Nursery.
(C) The structure Nursery is nested within the structure NurseryLand.
(D) The structure Nursery is nested within the structure Students.
Ans: C
The structure Nursery is nested within the structure NurseryLand.
Q.16 What will be the output of the following code segment, if any?
myfunc ( struct test t) {
strcpy(t.s, “world”);
}
main( ) {
struct test { char s[10]; } t;
strcpy(t.s, “Hello”);
printf(“%s”, t.s);
myfunc(t);
printf(“%s”, t.s);
}
(A) Hello Hello (B) world world
(C) Hello world (D) the program will not compile
Ans: D
The program will not compile because undefined symbol s for myfunc( ) function.
Structure should be defined before the main and the function where it is called.
Q.17 If a function is declared as void fn(int *p), then which of the following statements is
valid to call function fn?
(A) fn(x) where x is defined as int x;
(B) fn(x) where x is defined as int *x;
(C) fn(&x) where x is defined as int *x;
(D) fn(*x) where x is defined as int *x;
Ans: B
Function void fn(int *p) needs pointer to int as argument. When x is defined as int
*x, then x is pointer to integer and not *x.
Q.18 What is the following function computing? Assume a and b are positive integers.
int fn( int a, int b) {
if (b == 0)
return b;
else
return (a * fn(a, b - 1));
}
(A) Output will be 0 always (B) Output will always be b
(C) Computing ab (D) Computing a + b
AC05 Programming & Problem Solving Through ‘C’
AC06 Data Structures
5
Ans: A
The output is always be 0 because b is decremented in recursive function fn each time
by 1 till the terminating condition b==0 where it will return 0.
Q.19 What is the output of the following C program?
# include <stdio.h>
main ( )
{
int a, b=0;
static int c [10]={1,2,3,4,5,6,7,8,9,0};
for (a=0; a<10;+ + a)
if ((c[a]%2)= = 0) b+ = c [a];
printf (“%d”, b);
}
(A) 20 (B) 25
(C) 45 (D) 90
Ans: A
printf statement will print b which is sum of the those values from array c which get
divided by 2, that is 2+4+6+8=20.
Q.20 If a, b and c are integer variables with the values a=8, b=3 and c=-5. Then what is the
value of the arithmetic expression:
2 * b + 3 * (a-c)
(A) 45 (B) 6
(C) -16 (D) -1
Ans: A
the value of the arithmetic expression is 45 as 2*3+3*(8—5)=6+3*13=6+39=45

No comments:

Post a Comment