# C Programming Quiz Set 7

This quiz is based on C programming question asked in GATE exams. This quiz is helpful for aspiring candidates of GATE, UGC NET and other competitive exams.

Congratulations - you have completed .

You scored %%SCORE%% out of %%TOTAL%%.

Your performance has been rated as %%RATING%%

Your answers are highlighted below.

Question 1 |

Which one of the choices given below would be printed when the following program is executed?

#include<stdio.h> int a1[] = {6,7,8,18,34,67}; int a2[] = {23, 56, 28, 29}; int a3[] = {-12, 27, -31}; int* x[] = {a1, a2, a3}; void print(int* a[]) { printf("%d,",a[0][2]); printf("%d,",*a[2]); printf("%d,",*++a[0]); printf("%d,",*(++a)[0]); printf("%d\n",a[-1][+1]); } main() { print(x); }

**[GATE 2006, 2 Marks]**8, -12, 7, 23, 8 | |

8, 8, 7, 23, 7 | |

-12, -12, 27, -31, 23 | |

-12, -12, 27, -31, 56 |

Question 2 |

The following function computes the value of \(m \choose n\) correctly for all legal values \(m\) and \(n\) (\(m \ge 1 \), \(n \ge 0 \) and \(m \gt n \)).

int func(int m, int n) { if(E) return 1; else return (func(m-1,n) + func(m-1,n-1)); }In the above function, which of the following is the correct expression for E?

**[GATE 2006, 2 Marks]**(n==0) || (m == 1) | |

(n==0) && (m == 1) | |

(n==0) || (m == n) | |

(n==0) && (m == n) |

Question 3 |

Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code:

Subroutine swap(ix, iy) it = ix ix = iy /* L1 */ iy = it /* L2 */ end ia = 3 ib = 8 call swap(ia, ib + 5) print ia, ib end

**S1:**The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap.**S2:**On execution the code will generate a run-time error on line L1.**S3:**On execution the code will generate a run-time error on line L2.**S4:**The program will print 13 and 18.**S5:**The program will print 13 and -2.

**[GATE 2006, 2 Marks]**S1 and S2 | |

S1 and S4 | |

S3 | |

S1 and S5 |

Question 4 |

Consider the following C function in which a[n] and b[m] are two sorted integer arrays and c[n+m] be another integer array.

(I)

(II)

void xyz(int a[], int b[], int c[]){ int i,j,k; i = j = k = 0; while((i < n) && (j < m)) if(a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; }which of the following condition(s) hold(s) after the termination of the while loop?

(I)

` j < m, k = n+j-1, & a[n-1] < b[j] if i = n `

(II)

` i < n, k = m+i-1, & b[m-1] <= a[i] if j = m `

**[GATE 2006, 2 Marks]**only (I) | |

only (II) | |

either (I) or (II) but not both | |

neither (I) nor (II) |

Question 5 |

Consider these two functions and two statements S1 and S2 about them.

int work1(int* a, int i, int j){ int x = a[i+2]; a[j] = x + 1; return a[i+2]-3; }

int work2(int* a, int i, int j){ int t1 = i + 2; int t2 = a[t1]; a[j] = t2+1; return t2-3; }

Two statements are

**S1**The transformation from work-1 to work-2 is valid, i.e., for any program state and input arguments, work-2 will compute the same output and have the same effect on program state as work-1.**S2**All the transformation applied to work-1 to get work-2 will always improve the performance (i.e. reduce CPU time) of work-2 compared to work-1.

**[GATE 2006, 2 Marks]**S1 is False and S2 is False | |

S1 is False and S2 is True | |

S1 is True and S2 is False | |

S1 is True and S2 is True |

Question 6 |

Consider this C code two swap two integers and the following 5 statements. The code

void swap(int* px, int* py) { *px = *px - *py; *py = *px + *py; *px = *py - *px; }

- will generate a compilation error.
- may generate a segmentation fault at run-time depending on the argument passed.
- Correctly implements the swap procedure for all input pointers referring to integers stored in memory location accessible to the process.
- implements the swap procedure correctly for some but not all valid input pointers.
- may add or subtract integers and pointer.

**[GATE 2006, 2 Marks]**1 | |

2 and 3 | |

2 and 4 | |

2 and 5 |

Question 7 |

The function f is defined as follows:

(I) The function \(f\) terminates for finitely many different values of \(n \ge 1\).

(II) The function \(f\) terminates for infinitely many different values of \(n \ge 1\).

(III) The function \(f\) does not terminate for finitely many different values of \(n \ge 1\).

(IV) The function \(f\) does not terminate for infinitely many different values of \(n \ge 1\).

Which one of the following options is true of the above?

int f(int n) { if(n <= 1) return 1; else if(n%2 == 0) return f(n/2); else return f(3*n-1); }Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements:

(I) The function \(f\) terminates for finitely many different values of \(n \ge 1\).

(II) The function \(f\) terminates for infinitely many different values of \(n \ge 1\).

(III) The function \(f\) does not terminate for finitely many different values of \(n \ge 1\).

(IV) The function \(f\) does not terminate for infinitely many different values of \(n \ge 1\).

Which one of the following options is true of the above?

**[GATE 2007, 2 Marks]**1 and 3 | |

1 and 4 | |

2 and 3 | |

2 and 4 |

Question 8 |

Consider the C program given below:

#include<stdio.h> int main() { int sum = 0, maxsum = 0, i, n = 6; int a() = {2, -2, -1, 3, 4, 2} for(i = 0; i < n; i++) { if(i == 0 || a[i] < 0 || a[i] < a[i-1]) { if(sum > maxsum) maxsum = sum; sum = (a[i] > 0) ? a[i] : 0; } else sum += a[i]; } if(sum > maxsum) maxsum = sum; printf("%d\n", maxsum); }What is the value printed out when this program is executed?

**[GATE 2007, 2 Marks]**9 | |

8 | |

7 | |

6 |

Question 9 |

Consider the following C program:

#include<stdio.h> #define EOF -1 void push(int); /*push the argument on the stack */ int pop(void) /*pop the top of the stack */ void flagError(); int main() { int c,m,n,r; while((c = getchar()) != EOF) { if(isdigit(c)) push(c); else if((c == '+') || (c == '*')) { m = pop(); n = pop(); r = (c == '+')? n+m : n*m; push(r); } else if(c != '') flagError(); } printf("%c",pop()); }What is the output of the program for the following input? $$5\; 2\; *\; 3\; 3\; 2\; +\; *\; +\;$$

**[GATE 2007, 2 Marks]**15 | |

25 | |

30 | |

150 |

Question 10 |

Heap allocation is required for language

**[GATE 1997, 1 Marks]**that support recursion. | |

that support dynamic data structure. | |

that use dynamic scope rules. | |

None of the above. |

Once you are finished, click the button below. Any items you have not completed will be marked incorrect.

There are 10 questions to complete.