C Programming and Problem Solving Questions and Answers 151 to 160

C Programming and Problem Solving

Questions 151 to 160



151.
Read the following statements and choose the most appropriate one.
I.     An algorithm is an actual program that can be executed on a machine directly.
II.     An algorithm is a set of rules for carrying the calculations either by hand or on a machine.
III.    An algorithm can be a program dependent and machine dependent code to define a solution for a given problem.
IV.   An algorithm is an abstraction of a program to be executed on a machine.
(a)  Both (I) and (IV) above                     (b)  Both (I) and (III) above  
(c)  Both (II) and (IV) above                     (d)  Both (I) and (II) above                      
(e)  (II), (III) and (IV) above.
152.
Let A, B are the integer variables, and P, Q are the integer pointers, then which of the following are valid in a C program?
(a)  P = P*A;           (b)  P = A/P;            (c)  P = P/Q;           
(d)  P = P*Q;           (e)  P = P+A;.
153.
Read the following statements carefully, and pick the correct answer.
I.     An external variable declaration must begin with storage class specifier extern.
II.     Scope of an automatic variable can be smaller than the entire function.
III.    An automatic variable keeps its final value even after its function is terminated.

(a)  (I), (II) are TRUE and (III) is False       (b)  (I), (III) are TRUE and (II) is False
(c)  (II), (III) are TRUE and (I) is False       (d)  All are TRUE                                
(e)  All are FALSE.
154.
Pick the equivalent control structure for the following repetitive control structure
For ( E1; E3; E2 ) { statements; }
(a)  while( E1) { E2; statements; E3; }     
(b)  E1; while(E2) { statements; E3; }      
(c)  while( E3) { E1; statements; E2; }     
(d)  E1; while(E3) { statements; E2; }      
(e)  E1; E3; while(E2) { statements; }.
155.
For the initial values as a=3, b=40, c=13, d=10, what is the final value of a,
a = b^c|d&a;
(a)  Syntax error      (b)  39                     (c)  3                       (d)  35                     (e)  31.
156.
What is the output for the following statement, if the initial value of x is 20
       Printf (“%d,%d,%d,%d,%d”, x++, x--, --x, ++x, x--);
(a)  20,21,19,20,20  (b)  20,21,19,20,21                 (c)  18,19,19,20,20                  
(d)  19,19,19,20,20   (e)  19,18,19,20,19.
157.
What is the correct output of the following program?
       # include <string.h>
       void main()
       {
              char str[] = ”ICFAI UNIVERSITY”, rev[17];
              int i = strlen(str), j=0;
              for( ; i>=0; rev[j++] = str[i--]);
              rev[j] = ‘\0’;
              puts(rev);
       }
(a)  ICFAI                 (b)  Syntax error                                     (c)  YTISREVINU IAFCI                       
(d)  nothing             (e)  YTISREVINU.
158.
Choose the correct output for the following program
# include <stdio.h>
void main()
{
       int a=10, b=11, c=13,   d;
       d = (a=c, b+=a, c=a+b+c);
       printf(“ %d”,  d);
}
(a)  50                    (b)  34            (c)  garbage value            (d)  R value required                        (e)  Error.
159.
Consider the following code.    
       # include <stdio.h>
       void main()
       {
              int a[5] = {6,8,3,9,0}, i=0;
              if( i != 0)
              {
                     break;
                     printf(“ %d”, a[i]);
              }
              else
                     printf(“%d”, a[i++]);
       }
       What is the output of the above program?
(a)  6                       (b)  8                       (c)  Runtime error      (d)  no output         (e)  Syntax error.
160.
While declaring an array in C as : datatype arrayname[size]; the size can be
(a)  Any constant                                   (b)  An integer constant                      (c)  Any variable     
(d)  Any initialized integer variable           (e)  Both (b) and (d) above.

Answers

151.
Answer : (c)
Reason:  Because according to the definition of the algorithms it is a set of rules for carrying the calculations either by hand or on a machine, and it is an abstraction of a program to be executed on a machine. It is not a program to execute directly on a computer, moreover it should be program independent, machine independent and must define a generic solution for a given problem.
152.
Answer : (e)
Reason:  According to integer arithmetic rules for pointers all the operations P = P*A; P = A/P; P = P/Q; P = P*Q; are invalid where as the expression P = P+A; is purely valid.
153.
Answer : (a)
Reason:  Because the first two statements are perfectly valid as per the definitions of the external, automatic variables, and the statement III is wrong as an automatic variable can’t keeps or retains its final value even after its function is terminated.
154.
Answer : (d)
Reason:  Because the equivalent indefinite repetitive control structure (while) for the definite repetitive control structure (for) is option D, others are wrong
              for(initialization (E1); condition (E3); increment loop invariant (E2)) {statements;}
is same as
initialization( E1);
while(condition(E3))
{
statements;
increment loop invariant (E2 )
}
155.
Answer : (b)
Reason:  Because of the precedence of operators. The operator ^ (XOR) is having the high precedence when compared with other two operators, followed by & (AND) and | (OR) operators
156.
Answer : (c)
Reason:  Because the expressions in the predefined formatted output function
Printf (“%d,%d,%d,%d,%d”, x++, x--, --x, ++x, x--); are executed from right to left because of the stack involvement in arguments [assign to the called function (Known as C convention)]
157.
Answer : (d)
Reason:  Because the statement int i = strlen (str); stores the value 17 in i which is null ‘\0’ character . and hence when we run the for loop
For (; i>=0; rev[j++] = str[i--]);
       rev [j] = ‘\0’;
it first stores str [i] (nothing but null ‘\0’ character) in rev [j]. so once we print the output statement puts (rev); terminates because of null value a 0th location of string rev.
158.
Answer : (a)
Reason:  Because the initial values of variables is int a=10, b=11, c=13. then first a=c is evaluated and hence a = 13 now. Then b += a (b = b + a) sets b to 24.
then c=a + b + c is c=13+24+13 = 50 which is assigned to d. or other wise always the right most of the comma separated expression is itself the answer that can be assigned to variable at left hand side.
159.
Answer : (e)
Reason:  Syntax error because break can’t be used with selection control structures, but can be used with repetitive, or select control structure.
if( i != 0)
       {                    error
              break;
               …
       }
160.
Answer : (b)
Reason:  Because the size at the declaration can’t be Any constant (what if it is 34.67 float constant ???), can’t be Any variable (what if it is some var where var is float variable ???), and Any initialized integer variable is also not allowed.


<< Prev 1...  2...  3...  4...  5...  6...  7...  8...  9...  10...  11... 12...  13...  14...  15...  16...  17...  18...  19...  20...  21...  22...  23...  24...  25...  26...  27...  28...  29...  30...  31...  32...  33...  34...  35...  36...  Next >>


1 comment :