Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
376 views

C MCQ Final Test

The document provides 5 multiple choice questions related to C programming. It tests knowledge of pointers, arrays, structures, unions and memory allocation. The summary is: 1. The document contains 5 multiple choice questions about various C programming concepts like pointers, arrays, structures, unions and memory allocation. 2. The questions test knowledge of topics like pointer arithmetic, accessing array elements using pointers, calculating size of structures and unions, and functions for dynamic memory allocation. 3. The answers to the questions are also provided to help learners self-evaluate their understanding of these core C programming concepts.

Uploaded by

Deepak Malusare
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
376 views

C MCQ Final Test

The document provides 5 multiple choice questions related to C programming. It tests knowledge of pointers, arrays, structures, unions and memory allocation. The summary is: 1. The document contains 5 multiple choice questions about various C programming concepts like pointers, arrays, structures, unions and memory allocation. 2. The questions test knowledge of topics like pointer arithmetic, accessing array elements using pointers, calculating size of structures and unions, and functions for dynamic memory allocation. 3. The answers to the questions are also provided to help learners self-evaluate their understanding of these core C programming concepts.

Uploaded by

Deepak Malusare
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 19

http://www.gkseries.

com/computer-engineering/c-
programming/introduction-to-programming/1-download-pdf

1. Consider the following program:


main()
{
char *x="xyz;
f(k);
printf("%s\n",k);
}
f(char *k)
{
k=malloc(4);
strcpy(k,"pq");
}
What will be the output?
[A]
pq
[B]
xyz
[C]
syntax error
[D]
none of these
Answer & Explanation
Answer
: Option [C]
There is an opening quote in the third statement but no closing. So syntax
error occurs.

2. What does the following function print?


func(int i)
{if(i%2) return 0;
else return 1;}
main()
{
int i=3;
i=func(i);
i=func(i);
printf("%d", i);
}
[A]
3
[B]
1
[C]
0
[D]
2
Answer & Explanation
Answer
: Option [B]

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]

Memory Allocation - Objective type


Interview question with
answers on C Programming
1.
With every use of memory allocation function, what function should be
used to release allocated memory which is
no longer needed?
[A]
dropmem()
[B]
dealloc()
[C]
release()
[D]
free()
Answer & Explanation
Answer
: Option [D]
The library function free() is used to deallocate the memory that is no
longer be used. This deallocated memory is
dynamically allocated by malloc, calloc and realloc previously.
2.
Physically placing the machine instructions and data into main memory is
done by
[A]
Linker
[B]
Loader
[C]
Code Generator
[D]
Interpreter
Answer & Explanation
Answer
: Option [B]
3.
Which header file should be included to use function like malloc() and
calloc()?
[A]
memory.h
[B]
stdlib.h
[C]
string.h
[D]
dos.h
Answer & Explanation
Answer
: Option [B]
The stdlib.h header file contains malloc(), calloc() and realloc() dynamically
allocated array functions.
4.
How will you free the allocated memory?
[A]
remove(variable-name);
[B]
free(variable-name);
[C]
delete(variable-name);
[D]
dealloc(variable-name);
Answer & Explanation
Answer
: Option [B]
5.
Which data structure is used by malloc() for object creation?
[A]
Heap
[B]
Tree
[C]
Stack
[D]
Queue
Answer & Explanation
Answer
: Option [A]

Structures and Unions - C programming


question bank with
answers in multiple choice type
1.
Size of the following union (assume size of int=2, size of float=4 and size
of char=1);
union ABC
{
int a;
float b;
char c;
};
[A]
2
[B]
4
[C]
1
[D]
7
Answer & Explanation
Answer
: Option [D]
union is a data type in which all the members are stored in the same
location. The memory size of union is equal
to the memory size of the highest member variable. Each members of
union can be accessed one by one. In this
case size of float is 4. So the size of the union is 4.
2.
What is wrong with the following code?
struct Person{
char *name;
struct Person Mother, Father;
}Anita;
[A]
The ; should appear after the } and Anita be defined later
[B]
name should be defined as an array
[C]
struct Person Mother, Father; must be defined as struct Person *Mother,
*Father;
[D]
There is no error in the code
Answer & Explanation
Answer
: Option [C]
struct is new data type which contains many different types of member
variables under a single name.
3.
What will be the output of the following code?
struct abc{int a; int b;} v[3], *p;
main()
{
p=v;
p->a=3;
p->b=p->a;
printf("\n%d\t%d", v[0].a, v[0].b);
}
[A]
34
[B]
43
[C]
Any garbage value
[D]
33
Answer & Explanation
Answer
: Option [D]
4.
What will be the output of the following
program?
main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e={"Tiger"};
printf("\n%d%f"),e.age, e.sal;
}
[A]
0 0.000000
[B]
Garbage values
[C]
Error
[D]
None of the above
Answer & Explanation
Answer
: Option [A]
5.
What will be the output of the following
code?
struct
{
int a;
double d;
float cp;
}s;
void main()
{
printf("%d\t%d\t%d\t%d",
sizeof(s.a), sizeof(s.d),
sizeof(s.cp), sizeof(s));
}
[A]
4, 8, 4, 24
[B]
8, 2, 4, 12
[C]
10, 4, 6, 4
[D]
4, 8, 4, 14
Answer & Explanation
Answer
: Option [A]

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.

Find the error in the following program:


main()
{
int m;
char g;
switch(m)
{
case 3: grade="P";break;
case 2: grade="Q";break;
case 1: grade="R";break;
default: grade="S";break;
}
}
What will be the output of the program?
[A]
Undefined symbol "grade"
[B]
switch statement cannot have more than three labels
[C]
case label cannot be numbers
[D]
none of these
Answer & Explanation
Answer
: Option [A]
3.
Data type of the controlling statement of
a SWITCH statement cannot of the
type:
[A]
int
[B]
char
[C]
short
[D]
float
Answer & Explanation
Answer
: Option [D]
The SWITCH statement is a conditional statement. Using this statement we
handle number of cases
simultaneously. Float data type is not allowed in SWITCH statement.
4.
How long the following loop runs?
for(x=0;x=3;x++)
[A]
Three times
[B]
Four times
[C]
Forever
[D]
Never
Answer & Explanation
Answer
: Option [D]
The first statement of a for loop is initialize the loop counter, second is
conditional statment and the third one is
increment/decrement of the loop counter.
In the given expression the second statement is an assignment statement
instead of condition.
So the for loop never execute.
5.
The CONTINUE statemment cannot be used with
[A]
for
[B]
switch
[C]
do
[D]
while
Answer & Explanation
Answer
: Option [B]
The CONTINUE Keyword skips the code and immediately passes control to
the beginning of the statement for
next iteration.

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

Choose the option that contains only valid hexadecimal integers.


[A]
0x9F, 0xbcd, 0x1
[B]
037, 00x, 01000
[C]
0x561u, 0x9h, 0xdd
[D]
H9F, HFF, HAA
Answer & Explanation
Answer
: Option [D]
The base of Hexadecimal number system is 16. It contains 0 to 9 and
A,B,C.D,E,F to represent zero to fifteen.
42.
Which of the following is not valid variable names in 'C'?
[A]
float_int, keyword, A1
[B]
ANSI, ascii, cpu
[C]
valid, variable, name
[D]
None of these
Answer & Explanation
Answer
: Option [D]
43.
The result of a relational operator is always
[A]
either True or False
[B]
is less than or is more than
[C]
is equal or less or more
[D]
All of these
Answer & Explanation
Answer
: Option [A]
The relational operators in 'C' are ==, !=, >, <, >=, <=. They always
results in either True or False.
44.
Which of the following cannot be used as identifiers?
[A]
Letters
[B]
Digits
[C]
Underscores
[D]
Spaces
Answer & Explanation
Answer
: Option [D]
Variable name, function name, structure name etc. are called identifiers.
Spaces are not used in names.
45.
What is the return type of ftell function?
[A]
double
[B]
int
[C]
long
[D]
float
Answer & Explanation
Answer
: Option [C]

You might also like