Nothing Special   »   [go: up one dir, main page]

C Programming Questions and Answers - Pointers and Addresses

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 71

C Programming Questions and Answers

Pointers and Addresses


1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}

a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
View Answer
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%d\n", (int)*p);
return 0;
}

a) Compile time error


b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
View Answer
Answer:a
3. What is the output of this C code?

1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 10;
void *p = &i;
printf("%f\n", *(float*)p);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) 10
d) 0.000000
View Answer
Answer:d
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int *j = (int*)malloc(sizeof(int));
*j = 10;
return j;
}

a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{

10.
11.
12.

int j = 10;
return &j;
}

a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
View Answer
Answer:a
6. Comment on the following pointer declaration?
int *ptr, p;
a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}

a) 10,10
b) 10,11
c) 11,10
d) 11,11
View Answer
Answer:d
8. Comment on the following?
const int *ptr;
a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) Both (a) and (b)
d) You can change the pointer as well as the value pointed by it
View Answer

Answer:a
1. Which is an indirection operator among the following?
a) &
b) *
c) ->
d) .
View Answer
Answer:b
2. Which of the following does not initialize ptr to null (assuming variable declaration of
a as int a=0;?
a) int *ptr = &a;
b) int *ptr = &a &a;
c) int *ptr = a a;
d) All of the mentioned
View Answer
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}

a) Same address
b) Different address
c) Compile time error
d) Varies
View Answer
Answer:a
4. What is the output of this C code?
1.
2.
3.
4.
5.

#include <stdio.h>
int x = 0;
void main()
{
int *const ptr = &x;

6.
7.
8.
9.

printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);

a) 0 1
b) Compile time error
c) 0xbfd605e8 0xbfd605ec
d) 0xbfd605e8 0xbfd605e8
View Answer
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}

a) 0xbfd605e8 0xbfd605ec
b) 0xbfd605e8 0cbfd60520
c) 0xbfd605e8 0xbfd605e9
d) Run time error
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}

a) 5
b) Address of 5
c) Nothing
d) Compile time error
View Answer
Answer:d

7. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int x = 0;
int *ptr = &x;
printf("%d\n", *ptr);
}

a) Address of x
b) Junk value
c) 0
d) Run time error
View Answer
Answer:c

C Programming Questions and Answers


Pointers and Function Arguments
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void foo(int*);
int main()
{
int i = 10;
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
View Answer
Answer:c
2. What is the output of this C code?
1.

#include <stdio.h>

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault
View Answer
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}

a) 10.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
View Answer
Answer:b
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&i);
printf("%d ", *p);
}
void foo(int *p)
{

10.
11.
12.
13.

int j = 2;
p = &j;
printf("%d ", *p);

a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
int i = 97, *p = &i;
foo(&p);
printf("%d ", *p);
return 0;
}
void foo(int **p)
{
int j = 2;
*p = &j;
printf("%d ", **p);
}

a) 2 2
b) 2 97
c) Undefined behaviour
d) Segmentation fault/code crash
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{
int j = 10;
*p = &j;

13.
14.

printf("%d ", **p);

a) Compile time error


b) 10 10
c) Undefined behaviour
d) 10 11
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

#include <stdio.h>
int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p);
printf("%d ", *p);
}
void foo(int **const p)
{
int j = 11;
*p = &j;
printf("%d ", **p);
}

a) 11 11 11
b) 11 11 Undefined-value
c) Compile time error
d) Segmentation fault/code-crash
View Answer
Answer:b
8. What is the output of the code below?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
int main()
{
int i = 10;
int *const p = &i;
foo(&p);
printf("%d\n", *p);
}
void foo(int **p)
{
int j = 11;
*p = &j;
printf("%d\n", **p);

14.

a) 11 11
b) Undefined behaviour
c) Compile time error
d) Segmentation fault/code-crash
View Answer
Answer:a
9. Which of the following are correct syntaxes to send an array as a parameter to
function:
a) func(&array);
b) func(array);
c) func(*array);
d) func(array[size]);
View Answer
Answer:a & b
1. Which of the following can never be sent by call-by-value?
a) Variable
b) Array
c) Structures
d) Both (b) and (c)
View Answer
Answer:b
2. Which type of variables can have same name in different function:
a) global variables
b) static variables
c) Function arguments
d) Both (b) and (c)
View Answer
Answer:d
3. Arguments that take input by user before running a program are called?
a) main function arguments
b) main arguments
c) Command-Line arguments
d) Parameterized arguments
View Answer

Answer:c
4. The maximum number of arguments that can be passed in a single function
are_____________
a) 127
b) 253
c) 361
d) No limits in number of arguments
View Answer
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void m(int *p, int *q)
{
int temp = *p; *p = *q; *q = temp;
}
void main()
{
int a = 6, b = 5;
m(&a, &b);
printf("%d %d\n", a, b);
}

a) 5 6
b) 6 5
c) 5 5
d) 6 6
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void m(int *p)
{
int i = 0;
for(i = 0;i < 5; i++)
printf("%d\t", p[i]);
}
void main()
{
int a[5] = {6, 5, 3};
m(&a);
}

a) 0 0 0 0 0
b) 6 5 3 0 0
c) Run time error
d) 6 5 3 junk junk
View Answer
Answer:b
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

#include <stdio.h>
void m(int p, int q)
{
int temp = p;
p = q;
q = temp;
}
void main()
{
int a = 6, b = 5;
m(a, b);
printf("%d %d\n", a, b);
}

a) 5 6
b) 5 5
c) 6 5
d) 6 6
View Answer
Answer:c
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
void m(int p, int q)
{
printf("%d %d\n", p, q);
}
void main()
{
int a = 6, b = 5;
m(a);
}

a) 6
b) 6 5
c) 6 junk value
d) Compile time error
View Answer

Answer:d
9. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void m(int p)
{
printf("%d\n", p);
}
void main()
{
int a = 6, b = 5;
m(a, b);
printf("%d %d\n", a, b);
}

a) 6
b) 6 5
c) 6 junk value
d) Compile time error
View Answer
Answer:d

C Programming Questions and Answers


Pointers and Arrays
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
printf("%p\t%p", p, a);
}

a) Same address is printed.


b) Different address is printed.
c) Compile time error
d) Nothing
View Answer
Answer:a
2. What is the output of this C code?

1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%p\t%p", p, s);
}

a) Different address is printed


b) Same address is printed
c) Run time error
d) Nothing
View Answer
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%c\t%c", p[0], s[1]);
}

a) Run time error


b) h h
c) h e
d) h l
View Answer
Answer:c
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%c\t%c", *(p + 3),
}

a) h e
b) l l
c) l o
d) l e
View Answer

s[1]);

Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%c\t%c", 1[p], s[1]);
}

a) h h
b) Run time error
c) l l
d) e e
View Answer
Answer:d
6. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void foo( int[] );
int main()
{
int ary[4] = {1, 2, 3, 4};
foo(ary);
printf("%d ", ary[0]);
}
void foo(int p[4])
{
int i = 10;
p = &i;
printf("%d ", p[0]);
}

a) 10 10
b) Compile time error
c) 10 1
d) Undefined behaviour
View Answer
Answer:c
7. What is the output of the code given below?
1.
2.
3.
4.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};

5.
6.
7.

int *p = ary + 3;
printf("%d\n", p[-2]);
}

a) 1
b) 2
c) Compile time error
d) Some garbage value
View Answer
Answer:b
8. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p = ary + 3;
printf("%d %d\n", p[-2], ary[*p]);
}

a) 2 3
b) Compile time error
c) 2 4
d) 2 somegarbagevalue
View Answer
Answer:d
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
printf("%d\n", *ary);
}

a) 1
b) Compile time error
c) Some garbage value
d) Undefined variable
View Answer
Answer:a
2. What is the output of this C code?

1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
const int ary[4] = {1, 2, 3, 4};
int *p;
p = ary + 3;
*p = 5;
printf("%d\n", ary[3]);
}

a) 4
b) 5
c) Compile time error
d) 3
View Answer
Answer:b
3. Different ways to initialize an array with all elements as zero are
a) int array[5] = {};
b) int array[5] = {0};
c) int a = 0, b = 0, c = 0;
int array[5] = {a, b, c};
d) All of the mentioned
View Answer
Answer:d
4. The elements in the array of the following code are
int array[5] = {5};
a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5
View Answer
Answer:b
5. Which of the following declaration is illegal?
a) int a = 0, b = 1, c = 2;
int array[3] = {a, b, c};
b) int size = 3;
int array[size];
c) int size = 3;
int array[size] = {1, 2, 3};
d) All of the mentioned
View Answer

Answer:c
6. An array of similar data types which themselves are collection of dissimilar data type
are
a) Linked Lists
b) Trees
c) Array of Structure
d) All of the mentioned
View Answer
Answer:c
7. Comment on an array of void data type:
a) It can store any data-type
b) It only stores element of similar data type to first element
c) It acquires the data type with the highest precision in it
d) You cannot have an array of void data type
View Answer
Answer:d
8. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int p[4];
p = ary;
printf("%d\n", p[1]);
}

a) 1
b) Compile time error
c) Undefined behaviour
d) 2
View Answer
Answer:b

C Programming Questions and Answers


Address Arithmetic

1. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
double *ptr = (double *)100;
ptr = ptr + 2;
printf("%u", ptr);
}

a) 102
b) 104
c) 108
d) 116
View Answer
Answer:d
2. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int *p = (int *)2;
int *q = (int *)3;
printf("%d", p + q);
}

a) 2
b) 3
c) 5
d) Compile time error
View Answer
Answer:d
3. Which of the following operand can be applied to pointers p and q?
(Assuming initialization as int *a = (int *)2; int *b = (int *)3;)
a) a + b
b) a b
c) a * b
d) a / b
View Answer
Answer:b
4. What is the size of *ptr in a 32-bit machine, (assuming initialization as int *ptr = 10;)?
a) 1
b) 2

c) 4
d) 8
View Answer
Answer:c
5. Which of following logical operation can be applied to pointers?
(Assuming initialization int *a = 2; int *b = 3;)
a) a | b
b) a ^ b
c) a & b
d) None of the mentioned
View Answer
Answer:d
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *(p + 1), s[1]);
}

a) h e
b) e l
c) h h
d) e e
View Answer
Answer:d
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s;
printf("%c\t%c", *p, s[1]);
}

a) e h
b) Compile time error
c) h h
d) h e
View Answer

Answer:d
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
char *s = "hello";
char *n = "cjn";
char *p = s + n;
printf("%c\t%c", *p, s[1]);
}

a) h e
b) Compile time error
c) c o
d) h n
View Answer
Answer:b
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s = "hello";
char *p = s * 3;
printf("%c\t%c", *p, s[1]);
}

a) h e
b) l e
c) Compile time error
d) l h
View Answer
Answer:c
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s + 2;
printf("%c\t%c", *p, s[1]);
}

a) l e
b) h e
c) l l
d) h l
View Answer
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
void *p;
int a[4] = {1, 2, 3, 8};
p = &a[3];
int *ptr = &a[2];
int n = p - ptr;
printf("%d\n", n);
}

a) 1
b) Compile time error
c) Segmentation fault
d) 4
View Answer
Answer:b
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
void *p;
int a[4] = {1, 2, 3, 4};
p = &a[3];
int *ptr = &a[2];
int n = (int*)p - ptr;
printf("%d\n", n);
}

a) 1
b) Compile time error
c) Segmentation fault
d) 4
View Answer
Answer:a

5. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int b[4] = {1, 2, 3, 4};
int n = &b[3] - &a[2];
printf("%d\n", n);
}

a) -3
b) 5
c) 4
d) Compile time error
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *p = &a[1];
int *ptr = &a[2];
ptr = ptr * 1;
printf("%d\n", *ptr);
}

a) 2
b) 1
c) Compile time error
d) Undefined behaviour
View Answer
Answer:c
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
int *ptr = &a[2];
float n = 1;
ptr = ptr + n;
printf("%d\n", *ptr);
}

a) 4
b) 3
c) Compile time error
d) Undefined behaviour
View Answer
Answer:c
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
int a[4] = {1, 2, 3, 4};
void *p = &a[1];
void *ptr = &a[2];
int n = 1;
n = ptr - p;
printf("%d\n", n);
}

a) 1
b) 4
c) Compile time error
d) Depends on the compiler
View Answer
Answer:b

C Programming Questions and Answers


Character Pointers and Functions
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
char *str = "hello, world\n";
char *strc = "good morning\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}

a) hello, world
b) Crash/segmentation fault

c) Undefined behaviour
d) Run time error
View Answer
Answer:b
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
char *str = "hello world";
char strc[] = "good morning india\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}

a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
View Answer
Answer:a
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
char *str = "hello, world!!\n";
char strc[] = "good morning\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}

a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
View Answer
Answer:c
4. What is the output of this C code?
1.
2.

#include <stdio.h>
int main()

3.
4.
5.
6.
7.
8.

char *str = "hello, world\n";


str[5] = '.';
printf("%s\n", str);
return 0;

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
View Answer
Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
char str[] = "hello, world";
str[5] = '.';
printf("%s\n", str);
return 0;
}

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
char *str = "hello world";
char strary[] = "hello world";
printf("%d %d\n", sizeof(str), sizeof(strary));
return 0;
}

a) 11 11
b) 12 12
c) 4 12
d) 4 11
View Answer

Answer:c
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
char *str = "hello world";
char strary[] = "hello world";
printf("%d %d\n", strlen(str), strlen(strary));
return 0;
}

a) 11 11
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.
View Answer
Answer:a
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void f(char *k)
{
k++;
k[2] = 'm';
printf("%c\n", *k);
}
void main()
{
char s[] = "hello";
f(s);
}

a) l
b) e
c) h
d) o
View Answer
Answer:b
9. What is the output of this C code?
1.
2.
3.
4.
5.

#include <stdio.h>
void fun(char *k)
{
printf("%s", k);
}

6.
7.
8.
9.
10.

void main()
{
char s[] = "hello";
fun(s);
}

a) hello
b) Run time error
c) Nothing
d) h
View Answer
Answer:a
1. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
char *str = "This" //Line 1
char *ptr = "Program\n"; //Line 2
str = ptr; //Line 3
printf("%s, %s\n", str, ptr); //Line 4
}

a) Memory holding this is cleared at line 3


b) Memory holding this loses its reference at line 3
c) You cannot assign pointer like in Line 3
d) Output will be This, Program
View Answer
Answer:b
2. What type initialization is needed for the segment ptr[3] = 3; to work?
a) char *ptr = Hello!;
b) char ptr[] = Hello!;
c) Both (a) and (b)
d) None of the mentioned
View Answer
Answer:b
3. The syntax for constant pointer to address (i.e., fixed pointer address) is:
a)
b)
c)
d)

const <type> * <name>


<type> * const <name>
<type> const * <name>
Both (a) and (c)

View Answer
Answer:b
4. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int (*fn_ptr)(int, int);
fn_ptr = add;
printf("The sum of two numbers is: %d", (int)fn_ptr(2,
3));

11.

a) Compile time error, declaration of a function inside main.


b) Compile time error, no definition of function fn_ptr.
c) Compile time error, illegal application of statement fn_ptr = add.
d) No Run time error, output is 5.
View Answer
Answer:d
5. The correct way to declare and assign a function pointer is done by:
(Assuming the function to be assigned is int multi(int, int);)
a) int (*fn_ptr)(int, int) = multi;
b) int *fn_ptr(int, int) = multi;
c) int *fn_ptr(int, int) = &multi;
d) Both (b) & (c)
View Answer
Answer:a
6. Calling a function f with a an array variable a[3] where a is an array, is equivalent to
a) f(a[3])
b) f(*(a + 3))
c) f(3[a])
d) All of the mentioned
View Answer
Answer:d
7. What is the output of this C code?
1.
2.

#include <stdio.h>
void f(char *k)

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

k++;
k[2] = 'm';

}
void main()
{
char s[] = "hello";
f(s);
printf("%c\n", *s);
}

a) h
b) e
c) m
d) o;
View Answer
Answer:a
8.What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
void main()
{
char s[] = "hello";
s++;
printf("%c\n", *s);
}

a) Compile time error


b) h
c) e
d) o
View Answer
Answer:a

C Programming Questions and Answers


Pointers to Pointers
1. What is the output of this C code?
1.
2.
3.
4.

#include <stdio.h>
void main()
{
int k = 5;

5.
6.
7.
8.

int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Run time error
View Answer
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Compile time error
View Answer
Answer:d
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
**m = 6;
printf("%d\n", k);
}

a) 5
b) Compile time error
c) 6
d) Junk
View Answer

Answer:c
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int *r = &p;
printf("%d", (**r));
}

a) 1
b) Compile time error
c) Address of a
d) Junk value
View Answer
Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int **r = &p;
printf("%p %p", *r, a);
}

a) Different address is printed


b) 1 2
c) Same address is printed.
d) 1 1
View Answer
Answer:c
6. How many number of pointer (*) does C have against a pointer variable declaration?
a) 7
b) 127
c) 255
d) No limits.
View Answer
Answer:d

7. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a, *ptr2 = &b, *ptr3 = &c;
int **sptr = &ptr1; //-Ref
*sptr = ptr2;
}

a) ptr1 points to a
b) ptr1 points to b
c) sptr points to ptr2
d) None of the mentioned
View Answer
Answer:b
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int **r = &p;
printf("%p %p", *r, a);
}

a) Different address is printed


b) 1 2
c) Same address is printed.
d) 1 1
View Answer
Answer:c
1. What substitution should be made to //-Ref such that ptr1 points to variable C?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int a = 1, b = 2, c = 3;
int *ptr1 = &a;
int **sptr = &ptr1;
//-Ref
}

a) *sptr = &c;
b) **sptr = &c;
c) *ptr1 = &c;
d) None of the mentioned.
View Answer
Answer:a
2. Which of the following declaration throw run-time error?
a) int **c = &c;
b) int **c = &*c;
c) int **c = **c;
d) None of the mentioned
View Answer
Answer:d
3. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
int a = 10;
int **c -= &&a;
}

a) You cannot apply any arithmetic operand to a pointer.


b) We dont have address of an address operator
c) Both (a) and (b)
d) None of the mentioned.
View Answer
Answer:b
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk

d) Compile time error


View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}

a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Compile time error
View Answer
Answer:d
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
**m = 6;
printf("%d\n", k);
}

a) 5
b) Run time error
c) 6
d) Junk
View Answer
Answer:c
7. What is the output of this C code?
1.
2.
3.
4.

#include <stdio.h>
void main()
{
int a[3] = {1, 2, 3};

5.
6.
7.
8.

int *p = a;
int *r = &p;
printf("%d", (**r));

a) 1
b) Compile time error
c) Address of a
d) Junk value
View Answer
Answer:b

C Programming Questions and Answers


Multidimensional Arrays
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
int a[2][3] = {1, 2, 3, 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}

a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
View Answer
Answer:a
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
int a[2][3] = {1, 2, 3, , 4, 5};
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);

9.

a) 1 2 3 junk 4 5
b) Compile time error
c) 1 2 3 0 4 5
d) 1 2 3 3 4 5
View Answer
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void f(int a[][3])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {0};
f(a);
}

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
View Answer
Answer:a
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void f(int a[][])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0;i < 2; i++)
for (j = 0;j < 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {0};
f(a);
}

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
View Answer
Answer:c
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
void f(int a[2][])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0;i < 2; i++)
for (j = 0;j < 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {0};
f(a);
}

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
View Answer
Answer:c
6. Comment on the following statement:
int (*a)[7];
a) An array a of pointers.
b) A pointer a to an array.
c) A ragged array.
d) None of the mentioned
View Answer
Answer:b
7. Comment on the 2 arrays regarding P and Q:
int *a1[8];
int *(a3[8]);
P. Array of pointers
Q. Pointer to an array
a) a1 is P, a2 is Q

b) a1 is P, a2 is P
c) a1 is Q, a2 is P
d) a1 is Q, a2 is Q
View Answer
Answer:b
8. Which of the following is not possible statically in C?
a) Jagged Array
b) Rectangular Array
c) Cuboidal Array
d) Multidimensional Array
View Answer
Answer:a
1. What is the correct syntax to send a 3-dimensional array as a parameter?
(Assuming declaration int a[5][4][3];)
a) func(a);
b) func(&a);
c) func(*a);
d) func(**a);
View Answer
Answer:a
2. Applications of multidimensional array are?
a) Matrix-Multiplication
b) Minimum Spanning Tree
c) Finding connectivity between nodes
d) All of the mentioned
View Answer
Answer:d
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

#include <stdio.h>
int main()
{
int ary[2][3];
foo(ary);
}
void foo(int *ary[])
{
int i = 10, j = 2, k;
ary[0] = &i;

11.
12.
13.
14.
15.

ary[1] = &j;
*ary[0] = 2;
for (k = 0;k < 2; k++)
printf("%d\n", *ary[k]);
}

a) 2 2
b) Compile time error
c) Undefined behaviour
d) 10 2
View Answer
Answer:a
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include <stdio.h>
int main()
{
int ary[2][3];
foo(ary);
}
void foo(int (*ary)[3])
{
int i = 10, j = 2, k;
ary[0] = &i;
ary[1] = &j;
for (k = 0;k < 2; k++)
printf("%d\n", *ary[k]);
}

a) Compile time error


b) 10 2
c) Undefined behaviour
d) segmentation fault/code crash
View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
int main()
{
foo(ary);
}
void foo(int **ary)
{
int i = 10, k = 10, j = 2;
int *ary[2];
ary[0] = &i;
ary[1] = &j;

12.
13.

printf("%d\n", ary[0][1]);

a) 10
b) 2
c) Compile time error
d) Undefined behaviour
View Answer
Answer:d
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int ary[2][3][4], j = 20;
ary[0][0] = &j;
printf("%d\n", *ary[0][0]);
}

a) Compile time error


b) 20
c) Address of j
d) Undefined behaviour
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
int ary[2][3];
ary[][] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", ary[1][0]);
}

a) Compile time error


b) 4
c) 1
d) 2
View Answer
Answer:a

C Programming Questions and Answers


Initialization of Pointer Arrays
1. To declare a 3 dimension array using pointers, which of the following is the correct
syntax:
a) char *a[][];
b) char **a[];
c) char ***a;
d) All of the mentioned
View Answer
Answer:a
2. Comment on the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
char *a = {"p", "r", "o", "g", "r", "a", "m"};
printf("%s", a);
}

a) Output will be program


b) Output will be p
c) No output
d) Compile-time error
View Answer
Answer:b
3. An array of strings can be initialized by:
a) char *a[] = {Hello, World};
b) char *a[] = {Hello, Worlds};
c) char *b = Hello;
char *c = World;
char *a[] = {b, c};
d) All of the mentioned.
View Answer
Answer:d
4. What is the output of this C code?
1.
2.
3.

#include <stdio.h>
void main()
{

4.
5.
6.
7.
8.

char *a[10] = {"hi", "hello", "how"};


int i = 0;
for (i = 0;i < 10; i++)
printf("%s", *(a[i]));
}

a) Segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler
View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
void main()
{
char *a[10] = {"hi", "hello", "how"};
int i = 0, j = 0;
a[0] = "hey";
for (i = 0;i < 10; i++)
printf("%s\n", a[i]);
}

a) hi hello how Segmentation fault


b) hi hello how followed by 7 null values
c) hey hello how Segmentation fault
d) Depends on compiler
View Answer
Answer:c
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
char *a[10] = {"hi", "hello", "how"};
printf("%d\n", sizeof(a));
}

a) 10
b) 13
c) Run time error
d) 40
View Answer
Answer:d

7. What is the output of this C code?


1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
char *a[10] = {"hi", "hello", "how"};
printf("%d\n", sizeof(a[1]));
}

a) 6
b) 4
c) 5
d) 3
View Answer
Answer:b
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
void main()
{
char *a[10] = {"hi", "hello", "how"};
int i = 0;
for (i = 0;i < 10; i++)
printf("%s", a[i]);
}

a) hi hello how Segmentation fault


b) hi hello how null
c) hey hello how Segmentation fault
d) hi hello how followed by 7 nulls
View Answer
Answer:d
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char *p[1] = {"hello"};
printf("%s", (p)[0]);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) hello

d) None of the mentioned


View Answer
Answer:c
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char **p = {"hello", "hi", "bye"};
printf("%s", (p)[0]);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) hello
d) Address of hello
View Answer
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf("%d", (*a)[0]);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) 0
d) Some garbage value
View Answer
Answer:c
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf("%d", *a[0]);

7.
8.

return 0;

a) Compile time error


b) Undefined behaviour
c) 0
d) Some garbage value
View Answer
Answer:c
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.

#include <stdio.h>
int main()
{
int i = 0, j = 1;
int *a[] = {&i, &j};
printf("%d", (*a)[1]);
return 0;
}

a) Compile time error


b) Undefined behaviour
c) 1
d) Some garbage value
View Answer
Answer:d
6. Which of the following are generated from char pointer?
a) char *string = Hello.;
b) char *string;
scanf(%s, string);
c) char string[] = Hello.;
d) Both (a) and (c).
View Answer
Answer:a
7. Which of the following declaration are illegal?
a) int a[][] = {{1, 2, 3}, {2, 3, 4, 5}};
b) int *a[] = {{1, 2, 3}, {2, 3, 4, 5}};
c) int a[4][4] = {{1, 2, 3}, {2, 3, 4, 5}};
d) Both (a) and (b).
View Answer
Answer:a

C Programming Questions and Answers


Pointers Vs. Multi-dimensional Arrays
1. int a[10][20]; which is true for a
a) a is true two-dimensional array
b) 200 int-sized locations have been set aside
c) The conventional rectangular subscript calculation 20 * row + col is used to find the
element a[row, col]
d) All of the mentioned
View Answer
Answer:d
2. int *b[10]; which is true for b
a) The definition only allocates 10 pointers and does not initialize them
b) Initialization must be done explicitly
c) Both a and b
d) Error
View Answer
Answer:c
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
char a[10][5] = {"hi", "hello", "fellows"};
printf("%s", a[2]);
}

a) fellows
b) fellow
c) fello
d) fell
View Answer
Answer:c
4. What is the output of this C code?
1.
2.
3.
4.
5.

#include <stdio.h>
void main()
{
char a[10][5] = {"hi", "hello", "fellows"};
printf("%p\n", a);

6.
7.

printf("%p", a[0]);

a) Same address is printed


b) Different address is printed
c) hello
d) hi hello fello
View Answer
Answer:a
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
void main()
{
char a[10][5] = {"hi", "hello", "fellows"};
printf("%d", sizeof(a[1]));
}

a) 2
b) 4
c) 5
d) 10
View Answer
Answer:c
6. What is the output of the code given below?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char a[1][5] = {"hello"};
printf("%s", a[0]);
return 0;
}

a) Compile time error


b) hello
c) Undefined behaviour
d) hellon
View Answer
Answer:c
7. What is the output of the code given below?
1.

#include <stdio.h>

2.
3.
4.
5.
6.
7.

int main()
{
char *a[1] = {"hello"};
printf("%s", a[0]);
return 0;
}

a) Compile time error


b) hello
c) Undefined behaviour
d) hellon
View Answer
Answer:b
8. Which of the following statements are true?
P. Pointer to Array
Q. Multi-dimensional array
a) P are static, Q are static
b) P are static, Q are dynamic
c) P are dynamic, Q are static
d) P are dynamic, Q are dynamic
View Answer
Answer:c
1. What is the output of this C code (considering sizeof char is 1 and pointer is 4)?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char *a[2] = {"hello", "hi"};
printf("%d", sizeof(a));
return 0;
}

a) 9
b) 4
c) 8
d) 10
View Answer
Answer:c
2. What is the output of this C code?
1.
2.

#include <stdio.h>
int main()

3.
4.
5.
6.
7.

char a[2][6] = {"hello", "hi"};


printf("%d", sizeof(a));
return 0;

a) 9
b) 12
c) 8
d) 10
View Answer
Answer:b
3. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char a[2][6] = {"hello", "hi"};
printf("%s", *a + 1);
return 0;
}

a) hello
b) hi
c) ello
d) ello hi
View Answer
Answer:c
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main()
{
char *a[2] = {"hello", "hi"};
printf("%s", *(a + 1));
return 0;
}

a) hello
b) ello
c) hi
d) ello hi
View Answer
Answer:c

5. Advantage of a multi-dimension array over pointer array.


a) Pre-defined size.
b) Input can be taken from user.
c) Faster Access.
d) All of the mentioned
View Answer
Answer:d
6. Which of the following operation is possible using a pointer char?
(Assuming declaration char *a;)
a) Input via %s
b) Generation of multidimensional array
c) Changing address to point at another location
d) All of the mentioned
View Answer
Answer:c
7. Comment on the following two operations?
int *a[] = {{1, 2, 3}, {1, 2, 3, 4}}; //- 1
int b[4][4] = {{1, 2, 3}, {1, 2, 3, 4}};//- 2
a) 1 will work, 2 will not
b) 1 and 2, both will work
c) 1 wont work, 2 will work
d) Neither of them will work
View Answer
Answer:c
8. Comment on the following two operations?
int *a[] = {{1, 2, 3}, {1, 2, 3, 4}}; //- 1
int b[][] = {{1, 2, 3}, {1, 2, 3, 4}}; //- 2
a) 1 works, 2 doesnt
b) 2 works, 1 doesnt
c) Both of them work
d) Neither of them work
View Answer
Answer:d

C Programming Questions and Answers


Command Line Arguments
1. What does argv and argc indicate in command-line arguments?
(Assuming: int main(int argc, char *argv[]) )
a) argument count, argument variable
b) argument count, argument vector
c) argument control, argument variable
d) argument control, argument vector
View Answer
Answer:b
2. Which of the following syntax is correct for command-line arguments?
a) int main(int var, char *varg[])
b) int main(char *argv[], int argc)
c) int main()
{
int argv, char *argc[];
}
d) Both (a) and (b)
View Answer
Answer:a
3. In linux, argv[0] by command-line argument can be occupied by
a) ./a.out
b) ./test
c) ./fun.out.out
d) All of the mentioned
View Answer
Answer:d
4. What type of array is generally generated in Command-line argument?
a) Single dimension array
b) 2-Dimensional Square Array
c) Jagged Array
d) 2-Dimensional Rectangular Array
View Answer
Answer:c

5. What would be the output if we try to execute following segment of code (assuming
the following input cool brother in city)?
printf(%s\n, argv[argc]);
a) (null)
b) City
c) In
D. Segmentation Fault
View Answer
Answer:a
6. The first argument in command line arguments is
a) The number of command-line arguments the program was invoked with;
b) A pointer to an array of character strings that contain the arguments
c) Nothing
d) Both a & b
View Answer
Answer:a
7. The second (argument vector) in command line arguments is
a) The number of command-line arguments the program was invoked with;
b) A pointer to an array of character strings that contain the arguments,one per string.
c) Nothing
d) Both a & b
View Answer
Answer:b
8. argv[0] in command line arguments, is
a) The name by which the program was invoked
b) The name of the files which are passed to the program
c) Count of the arguments in argv[] vector
d) Both a & b
View Answer
Answer:a
1. A program that has no command line arguments will have argc
a) Zero
b) Negative
c) One
d) Two
View Answer

Answer:c
2. The index of the last argument in command line arguments is
a) argc 2
b) argc + 1
c) argc
d) argc 1
View Answer
Answer:d
3. What is the output of this C code (if run with no options or arguments)?
1.
2.
3.
4.
5.
6.

#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d\n", argc);
return 0;
}

a) 0
b) 1
c) Depends on the platform
d) Depends on the compiler
View Answer
Answer:b
4. What is the output of this C code (run without any commandline arguments)?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main(int argc, char *argv[])
{
while (argc--)
printf("%s\n", argv[argc]);
return 0;
}

a) Compile time error


b) Executablefilename
c) Segmentation fault
d) Undefined
View Answer
Answer:b
5. What is the output of this C code (run without any commandline arguments)?
1.

#include <stdio.h>

2.
3.
4.
5.
6.

int main(int argc, char *argv[])


{
printf("%s\n", argv[argc]);
return 0;
}

a) Segmentation fault/code crash


b) Executable file name
c) Depends on the platform
d) Depends on the compiler
View Answer
Answer:a
6. What is the output of this C code (run without any commandline arguments)?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main(int argc, char *argv[])
{
while (*argv++ != NULL)
printf("%s\n", *argv);
return 0;
}

a) Segmentation fault/code crash


b) Executable file name
c) Depends on the platform
d) Depends on the compiler
View Answer
Answer:a
7. What is the output of this C code (run without any command line arguments)?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main(int argc, char *argv[])
{
while (*argv != NULL)
printf("%s\n", *(argv++));
return 0;
}

a) Segmentation fault/code crash


b) Executable file name
c) Depends on the platform
d) Depends on the compiler
View Answer
Answer:b

8. What is the output of this C code(run without any command line arguments)?
1.
2.
3.
4.
5.
6.
7.

#include <stdio.h>
int main(int argc, char *argv[])
{
while (argv != NULL)
printf("%s\n", *(argv++));
return 0;
}

a) Segmentation fault/code crash


b) Executable file name
c) Depends on the platform
d) Depends on the compiler
View Answer
Answer:a

C Programming Questions and Answers


Pointers to Functions
1. Which function is not called in the following program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.

#include <stdio.h>
void first()
{
printf("first");
}
void second()
{
first();
}
void third()
{
second();
}
void main()
{
void (*ptr)();
ptr = third;
ptr();
}

a) Function first
b) Function second
c) Function third
d) None of the mentioned
View Answer

Answer:d
2. How to call a function without using the function name to send parameters?
a) typedefs
b) Function pointer
c) Both (a) and (b)
d) None of the mentioned
View Answer
Answer:b
3. Correct syntax to pass a Function Pointer as an argument
a) void pass(int (*fptr)(int, float, char)){}
b) void pass(*fptr(int, float, char)){}
c) void pass(int (*fptr)){}
d) void pass(*fptr){}
View Answer
Answer:a
4. Which of the following is not possible in C?
a) Array of function pointer
b) Returning a function pointer
c) Comparison of function pointer
d) None of the mentioned
View Answer
Answer:d
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void first()
{
printf("Hello World");
}
void main()
{
void *ptr() = first;
ptr++
ptr();
}

a) Illegal application of ++ to void data type


b) pointer function initialized like a variable
c) Both (a) and (b)
d) None of the mentioned
View Answer

Answer:c
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int (*function_pointer)(int, int, int);
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}

a) The product of three numbers is:24


b) Run time error
c) Nothing
d) Varies
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int (function_pointer)(int, int, int);
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}

a) The product of three numbers is:24


b) Compile time error
c) Nothing
d) Varies
View Answer
Answer:b
8. What is the output of this C code?
1.

#include <stdio.h>

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

void f(int (*x)(int));


int myfoo(int);
int (*fooptr)(int);
int ((*foo(int)))(int);
int main()
{
fooptr = foo(0);
fooptr(10);
}
int ((*foo(int i)))(int)
{
return myfoo;
}
int myfoo(int i)
{
printf("%d\n", i + 1);
}

a) 10
b) 11
c) Compile time error
d) Undefined behaviour
View Answer
Answer:b
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int mul(int a, int b, int c)
{
return a * b * c;
}
void main()
{
int *function_pointer;
function_pointer = mul;
printf("The product of three numbers is:%d",
function_pointer(2, 3, 4));
}

a) The product of three numbers is:24


b) Compile time error
c) Nothing
d) Varies
View Answer
Answer:b
2. What is the output of this C code?

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
int sub(int a, int b, int c)
{
return a - b - c;
}
void main()
{
int (*function_pointer)(int, int, int);
function_pointer = &sub;
printf("The difference of three numbers is:%d",
(*function_pointer)(2, 3, 4));
}

a) The difference of three numbers is:1


b) Run time error
c) The difference of three numbers is:-5
d) Varies
View Answer
Answer:c
3. One of the uses for function pointers in C is
a) Nothing
b) There are no function pointers in c
c) To invoke a function
d) To call a function defined at run-time.
View Answer
Answer:d
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void f(int);
void (*foo)() = f;
int main(int argc, char *argv[])
{
foo(10);
return 0;
}
void f(int i)
{
printf("%d\n", i);
}

a) Compile time error


b) 10
c) Undefined behaviour
d) None of the mentioned
View Answer

Answer:b
5. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void f(int);
void (*foo)(void) = f;
int main(int argc, char *argv[])
{
foo(10);
return 0;
}
void f(int i)
{
printf("%d\n", i);
}

a) Compile time error


b) 10
c) Undefined behaviour
d) None of the mentioned
View Answer
Answer:a
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

#include <stdio.h>
void f(int);
void (*foo)(float) = f;
int main()
{
foo(10);
}
void f(int i)
{
printf("%d\n", i);
}

a) Compile time error


b) 10
c) 10.000000
d) Undefined behaviour
View Answer
Answer:d
7. What is the output of this C code?
1.
2.

#include <stdio.h>
void f(int (*x)(int));

3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

int myfoo(int i);


int (*foo)(int) = myfoo;
int main()
{
f(foo(10));
}
void f(int (*i)(int))
{
i(11);
}
int myfoo(int i)
{
printf("%d\n", i);
return i;
}

a) Compile time error


b) Undefined behaviour
c) 10 11
d) 10 Segmentation fault
View Answer
Answer:d
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

#include <stdio.h>
void f(int (*x)(int));
int myfoo(int);
int (*foo)() = myfoo;
int main()
{
f(foo);
}
void f(int(*i)(int ))
{
i(11);
}
int myfoo(int i)
{
printf("%d\n", i);
return i;
}

a) 10 11
b) 11
c) 10
d) Undefined behaviour
View Answer
Answer:b

C Programming Questions and Answers


Complicated Declarations
1. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
no = 8;
printf("%d", no);
}

a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer:b
2. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no;
char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

a) Run time error


b) Nothing
c) hello
d) Varies
View Answer
Answer:c

3. What is the output of this C code?


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no = 5;
char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

a) Nothing
b) Compile time error
c) hello
d) Varies
View Answer
Answer:b
4. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no;
char name[20];
};
void main()
{
student s;
s.name = "hello";
printf("hello");
}

a) Nothing
b) hello
c) Compile time error
d) Varies
View Answer
Answer:c
5. What is the output of this C code?
1.
2.
3.

#include <stdio.h>
void main()
{

4.
5.
6.
7.
8.
9.
10.
11.
12.

struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%s", s.name);
}

a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer:c
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no;
char name[20];
};
struct student s;
void main()
{
s.no = 8;
printf("%s", s.name);
}

a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.

#include <stdio.h>
int main()
{
int *((*x)())[2];
x();
printf("after x\n");
}
int *((*x)())[2]
{

10.
11.
12.
13.

int **str;
str = (int*)malloc(sizeof(int)* 2);
return str;

a) Compile time error


b) Undefined behaviour
c) After x
d) None of the mentioned
View Answer
Answer:a
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.

#include <stdio.h>
void (*(f)())(int, float);
void (*(*x)())(int, float) = f;
void ((*y)(int, float));
void foo(int i, float f);
int main()
{
y = x();
y(1, 2);
}
void (*(f)())(int, float)
{
return foo;
}
void foo(int i, float f)
{
printf("%d %f\n", i, f);
}

a) 1 2.000000
b) 1 2
c) Compile time error
d) Segmentation fault/code crash
View Answer
Answer:a
9. What does this declaration say?
int (*(*y)())[2];
a) y is pointer to the function which returns pointer to integer array
b) y is pointer to the function which returns array of pointers
c) y is function which returns function pointer which in turn returns pointer to integer
array
d) y is function which returns array of integers
View Answer

Answer:a
10. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

#include <stdio.h>
void (*(f)())(int, float);
typedef void (*(*x)())(int, float);
void foo(int i, float f);
int main()
{
x = f;
x();
}
void (*(f)())(int, float)
{
return foo;
}
void foo(int i, float f)
{
printf("%d %f\n", i, f);
}

a) Compile time error


b) Undefined behaviour
c) 1 2.000000
d) Nothing
View Answer
Answer:a
11. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.

#include <stdio.h>
void (*(f)())(int, float);
typedef void (*(*x)())(int, float);
void foo(int i, float f);
int main()
{
x p = f;
p();
}
void (*(f)())(int, float)
{
return foo;
}
void foo(int i, float f)
{
printf("%d %f\n", i, f);
}

a) Compile time error


b) Undefined behaviour

c) 1 2.000000
d) Nothing
View Answer
Answer:d
1. Read the following expression?
void (*ptr)(int);
a) ptr is pointer to int that converts its type to void
b) ptr is pointer to function passing int returning void
c) ptr is pointer to void that converts its type to int
d) ptr is pointer to function passing void returning int
View Answer
Answer:b
2. Which of the following expression is true for the following?
ptr is array with 3 elements of pointer to function returning pointer of int
a) int **ptr[3]();
b) int *(*ptr[3])();
c) int (*(*ptr[3])());
d) None of the mentioned
View Answer
Answer:b
3. What do the following declaration denote?
int **ptr;
a) ptr is a function pointer that returns pointer to int type
b) ptr is a pointer to an int pointer
c) ptr is a pointer to pointer to type int
d) None of the mentioned
View Answer
Answer:b
4. What do the following declaration denote?
char *str[5];
a) str is an array of 5 element pointer to type char
b) str is a pointer to an array of 5 elements
c) str is a function pointer of 5 elements returning char
d) None of the mentioned
View Answer
Answer:a

5. Comment on the following declaration?


int (*ptr)(); // i)
char *ptr[]; // ii)
a) Both i) and ii) and cannot exist due to same name
b) i) is legal, ii) is illegal
c) i) is illegal, ii) is legal
d) Both i) and ii) will work legal and flawlessly
View Answer
Answer:d
6. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no;
char name[20];
}
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

a) Compile time error


b) Nothing
c) hello
d) Varies
View Answer
Answer:a
7. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no = 5;
char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

a) Nothing
b) Compile time error

c) hello
d) Varies
View Answer
Answer:b
8. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
struct student
{
int no;
char name[20];
};
void main()
{
student s;
s.no = 8;
printf("hello");
}

a) Nothing
b) hello
c) Compile time error
d) Varies
View Answer
Answer:c
9. What is the output of this C code?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

#include <stdio.h>
void main()
{
struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}

a) Nothing
b) Compile time error
c) Junk
d) 8
View Answer
Answer:d

10. Is the below declaration legal?


int* ((*x)())[2];
a) true
b) false
c) Undefined behaviour
d) Depends on the standard
View Answer
Answer:b

You might also like