Monday, October 05, 2020

C programming and Data structure

 Q.1 A global variable is a variable
(A) declared in the main ( ) function.
(B) declared in any function other than the main ( ) function.
(C) declared outside the body of every function.
(D) declared any where in the C program.
Ans: C
A global variable is declared outside the body of every function.

Q.2 main ( ) is an example of
(A) library function
(B) user defined function
(C) header
(D) statement
Ans: A
main() is a special function used by C system to tell the computer where the program starts.

Q.3 While incrementing a pointer, its value gets increased by the length of the data type to which it points. This length is called
(A) scale factor
(B) length factor
(C) pointer factor
(D) increment factor
Ans: D
While incrementing a pointer, its value gets increased by the length of the data type to which it points.

Q.4 The first digit of a decimal constant must be
(A) a zero
(B) a non zero number
(C) a negative number
(D) an integer
Ans: D
Decimal constants consist of a set of digit, 0 to 9, preceded by an optional – or + sign.

Q.5 What is the output of the following statement:
printf (“%-3d”,12345);
(A) 1 2 3
(B) -1 2 3
(C) 1 2 3 4 5
(D) 12
Ans: C
printf statement would print 12345.

Q.6 A single character input from the keyboard can be obtained by using the function.
(A) printf ( )
(B) scanf ( )
(C) putchar ( )
(D) getchar ( )
Ans: D
Reading a single character can be done by using the function getchar( ).

Q.7 The function ftell ( )
(A) reads a character from a file
(B) reads an integer from a file
(C) gives the current position in the file
(D) sets the position to the beginning of the file.
Ans: C
ftell( ) takes a file pointer and returns a number of type long, that corresponds to the current position.

Q.8 If the variables i, j and k are assigned the values 5,3 and 2 respectively, then the expression i = j + ( k + + = 6 ) + 7
(A) gives an error message
(B) assigns a value 16 to i
(C) assigns a value 18 to i
(D) assigns a value 19 to i
Ans: A
It gives an error message-Lvalue required.

Q.9 If an integer needs two bytes of storage, then the maximum value of a signed integer is
(A) 216-1
(B) 215-1
(C) 216
(D) 215
Ans: B
If we use a 16 bit word length, the size of the integer value is limited to the range -215 to 215-1

Q.10 Literal means
(A) a string
(B) a string constant
(C) a character
(D) an alphabet
Ans: B
Literal means a string constant.

Q.11 If ‘y’ is of integer type then the expressions
3* (y − 8)/9 and (y − 8)/9 * 3
(A) must yield the same value.
(B) must yield different values.
(C) may or may not yield the same value.
(D) none of the above.
Ans: C
The expression may or may not yield the same value.

Q.12 In the following code fragment
int x, y = 2, z, a;
x=(y*=2) + (z=a=y);
printf (‘%d’,x);
(A) prints 8
(B) prints 6
(C) prints 6 or 8 depending on the compiler
(D) is syntactically wrong
Ans: A
It will print 8 because x=(y*=2)+(z=a=y)=4+4=8.

Q.13 A possible output of the following program fragment is
for (i=getchar();; i=get.char())
if (i==‘x’) break;
else putchar(i);
(A) mi
(B) mix
(C) mixx
(D) none of the above
Ans: D
None of the above as it is wrong syntax.

Q.14 In a for loop, if the condition is missing, then,
(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 results in a syntax error.
(D) Execution will be terminated abruptly.
Ans: B

Q.15 If storage class is missing in the array definition, by default it will be taken to be
(A) automatic
(B) external
(C) static
(D) either automatic or external depending on the place of occurrence.
Ans: A
A variable declared inside inside a function without storage class specification is, by default, an automatic variable.

Q.16 The maximum number of dimensions an array can have in C is
(A) 3
(B) 4
(C) 5
(D) compiler dependent
Ans: D
C allows arrays of three or more dimensions. The exact limit is determined by the compiler.

Q.17 puts(argv[0]);
(A) prints the name of the source code file.
(B) prints argv.
(C) prints the number of command line arguments.
(D) prints the name of the executable code file.
Ans: D
argv[0] represent the filename where the executable code of the program is stored.

Q.18 printf(“%–10s”, “ABDUL”); displays
(A) ABDULbbbbb
(B) bbbbbABDUL
(C) ABDULbbbbbbbbbb
(D) bbbbbbbbbbABDUL
Ans: A
-10s will print ABDUL in 10 space ABDUL followed by 5 blank space.

Q.19 Which amongst the following expression uses bitwise operator?
(A) a++
(B) !a>5
(C) a|b
(D) a!=b
Ans: C
| is bitwise OR.

Q.20 The output of the following program is
main( )
{ float y;
y=198.7361;
printf(“%7.2f”, y);
}
(A) 1 9 8 . 7 3 6
(B) 1 9 8 . 7 3
(C) 1 9 8 . 7 4
(D) 1 9 8 . 7 4
Ans: C
The printf statement is giving formatted output till two places of decimal.

Saturday, October 03, 2020

C Programming and Data Structure

 Q.1 What is the output of the following program?
main ( )
{ int x = 2, y = 5;
if (x < y) return (x = x+y); else printf (“z1”);
printf(“z2”);
}
(A) z2
(B) z1z2
(C) Compilation error
(D) None of these
Ans: D
There is no compilation error but there will no output because function is returning a value and if statement is true in this case.

Q.2 Choose the correct one
(A) Address operator can not be applied to register variables
(B) Address operator can be applied to register variables
(C) Use of register declaration will increase the execution time
(D) None of the above
Ans: D
A register access is much faster than a memory access, keeping the frequently accessed variables in the register will lead to faster execution of programs.

Q.3 What is the following program doing?
main ()
{ int d = 1;
do
printf(“%d\n”, d++);
while (d < = 9);}
(A) Adding 9 integers
(B) Adding integers from 1 to 9
(C) Displaying integers from 1 to 9
(D) None of these
Ans: C
d starting from 1 is incrementing one by one till d=9 so the printf statement is printing numbers from 1 to 9.

Q.4 What is the output of the following program?
main ( )
{ extern int x;
x = 20;
printf(“\n%d”, x);
}
(A) 0
(B) 20
(C) error
(D) garbage value
Ans: C
Output of the given program will be “Linker error-undefined symbol x”. External variables are declared outside a function.

Q.5 If x is one dimensional array, then pick up the correct answer
(A) *(x + i) is same as &x[i]
(B) *&x[i] is same as x + i
(C) *(x + i) is same as x[i] +1
(D) *(x + i) is same as *x[i]
Ans: A
num[i] is same as *(num+i)

Q.6 Consider the following declaration
int a, *b = &a, **c = &b;
The following program fragment
a = 4;
**c = 5;
(A) does not change the value of a
(B) assigns address of c to a
(C) assigns the value of b to a
(D) assigns 5 to a
Ans: D
The given statements assigns 5 to a

Q.7 Choose the correct answer
(A) enum variable can not be assigned new values
(B) enum variable can be compared
(C) enumeration feature increase the power of C
(D) None of the above
Ans: C
The enumerated data types give an opportunity to invent our own data typeand define what value the variable of this data type can take.

Q.8 The content of file will be lost if it is opened in
(A) w mode
(B) w+ mode
(C) a mode
(D) a+ mode
Ans: A
When the mode is writing, the contents are deleted and the file is opened as a new file.

Q.9 Consider the following code segment:
int a[10], *p1, *p2;
p1 = &a[4];
p2 = &a[6];
Which of the following statements is incorrect w.r.t. pointers?
(A) p1 + 2
(B) p2 – 2
(C) p2 + p1
(D) p2 – p1
Ans: C
Addition of two pointers is not allowed.

Q.10 The second expression (j – k) in the following expression will be evaluated
(i + 5) && (j – k)
(A) if expression (i + 5) is true.
(B) if expression (i + 5) is false.
(C) irrespective of whether (i + 5) is true or false.
(D) will not be evaluated in any case.
Ans: A
In a compound logical expression combined with &&, the second expression is evaluated only if first is evaluated in true.

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;
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
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

Search Aptipedia