Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

1.

Find output of the following


int rec(int num){
return (num) ? num%10 + rec(num/10):0;
}

main(){

printf("%d",rec(4567));
}
a) 4
b) 12
c) 22
d) 21
Ans:c

2.Find output of the following


int something(int number)
{
if(number <= 0)
return 1;
else
return number * something(number-1);

}
main()
{
printf("%d",something(4));}
a) 12
b) 24
c) 1
d) 0
Ans:b

3. int func(int a, int b){

if(b==0)
return 0;
if(b==1)
return a;

return a + func(a,b-1);
}
what will be the output of func(3,8) .
a) 11
b) 24
c) 22
d) 21
Ans:b

4. what will be the output of following code:


#include<stdio.h>
print(int n)
{
if (n == 0)
return 0;

printf("%d", n%2);

print(n/2);
}

What will be the output of print(12).


a) 00110
b) 11001
c) 10010
d) 10001
Ans:a

5.int sum(int n) {

if (n==0)
return n;
else
return n + sum(n-1);
}
What will be the output of sum(8).
a)36
b)35
c)34
d)none
Ans:a

6. Which of the following problems can’t be solved using recursion?


a) Factorial of a number
b) Nth fibonacci number
c) Length of a string
d) Problems without base case
Ans:d

7. How many times is the recursive function called, when the following code is executed?
void my_recursive_function(int n)
{
if(n == 0)
return;
printf("%d ",n);
my_recursive_function(n-1);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) 9
b) 10
c) 11
d) 12
Ans:c

8. What does the following recursive code do?


void my_recursive_function(int n)
{
if(n == 0)
return;
my_recursive_function(n-1);
printf("%d ",n);
}
int main()
{
my_recursive_function(10);
return 0;
}
a) Prints the numbers from 10 to 1
b) Prints the numbers from 10 to 0
c) Prints the numbers from 1 to 10
d) Prints the numbers from 0 to 10
Ans:c

9. What will be the output of the following code?


int cnt=0;
void my_recursive_function(int n)
{
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
}
int main()
{
my_recursive_function(123456789);
printf("%d",cnt);
return 0;
}
a)123456789
b) 10
c) 0
d) 9
Ans:d

10. What will be the output of the following code?


void my_recursive_function(int n)
{
if(n == 0)
{
printf("False");
return;
}
if(n == 1)
{
printf("True");
return;
}
if(n%2==0)
my_recursive_function(n/2);
else
{
printf("False");
return;
}
}
int main()
{
my_recursive_function(100);
return 0;
}
a) True
b) False
b)compilation error
d)run time error
Ans:b

11. What is the output of the following code?


int cnt = 0;
void my_recursive_function(char *s, int i)
{
if(s[i] == '\0')
return;
if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
cnt++;
my_recursive_function(s,i+1);
}
int main()
{
my_recursive_function("thisisrecursion",0);
printf("%d",cnt);
return 0;
}
a) 6
b) 9
c) 5
d) 10
Ans:a

12. What is the output of the following code?


void my_recursive_function(int *arr, int val, int idx, int len)
{
if(idx == len)
{
printf("-1");
return ;
}
if(arr[idx] == val)
{
printf("%d",idx);
return;
}
my_recursive_function(arr,val,idx+1,len);
}
int main()
{
int array[10] = {7, 6, 4, 3, 2, 1, 9, 5, 0, 8};
int value = 2;
int len = 10;
my_recursive_function(array, value, 0, len);
return 0;
}
a) 3
b) 4
c) 5
d) 6
Ans:b

13. Recursion is similar to which of the following?


a) Switch Case
b) Loop
c) If-else
d) if elif else
Ans:b

14.What is the output of this C code?

void main()
{
static int x = 3;
x++;
if (x <= 5)
{
printf("hi");
main();
}
}
A. Run time error
B. hi
C. infinite hi
D. hihi
Ans:D

15. What is the output of this C code?

void main()
{
static int x;
if (x++ < 2)
main();
}
A. Infinite calls to main
B. Run time error
C. Varies
D. main is called twice
Ans:d

16.What will be the output of the program?

#include<stdio.h>
void fun(int);

int main()
{
int a=3;
fun(a);
return 0;
}
void fun(int n)
{
if(n > 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
}
A. 0, 2, 1, 0,
B. 1, 1, 2, 0,
C. 0, 1, 0, 2,
D. 0, 1, 2, 0,

Ans:d
17.Find output of the following
#include<stdio.h>
int sumdig(int);
int main()
{
int a, b;
a = sumdig(123);
b = sumdig(123);
printf("%d, %d\n", a, b);
return 0;
}
int sumdig(int n)
{
int s, d;
if(n!=0)
{
d = n%10;
n = n/10;
s = d+sumdig(n);
}
else
return 0;
return s;
}
A. 4, 4
B. 3, 3
C. 6, 6
D. 12, 12
Ans:C

18. What is the output of this C code?


#include <stdio.h>
int fun(int n)
{
int i, j, sum = 0;
for(i = 1;i<=n;i++)
for(j=i;j<=i;j++)
sum=sum+j;
return(sum);
}

int main()
{
printf("%d", fun(5));
return 0;
}

A) 55
B) 75
C) 15
D) 225

ANS: A

19. What will be the output of the program?

#include<stdio.h>
int check(int);
int main()
{
int i=45, c;
c = check(i);
printf("%d\n", c);
return 0;
}
int check(int ch)
{
if(ch >= 45)
return 100;
else
return 10;
}
A. 100
B. 10
C. 1
D. 0
ANS: A

20. Point out the error in the function:

f(int a, int b)
{
int a;
a = 20;
return a;
}
A. Missing parenthesis in return statement
B. The function should be defined as int f(int a, int b)
C. Redeclaration of a
D. None of above
ANS: C
1. Which of the following statement are correct?
(i) The value stored in the CPU register can always be accessed faster than that stored in
memory.
(ii) A register storage class variable will always be stored in a CPU register.

a) Only I is correct
b) Only II is correct
c) Both I & II are correct
d) Both I & II are incorrect

Ans: a)

2. Suppose a variable is declared with register storage class but no register is free then:
a) compilation error
b) data loss
c) it will occupy main memory
d) None of these

Ans: c)

3. Predict the output of following code:


#include<stdio.h>
auto int var=200;
main()
{
printf("The value of var is %d",var);
}
a) 200
b) garbage value
c) compilation error
d) 0
Ans: c)

4. What will output of the code given bellow?


#include<stdio.h>
fib_term()
{
static int a=0,b=1;
int c;
c=a+b;a=b;b=c;
return c;
}
main()
{
int count=0,i;
for(i=0;i<5;i++)
printf("%d ",fib_term());
}
a) 1 2 3 4 5
b) 1 2 3 5 8
c) 1 1 1 1 1
d) compilation error
Ans: b)

5. What will be output of the code given bellow?


#include<stdio.h>
int main()
{
register int a = 0;
printf("%u",&a);
}
a) garbage
b) hexadecimal memory location
c) 0
d) compilation error
Ans: d)

6. What will be output of the code given bellow?


#include<stdio.h>
int main()
{
typedef int i;
i a = 0;
printf("%d", a);
return 0;
}
a) Compiler Error
b) Runtime Error
c) 0
d) 1
Ans: c)

7. What is the output of the following program?


#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d", sizeof(i));
return 0;
}
a) 20
b) 0
c) Undefined reference to i
d) 4
Ans: c)

8. What will be the output of the following code?


#include<stdio.h>
int main()
{
static int i=5;
if(--i)
{
main();
printf("%d ",i);
}
}
a) 5 5 5 5
b) 0 0 0 0
c) 1 1 1 1
d) 4 4 4 4
Ans: b)

9. What will be the output of the following code?


#include<stdio.h>
int main()
{
extern int var;
printf("The value of var is %d", var);
return 0;
}
a) garbage
b) 0
c) No output
d) linking error
Ans: d)

10. What will be the output of the following code?


#include<stdio.h>
int var;
int main()
{
printf("%d", var);
return 0;
}
a) garbage
b) 0
c) no output
d) Compilation error
Ans: b)

11. What will be the output of the following code?


#include<stdio.h>
int main()
{
int i=5;
printf("%d",i--);
if(i)
main();
return 0;
}
a) 5 4 3 2 1
b) 5 5 5 5 5.....till stack overflow
c) 4 4 4 4 4.....till stack overflow
d) compilation error
Ans: b)

12. What will be the output of the following code?


#include<stdio.h>
int main()
{
typedef static int i;
i var;
printf("%d", var);
return 0;
}
a) garbage
b) 0
c) no output
d) Compilation error
Ans: d)

13. What will be the output of the following code?


#include<stdio.h>
int main()
{
typedef int i;
printf("%d", i);
return 0;
}
a) garbage
b) 0
c) no output
d) Compilation error
Ans: d)

14. The memory space to local variables is allocated from


a) Stack
b) Heap
c) Queue
d) Data segment
Ans: a)

15. What will be the output of the following code?


#include<stdio.h>
int main()
{
int i;
for(i=0;i<3;i++)
{
int x=0;
static int y=0;
printf("x=%d, y=%d\n",x++,y++);
}
return 0;
}
a) x=0, y=0
x=1, y=1
x=2, y=2
b) x=0, y=0
x=0, y=1
x=0, y=2
c) x=0, y=0
x=0, y=0
x=0, y=0
d) x=1, y=1
x=2, y=2
x=3, y=3
Ans: b)

16. What will be the output of the following code?


#include<stdio.h>
function(static int para)
{
printf("%d",para);
}
int main()
{
int i=200;
printf("%d ", i);
function(i);
return 0;
}
a) 200 200
b) 200 0
c) 200 garbage
d) Compilation error
Ans: d)

17. What will be the output of the following code?


#include<stdio.h>
int main()
{
extern int i;
printf("%d", i);
return 0;
}
int i=100;
a) garbage
b) 0
c) 100
d) Compilation error
Ans: c)
18. What will be the output of the program?
#include<stdio.h>
int main()
{
typedef int arr[5];
arr iarr = {1, 2, 3, 4, 5};
int i;
for(i=0; i<4; i++)
printf("%d, ", iarr[i]);
return 0;
}
a) 1, 2, 3, 4,
b) 1, 2, 3, 4, 5,
c) 1,2,3,4
d) Error: Cannot use typedef with an array
Ans: a)

19. Consider the following C program segment:

main()
{
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length - i];
printf("%s",p);
}
The output of the program is:
a) gnirts
b) gnirt
c) string
d) no output is printed

Ans: D

20. Consider the following C function


void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}

In order to exchange the values of two variables x and y.

a) call swap (x, y)


b) call swap (&x, &y)
c) swap (x,y) cannot be used as it does not return any value
d) swap (x,y) cannot be used as the parameters are passed by value

Ans: D
1. In the following code what is 'C'?

typedef char* charp;


const charp C;

a. C is a constant.
b. C is a character constant.
c. C is character type.
d. None of the above.
Ans: a

2. The size of a void pointer in 16-bit system is __________?

a. 1 Byte
b. 2 Bytes
c. 4 Bytes
d. 8 Bytes
Ans: b

3. Which of the following does not initialize ptr to null


(assuming variable declaration of 'var' as int var=0)?

a. int *p = &var - &var;


b. int *p = var – var;
c. int *p = &var;
d. int *ptr=NULL;
Ans: c

4. What is (void*)0?

a. Representation of void pointer


b. Representation of null pointer
c. Compilation error
d. None of them
Ans: b

5. What is the output of following program?

#include<stdio.h>
int main()
{
int *ptr, var=5;
ptr = &var;
*ptr += 1;
printf("%d, %d", *ptr, var);
return 0;
}

a. 5, 5
b. 6, 5
c. 5, 6
d. 6, 6
Ans: d

6. What the following, instruction tells?


const int *ptr;

a. We cannot change the pointer ptr itself.


b. We cannot change the value pointed by ptr.
c. Both (a) and (b).
d. We can change the pointer as well as the value pointed by it.
Ans: b

7. What does the following declaration mean?


int (*ptr)[10];

a. ptr is 1-D array of size 10, of pointers to integers.


b. ptr is a pointer to a 10 elements integer array.
c. The same as int *ptr[10]
d. None of these.
Ans: b

8. What will be the output of the following program?

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

a. 5
b. Runtime Error
c. Compilation Error
d. Undefined behavior
Ans: c

9. What will be the output of the following program code?

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

a. 65624
b. 0.000000
c. 15
d. Error
Ans: b

10. What will be the output of the following code?

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

a. 5
b. Garbage value
c. Compilation Error
d. None of these.
Ans: a

11. What is the output of the following code?

#include<stdio.h>
int main()
{
char *ptr;
ptr = "A BCD EFGH";
printf("%c",*&*ptr);
return 0;
}

a. A
b. A BCD EFGH
c. Some address will be printed
d. Compilation Error
Ans: a

12. What is the output of the following code?

#include<stdio.h>
void func(int, int);
int main()
{
int i = 5, j = 2;
func (i,j);
printf("%d %d\n", i, j);
return 0;
}
void func (int i, int j)
{
i = i * i;
j = j * j;
}

a. 5, 5
b. 2, 2
c. 25, 4
d. 5, 2
Ans: d
13. What is the output of the following code?

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

a. 5, 5
b. 2, 2
c. 25, 4
d. 5, 2
Ans: c

14. What is the output of following code?

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

a. 5
b. Garbage Value
c. Compilation Error
d. Segmentation Fault
Ans: a

15. What is the output of the following code?

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

a. 2 97
b. 2 2
c. Undefined Behaviour
d. Segmentation Fault
Ans: b

16. What is the output of the following code?

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

a. 6
b. Compile time error
c. 5
d. Junk
Ans: a
17. What is the output of this C code?

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

a. 1
b. Compile time error
c. Address of a
d. Junk value
Ans: b

18. What is the output of this C code?

#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
Ans: c

19. What is the output of this C code?

#include<stdio.h>
void 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. 5
b. 9
c. 6
d. 2
Ans: a

20. What is a dangling pointer in C programming language?

a. If pointer is assigned to more than one variable.


b. If pointer is not defined properly.
c. If pointer is pointing to a memory location from where variable has been deleted.
d. None of above.
Ans: c
1) What is the output of C Program.?
int main()
{
char grade[] = {'A','B','C'};
printf("GRADE=%c, ", *grade);
printf("GRADE=%u", grade);
}
A) GRADE=some address of array, GRADE=A
B) GRADE=A, GRADE=some address of array
C) GRADE=A, GRADE=A
D) Compiler error
Answer B

2) What is the output of C Program.?


int main()
{
int a[3] = {20,30,40};
a[0]++;
int i=0;
while(i<3)
{
printf("%d", i[a]);
i++;
}
}
A) 20 30 40
B) 41 30 20
C) 21 30 40
D) None of the above
Answer C

3) What is the output of C program with arrays.?


int main()
{
int a[3] = {20,30,40};
int b[3];
b=a;
printf("%d", b[0]);
}
A) 20
B) 30
C) address of 0th element.
D) Compiler error
Answer D

4) What is the output of C Program with arrays and pointers?


int main()
{
int a[3] = {20,30,40};
int (*p)[3];
p=&a;
printf("%d", (*p)[0]);
}
A) 20
B) 0
C) address of element 20
D) Compiler error
Answer A

5) What is the output of C program with arrays and pointers.?


int main()
{
int a[3] = {20,30,40};
int *p[3];
p=&a;
printf("%d", *p[0]);
}
A) 20
B) address of element 20
C) Garbage value
D) Compiler error
Answer D

6) What is the output of C program with arrays and pointers.?


int main()
{
int a[3] = {20,30,40};
printf("%d", *(a+1));
}
A) 20
B) 30
C) 40
D) Compiler error
Answer B

7) What is the output of C Program with arrays and pointers.?


void change(int[]);
int main()
{
int a[3] = {20,30,40};
change(a);
printf("%d %d", *a, a[0]);
}
void change(int a[])
{
a[0] = 10;
}
A) 20 20
B) 10 20
C) 10 10
D) 20 30
Answer C

8) An entire array is always passed by ___ to a called function.


A) Call by value
B) Call by reference
C) Address relocation
D) Address restructure
Answer B

9. Which sorting algorithm will take least time when all elements of input array are identical?
Consider typical implementations of sorting algorithms.
(A) Insertion Sort
(B) Heap Sort
(C) Merge Sort
(D) Selection Sort

Answer: a

10. Consider the code given below, which runs insertion sort:

void insertionSort(int arr[], int array_size)


{
int i, j, value;
for (i = 1; i < array_size; i++)
{
value = arr[i];
j = i;
while (________ )
{
arr[j] = arr[j - 1];
j = j - 1;
}
arr[j] = value;
}

Which condition will correctly implement the while loop?


a) (j > 0) || (arr[j - 1] > value)
b) (j > 0) && (arr[j - 1] > value)
c) (j > 0) && (arr[j + 1] > value)
d) (j > 0) && (arr[j + 1] < value)

Answer: b

11. Consider an array of length 5, arr[5] = {9,7,4,2,1}.


What are the steps of insertions done while running insertion sort on the array?
a) 7 9 4 2 1 4 7 9 2 1 2 4 7 9 1 1 2 4 7 9
b) 9 7 4 1 2 9 7 1 2 4 9 1 2 4 7 1 2 4 7 9
c) 7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2
d) 7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9

Answer: a

12. Which of the following is not an exchange sort?


a) Bubble Sort
b) Quick Sort
c) Partition-exchange Sort
d) Insertion Sort

Answer: d

13. Suppose we are sorting an array of eight integers using some quadratic sorting algorithm.
After four iterations of the algorithm’s main loop, the array elements are ordered as shown here:
24578136
A. Insertion sort
B. Selection sort
C. Either of a and b
D. None of the above

Answer: a

14. Consider the situation in which assignment operation is very costly.


Which of the following sorting algorithm should be performed so that the number of assignment
operations is minimized in general?
a) Insertion sort
b) Selection sort
c) Bubble Sort
d) None

Answer: b

15. What is the output of following program?

#include <stdio.h>

void print(int n, int j)


{
if (j >= n)
return;
if (n-j > 0 && n-j >= j)
printf("%d %d\n", j, n-j);
print(n, j+1);
}

int main()
{
int n = 8;
print(n, 1);
}

(A) 1 7
26
35
44
44

(B) 1 7
26
35
44
(C) 1 7
26
35
(D) 1 2
34
56
78

Answer: b

16. Comment on the following 2 arrays with respect to P and Q.

int *a1[8];
int *(a2[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
Answer B

17. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int ary[2][3];
foo(ary);
}
void foo(int *ary[])
{
int i = 10, j = 2, k;
ary[0] = &i;
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
Answer A

18. What will be the output of the following C code?

#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
Answer A

19. What will be the output of the following C code?

#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;
printf("%d\n", ary[0][1]);
}
a) 10
b) 2
c) Compile time error
d) Undefined behaviour
Answer D

20. What will be the output of the following C code?

#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
Answer A
1.What will be the output of the program ?
#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
A.2, 1, 15
B.1, 2, 5
C.3, 2, 15
D.2, 3, 20
Answer C

2.What will be the output of the program ?


#include<stdio.h>
int main()
{
void fun(int, int[]);
int arr[] = {1, 2, 3, 4};
int i;
fun(4, arr);
for(i=0; i<4; i++)
printf("%d,", arr[i]);
return 0;
}
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++ < n)
p = &arr[i];
*p=0;
}
A.2, 3, 4, 5
B.1, 2, 3, 4
C.0, 1, 2, 3
D.3, 2, 1 0
Answer B

3. What will be the output of the program in Turb C (under DOS)?


#include<stdio.h>

int main()
{
int arr[5], i=0;
while(i<5)
arr[i]=++i;

for(i=0; i<5; i++)


printf("%d, ", arr[i]);

return 0;
}
A. 1, 2, 3, 4, 5,
B. Garbage value, 1, 2, 3, 4,
C. 0, 1, 2, 3, 4,
D. 2, 3, 4, 5, 6,
ANS: B

4. What is meaning of the following statement ?


int *ptr[20];
A Array of Integer Pointers of size 20
B None of these
C Integer Array to Integer Pointers having size 20
D Integer Array of size 20 pointing to an Integer Pointer
Answer A

5. In C Programming, If we need to store word "INDIA" then syntax is as below -


A) char name[];
name = "INDIA"
B) char name[6] = {"I","N","D","I","A"}
C) char name[6] = {'I','N','D','I','A'}
D) char name[6] = {'I','N','D','I','A','\0'}
Answer D

6. Where is linear searching used?


a) When the list has only a few elements
b) When performing a single search in an unordered list
c) Used all the time
d) When the list has only a few elements and When performing a single search in an unordered
list

Answer: d

7. What is the best case for linear search?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)

Answer: d

8. What is the worst case for linear search?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(1)

Answer: c

9. The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending
order, using bubble sort is
(A) 11
(B) 12
(C) 13
(D) 10

Answer: d

10. What is the worst case complexity of binary search using recursion?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)

Answer: b

11. The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements.
How many iterations will be done to sort the array?
a) 4
b) 2
c) 1
d) 0

Answer: a

12. Bubble sort is also known as ___________


a) stupid sort
b) ship sort
c) sinking sort
d) shell sort

Answer: c

13. What will be the output of the following C code?

#include <stdio.h>
int main()
{
char *a = {"p", "r", "o", "g", "r", "a", "m"};
printf("%s", a);
}
a) program
b) p
c) No output
d) Compile-time error
Answer B

14. An array of strings can be initialized by _________


a) char *a[] = {“Hello”, “World”};
b) char a[][10] = {“Hello”, “Worlds”};
c)char *b = "Hello";
char *c = "World";
char *a[] = {b, c};
d) all of the mentioned
Answer D

15. What will be the output of the following C code?

#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) segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler
Answer A

16. What will be the output of the following C code?

#include <stdio.h>
void main()
{
char *a[10] = {"hi", "hello", "how"};
int i = 0, j = 0;
a[0] = "hey";
for (i = 0;i < 3; 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
d) depends on compiler
Answer C

17. What will be the output of the following C code on a 32-bit system?

#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
Answer D

18. What will be the output of the following C code on a 32-bit system?
#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
Answer B

19. What will be the output of the following C code?

#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
Answer D

20. What will be the output of the following C code?

#include <stdio.h>
void main()
{
char a[10][5] = {"hi", "hello", "fellows"};
printf("%s", a[1]);
}
a) hello
b) hello followed by Garbage
c) hellofello
d) Compiler Error
Answer C
1. Assuming, integer is 2 byte, What will be the output of the program?

#include<stdio.h>

int main()
{
printf("%x\n", -2<<2);
return 0;
}
A. ffff
B. 0
C. fff8
D. Error

Ans: C

2. What will be the output of the program?

#include<stdio.h>
int main()
{
int x=12, y=7, z;
z = x!=4 || y == 2;
printf("z=%d\n", z);
return 0;
}
A. z=0
B. z=1
C. z=4
D. z=2

Ans: B

3. What will be the output of the program?

#include<stdio.h>
int main()
{
int i=4, j=-1, k=0, w, x, y, z;
w = i || j || k;
x = i && j && k;
y = i || j &&k;
z = i && j || k;
printf("%d, %d, %d, %d\n", w, x, y, z);
return 0;
}
A. 1, 1, 1, 1
B. 1, 1, 0, 1
C. 1, 0, 0, 1
D. 1, 0, 1, 1

Ans: D

4. What will be the output of the program?

#include<stdio.h>
int reverse(int);

int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
A. Print 5, 4, 3, 2, 1
B. Print 1, 2, 3, 4, 5
C. Print 5, 4, 3, 2, 1, 0
D. Infinite loop

Ans: C///D

5. What will be the output of the program?

#include<stdio.h>
int main()
{
void fun(char*);
char a[100];
a[0] = 'A'; a[1] = 'B';
a[2] = 'C'; a[3] = 'D';
fun(&a[0]);
return 0;
}
void fun(char *a)
{
a++;
printf("%c", *a);
a++;
printf("%c", *a);
}
A. AB
B. BC
C. CD
D. No output

Ans: B

6. What will be the output of the program?

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

int fun(int i)
{
return (i++);
}
A. 9
B. 10
C. 11
D. 8

Ans: A
7. What will be the output of the program?

#include<stdio.h>
int check (int, int);

int main()
{
int c;
c = check(10, 20);
printf("c=%d\n", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
i>=45 ? return(*p): return(*q);
}
A. Print 10
B. Print 20
C. Print 1
D. Compile error

Ans: D

8. What will be the output of the following C code?

#include <stdio.h>
void main()
{
char *s= "hello";
char *p = s;
printf("%s ,%s", p+1, s+2);
}
a) Run time error
b) hello,llo
c) ello,llo
d) e,l

Ans: C

9. What will be the output of the following C code?


#include <stdio.h>
int main()
{
int ary[4] = {1, 2, 3, 4};
int *p = ary + 3;
printf("%d\n", p[-2]);
}
a) 1
b) 2
c) Compile time error
d) Some garbage value

Ans: B

10. What will be the output of the following C code?

#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

Ans: D

11. What will be the output of the program ?


#include<stdio.h>
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
A.2, 1, 15
B.1, 2, 5
C.3, 2, 15
D.2, 3, 20
Ans: C

12. The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending
order, using bubble sort is
(A) 11
(B) 12
(C) 13
(D) 10

Ans: d

13. Consider the following three C functions :


[PI] int * g (void)
{
int x = 10;
return (&x);
}

[P2] int * g (void)


{
int * px;
*px = 10;
return px;
}

[P3] int *g (void)


{
int *px;
px = (int *) malloc (sizeof(int));
*px = 10;
return px;
}
Which of the above three functions are likely to cause problems with pointers?
(a) Only P3
(b) Only P1 and P3
(c) Only P1 and P2
(d) P1, P2 and P3
Ans: C

14. The value of j at the end of the execution of the following C program.
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main ()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
(a) 10
(b) 4
(c) 6
(d) 7

Ans: A

15. Consider the following C declaration


struct
{
short s [5]
union
{
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes,
respectively. The memory requirement for variable t, ignoring alignment
considerations, is
(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes

Ans: C
16. In the C language:
a) At most one activation record exists between the current activation record and the activation
record for the main
b) The number of activation records between the current activation record and the activation
record for the main depends on the actual function calling sequence.
c) The visibility of global variables depends on the actual function calling sequence.
d) Recursion requires the activation record for the recursive function to be saved on a different
stack before the recursive function can be called.

Ans: B

17. Consider the C program shown below.


# include <stdio.h>
# define print(x) printf ("%d ", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y+2;
Q(x);
*y = x-1;
print(x);
}

main(void)
{
x=5;
P(&x);
print(x);
getchar();
}
The output of this program is:
a) 12 7 6
b) 22 12 11
c) 14 6 6
d) 766

Ans: A
18. Consider the following declaration of a ‘two-dimensional array in C:
char a[100][100];
Assuming that the main memory is byte-addressable and that the array is stored starting from
memory address 0,
and size of char is 1 byte. In row major implementation, the address of a[40][50] is :

a) 4040
b) 4050
c) 5040
d) 5050

Ans: B

19. The most appropriate matching for the following pairs:

X: m=malloc(5); m= NULL; 1: using dangling pointers


Y: free(ptr); ptr->value=5; 2: using uninitialized pointers
Z: char *p; *p = ’a’; 3. lost memory is:

(a) X—1 Y—3 Z-2


(b) X—2 Y—1 Z-3
(C) X—3 Y—2 Z-1
(d) X—3 Y—1 Z-2

Ans: D

20. Consider the following C program segment:

main()
{
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
{
p[i] = s[length - i-1];
if(s[length-i-1]== 'a'|| s[length-i-1]== 'e' || s[length-i-1]== 'i'|| s[length-i-1]== 'o'|| s[length-i-1]==
'u')
break;
}
p[i]='\0'
printf("%s",p);
}
The output of the program is:
a) gni
b) gn
c) string
d) no output is printed

Ans: B

You might also like