C Programming
C Programming
A. True
B. False
Ans: A
_____________________________________________________________________________________
Inline functions involves some additional overhead in running time
A. True
B. False
Ans: A
_____________________________________________________________________________________
A function that calls itself for its processing is known as
A. Inline Function
B. Nested Function
C. Overloaded Function
D. Recursive Function
Ans: D
_____________________________________________________________________________________
We declare a function with ______ if it does not have any return type
A. long
B. double
C. void
D. int
Ans: C
Arguments of a functions are separated with
A. comma
B. semicolon
C. colon
D. None of these
Ans: A
_____________________________________________________________________________________
Variables inside parenthesis of functions declarations have _____ level access
A. Local
B. Global
C. Module
D. Universal
Ans: A
_____________________________________________________________________________________
Observe following function declaration and choose the best Ans:
int divide ( int a, int b = 2 )
A. Variable b is of integer type and will always have value 2
B. Variable a and b are of int type and the initial value of both variables is 2
C. Variable b is international scope and will have value 2
D. Variable b will have value 2 if not specified when calling function
Ans: D
_____________________________________________________________________________________
The keyword endl
A. Ends the execution of program where it is written
B. Ends the output in cout statement
C. Ends the line in program. There can be no statements after endl
D. Ends current line and starts a new line in cout statement.
Ans: D
_____________________________________________________________________________________
Strings are character arrays. The last index of it contains the null-terminated character
A. \n
B. \t
C. \0
D. \1
Ans: C
_____________________________________________________________________________________
Union elements can be of different sizes
A. True
B. False
Ans: A
#include
int main()
{
int const * p=5;
*p=*p+10;
printf("%d",*p);
return(0);
}
Output:
Compiler error:Cannot modify a constant value.
#include
int main()
{
char ch = 'y';
switch (ch)
{
case 'w': printf("%c",'w');
case 'x': printf("%c",'x');
case 'y': printf("%c",'y');
case 'z': printf("%c",'z');
}
}
Answer:
output: yz
3
char s[15];
strcpy(s, "csecode");
sizeof(s) = ?
strlen(s) = ?
Answer:
Output:sizeof(s)= 15 (it is equal to 15 not 7 because s has size 15)
strlen(s) = 7
C MCQ 4
JANUARY , 2012
4
Can below programe is correct for checking given year is leap year or not?-
main()
{
int y;
scanf("%d",&y);
if( (y%4==0 && y%100 != 0) || y%100 == 0 )
printf("leap year");
else
printf("not a leap year");
}
Answer:
Yes.
C MCQ 5
JANUARY , 2012
5
#include
main()
{
char a[4]="code";
printf("%s",a);
}
Answer:
Output:code
Explanation:
The array size is four so it has the memory just enough to hold the string
code and doesnt has space to store the NULL character.
So it prints the 'code' correctly and continues to print garbage
values till it accidentally get a NULL character.
NOTE:Avoid such situation on writing programe.
C MCQ 6
JANUARY , 2012
6
#define product(a,b) a*b
main()
{
int x=5,y=2;
printf("%d",product(x+4,y-3));
}
Answer:
Output: 10
explanation:
x+4*y-3=5+4*2-3=10
C MCQ7
FEBRUARY , 2012
7
void main()
{
char ch1[]="byee",ch2[]="byee";
if(ch1==ch2)print("equal");
else print("unequal");
}
Answer:
Output:unequal
C MCQ8
FEBRUARY , 2012
8
char ch[16]="xyz is best";
ch[13]='c';
print(ch);
Answer:
Output:xyz is best
C MCQ9
FEBRUARY , 2012
9
int main()
{
char s1[8]= { '\0' },s2[8]= { '\0' };
scanf("%7[^wxyz]%7s", s1, s2);
printf("s1 = %s s2 = %s\n", s1, s2);
}
Input 1:
wxyzabcd
Input 2:
abcdwxyz
Answer:
Output 1: s1 = s2 =
Output 2:s1 = abcd s2 = wxyz
Explanation:
%7[^wxyz] mean scanf take characters which is not matching w or x or y or z
,and
7 specifies match one or more contigious (up to 7) characters and
store the matched characters as a string into the area provided,and
skip any whitespace (the %s format specifier skips leading whitespace).
and "[" format specifier must match at least one character for it to be
successful(thats why in first output s2 is also blanck),and The scanf
functions
process one character at a time, if at any time the current character does
not
match the format specifier the match fails and the function returns.
e.g-
scanf ("%[^\n]%*c",string);
it take a string as input until found newline character.
This is a way to take a input with leading whitespace
using scanf.
C MCQ10
FEBRUARY , 2012
10
#include
int main()
{
int i;
i=1,2,3;
printf("%d",i);
}
Answer:
Output: 1
C MCQ10
FEBRUARY , 2012
11
#include
main()
{
int printf=107;
printf("\n%d",printf);
}
Answer:
Output: error: called object printf is not a function
C MCQ10
MARCH , 2012
12
main(){
int a[]={1,2};
printf("%d %d\n",a[0],a[1]);
a[1,0]=22;
printf("%d %d",a[0],a[1]);
}
Answer:
Output:
1 2
22 2
Explanation:
a[i,j]=n; the value of a[j] become n and the value of a[i] is will not
changed .
C MCQ10
MARCH , 2012
13
main()
{
int a[]={1,0,7,1,0,7};
printf("%d",(&a[4]-&a[1]));
}
Answer:
Output: 3
C MCQ10
MARCH , 2012
14
main()
{
unsigned int i;
for(i=20;i>=0;i--)
printf("%d",i--);
}
Answer:
Output: Infinite Loop.
1. How many times will the following loop be executed?
ch = 'b';
while(ch >= 'a' && ch <= 'z')
A. 0 B. 25
C. 26 D. 1
Ans. B
2. Consider the following program fragment
switch(input)
{
case '1':
printf("One");
case '2':
printf("Two");
case '3':
printf(""Three");
default:
Printf("Default");
break;
}
What will be printed when input is 2?
A. Two Three Default B. Two
C. Two Default D. Two Two Default
Ans. A
3. The advantage of 'switch' statement over 'if' is that it leads to more structured program.
A. True B. False
Ans. A
4. The statement
for(;;);
is perfectly valid 'C' statement
A. True B. False
Ans. A
5. Consider the following 'C' program
#include<stdio.h>
int main()
{
int a=7, b=5;
switch(a = a % b)
{
case 1:
a = a - b;
case 2:
a = a + b;
case 3:
a = a * b;
case 4:
a = a / b;
default:
a = a;
}
return 0;
}
On the execution of the above program what will be the value of a?
A. 7 B. 5
C. 2 D. None of the above
A
6. Study the following 'C' program
void main()
{
static a,b;
while(a > b++)
}
What will be the value of a and b?
A. a = 0, b = 1 B. a = 0, b = 0
C. a = 1, b = 1 D. None of these
Ans A
7. Consider the following program fragment
for(digit = 0;digit < 9; digit++)
{
digit = digit *2;
digit--;
}
How many times the loop will be executed?
A. Infinite B. 9
C. 4 D. 0
Ans. A
View Answer & Explanation
8. An if statement may contain compound statements only in the else clause.
A. True B. False
Ans. B
9. A 'while' loop may always be converted to an equivalent 'for' loop.
A. True B. False
Ans A
10. 'default' case is mandatory in a switch statement
A. True B. False
Ans B
11. How many times the string is printed?
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf("Welcome\n");
}
return 0;
}
A. Infinite Times B. 11
C. 0 D. 10
Ans C
12. The operator && in 'C' language is a
A. OR Operator B. NOT Operator
C. AND Operator D. None of the above
Ans C
13. Which of the following cannot be checked in a switch-case statement?
A. Character B. Integer
C. Float D. enum
Ans C
14. Can we use a switch statement to switch on strings?
A. True B. False
Ans. B
15. Point out the error, if any in the for loop.
#include<stdio.h>
int main()
{
int i=1;
for(;;)
{
printf("%d\n", i++);
if(i>10)
break;
}
return 0;
}
A. There should be a condition in the for loop B. The two semicolons should be dropped
C. The for loop should be replaced with while loop. D. No error
Ans. D
16. Which of the following errors would be reported by the compiler on compiling the program
given below?
#include<stdio.h>
int main()
{
int a = 5;
switch(a)
{
case 1:
printf("First");
case 2:
printf("Second");
case 3 + 2:
printf("Third");
case 5:
printf("Final");
break;
}
return 0;
}
A. There is no break statement in each case. B. Expression as in case 3 + 2 is not
allowed.
C. Duplicate case case 5: D. No error will be reported.
Ans. C
17. Point out the error, if any in the program.
#include<stdio.h>
int main()
{
int a = 10, b;
a >=5 ? b=100: b=200;
printf("%d\n", b);
return 0;
}
A. 100 B. 200
C. Error: L value required for b D. Garbage value
Ans. C
18. What will be the output of the program?
#include<stdio.h>
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d", i);
return 0;
}
A. 0, 1, 2, 3, 4, 5 B. 5
C. 1, 2, 3, 4 D. 6
Ans. D
19. What will be the output of the program?
#include<stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d\n", b, c);
return 0;
}
A. b = 300 c = 200 B. b = 100 c = garbage
A. b = 300 c = garbage B. b = 100 c = 200
Ans. D
20. What will be the output of the program?
#include<stdio.h>
int main()
{
int k, num = 30;
k = (num < 10) ? 100 : 200;
printf("%d\n", num);
return 0;
}
A. 200 B. 30
C. 100 D. 500
Ans. B
main()
{
int a,b;
printf(\"%d\",scanf(\"%d%d\",&a,&b)); // values 10,20 are given as inputs here
}
Answers:
a.2
b.1
c.20,10
d.10,20
Ans a
What will be the output of this program?
void main()
{
int const * p=5;
printf("%d",++(*p));
}
1) 6
2) 7
3) Comiler Error: Cannot Modify a Constant Value
4) Linker Error
Ans. 3
What should be the output of the following?
void pascal f(int i,int j,int k)
{
printf(%d %d %d,i, j, k);
}
void cdecl f(int i,int j,int k)
{
printf(%d %d %d,i, j, k);
}
main()
{
int i=10;
f(i++,i++,i++);
printf(\" %d\\n\",i);
i=10;
f(i++,i++,i++);
printf(\" %d\",i);
}
a)10 11 12 13
12 11 10 13
b)10 11 12 13
11 12 10 13
c)10 12 11 13
12 11 10 13
d)10 11 13 12
12 10 11 13
Ans a
What is the output for the program given below
typedef enum errorType{warning, error, exception,}error;
main()
{
error g1;
g1=1;
printf(\"%d\",g1);
}
a)Compiler error: Multiple declaration for error
b)Garbage error
c)No error
d)0
Ans. a
What is the output of the following?
main()
{
int i;
i = 64/square(4);
printf(\"%d\",i);
}
a) 16
b) 4
c) 64
d) error
Ans c
What is the output of the following?
main()
{
struct xx
{
int x=3;
char name[]=\"hello\";
};
struct xx *s;
printf(\"%d\",s->x);
printf(\"%s\",s->name);
}
a) Compiler Error
b) hello
c) 3
d) 0
Ans. a