C Program Output MCQ Examveda

You are on page 1of 12

C Programming Output

-MCQ Questions and Answers-


Courtesy: Examveda.com
Edited by: Md.Otul kabir

1. Determine 3. Determine
output: void main() Output: void main()
{ {
int const *p=5; float me = 1.1;
printf("%d", ++(*p)); double you =
} 1.1; if(me==you)
printf("I hate
A. 6 Examveda"); else
B. 5 printf("I love Examveda");
C. Garbage Value }
D. Compiler Error
A. I hate Examveda
Answer: D B. I love Examveda
C. Error
Solution (By Examveda Team) D. None of These
p is a pointer to a "constant integer". But we
tried to change the value of the "constant Answer: B
integer".
Solution (By Examveda Team)
2. Determine For floating point numbers (float, double, long
Output: void main() double) the values cannot be predicted exactly.
{ Depending on the number of bytes, the
char s[]="man"; precession with the value represented varies.
int i; Float takes 4 bytes and long double takes 10
for(i=0; s[i]; i++) bytes. So float stores 0.9 with less precision than
printf("%c%c%c%c ", s[i], *(s+i), *(i+s), long double.
i[s]); Rule of Thumb: Never compare or at-least be
} cautious when using floating point numbers with
relational operators (== , >, <, <=, >=,!= ) .
A. mmm nnn aaa
B. mmmm nnnn aaaa 4. Determine
C. Compiler Error Output: void main()
D. None of These {
static int var = 5;
Answer: D printf("%d ", var--
); if(var)
Solution (By Examveda Team) main();
Correct Output : mmmm aaaa nnnn }
s[i], *(i+s), *(s+i), i[s] are all different ways of
expressing the same idea. Generally array name A.55555
is the base address for that array. Here s is the B.54321
base address. i is the index number/displacement C. Infinite Loop
from the base address. So, indirecting it with * is D. None of These
same as s[i]. i[s] may be surprising. But in the
case of C it is same as s[i]. Answer: B

1 Courtesy: Examveda.com Edited by: Md.Otul kabir


2 Courtesy: Examveda.com Edited by: Md.Otul kabir
Solution (By Examveda Team) Solution (By Examveda Team) \n -
When static storage class is given, it is newline - printf("\nab"); - Prints ab
initialized once. The change in the value of a \b - backspace - printf("\bsi"); - firstly '\b'
static variable is retained even between the removes 'b' from 'ab ' and then prints 'si'. So after
function calls. Main is also treated like any other execution of printf
ordinary function, which can be called
recursively. 7. Determine
Output: void main()
5. Determine {
Output: void main() int i=10; i=!i>14;
{ printf("i=%d", i);
char *p;
printf("%d %d", sizeof(*p), sizeof(p)); }
}
A. 10
A.11 B. 14
B.12 C. 0
C.21 D. 1
D.22
Answer: C
Answer: B
Solution (By Examveda Team)
Solution (By Examveda Team) In the expression !i>14 , NOT (!) operator has
The sizeof() operator gives the number of bytes more precedence than ">" symbol. ! is a unary
taken by its operand. P is a character pointer, logical operator. !i (!10) is 0 (not of true is
which needs one byte for storing its value (a false). 0>14 is false (zero).
character). Hence sizeof(*p) gives a value of 1.
Since it needs two bytes to store the address of 8. Determine
the character pointer sizeof(p) gives 2. Output: void main()
{
6. Determine the Final Output: int c = - -2;
void main() printf("c=%d", c);
{ }
printf("\nab");
printf("\bsi"); A. 1
printf("\rha"); B. -2
} C. 2
D. Error
A. absiha
B. asiha Answer: C
C. haasi
D. hai Solution (By Examveda Team)
Here unary minus (or negation) operator is used
Answer: D twice. Same maths rule applies, ie. minus *
minus= plus.
Note: However you cannot give like --2.
Because -- operator can only be applied to
variables as a decrement operator (eg., i--). 2 is a
constant and not a variable.

3 Courtesy: Examveda.com Edited by: Md.Otul kabir


9. Determine Output: }
#define int char void display(char *string)
void main() {
{ printf("%s", string);
int i = 65; printf("sizeof(i)=%d", }
sizeof(i));
} A. will print Hello World
B. Compiler Error
A. sizeof(i)=2 C. Can't Say
B. sizeof(i)=1 D. None of These
C. Compiler Error
D. None of These Answer: B

Answer: B Solution (By Examveda Team)


Type mismatch in redeclaration of function
Solution (By Examveda Team) display. In third line, when the function display
Since the #define replaces the string int by the is encountered, the compiler doesn't know
macro char. anything about the function display. It assumes
the arguments and return types to be integers,
10. Determine Output: (which is the default type). When it sees the
void main() actual function display, the arguments and type
{ contradicts with what it has assumed previously.
int i=3; Hence a compile time error occurs.
switch(i)
{ 12. Determine Output:
default: printf("zero"); void main()
case 1: printf("one"); break; {
case 2: printf("two"); break; int i=5;
case 3: printf("three"); break; printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
} }
}
A. 45545
A. zero B. 54544
B. three C. 55445
C. Error D. 54554
D. None of These
Answer: A
Answer: B
Solution (By Examveda Team)
Solution (By Examveda Team) The arguments in a function call are pushed into
The default case can be placed anywhere inside the stack from left to right. The evaluation is by
the loop. It is executed only when all other cases popping out from the stack. and the evaluation is
doesn't match. from right to left, hence the result.

11. Determine Output:


void main()
{
char string[]="Hello
World"; display(string);

4 Courtesy: Examveda.com Edited by: Md.Otul kabir


14. Determine Output:
void main()
{
char *p="hi friends",
*p1; p1=p;
while(*p!='\0') ++*p++;
printf("%s", p1);
}

A. hi friends
B. ij!gsjfoet
C. hj grjeodt
D. None of These

Answer: B

Solution (By Examveda Team)


++*p++ will be parse in the given order :
1. *p that is value at the location currently
pointed by p will be taken
2. ++*p the retrieved value will be incremented
3. when ; is encountered the location will be
incremented that is p++ will be executed.

15. Determine Output:


#include<stdio.h>
13. Determine Output: #define a 10
#define square(x) x*x void main()
void main() {
{ #define a 50
int i; printf("%d", a);
i = 64/square(4); }
printf("%d", i);
} A. 50
B. 10
A. 4 C. Compiler Error
B. 64 D. None of These
C. 16
D. None of These Answer: A

Answer: B Solution (By Examveda Team)


The preprocessor directives can be redefined
Solution (By Examveda Team) anywhere in the program. So the most recently
The macro call square(4) will substituted by 4*4 assigned value will be taken.
so the expression becomes i = 64/4*4 . Since /
and * has equal priority the expression will be
evaluated as (64/4)*4 i.e. 16*4 = 64

5 Courtesy: Examveda.com Edited by: Md.Otul kabir


16. Determine Output: 18. Determine Output:
#define clrscr() 100 void main()
void main() {
{ char far *farther, *farthest;
clrscr(); printf("%d..%d", sizeof(farther),
printf("%d", clrscr()); sizeof(farthest));
} }

A. 0 A. 4..2
B. 1 B. 2..2
C. 100 C. 4..4
D. Error D. 2..4

Answer: C Answer: A

Solution (By Examveda Team) Solution (By Examveda Team)


Preprocessor executes as a seperate pass before The second pointer is of char type and is not a
the execution of the compiler. So textual far pointer.
replacement of clrscr() to 100 occurs. The input
program to compiler looks like this: 19. Determine Output:
void main()
void main() {
{ char *p; p="Hello";
100; printf("%c",
printf("%d", 100); *&*p);
} }
Note: 100; is an executable statement but with
no action. So it doesn't give any problem. A. Hello
B. H
17. Determine Output: C. Some Address will be printed
void main() D. None of These
{
printf("%p", main); Answer: B
}
Solution (By Examveda Team)
A. Error * is a dereference operator & is a reference
B. make an infinite loop operator. They can be applied any number of
C. Some address will be printed times provided it is meaningful. Here p points to
D. None of These the first character in the string "Hello". *p
dereferences it and so its value is H. Again &
Answer: C references it to an address and * dereferences it
to the value H.
Solution (By Examveda Team)
Function names are just addresses (just like
array names are addresses). main() is also a
function. So the address of function main will be
printed. %p in printf() specifies that the
argument is an address. They are printed as
hexadecimal numbers.

6 Courtesy: Examveda.com Edited by: Md.Otul kabir


19. Determine Output: Solution (By Examveda Team)
void main() Compiler Error: Constant expression required in
{ function main.
int i=1; The case statement can have only constant
while(i<=5) expressions (this implies that we cannot use
{ variable names directly so an error).
printf("%d",
i); if(i>2) Note: Enumerated types can be used in case
goto here; statements.
i++;
} 21. Determine Output:
} void main()
fun() {
{ int i;
here: printf("PP"); printf("%d", scanf("%d", &i)); // value 10 is
} given as input here
}
A. 12PP
B. 12PP345 A. 10
C. Compiler Error B. 1
D. None of These C. Garbage Value
D. None of These
Answer: C
Answer: B
Solution (By Examveda Team)
Compiler error: Undefined label 'here' in Solution (By Examveda Team)
function main Labels have functions scope, in scanf returns number of items successfully read
other words The scope of the labels is limited to and not 1/0. Here 10 is given as input which
functions. The label 'here' is available in should have been scanned successfully. So
function fun() Hence it is not visible in function number of items read is 1.
main.
22. Determine Output:
20. Determine Output: void main()
void main() {
{ int i=0;
int i=1, j=2; for(;i++;printf("%d",
switch(i) i)); printf("%d", i);
{ }
case 1: printf("GOOD"); break;
case j: printf("BAD"); break; A. 1
} B. 11
} C. 12
D. Error
A. GOOD BAD
B. GOOD Answer: A
C. Compiler Error
D. None of These Solution (By Examveda Team)
Before entering into the for loop checking
Answer: C condition is "evaluated". Here it evaluates to 0

7 Courtesy: Examveda.com Edited by: Md.Otul kabir


(false) and comes out of the loop, and i is linking the linker searches for the definition of i.
incremented (note the semicolon after the for Since it is not found the linker flags an error.
loop).
25. Determine Output:
23. Determine Output: void main()
void main() {
{ int i=0, j=0;
struct xx if(i && j++)
{ printf("%d..%d", i++,
int x=3; j); printf("%d..%d", i, j);
char name[] = "hello"; }
};
struct xx *s = malloc(sizeof(struct A. 0..1
xx)); printf("%d", s->x); B. 1..0
printf("%s", s->name); C. 0..0
} D. 1..1

A. 3 hello Answer: C
B. Compiler Error
C. Linking error Solution (By Examveda Team)
D. None of these The value of i is 0. Since this information is
enough to determine the truth value of the
Answer: B boolean expression. So the statement following
the if statement is not executed. The values of i
Solution (By Examveda Team) and j remains unchanged and get printed.
Initialization should not be done for structure
members inside the structure declaration. 26. Determine Output:
void main()
24. Determine output: {
void main() static int
{ i=5; if(--i){
extern int main();
i; i=20; printf("%d ", i);
printf("%d", sizeof(i)); }
} }

A. 20 A.54321
B. 2 B.0000
C. Compiler Error C. Infinite Loop
D. Linker Error D. None of These

Answer: D Answer: B

Solution (By Examveda Team) Solution (By Examveda Team)


Linker error: undefined symbol '_i'. Explanation: The variable "i" is declared as static, hence
extern declaration specifies that the variable i is memory for I will be allocated for only once, as
defined somewhere else. The compiler passes it encounters the statement. The function main()
the external variable to be resolved by the linker. will be called recursively unless I becomes equal
So compiler doesn't find any error. During to 0, and since main() is recursively called, so

8 Courtesy: Examveda.com Edited by: Md.Otul kabir


the value of static I ie., 0 will be printed every because it has no effect in the expressions
time the control is returned. (hence the name dummy operator).

27. Determine Output: 29. Determine Output:


void main() void main()
{ {
int i=-1, j=-1, k=0, l=2, m; char *str1 = "abcd";
m = i++ && j++ && k++ || l++; char str2[] = "abcd";
printf("%d %d %d %d %d", i, j, k, l, m); printf("%d %d %d", sizeof(str1),
} sizeof(str2), sizeof("abcd"));
A.00120 }
B.00130 A.255
C.00131 B.555
D.00021 C.245
D.244
Answer: C
Answer: A
Solution (By Examveda Team)
Logical operations always give a result of 1 or 0 Solution (By Examveda Team)
. And also the logical AND (&&) operator has In first sizeof, str1 is a character pointer so it
higher priority over the logical OR (||) operator. gives you the size of the pointer variable. In
So the expression 'i++ && j++ && k++' is second sizeof the name str2 indicates the name
executed first. The result of this expression is 0 of the array whose size is 5 (including the '\0'
(-1 && -1 && 0 = 0). Now the expression is 0 || termination character). The third sizeof is similar
2 which evaluates to 1 (because OR operator to the second one.
always gives 1 except for '0 || 0' combination-for
which it gives 0). So the value of m is 1. The 30. Determine Output:
values of other variables are also incremented by void main()
1. {
char not; not =
28. Determine Output: !2; printf("%d",
void main() not);
{ }
int i = -
1; +i; A. 2
printf("i = %d, +i = %d", i, +i); B. Garbage Value
} C. 0
D. None of These
A. i = -1, +i = 1
B. i = 1, +i = 1 Answer: C
C. i = -1, +i = -1
D. None of These Solution (By Examveda Team)
! is a logical operator. In C the value 0 is
Answer: C considered to be the boolean value FALSE, and
any non-zero value is considered to be the
Solution (By Examveda Team) boolean value TRUE. Here 2 is a non-zero value
Unary + is the only dummy operator in C. so TRUE. !TRUE is FALSE (0) so it prints 0.
Where-ever it comes you can just ignore it just

9 Courtesy: Examveda.com Edited by: Md.Otul kabir


31. Determine Output: A. 10
#include<stdio.h> void B. 9
main() C. 11
{ D. None of These
register i=5; char j[]=
"hello"; printf("%s Answer: B
%d", j, i);
} Solution (By Examveda Team)
"return(i++)", it will first return i and then
A. hello 5 increment it. i.e. 10 will be returned.
B. hello garbage value
C. Error 34. Determine Output:
D. None of These
void main()
Answer: A {
char a[]="12345";
Solution (By Examveda Team) int i = strlen(a);
If you declare i as register compiler will treat it printf("%d", ++i);
as ordinary integer and it will take integer value. }
i value may be stored either in register or in
memory. A. 5
B. 6
32. Determine Output: C. 7
void main() D. None of These
{
int i=5, j=6, z; Answer: B
printf("%d", i+++j);
} Solution (By Examveda Team)
The char array 'a' will hold the initialized string,
A. 12 whose length will be counted from 0 till the null
B. 13 character. Hence the 'i' will hold the value equal
C. 11 to 5, after the pre-increment in the printf
D. None of These statement, then 6 will be printed.

Answer: C 35. Determine Output:


void main()
Solution (By Examveda Team) {
The expression i+++j is treated as ((i++) + j) int i;
char a[]="�";
33. Determine Output: if(printf("%sn", a))
void main() printf("Ok here
{ n"); else
int i = abc(10); printf("Forget itn");
printf("%d", --i); }
}
int abc(int i) A. Ok here
{ B. Forget it
return(i++); C. Error
} D. None of These

10 Courtesy: Examveda.com Edited by: Md.Otul kabir


Answer: A 38. Determine Output:
#define prod(a,b) a*b
Solution (By Examveda Team) void main()
Printf will return how many characters does it {
print. Hence printing a null character returns 1 int x=3, y=4;
which makes the if statement true, thus "Ok printf("%d", prod(x+2, y-1));
here" is printed. }

36. Determine Output: A. 15


void main() B. 10
{ C. 12
int i=i++, j=j++, k=k++; D. 11
printf("%d %d %d", i, j, k);
} Answer: B

A.111 Solution (By Examveda Team)


B.000 The macro expands and evaluates to as:
C. garbage values x+2*y-1 => x+(2*y)-1 => 10
D. Error
39. Determine Output:
Answer: C void main()
{
Solution (By Examveda Team) char p[]="%dn";
An identifier is available to use in program code p[1] = 'c';
from the point of its declaration. So expressions printf(p, 65);
such as i = i++ are valid statements. The i, j and }
k are automatic variables and so they contain
some garbage value. A. 65
B. c
37. Determine Output: C. A
void main() D. Error
{
static int i=i++, j=j++, k=k++; Answer: C
printf("%d %d %d", i, j, k);
} Solution (By Examveda Team)
Due to the assignment p[1] = 'c' the string
A.111 becomes, "%c\n". Since this string becomes the
B.000 format string for printf and ASCII value of 65 is
C. garbage values 'A', the same gets printed.
D. Error
40. Determine Output:
Answer: A void main()
{
Solution (By Examveda Team) char *p;
Since static variables are initialized to zero by p="%dn"; p++;
default. p++; printf(p-2,
300);

11 Courtesy: Examveda.com Edited by: Md.Otul kabir


12 Courtesy: Examveda.com Edited by: Md.Otul kabir

You might also like