C MCQ Final Test
C MCQ Final Test
com/computer-engineering/c-
programming/introduction-to-programming/1-download-pdf
3.
What is wrong with the following
function?
int Main(int ac, char *av[])
{
if(ac==0) return 0;
else
{
printf("%s", av[ac-1]);
Main(ac-1, av);
}
return 0;
}
[A]
Function cannot have name as Main, it should be main only
[B]
The arguments' name must be argc and argv, respectively
[C]
There cannot be two return statements in the function
[D]
There error in the function
Answer & Explanation
Answer
: Option [D]
There is no error in the function. Here the Main() function differenciate with
the main(). In the given problem the
Main() has two arguments as int ac, char *av[]
4.
What is the following function determining?
int fn(int a, int b)
{
if (b==0) return 0;
if (b==1) return a;
return a+fn(a, b-1);
}
[A]
a+b where a and b are integers
[B]
a+b where a and b are non-negative integers
[C]
a*b where a and b are integers
[D]
a*b where a and b are non-negative integers
Answer & Explanation
Answer
: Option [B]
The above function is a recursive function. The function will return a+b
where a and b are non-negative integers
5.
What is the output of the following code?
main()
{
int a=1, b=10;
swap(a,b);
printf("\n%d%d", a,b);
}
swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
[A]
11
[B]
1 10
[C]
10 1
[D]
None of these
Answer & Explanation
Answer
: Option [B]
The 'call by value' method is applied in this program. Here the data is
passed by value in the main(). So the
variables are not changed.
1.
Regarding the scope of the variables identify the incorrect statement:
[A]
Automatic variables are automatically initialized to 0
[B]
Static variables are automatically initialized to 0
[C]
The address of a register variable is not accessible
[D]
Static variables cannot be initialized with any expression
Answer & Explanation
Answer
: Option [A]
By default Automatic variables are initialized to Garbage value.
2.
What will be the output of the following code segment?
void fn()
{
static int i=10;
printf("%d",++i);
}
main()
{
fn();
fn();
}
[A]
10 10
[B]
11 11
[C]
11 12
[D]
12 12
Answer & Explanation
Answer
: Option [C]
3.
Which of the following is not a proper
storage class in 'C'?
[A]
auto
[B]
dec
[C]
static
[D]
extern
Answer & Explanation
Answer
: Option [B]
The storage classes in C are auto,
extern, static and global. dec is not a
storage class.
4.
What is the output of the following code?
main()
{
static int num=8;
printf("%d",num=num-2);
if(num!=0)
main();
}
[A]
8642
[B]
Infinite output
[C]
6420
[D]
invalid because main function cannot call itself
Answer & Explanation
Answer
: Option [C]
5.
Value of static storage variable
[A]
changes during different function
calls
[B]
persists between different function
calls
[C]
increases during different function
calls
[D]
decreases during different function
calls
Answer & Explanation
Answer
: Option [B]
1.
Given the following code fragment:
main()
{
int row[20],i,sum=0;
int *p=row;
for(i=0;i<20;i++)
*(p+i)=1;
for(i=0;i<20;i+=sizeof(int))
sum+=*(p+i);
printf("sum=%d\n",sum);
}
What will be the result of execution?
[A]
sum=10
[B]
sum=40
[C]
sum=60
[D]
sum=190
Answer & Explanation
Answer
: Option [A]
2.
What is the missing statement in the following function which copies string
x into string y?
void strcpy (char *x, char *y)
{
while (*y!='\0')
................./*missing statement*/
*x='\0';
}
What will be the result of execution?
[A]
x=y
[B]
*x++=*y++
[C]
(*x)++=(*y)++
[D]
none of these
Answer & Explanation
Answer
: Option [B]
Pointer variable char *x is pointing to a location and the char *y is
assigned to that location. If we assume the
missing statement is *x++=*y++ then both the variables point to the
next respective location till null ('\0') found.
3.
char *ptr;
char myString[]="abcdefg";
ptr=myString
ptr+=5;
The pointer ptr points to which string?
[A]
fg
[B]
efg
[C]
defg
[D]
cdefg
Answer & Explanation
Answer
: Option [A]
ptr+=5 means ptr=ptr+5
That means the pointer variable is incremented by 5. Hence it is pointing
to the 6th location. i.e. fg
4.
Output of the following program will be
main()
{
int a[]={1,2,9,8,6,3,5,7,8,9};
int *p=a+1;
int *q=a+6;
printf("\n%d",q-p);
}
[A]
9
[B]
5
[C]
2
[D]
None of these
Answer & Explanation
Answer
: Option [B]
5.
Given float *pf; int *pi; Which of the
following is true?
[A]
sizeof(pf) > sizeof(pi)
[B]
sizeof(pi) < sizeof(pf)
[C]
sizeof(pf) == sizeof(pi)
[D]
None of these above
Answer & Explanation
Answer
: Option [C]
2.
Given the piece of code
int a[50];
int *pa;
pa=a;
To access the 6th element of the array which of the following is incorrect?
[A]
*(a+5)
[B]
a[5]
[C]
pa[5]
[D]
*(*pa+5)
Answer & Explanation
Answer
: Option [D]
3.
What will be the output of the following code segment?
int a[10]={1,2,3,4,5,6,7,8,9,10};
*p=a;
printf("\n%d:%d", p[7], p[a[7]]);
[A]
7:7
[B]
7:8
[C]
8:9
[D]
8:8
Answer & Explanation
Answer
: Option [C]
The first element of the array i.e. a[0] is assigned by *p=a. Therefore
a[0]=1. Then p[7]=8 and p[a[7]]=p[8]=9
Hence 8:9
4.
What is the effect of the following code?
main()
{
int a[4]={1,5};
printf("%d",a[3]);
}
[A]
0
[B]
Syntax error because of improper
initialization
[C]
5
[D]
Syntax error because of invalid
index
Answer & Explanation
Answer
: Option [A]
Given that int a[4]={1,5}
So a[2], a[3] etc. are 0
5.
For the following definition, which of the given option is correct?
int a[10];
[A]
a++;
[B]
a=a+1
[C]
*a++
[D]
*a[1]
Answer & Explanation
Answer
: Option [C]
*a+0 points to the a[0] location.
1.
Given in the following code segment:
void main(void)
{
char x='\0';
char n='N';
printf("%u""%s\n",&n, &n);
}
What will be the result of execution?
[A]
ddddd N(where d represents any digit)
[B]
78 N
[C]
78 garbage
[D]
compilation error
Answer & Explanation
Answer
: Option [D]
2.
Given in the following program:
main()
{
int x=49;
for (;x;)
x--;
printf("%d\n",x);
}
What will be the output of the program?
[A]
49
[B]
0
[C]
-49
[D]
none of these
Answer & Explanation
Answer
: Option [B]
For all non-zero values of x the for will execute. x is decremented from 49
to 0. Hence 0 is printed in the screen.
3.
Given in the following program:
main()
{
int x=3, y=2;
dp(x/y)
}
What will be the output of the program?
[A]
prints x/y=1
[B]
prints #e=1.5
[C]
prints #x/y=1
[D]
none of these
Answer & Explanation
Answer
: Option [A]
4.
In the following code segment:
int z,x=5,y=-10,a=4,b=2;
z=x++ - --y* b/a;
What will be the final value of z?
[A]
5
[B]
6
[C]
10
[D]
11
Answer & Explanation
Answer
: Option [C]
The post increment operator is associated with the variable x. So the
current variable is considered i.e. x=5.
The pre increment operator is associated with the variable y. So the value
of y decreses by 1. That is y=-11.
Multiplication operator and division operator has the same precedence. So
the first one is executed first.
So the expression is:
z=5-(-11)*2/4
=5-(-22/4)
=5-(-5)
=10
5.
Suppose that x is initialized as:
short int x; /*assume x is 16 bits in size*/
What is maximum number that can be printed using
printf("%d\n",x);
[A]
127
[B]
128
[C]
255
[D]
32767
Answer & Explanation
Answer
: Option [D]
By default short integer is signed. 1 bit is reserveed for sign i.e. only 15
bits are available.
So, the largest number will be:
2
15
+2
14
+..........+upto 2
0
i.e. 32767