0 ratings0% found this document useful (0 votes) 126 views36 pagesBook Chapter 3 Programming With C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
aL
‘The minimum number of temporary variables needed to swap the contents of two vari-
ablesis
@ 1 (b) 2 3 @o
. The purpose of the following program fragment
bos +b,
s=b- 8;
b=b
where 6, ate two integers isto
(a) transfer the contents of s to b
(©) transfer the contents of b to s
(©) exchange (swap) the contents of s and bo
(@) negate the contents of = and b
|. Consider the function
find(int x, int y)
freturn(( x < y) ? 0: (x - y)ie
'b be two non-negative integers. The call find (a, find(a, b)) can be used
to find the
(a) maximum of a, > (b) positive difference of a,b
(©) sumofia, b (@) minimum of a,b
J. Let a, b be two non-negative integers. Which of the following calls finds the positive
difference of 2 and b?
@) find(a,b) + find (b, a) @) find (a, find (a,b) )
(c) a + find(a,b) (d) b + find(a,b)
. If an integer needs two bytes of storage then maximum value of an unsigned integer is
(a) 2-1 (b) 28-1 © 2 w 28Programming with ¢ 109
%6, Ifan integer nceds two bytes of storage then maximum value of a signed integer is
(@) 2-1 (b) 28-1 © 2° @ 2°
¥7. printf("$a”, printé(“tim"));
(@) results in a syntax error (b) outputs cin
(©) outputs garbage (@) prints tim and terminates abruptly
*8, If abs is the input then the following program fragment results in
char x, ¥, 7
printf ("Sa", scanf(*Sete%e”, 6x, Gy, 62)); results in
(a) a syntax error (©) a fatal error
(©) segmentation violation (@) printing of 3
¥9, Consider the statements:
putehar (getehar ())7
putchaz (getchar(})7
i
»
is the input, the output will be
(a) an error message () this can’t be the input
(©) ab ab
10, Let a, be two positive integers. Which of the following options correctly relates /
and 3?
(@) b = (a/b)*b + ab (b) a = (a/b)*» + ate
(©) b = (a%b)*b + a/b (@ a = (a%b)*b + a/b
11. Consider the following program fragment:
char ¢ = talz
while (ctt <= *2")
putehar (xxx);
If the required output is abcde fghijklmnoparstuvwxyz then xxx should be
@e (b) c#+ © cl (@ -c
*12, 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
(©) may or may not yield the same value (4) none of the above
13. If y is of integer type then the expressions
3* (y - 8) / 9 and (y - 8) / 9% 3
yield the same value if
(a) y is an even number (b) y is an odd number
(©) y = 8isan integral multiple of 9 (4) y = 8 is an integral multiple of 310 McQ in Computer Science
14, The statement
if(myPtr {= NULL)
myPtr = NULL 5
else
emyPtr = NULL 5
hhas the same effect as the statement(s)
(@) if(myPtr) *myPtr = NOLL ;
else *myPtr = NULL ;
(b) *myPtr = NOLL;
(© i£(imyptr) *myPtr = NULL ;
else *myPtr = NULL
(@) if (myPtr == NULL) *myPtr = NULL ;
else *myPtr = NULL
+15. The following code fragment
ant %, y= 2% a
x (yt 2) + (ets ys
printt ("Sd") x);
(a) prints 8
(©) prints 6
(©) prints 6 or 8 depending on the compiler implementation
(@) is syntactically wrong
*16. If n has the value 3 then the output of the statement print£ ("Sd $a”, né+,
ten):
(a (b) isa 5
4 (4) is implementation dependent
v7. y + 14 does the same as
xo ytd () x= -x-y-d
@xs-xty el @x=x-y-2
*18, The expression 5 - 2 - 3 * 5 ~ 2 will evaluate to 18, if
(a) — is left associative and * has precedence over —
(b) ~ is right associative and * has precedence over ~
(©) ~ is right associative and - has precedence over *
(@) ~is left associative and — has precedence over *
19, peint£("Sc", 100);
(a) prints 100 (b) prints the ASCII equivalent of 100
(©) prints garbage (a) none of the above
*20, The program fragment
int i = 263;
putchar (i);(@) prints 263
(©) rings the bell
+21. The following statement
printé("$£%, 9/5);
prints
(@ 1.8
(© 2.0
22. The statement
printf (™sf",
prints
(@) 1.8
(©) 2.0
+23. The following program fragment
unsigned i
int j = -4;
printe(Su", i + 3)
prints
(a) garbage
os
(Moat) 9/5);
Programming with ¢ oo
() prints the ASCH equivalent of 263
(@) prints garbage
(b) 1.0
(@) none of the above
(b) 1.0
(d) none of the above
(©) an integer that changes from machine to machine
(@) none of the above
*24, Ifthe following program fragment (assume negative numbers ate stored in 2's comple-
ment form)
unsigned i =
int j= "47
printg("su", i + 3)s
prints x then printf ("sa”,
8* sizeof (int));
‘outputs an integer that is same as (og in the answers are to the base two)
(a) an unpredictable value
(©) Log (x43)
*25. The following program fragment
for(i = 3; i < 15;
printf ("8d") ids
results in
(a) a syntax error
(©) printing of 12
*26, The following program fragment
for (i = 1; i < 5; +44)
if i
else printf("sd “, is
3) continue;
(b) 8 * Log (x+3)
(@) none of the above
it 3);
(b) an execution error
(@) printing of 1527.
28.
+29,
30,
ai.
32,
McQ in Computer Science
results in the printing of
@1245 W124
245 (4) none of the above
‘The following program fragment
if (a = 0)
printé(*a is zero”);
else
print£(*a is not zero”);
results in the printing of
(@) 2 is zero (b) a is not zero
(©) nothing (@) garbage
‘The following program fragment
ifla = 7)
printé("a is seven");
else
print£(*a is not seven”);
results in the printing of
(a) a is seven (b) a is not seven
(©) nothing (@) garbage
‘The following program fragment
int k= 77;
printé ("8a") 0 < Ik);
(a) prints 0 (b) prints a non-zero value
(©) is illegal (A) prints an unpredictable value
The following loop
for(putchar(‘c’); putchar(‘a’); putchar(‘r’)}
putehar(*t!);
outputs
(@) a syntax error () cartet
(© catrat (@) catratratratrat...
The following loop
for(i = 1, 3 = 10; i < 6; #44, --3)
printf("sd $d", i, 3):
prints
(@) 11029384756 (b) 12345109876
(1111199999 (4) none of the above
‘The following program fragment
int a= 4, b= 6
printt("$a", aProgramming with ¢ 13
(a) outputs an error message () prints 0
(©) prints 2 (8) none of the above
33. The following program fragment
int a= 4, b= 6;
printf("se", a != b)s
(a) outputs an error message (b) prinss 0
(©) prints 1 (@) none of the above
34, The following program fragment
int a= 4, b= 6;
print£("8a", a = bis
(a) outputs an error message (b) prints 0
(©) prints 2 (A) none of the above
38. A possible output of the following program fragment
for(i = getchar();; i = getchar())
if(i == 'x") break;
else putchar (i);
is
(@) mi (b) mix
(©) mixx (d) none of the above
36. The following program
main()
{
int i = 5;
if (== 5) return;
else printf(*i is not five");
prints ("over");
)
results in
(a) a syntax error
(b) an execution error
(©) printing of over
(@) execution termination, without printing anything
‘The following program fragment
int i= 5;
do {putchar(i + 100); printf(™sa”, i
while (i):
results in the printing of
(a) isn4g3£2e1 (b) ign3g2teo
(©) an error message (4) none of the above
#37.14 McQ in Computer Science
#38, The following program fragment
int i= 107, x = 5;
print£((x > 7)? “ad”: “Bem, ids
results in
(a) an execution error (b) a syntax error
(©) printing of & (A) none of the above
+39, Replacing > by < in the previous question results in
(a) printing of 207 (b) a syntax error
(©) printing of (@) none of the above
+40, ‘The following loop
while (print£("4d", print£(*az”)))
printé ("by");
(a) prints azbybybyby.. . (b) prints azbyazbyazbyazby...
(©) results in a syntax error (A) none of the above
41. The following statements
for(i = 3; 4 < 15; 4 4-3)
{ printf(msd “, ide
)
will result in the printing of
(a) 36912 3691215 (3711 @ 371115
42, Ifa=9,b=5 and ¢=3, then the expression (a - a/b * b%c) > aSbic evalu-
atesto
(a) true (b) false (6) invalid wo
43. Consider the following program fragment
if (a > b)
if (b > c)
sl:
else 52;
2 will be executed if
@ a () bee
(©) bbandb<=e
‘*44, The following program fragment
if (2 <1)
else
x = (2 < 0)? printf (Yone”) : printé(*four");
printf ("Sd") x);
(a) prints nothing, (b) results in a syntax error
(©) prints fourd (@) none of the aboveProgramming with ¢ 45
*45, Consider the following program fragment
if (a > b)
printf ("a > b");
else
printf ("else part”);
printé ("a <= bY’);
a <= b will be printed if
(a) a>d (by a
Fig. 3
Which of the following correctly implements the above flow chart?
(@ if (a > b) ) if (a <= »)
if (b> oe) if (b> co}
: asl;
fc > a else if (oc <= @
b= 2;
© b) @ if (a > b)
(b>) (b>)
or) (>a
+*47. The body of the following fox loop
for (putchar (‘a’); putchar(0); putchar(‘c’))
putchar(%b’) ;
will be executed
(@) O times
(b) I time
(©) infinitely many times
(@ will not be executed because of syntax error16 McQ in Computer Science
48, The following statement
if (a > b)
if te > b)
printé ("one");
else
if (co == a) printé(*two");
else printf ("three");
else printf(*four");
(@) results in a syntax error (©) prinss four ife
(c) prints two ife <=b (d) prints four ifa
49. ‘The statement in the previous question can never print
(a) one (b) two (c) three (d) four
50, The following program fragment
int x= 4, y= ox ar
for (i = 1; i < 4 444)
‘outputs an integer that is same as
@ety (bby * (+ 2 + 3 + 4)
@y*4 @yty
51. Consider the declaration
static char hello[]="hello";
‘The output of printf (™¥s\n", hello);
will be the same as that of
(a) puts (“hello”); (b) puts (hello);
(© printf (s\n, “hello"); (d) puts (*helio\n”
52. The following program fragment
int x(5)[5], 4, 47
for(i = 0; i < 5; #4)
fort} = 0; 9 <5 7 9H)
xLilg] = x3] (le
(a) transposes the given matrix x (b) makes the given matrix x, symmetric
(©) doesn’t alter the matrix x (@) none of the above
53. Consider the following type definition.
typedef char x[10];
x myArray(S];
What will sizeof(myArray) be? (Assume one character occupies I byte)
(a) 15 bytes (b) 10 bytes (©) 50 bytes (a) 30 bytesProgramming with ¢ 17
#54, The following program
main( )
(
static int al] = 17,89};
print("sa”, 2[a] + al2])+
»
(a) results in bus error (b) results in segmentation violation error
(©) will not compile successfully (2) none of the above
55. The following program fragment
int mn, b= m= an
char wer[80
sprintf (wer, “Sd¥dsd", m, ny b)F
puts (wer) ;
(@ prints the string 8 8 @ (b) prints the mull string
(©) prints the string 888 (@ none of the above
+56. The following program
main()
(
static char a[3][4] = (“abed”, “nnop”, “Eghi"};
putchar (**a);
»
(a) will not compile successfully (b) results in run-time error
(©) prints garbage (@) none of the above
Answer the next three questions based on the program fragment given below.
int hh
static char wer(] = ‘NO SUBSTITUTS FOR HARD WORK”
167
87. print£("#10.5s", wer);
‘ourputs
(@) No su
(6) NO suBsTIT
(© No su
(@) UTE F
$8. print£(*$-10.5s", wer);
outputs
(@) No su
(b) No sussTIT
(©) No su
(@) UTE F18 McQ in Computer Science
59. printf(*$-10.48", hh,
outputs
(a) No su (b) No SUBSTITUTE FO
(©) NO su (A) error message
60, If n has the value 3, then the statement a[++n] = n++5
(@) assigns 3 to a5)
(©) assigns 4 to a(4)
(b) assigns 4 to a[5]
(@) assigns compiler-dependentvalueto a [5]
*61. The default parameter passing mechanism is
(a) call by value
(6) call by value result
+62. The following program
main()
(prints (“tim”) ;
main (i)
(a) is illegal
(6) prints tir once
*63. Consider the following program,
main()
{ putchar (*M");
first;
putchar(‘m’); }
first ()
( )
second()
{ putchar (‘a’); }
(b) call by reference
(d) none of the above
(b) keeps on printing tim
(none of the above
If Madam is the required output then the body of first () must be
(a) empty
(b) second (); putchar( +a’)
(©) putchar(‘a"}; second ();
(@) none of the above.
print£(™tc”, 'a");
64. max is a function that returns the larger of the two integers, given as arguments, Which
of the following statements finds the largest of three given numbers?
(a) max(max(a, b), max(a,
(b) max(a, max(a, c))
(©) max(max(a, b), max(b,
(@) max(b, max(a, ¢))
+65. The following program
maia()
{int a= 4;
change (a);
printé("8d”, a);Programming with ¢ 19
change (a)
int ay
( printe("sa", +42) 3)
outputs
(@) 55 () 45 © 54 (44
+66. The following program
nain()
(
int i=
(int is 4, 5 5 5;
printe(*sdta", i, 3);
}
print£("sdsd”, i, i);
,
(a) will not compile suecessfully (b) prints 4525
(©) prints 2525 (4) none of the above
*67. ‘The following program
main()
{
incQ; ineQs incOs
)
inc)
t
static int x;
printé ("8a"), 443) 5
,
(a) prints 012
(6) prints 123
(©) prints 3 consecutive, but unpredictable numbers
(@) prints 221
68, printf (“ab”, “ca”, “ef");
prints
(a) ab (b) abedet
(©) abcdef, followed by garbage (4) none of the above
69. The expression 4 + 6 / 3 * 2 - 2 + 7 % 3evaluatesto
@ 3 (b) 4 © 6 @7
70. Consider the following program segment.
i = 6720; 3 = 4%
while ((i83)==0)ne McQ in Computer Science
On termination j will have the value
(a) 4 8 Or (@ 6720
71. ‘The output of the following program is
main()
( static int x[] = (1, 2, 3, 4) 5, 6) Ty ale
dat 4;
for(i = 27 4 < 6; +44)
xletal) = xLi)e
for (i = 0; i < 8 +44)
printé (8d %, xLi])s
,
@12335578 12345678
87654321 12354678
*M. main ()
( int a= 5, b
printf ("Sd") at+tb);
,
(a) results in syntax error (b) prints 7
(©) prints &
*73. The program fragment
int a= 5, b= 2;
printf ("8a") a-titeb);
(a) prints 7
(©) prints 9
*74, Consider the following program:
main()
{int x= 2, y = 5;
if (x < y) return (x
else printf (*2l");
printé ("22");
}
Choose the correet statements:
(a) The output is 22
(©) This will result in compilation error
¥78. puts (argy(0]);
(@) none of the above
() prints 8
(@) none of the above
ye
(b) The output is 2122
(@) None of the above
(a) prints the name of the source code file
(b) prints argvProgramming with ¢ nm
(©) prints the number of command line arguments
(d) prints the name of the executable code file
76. A possible output of the following program fragment
static char wer(1[5] = {*harmot”, “merli", “axari"};
print£(("sd td 8d”, wer, wer[0], swer(0](0])s
(a) 262164 262164 262164 (b) 262164 262165 262166
(© 262164 262165 262165 (A) 262164 262164 267165
¥77. The following program
nain()
(printé(*su", main)s}
results in
(a) printing of a garbage number
(b) an execution error
(©) printing of starting address of the funetion main
(@) an infinite loop
+78. ‘The following program
main()
( int abcd:
abe);
(abe) 7
}
int abe()
( printe(*come”); }
(a) results in a compilation error (b) prints come come
(©) results in a run time error (@) prints come come
The next five questions are based on the following program fragment.
C static char wer(3](4] = {“bag”, “let”, “bud”);
char (*ptr) (4) = wer: _
#79, The possible output of print é (td $a", pte, ptetl): is
(@) 262 262 (b) 262 266 (6) 262 263 (@) 262 265
80. The possible output of printf (“td $4”, wer [1], wer[i]#1) is
(@) 162 163 (b) 162 162 (@) 162 166 @) 162 165
81. The possible output of printed Se", wer, wert); is
(@) 262 262 () 262 266 (6) 262 263) 262. 265
82. putchar (*(wer{i]+1));
(a) prints & (b) prints a (©) prints 1 (2) prints b84.
#85,
#86,
87.
88,
89,
McQ in Computer Science
. In which of the following cases will the character ‘t’ be printed?
(a) putchar(*(*(ptr+1) + 2))7
(b) putchar(*(wer(1] + 2))2
(©) putchar(*(ptrti) + 2);
(@) none of the above
‘A set of names can be represented as a
(a) two-dimensional array of characters
(b) one-dimensional array of strings
(©) one-dimensional array of pointers to character
(d) none of these
Consider the following declaration.
int a, *h = fa, *#e =
‘The following program fragment
as 4
veo = 57
(a) does not change the value of a (b) assigns address of c to a
(©) assigns the value of b to a (@) assigns 5 to a
If the statement
b= (int *) ey
is appended to the above program fragment then
(a) value of b is unaffected (b) value of b will be the address of
(©) value of b becomes 5 (@) none of these
Consider the two declarations
void *voidptr ;
char *charPtr ;
Which of the following assignments are syntactically correct?
(a) voidPtr = charPtr (b) charPtr = voidptr
(© *voidPtr = *char?tr (@ *charPtr = voidPtr
Consider the following program fragment
int v= 3, *py = av;
printf (“td ta", v, *py);
‘The output will be
(a) an error message (b) 3 address of v
33 (4) none of the above
If the two statements
spy = 0;
printé ("Sd ad”, *py, wv):
are appended to the previous program fragment then the output will be
(@o3 0 0
(©) unpredictable (none of the aboveProgramming with ¢ 23
90. Consider the following program.
main()
char x(10], *ptr = x7
seant("$s", x);
change (&x(4))¢
,
change (char a{])
puts(a);}
If abcdefa is the input, the output will be
(a) abcd (b) abe (©) ety (@ garbage
91, For the previous question the function calls
change (x) ; and change (ptr) +
(a) serves the same purpose (b) the second call is illegal
(©) both the calls are illegal (@) none of the above
92. If x isan array of integer, then the value of &x {4 is same as that of
(@) &x(i-1] + sizeof(int) (b) x + sizeof (int) *i
xed (+e (ex lil)
93. Pick the correct answers.
If x is an one-dimensional array then
(a) &x(+) issame asx = § - 1 (b) ¥(x + 4) issameas *(exLi])
(©) * Ox + 4) is same as x4] (tix + 4) issameastx +f
94, Let x be an array. Which of the following cannot be present in the left-hand side of an
assignment statement?
(a) x xe (toe + id @) Sx (a)
98. Let x be an array. Which of the following operations are illegal?
(a) +4 bx 1 (©) xe+ @x*2
96. Consider the declaration
char x(] = “WHATIATT”;
char *y = “WHATIZIT";
Pick the correct answers.
(a) The output of put s(x) and puts Cy) will be the same,
(b) The output of puts (x) and puts (y) will be different.
(©) The output of puts (y) is implementation dependent.
(@) None of the above comments is true.
97. If func isa function needing three arguments 21, a2, 23 then func ean be invoked
by
(@) func(al, a2, a3); @) (efuncyiat, a2, a3);
(© *functal, a2, a3); (A) all of the above4 McQ in Computer Science
*98. Consider the declarations
char first (int (*) (char, float));
int second(char, float);
Which of the following function invocation is valid?”
(@) first (second); (b) first (second);
(©) first (second); (@) none of the above
99. The declaration
int (*p) [917
(a) p is a one dimensional array of size 5, of pointers to integers
(b) p is a pointer to a 5 clement integer array
(©) the same as int *p [5]
(@) none of the above
100, A function « that accepts a pointer to a character as argument and returns @ pointer to
an array of interger, can be declared as
(@) int (*q(char*)) 11 (b) int *afehar *) 1
(©) int (ra) (char *) () (d) none of the above
*101. Consider the declaration
int a= 5, *b = gay
‘The statement
print£(™td", a * bye
prints
(a) 25 (b) garbage
(©) 5 x address of b (4) an error message
*102, In the previous question, printf (™$a”, a**b) ¢ prints
(a) 25 (b) garbage
0 (4) an error message
103. The following program
main()
t
float a= .5, b= .75
if (b< .7)
if (a < .5)
printé ("TELO");
else
printé (“LTTE”);
else
printé ("JKLE");
outputsProgramming with ¢ 25
(@) LITE (b) T2L0 (© oKLe @ Lo
104, What is the output of the following program segment?
void max(int x, int y, int m)
( 4f(x > 5) m= x
else m= ys)
int main()
(int i= 20, 9 k = 07
max(i, 3, x); printe(™sd", Kye }
@ 5 (b) 20
© 0 (d) None of the above
10S. Consider the program
main( )
‘
int yoig
printg("$a”, (*(char *)&x)) +
}
If the machine in which this program is executed is little-endian (meaning, the lower
significant digits occupy lower addresses), then the output will be
(a) 0 (b) 99999999 ol (A) unpredictable
106. a b is syntactically correct if
(a) a and b are structures
(b) ais a structure and b is a pointer to a structure
(©) ais.a pointer to a structure and b is a structure
(@) is a pointer to a structure in which b is a field
107. The program
main()
‘
int i = 55
i= i) J Get);
print£(™8a”, is
,
prints
@ 2 ) 5 1 @s6
‘Answer the next four questions, based on the following declaration.
struct addr
(
char city[10];
char street [20];26 McQ in Computer Science
int pincode;
06
struct
‘
char name[20]7
int sex;
struct addr locate ;
} criminal, *kd = criminal;
108, sex can be accessed by
(@) criminal.sex (b) ka > sex
© (ka) .sex (@) cither (a) or (6), but not (b)
109. pincode can be accessed by
(a) criminal.locate.pincode (b) criminal.pincode
(©) kd > locate.pincode (@) Xé.locate + pincode
110. The third character in the eriminal name ean be accessed by
(a) criminal.name[21 () ka > same {21
(©) (kd) name) [2] (4) either (b) oF (@), but not (a)
IL, *(ka — name + 2) can be used instead of
(@) *(criminal.name + 2) () ke > (name + 2)
(© *((*kd) name + 2) (4) either (a) or (b), but not (e)
112, The output of the following program
main)
(int a=, b= 2, ¢
printe("8d”, a t= (a += 3, 5, adds
will be
(a) 8 () 12 9 @6
113. Consider the following program segment,
char ta, *b, ¢[10], d[101;
a= b
b- oc
a
Choose the statements having errors.
(a) No error (b) a = by andb = c;
(c= di andd = a; (a = brandd = a
“114. The operation of a staircase switch best explains the
(a) or operation (b) anc operation
(©) exclusive nor operation (@) exclusive ox operation115,
116.
17.
18.
19.
120.
121.
122.
7123.
Programming with ¢ 27
a << 1 is equivalent to
(a) multiplying a by 2 (b) dividing a by 2
(©) adding 2 to a (4) none of the above
Ina certain machine, the sum of an integer and its 1's complement is 2”? - 1, Then
sizeof (int), in bits, will be
(@ 26 (b) 32
(©) unpredictable (A) none of the above
If the word size is 16 bit then ~0xc5 will be
(a) Ox3a (b) Oxf faa
(©) 0x5e (@) none of the above
Which of the following operations produce an 1, if the input bits are 1 and 1?
(a) or (b) and (6) exclusive or (4) exclusive nor
‘The mumber of possible values of m, such that m & Ox3f equals 0x23 is
(1 2 3 @4
‘The for loop
for(i i < 10; 444)
printf("td", i & 1s
prints
(@) 0101010101 (b) 0111111111 (@) 0000000000 (d) 1111111111
calloc(m, n); is equivalent to
(a) malloc(m*n, 0);
(b) memset(0, m*n);
(©) ptr~ malloc(ay*n); memsex(p, 0, m*n);
(@) ptr = malloctm*n); strepyip, 0);
Consider the program fragment.
jos 2:
while (4 8 3)
pote
if G < i) printf (8d) ids
Ii >= 2, then the value of $, will be printed only if
(a) 4 is prime (b) 5 does not divide &
(©) 4 is oad (4) i is not prime
Consider the following statement.
# define Aypotenuse(a, ») sqrt(a * a + b * b):
The macro-call hypotenuse (a + 2, b + 3);
(a) finds the hypotenuse of a triangle with sides a + 2andb + 3
(b) finds the square root of (a + 2)? + (b + 3)?
(©) is invalid
(@) finds the square root of 3*2 + 4*b + 5
ot)ns
124,
“12s.
126.
127,
128,
129.
“131.
132.
McQ in Computer Science
For the previous question, which of the following macro-calls will find the hypotenuse
of a right-angled triangle with sides a + 1 and b + 1?
(a) hypotenuse (a+1, b+1) — (b) hypotenuse (++a, ++b)
(© hypotenuse (a++, b++) — (d) none of the above
If a variable can take only integral values from 0 to n, where n is a constant integer,
then the Variable can be represented as a bit-field whose width is the integral part of
(the Log in the answers are to the base 2)
(a) login) + 1 () login - 1) #1
(©) login + ay + a (A) none of the above
‘The statement printf(*$a", 102025:11:12);
prints
(@ 10 0 (©) 12 @1
‘The statement print£(™se”, (a4) )+ prints
(a) the current value of a (b) the current value of a + 1
(©) an error message (@) garbage
The statement printf (*$a", +45) prints
(a 5 () 6 (©) an error message (d) garbage
The statement printf (*$a”, sizeof (™")): prints
(a) an error message (b) 0
(©) garbage @1
|. If p is @ pointer to an integer and & is @ pointer to a character then sizeof (p) will
be
(a) same as that of sizeof (t) (b) greater than that of sizeof (t)
(©) less than that of sizeof (t) (d) none of the above
Consider the declaration
char street(i0] = “abedefghi";
Choose the correct remark(s).
(@) &street and street will have different values
(b) street is meaningless
(©) &street+ and street +1 will have the same values
(@) None of the above
Consider the following program fragment
a= 0;
for(i = 1; i < 31; +i)
for(j = 1; 3 < 31; +43)
for(k = 1; k < 31; ++k)
if # 5 +k) 8 3) = OF
d= atl;
prints("$a",d);7133,
“134,
135,
7136.
Programming with ¢ 19
‘The output will be
(a) 9000 (b) 27000
(© 3000 (@) none ofthe above
‘The number of additions performed by the above program fragment is
(@ 27000 () 27000 x3
(©) 9000 +3 x 27000 (@) 9930 + 270003
Consider the following segment of C-code (GATE 2007)
int 3, a7
a4
while G <=
3 = 382;
‘The number of comparisons made inthe execution of the loop for any n > 0 is:
(@) Tlog,nl+ 1) m (© Mtog,n] @) Log, n) +1
Consider the following C function: (GATE 2007)
int £(int n)
{static int x = 0;
iE (2 <0) return ly
aE (n> 3)
(z= ne
return £(n-2) +2;
}
return £(n-1) +47
)
What is the value of f(5)?
(a) 5 () 7 9 (@) 18
Consider the following C program segment where CellNode represents a node in a binary
tree (GATE 2007)
struct CellNode
struct CellNode *leftChild;
int element;
struct CellNode *rightChild:
he
int GetValue (struct CellNode *ptr) (
int value = 0;
if (ptr != NULL) {
if ((ptrsleftchild == NULL) &&
(ptrorightChild == NULL))
value = 1;
else
value = value + GetValue (ptroleftChild)
+ GetValue (ptrorightChild);
)
return (value);
}McQ in Computer Science
‘The value returned by GetValue when a pointer to the root of a binary tree is passed
as its argument is,
(a) the number of nodes in the tree (b) the number of internal nodes in the tree
(®) the number of leaf nodes in the tree (d) the height of the tree
*137. Which combination of the integer variables x, y and z makes the variable a get the
value 4 in the following expression? (GATE 2008)
a=(e>y) 22) 2x2): >2)2V 22)
(a) x=3, (b) x= 6 y= 5,253
(©) x6 y= 3,2 (@) x= 5,y-4,2>5
138, What is printed by the following C program? (GATE 2008)
int £(int x, int *py, int **ppz) void main( )
{ {
int y, 2 int c, tb, ta;
‘eppz t= 1; z= **ppzi © b = 67
a= kb;
*ey #22; y = *py? printé("sd", £(cybea))
x3 ,
return x + y + 2
)
(a) 18 (b) 19 2 (@ 2
#139, Choose the correct option to fill 71 and ?2 so that the program below prints an input
siring in reverse order. Assume that the input string is terminated by @ newline character.
void reverse (void) {
int cy
if (21) reverse( )
22.
)
main () 4
printf("Enter Text"); printf("\n") 5
reverse( ); print£("\n");
)
(GATE 2008)
(@ 21 is (getenar( ) != a")
22 is getchar(c);
() 21 is (c = getehar( ) ) != "\n")
22 is getchar(c);
© 21 is fe f= "\n')
?2 is putchar(c);
@ 21 is ({e = getehar( ) }
22 is putchar(c);
"\nty140.
“141.
142.
Programming with ¢ oa
The next two questions are based on the following program fragment.
Consider the following C program that attempts to locate an element x in an array YL]
using binary search, The program is erroneous. (GATE 2008)
1 £(int ¥(10), int x) 4
2 int uy 3,
3B i= 07 5 = 9
4, do {
5. ke +4) / 2s
6 if (¥[k] 2
(d) Yis [2468 10 12 14 16 18 20] and 2
int fun(int n, int *fp) ¢
)
t = fun (n-1, fps
E- tt fp:
tepsts
)
int main() {a2 McQ in Computer Science
int x = 15;
printf ("Sa\n", fun(5, 6x))7
return 0;
}
‘The value printed is
(a) 6 (b) 8 14 (@) 15
"143, What does the following program print? (GATE 2010)
#include
void f(int *p, int *q) {
pra
“p= 2;
)
int i 0,
int main () 4
Elai, 83);
printf ("Sd %d\n", i, 3) 7
return 0;
)
(a 22 @) 21 (ol (@ 02
“144, What is the value printed by the following C program? (GATE 2010)
¥include
int f(int ta, int n)
t
if (n <= 0) return 0;
else if(*a82 == 0) return “a + f(atl,n-1);
else return *a - f(atl, ni);
)
int main ( )
{
int af] = (12, 7, 13, 4 11, 6}
printf ("Sd", £(a,6))¢
return 07
)
(@) -9 (b) 5 (©) 15 19
+145. The following C function takes a singly linked list as input argument. It modifies the list
by moving the last element to the front of the list and returns the modified list. Some
part of the code is left blank.Programming with ¢ oy
typedef struct node {
int value;
struct node ‘next;
} Node;
Nede *move_to_front (Node *head) (
Node *p, *ay
if ((head
head;
q = NULL; p = head;
while p->next!= NULL
NULL) || (head->next
NULL)} return
ap
p = p-onext:
)
return head;
}
Choose the correct alternative to replace the blank line,
(a) q = NULL; p->next = head; head = pr
(b) q->next = NULL; head = p; po>next = hea
(©) head = py po>next = qy q->ne NULL;
@ q->next = NULL; p->next = head; head
"146, What does the following fragment of C program print? (GATE 2011)
char e[] = “GATE2011";
char *p = c7
printe("$s”, p + p{3] - pil):
(@) GATE2011 —(b) E2011 (© 2011 @ on
Common Data for Questions 147 and 148:
Consider the following recursive C funetion that takes two arguments.
unsigned int foo (unsigned int , unsigned int x) (
if (n>0) return ((n¥r) + foo(s/r, £)) i
else return 0;
)
+147. What is the return value of the function foo when it is called as foo (345, 10)?
(GATE 2011)
(a) 345 12 5 @3
148. What is the return value of the function foo when it is called as Foo (513, 2)?
(GATE 2011)
@9 8 5 2
*149, What will be the output of the following C program segment? (GATE 2012)
Char inChar = ‘At
switch ( inChar ) (4 McQ in Computer Science
case 'A' : printé
case '5"
case 'C' : printé
case 'D'
case 'B*
default
(2) No Choice
(b) Choice A
(©) Choice A
Choice B No Choice
("choice A\n")
("choice B") ;
print£ ("No Choice" ) 7 }
(@) Program gives no output as itis erroneous
Common Data for Questions 150 and 151:
Consider the following C code segment.
int a, b, © = 0;
void prtFun (void);
main ( )
{ static int a =
prtfun( };
ath
prtfun( };
printf(" \n Sd ta *
)
void prtFun (void)
{ static int a = 2;
int b= 1;
4b;
printf(" \n td Sd
)
(Line 1*/
a,b);
(* Line 2*/
. a,b);
“150. What output will be generated by the given code segment? (GATE 2012)
A) (B) © @)
3 1 4 2 4 2 3 1
4 1 6 1 6 2 5 2
4 2 6 1 2 0 5 2
+151. What output will be generated by the given code segment if: (GATE 2012)
Line | is replaced by auto int a = 2;
Line 2 is replaced by register int a = 2
“w (B) © @)
3 1 4 2 4 2 4 2
4 1 6 1 6 2 4 2
4 2 6 1 2 0 2 010,
4,
18,
2.
26.
30.
34,
38.
42,
46.
50.
54,
58.
62
66.
70.
14.
2
82
86.
90.
94,
98,
102.
106,
110.
14.
Ls,
122.
126.
130,
134,
138,
142,
146
150,
"
15,
19,
23.
27,
31
35,
39.
43,
47
51
55.
59,
63
67.
1
18.
79.
83.
87.
1
95.
99,
103.
107.
il
1s.
19.
123
127,
131.
135.
139.
143.
147.
151.
od
Programming with ¢
12
16,
20.
24,
28,
32
36,
40,
44
48
52,
56
60.
68,
R.
16.
80.
84,
88,
92.
96.
100.
104.
108.
112.
116.
120.
124,
128,
132
136.
140.
144,
148.
3536 [McQs in Computer Science
Ea -+-—_—__—_——
1. Without any temporary variable, one can swap two given variables. Refer Qn. 2.
6. In signed magnitude form, one bit is dedicated to store the sign. (e.g., | for negative
and 0, otherwise). Only the remaining 15 bits are available to store the magnitude. Hence
the answer.
7. Any function (including main ()), retums a value to the calling environment. In the
case of printf, itis the number of characters it printed, So, the output will be t im3
(Since it printed the three characters a, b, ©).
8. Refer Qn. 7
‘The scané function returns the number of successful matches. i.e., 3 in this case.
9. The input is actually @\\nib. Since we are reading only two characters, only a and \n
will be read and printed,
12, Ify = 11, the expression 3. * (y - 8) / 9bccomes3 * 3 / 9, which evalue
ates to 1. But the expression (y - 8) / 9 * 3 becomes3 / 9 * 3, which
evaluates to 0 (since 3/9 is 0)
15, y *= 2 means y = y*2, ie, y = 4, inthis question. So, the expression is equiva-
lent to x = 4 + 4, which is 8, So, 8 will be printed. However, the order in which
the operands are evaluated is implementation-dependent. If the right operand is evaluated
first, the result will be 6. Don’t take things for granted.
16. Most of the compilers give 4 4 as the output. This is because most of the compilers use
stacks to evaluate the arguments. If so, the frst argument n+ + will be pushed before
the +n is pushed. This implies that ++n will be evaluated before n++ is evaluated.
However, the order of printing will be in accordance with the order the variables are
listed in the print £ statement.
“18,5 - 2 - 3 * 5 — 2will yield 18, ifitis teated as (5 - (2 - 3)) *
(5. ~ 2). ie, if has precedence over * and if it associates from the right.
20, 263 in binary form is 100000111. If one tres to print an integer as a character, only the
last 8 bits will be considered—the rest chopped off. So, in this case the ASCH value of
00000111 (j., decimal 7) will be printed. Look in the ASCII table. Iti ringing a bell!
21. 9/5 yields integer 1. Printing 1 as a floating point number prints garbage.
23. In the computer [used to execute this program, the output was 4294967298. That's because
in my system,
sizeof (int) is4 bytes (32 bits), and negative numbers are represented in 2's comple-
ment form. This means ~4 will be represented as 11111111 IUIL111 1111111 11111100
(ie. 30 one’s followed by 2 zeroes). Note that this number is 2" 1-3. Before j gets
added to i, it will be converted to an unsigned integer. So, i+, is essentially adding 1
to 2” — 1-3, which gives 4294967293,
24, Let sizeof (int) = 1. So, ~4 will be stored as 11211100. Since we are adding
unsigned and signed integers, the signed gets converted to unsigned. So,
i + j will become 11111101, We are trying to print this as an uns igned integer. So,
what is printed will be 2° - 1 ~ 2.S0,1log(x + 3) = 8{ie, 8*sizeof(int))25,
26.
29.
38.
39.
40.
44,
4s.
2.
63.
68.
Programming with ¢ 37
‘i" is initialized to 3, and incremented by 3. When ; is 15, control will go out of the loop.
So 15 will be printed. (The cmpty semicolon immediately following the ‘for’ state-
ment, means the body of the £or loop is empty.)
The use of continue statement forces the execution to skip the remainder of the cur-
rent pass over the loop and initiates the next. If ‘it is 3, print é statement will be
skipped. Hence the answer is b.
k = ~7. So, if*k’ is used as a Boolean variable, it will be treated as a true condition,
So, !k will be false ie., 0. So, 0 < 1k is actually 0 < 0, which is false. So, 0 will be
printed.
putchar (105) will print the ASCII equivalent of 105 ie., ‘i’, The print statement
prints the current value of i, ie., 5 and then decrements it, So, h4 will be printed in the
next pass. This continues until ‘i’ becomes 0, at which point the loop gets terminated,
Since x > 7 is false, the ternary operator ?: retums "$c". So, printf ("se") i)
will be executed. So, the ASCII character corresponding to 107, ic., *k’ will be printed.
Refer Qn. 38.
printf (%a2”) prints az and retums a value 2 (since it printed two characters). So,
the condition results in the printing of a22. Since it always returns 2, it is an infinite
loop. The output will be a22byaz2by.
Refer Qn. 40
Here the ¢1s¢ clause will be executed. Since 2 < 0 is false, Cour will be printed,
‘The ese clause has no braces ie., { and }. This means the ¢1se clause is made up of
only one statement, So, printf (Ya <= b"); will be executed anyway, ie. if a>
or a<=b, Hence the answer,
1. The condition is put char (0) . This returns a value 0 which is a false condition. So,
the loop will not be executed even once.
[2] will be converted to *(a + 2)
*(a + 2) cam as well be written as *(2 + a).
«(2 + a) isnothing but 2[a] . So, a[2) is essentially same as 2 [a], which is same as,
* (2 + a). So, it prints 9~ 9 = 18, Some of the modern compilers don’t accept 2[a].
. *a points to the string “abcd”. **a is the first character of “abco”, which is the
character a"
|. Which means a function will be manipulating a copy of the local variable, passed as argu-
ment. So, any change will be local and hence will not be reflected in the calling routine.
(Refer Qn. 65)
This involves recursion ~ main () calling itself. So, it keeps on printing tim
Since Madam is the required output, the function first (), should print ‘a’, call the
function second) that prints the ‘a’ and print ‘a’ again. Hence c is the correct
Refer Qn. 61. change (a), prints 5 but the value of 'a" in main () is still 4. So,
main () will print 4.