gate question booklet

52
DEPARTMENT OF INFORMATION TECHNOLOGY GATE QUESTION BOOKLET COMPUTER ARCHITECTURE 2019 2020 EVEN SEMESTER Questions From 2000 To 2019 COMPILED BY Ms.V.Deepa Priya, AP/IT NAME OF THE STUDENT : CLASS : I IT

Upload: others

Post on 30-Nov-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GATE QUESTION BOOKLET

DEPARTMENT OF INFORMATION TECHNOLOGY

GATE QUESTION

BOOKLET

COMPUTER ARCHITECTURE

2019 – 2020 EVEN SEMESTER

Questions From 2000 To 2019

COMPILED BY

Ms.V.Deepa Priya, AP/IT

NAME OF THE STUDENT :

CLASS : I IT

Page 2: GATE QUESTION BOOKLET

GATE SYLLABUS

CS Computer Science and Information Technology

Section1: Engineering Mathematics

Discrete Mathematics: Propositional and first order logic. Sets, relations, functions, partial orders and

lattices. Groups. Graphs: connectivity, matching, coloring. Combinatorics: counting, recurrence relations,

generating functions. Linear Algebra: Matrices, determinants, system of linear equations, eigenvalues and

eigenvectors, LU decomposition. Calculus: Limits, continuity and differentiability. Maxima and minima.

Mean value theorem. Integration. Probability: Random variables. Uniform, normal, exponential, poisson

and binomial distributions. Mean, median, mode and standard deviation. Conditional probability and

Bayes theorem.

Computer Science and Information Technology

Section 2: Digital Logic

Boolean algebra. Combinational and sequential circuits. Minimization. Number representations and

computer arithmetic (fixed and floating point).

Section 3: Computer Organization and Architecture

Machine instructions and addressing modes. ALU, data‐path and control unit. Instruction pipelining.

Memory hierarchy: cache, main memory and secondary storage; I/O interface (interrupt and DMA mode).

Section 4: Programming and Data Structures

Programming in C. Recursion. Arrays, Stacks, Queues, Linked Lists, Trees, Binary Search Trees, Binary

Heaps, Graphs.

Section 5: Algorithms

Searching, Sorting, Hashing. Asymptotic Worst Case Time and Space Complexity. Algorithm Design

Techniques: Greedy, Dynamic programming and Divide‐and‐Conquer. Graph Search, Minimum

Spanning Trees, Shortest Paths.

Section 6: Theory of Computation

Regular expressions and finite automata. Context-free grammars and push-down automata. Regular and

contex-free languages, pumping lemma. Turing machines and undecidability.

Section 7: Compiler Design

Lexical analysis, parsing, syntax-directed translation. Runtime environments. Intermediate code

generation.

Section 8: Operating System

Processes, threads, inter‐process communication, concurrency and synchronization. Deadlock. CPU

scheduling. Memory management and virtual memory. File systems.

Page 3: GATE QUESTION BOOKLET

CS8251 Programming in C Syllabus

UNIT I BASICS OF C PROGRAMMING

Introduction to programming paradigms - Structure of C program - C programming: Data Types –

Storage classes - Constants – Enumeration Constants - Keywords – Operators: Precedence and

Associativity - Expressions - Input/Output statements, Assignment statements – Decision making

statements - Switch statement - Looping statements – Pre-processor directives - Compilation process

UNIT II ARRAYS AND STRINGS

Introduction to Arrays: Declaration, Initialization – One dimensional array – Example Program:

Computing Mean, Median and Mode - Two dimensional arrays – Example Program: Matrix Operations

(Addition, Scaling, Determinant and Transpose) - String operations: length, compare, concatenate, copy –

Selection sort, linear and binary search

UNIT III FUNCTIONS AND POINTERS

Introduction to functions: Function prototype, function definition, function call, Built-in functions (string

functions, math functions) – Recursion – Example Program: Computation of Sine series, Scientific

calculator using built-in functions, Binary Search using recursive functions – Pointers – Pointer operators

– Pointer arithmetic – Arrays and pointers – Array of pointers – Example Program: Sorting of names –

Parameter passing: Pass by value, Pass by reference – Example Program: Swapping of two numbers and

changing the value of a variable using pass by reference.

UNIT IV STRUCTURES

Structure - Nested structures – Pointer and Structures – Array of structures – Example Program using

structures and pointers – Self referential structures – Dynamic memory allocation - Singly linked list -

typedef

UNIT V FILE PROCESSING

Files – Types of file processing: Sequential access, Random access – Sequential access file - Example

Program: Finding average of numbers stored in sequential access file - Random access file - Example

Program: Transaction processing using random access files – Command line arguments

OUTCOMES: Upon completion of the course, the students will be able to

Develop simple applications in C using basic constructs

Design and implement applications using arrays and strings

Develop and implement applications in C using functions and pointers.

Develop applications in C using structures.

Design applications using sequential and random access file processing.

TEXT BOOKS:

Page 4: GATE QUESTION BOOKLET

1. Reema Thareja, ―Programming in C‖, Oxford University Press, Second Edition, 2016.

2. Kernighan, B.W and Ritchie,D.M, ―The C Programming language‖, Second Edition, Pearson

Education, 2006

REFERENCES:

1. Paul Deitel and Harvey Deitel, ―C How to Program‖, Seventh edition, Pearson Publication

2. Juneja, B. L and Anita Seth, ―Programming in C‖, CENGAGE Learning India pvt. Ltd., 2011

3. Pradip Dey, Manas Ghosh, ―Fundamentals of Computing and Programming in C‖, First Edition,

Oxford University Press, 2009.

4. Anita Goel and Ajay Mittal, ―Computer Fundamentals and Programming in C‖, Dorling Kindersley

(India) Pvt. Ltd., Pearson Education in South Asia, 2011.

5. Byron S. Gottfried, "Schaum's Outline of Theory and Problems of Programming with C", McGraw-Hill

Education, 1996.

Page 5: GATE QUESTION BOOKLET

DEPARTMENT OF INFORMATION TECHNOLOGY

GATE QUESTION BOOKLET

COMPUTER ARCHITECTURE

INDEX

Academic Year : 2019 – 2020 EVEN

Question Paper Years : 1998 to 2019

Name of the Subject In – Charge : Ms.V.Deepa Priya AP/IT

Name of the Student Mentor :

Name of the Student :

S. No. Name of the Topic Number of Questions Page Number

1 C Basics 5 1-4

2 Conditional Statements 1 5

3 Switch Statements 1 6

4 Looping Statements 2 7-8

5 Strings 3 9-11

6 Arrays 3 12-14

7 Functions 14 15-28

8 Pointers 12 40

9 Structure 1 41

Subject In - Charge GATE Coordinator HoD/IT

Page 6: GATE QUESTION BOOKLET

DEPARTMENT OF INFORMATION TECHNOLOGY

GATE QUESTION BOOKLET

PROGRAMMING IN C

ASSESSMENT REPORT

S. No. Date Hour Name of the Topic

Question

Numbers

Solved

Mark

(10)

Signature

of the

Student

Mentor

Signature

of the

Subject In

– Charge

1.

C Basics

2.

C Basics

3.

C Basics

4.

C Basics

5.

Conditional Statements

6.

Switch Statements

7.

Looping Statements

8.

Looping Statements

9.

Strings

10.

Strings

11.

Strings

12.

Arrays

13.

Arrays

Page 7: GATE QUESTION BOOKLET

S. No. Date Hour Name of the Topic

Question

Numbers

Solved

Mark

(10)

Signature

of the

Student

Mentor

Signature

of the

Subject In

– Charge

14.

Arrays

15. Arrays

16.

Functions

17.

Functions

18.

Functions

19.

Functions

20.

Functions

21.

Functions

22.

Functions

23.

Functions

24.

Functions

25.

Functions

26.

Functions

27.

Functions

28.

Functions

29.

Functions

30.

Pointers

31. Pointers

Page 8: GATE QUESTION BOOKLET

S. No. Date Hour Name of the Topic

Question

Numbers

Solved

Mark

(10)

Signature

of the

Student

Mentor

Signature

of the

Subject In

– Charge

32. Pointers

33. Pointers

34. Pointers

35. Pointers

36. Pointers

37. Pointers

38. Pointers

39. Pointers

40. Pointers

41. Structure

42.

43.

44.

45.

46.

47.

48.

Signature of GATE Co-ordinator HoD/IT

Page 9: GATE QUESTION BOOKLET

CERTIFICATE OF COMPLETION

This is to certify that ___________________________________________________ of II year B.Tech.

Information Technology Department has completed GATE training on Programming in C and secured

______________________________ mark out of _______________ during the academic year 2019 –

2020 EVEN.

Subject In - Charge GATE Coordinator HoD/IT

Page 10: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 1

GATE QUESTIONS

PROGRAMMING IN C

C BASICS

1. Consider the following C function (GATE CS2005)

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. (GATE CS 2004)

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

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 11: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 2

GATE QUESTIONS

PROGRAMMING IN C

C BASICS

1. The C language is. (GATE CS 2002)

a) A context free language

b) A context sensitive language

c) A regular language

d) Parsable fully only by a Turing machine

2. The number of tokens in the following C statement. (GATE CS2000)

printf("i = %d, &i = %x", i, &i);

is

(a) 3

(b) 26

(c) 10

(d) 21

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 12: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 3

GATE QUESTIONS

PROGRAMMING IN C

C Basics

1. Match the Following (GATE 2017)

a. P-->(ii), Q-->(iv), R-->(i), S-->(iii)

b. P-->(ii), Q-->(i), R-->(iv), S-->(iii)

c. P-->(ii), Q-->(iv), R-->(iii), S-->(i)

d. P-->(iii), Q-->(iv), R-->(i), S-->(ii)

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 13: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 4

GATE QUESTIONS

PROGRAMMING IN C

C Basics

1. Consider the following C program:(GATE 2017)

#include <stdio.h>

int main()

{

int m = 10;

int n, n1;

n = ++m;

n1 = m++;

n--;

--n1;

n -= n1;

printf("%d",n);

return 0;

}

The output of the program is ______. Note: This questions appeared as Numerical Answer

Type.

a. 0

b. 1

c. 2

d. 3

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 14: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 5

GATE QUESTIONS

PROGRAMMING IN C

CONDITIONAL STATEMENT (IF)

GATE CS 2004

1. Consider the following C function:

int f(int n)

{

static int i = 1;

if (n >= 5)

return n;

n = n+i;

i++;

return f(n);

}

The value returned by f(1) is () a) 5

b) 6

c) 7

d) 8

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 15: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 6

GATE QUESTIONS

PROGRAMMING IN C

SWITCH CASE STATEMENT

1. What will be the output of the following C program segment? (GATE CS 2012)

char inchar = 'A';

switch (inchar)

{

case 'A' :

printf ("choice A \n") ;

case 'B' :

printf ("choice B ") ;

case 'C' :

case 'D' :

case 'E' :

default:

printf ("No Choice") ;

}

(A) No choice

(B) Choice A

(C) Choice A

Choice B No choice

(D) Program gives no output as it is erroneous

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 16: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 7

GATE QUESTIONS

PROGRAMMING IN C

LOOPING

1) Consider the following C code. Assume that unsigned long int type length is 64

bits.(GATE 2018)

unsigned long int fun(unsigned long int n) {

unsigned long int i, j = 0, sum = 0;

for( i = n; i > 1; i = i/2) j++;

for( ; j > 1; j = j/2) sum++;

return sum;

}

The value returned when we call fun with the input 240 is

a. 4

b. 5

c. 6

d. 40

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 17: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 8

GATE QUESTIONS

PROGRAMMING IN C

LOOPING

1. Consider the following pseudo code. What is the total number of multiplications to be

performed?

D = 2

for i = 1 to n do

for j = i to n do

for k = j + 1 to n do

D = D * 3

a. Half of the product of the 3 consecutive integers.

b. One-third of the product of the 3 consecutive integers.

c. One-sixth of the product of the 3 consecutive integers.

d.None of the above

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 18: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 9

GATE QUESTIONS

PROGRAMMING IN C

STRINGS

1) Choose the correct option to fill ?1 and ?2 so that the program below prints an input

string in reverse order. Assume that the input string is terminated by a newline

character. (GATE 2008)

void reverse(void)

{

int c;

if (?1) reverse() ;

?2

}

main()

{

printf ("Enter Text ") ;

printf ("\n") ;

reverse();

printf ("\n") ;

}

(A) ?1 is (getchar() != ’\n’)

?2 is getchar(c);

(B) ?1 is (c = getchar() ) != ’\n’)

?2 is getchar(c);

(C) ?1 is (c != ’\n’)

?2 is putchar(c);

(D) ?1 is ((c = getchar()) != ’\n’)

?2 is putchar(c);

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 19: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 10

GATE QUESTIONS

PROGRAMMING IN C

STRINGS

1. Consider the following C program segment (GATE 2004)

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

Page 20: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 11

GATE QUESTIONS

PROGRAMMING IN C

STRINGS

Consider the following C program. (GATE 2017)

#include <stdio.h>

#include <string.h>

void printlength (char *s, char *t)

{

unsigned int c = 0;

int len = ((strlen (s) - strlen (t)) > c) ? strlen (s) : strlen (t);

printf("%dn", len);

}

void main()

{

char *x = "abc";

char *y = "defgh";

printlength(x, y);

}

Recall that strlen is defined in string.h as returning a value of type size_t, which is

an unsigned int . The output of the program is _________. Note: This questions appeared as

Numerical Answer Type.

a. 2

b. 3

c. 4

d. 5

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 21: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 12

GATE QUESTIONS

PROGRAMMING IN C

ARRAYS

1) Consider the C function given below. Assume that the array listA contains n (> 0)

elements, sorted in ascending order. (GATE CS 2014)

int ProcessArray(int *listA, int x, int n)

{

int i, j, k;

i = 0;

j = n-1;

do{

k = (i+j)/2;

if (x <= listA[k])

j = k-1;

if (listA[k] <= x)

i = k+1;

} while (i <= j);

if (listA[k] == x)

return(k);

else

return -1;

}

Which one of the following statements about the function ProcessArray is CORRECT?

(A) It will run into an infinite loop when x is not in listA.

(B) It is an implementation of binary search.

(C) It will always find the maximum element in listA.

(D) It will return -1 even when x is present in listA.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 22: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 13

GATE QUESTIONS

PROGRAMMING IN C

ARRAYS

1. Assume the following C variable declaration (GATE 2003)

int *A [10], B[10][10];

Of the following expressions

I A[2]

II A[2][3]

III B[1]

IV B[2][3]

which will not give compile-time errors if used as left hand sides of assignment statements in

a C program (GATE CS 2003)?

a) I, II, and IV only

b) II, III, and IV only

c) II and IV only

d) IV only

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 23: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 14

GATE QUESTIONS

PROGRAMMING IN C

ARRAYS

1. 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, the address of a[40][50] is (GATE CS 2002)

a) 4040

b) 4050

c) 5040

d) 5050

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 24: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 15

GATE QUESTIONS

PROGRAMMING IN C

FUNCTION

1) Consider the following C program: (GATE 2019)

#include <stdio.h>

int jumble(int x, int y) {

x = 2 * x + y;

return x;

}

int main() {

int x = 2, y = 5;

y = jumble(y, x);

x = jumble(y, x);

printf("%dn", x);

return 0;

}

The value printed by program is __________ .

Note: This was Numerical Type question.

A) 26

B) 2

C) 5

D) 12

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 25: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 16

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the following C program: (GATE 2019)

void convert(int n) {

if (n < 0)

printf(“ % d”, n);

else {

convert(n / 2);

printf(“ % d”, n % 2);

}

}

Which one of the following will happen when the function convert is called with any positive

integer n as argument?

a) It will print the binary representation of n in the reverse order and terminate.

b) It will print the binary representation of n but will not terminate

c) It will not print anything and will not terminate.

d) It will print the binary representation of n and terminate.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 26: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 17

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

1) Consider the following C program: (GATE 2019)

#include<stdio.h>

int r(){

int static num=7;

return num--;

}

int main() {

for(r();r();r()) {

printf("%d ",r());

};

return 0;

}

Page 27: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 18

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the following program written in pseudo-code. Assume that x and y are integers.

Count (x, y) {

if (y !=1 ) {

if (x !=1) {

print("*");

Count (x/2, y);

}

else {

y=y-1;

Count (1024, y);

}

}

}

The number of times that the print statement is executed by the call Count(1024, 1024) is

_______ . Note -This was Numerical Type question.

a. 10230

b. 10

c. 1023

d. 23010

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 28: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 19

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the following C function.

int fun1 (int n)

{

int i, j, k, p, q = 0;

for (i = 1; i<n; ++i)

{

p = 0;

for (j = n; j > 1; j = j/2)

++p;

for (k = 1; k < p; k = k*2)

++q;

}

return q;

}

Which one of the following most closely approximates the return value of the function fun1?

a. n3

b. n (logn)2

c. nlogn

d. nlog(logn)

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 29: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 20

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1. Consider the following C program: (GATE 2018)

#include <stdio.h>

void fun1(char *s1, char *s2) {

char *temp;

temp = s1;

s1 = s2;

s2 = temp;

}

void fun2(char **s1, char **s2) {

char *temp;

temp = *s1;

*s1 = *s2;

*s2 = temp;

}

int main() {

char *str1 = "Hi", *str2 = "Bye";

fun1(str1, str2);

printf("%s %s", str1, str2);

fun2(&str1, &str2);

printf("%s %s", str1, str2);

return 0;

}

The output of the program above is

a. Hi Bye Bye Hi

b.Hi Bye Hi Bye

c.Bye Hi Hi Bye

d. Bye Hi Bye Hi

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 30: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 21

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the following C program: (GATE 2018)

#include <stdio.h>

int counter = 0;

int calc(int a, int b) {

int c;

counter++;

if (b == 3)

return (a * a * a);

else {

c = calc(a, b / 3);

return (c * c * c);

}

}

int main() {

calc(4, 81);

printf("%d", counter);

}

The output of this program is ________ .

a. 5

b. 4

c. 3

d. None of these

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 31: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 22

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the C functions foo and bar given below: (GATE 2017)

int foo(int val)

{

int x = 0;

while (val > 0)

{

x = x + foo(val--);

}

return val;

}

int bar(int val)

{

int x = 0;

while (val > 0)

{

x = x + bar(val-1);

}

return val;

}

Invocations of foo(3) and bar(3) will result in:

a. Return of 6 and 6 respectively

b. Infinite loop and abnormal termination respectively

c. Abnormal termination and infinite loop respectively

d. Both terminating abnormally

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 32: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 23

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the following two functions (GATE 2017)

void fun1(int n){

if(n == 0) return;

printf(“%d”, n);

fun2(n-2);

printf(“%d”, n);

}

void fun2(int n){

if(n == 0) return;

printf(“%d”, n);

fun1(++n);

printf(“%d”, n);

}

The output printed when fun1 (5) is called is

a. 53423122233445

b. 53423120112233

c. 53423122132435

d. 53423120213243

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 33: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 24

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1. The following function computes XY for positive integers X and Y. (GATE 2016)

int exp(int X, int Y)

{

int res = 1, a = X, b = Y;

while ( b != 0 )

{

if ( b%2 == 0)

{

a = a*a;

b = b/2;

}

else

{

res = res*a;

b = b-1;

}

}

return res;

}

Which one of the following conditions is TRUE before every iteration of the loop

a. A

b. B

c. C

d. D

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 34: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 25

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1. Consider the following C function. (GATE 2015)

int fun (int n)

{

int x=1, k;

if (n==1) return x;

for (k=1; k<n; ++k)

x = x + fun(k) * fun(n – k);

return x;

}

The return value of fun(5) is __________.

a. 0

b. 26

c. 51

d. 71

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 35: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 26

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

1) Consider the C program below. (GATE 2015)

#include <stdio.h>

int *A, stkTop;

int stkFunc (int opcode, int val)

{

static int size=0, stkTop=0;

switch (opcode)

{

case -1:

size = val;

break;

case 0:

if (stkTop < size ) A[stkTop++]=val;

break;

default:

if (stkTop) return A[--stkTop];

}

return -1;

}

int main()

{

int B[20];

A=B;

stkTop = -1;

stkFunc (-1, 10);

stkFunc (0, 5);

stkFunc (0, 10);

printf ("%dn", stkFunc(1, 0)+ stkFunc(1, 0));

}

The value printed by the above program is ___________

a. 9

b. 10

c. 15

d. 17

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 36: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 27

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

(GATE 2015)

1. Suppose you are provided with the following function declaration in the C programming

language.

int partition (int a[], int n);

The function treats the first element of a[] as a pivot, and rearranges the array so that all

elements less than or equal to the pivot is in the left part of the array, and all elements greater

than the pivot is in the right part. In addition, it moves the pivot so that the pivot is the last

element of the left part. The return value is the number of elements in the left part. The

following partially given function in the C programming language is used to find the kth

smallest element in an array a[ ] of size n using the partition function. We assume k ≤ n

int kth_smallest (int a[], int n, int k)

{

int left_end = partition (a, n);

if (left_end+1==k)

{

return a [left_end];

}

if (left_end+1 > k)

{

return kth_smallest (____________________);

}

else

{

return kth_smallest (____________________);

}

}

The missing argument lists are respectively

a. (a, left_end, k) and (a+left_end+1, n–left_end–1, k–left_end–1)

b. (a, left_end, k) and (a, n–left_end–1, k–left_end–1)

c. (a, left_end+1, N–left_end–1, K–left_end–1) and(a, left_end, k)

d. (a, n–left_end–1, k–left_end–1) and (a, left_end, k)

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 37: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 28

GATE QUESTIONS

PROGRAMMING IN C

FUNCTIONS

Consider the following C function in which size is the number of elements in the array E: The

value returned by the function MyX is the (GATE 2014)

int MyX(int *E, unsigned int size)

{

int Y = 0;

int Z;

int i, j, k;

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

Y = Y + E[i];

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

for(j = i; j < size; j++)

{

Z = 0;

for(k = i; k <= j; k++)

Z = Z + E[k];

if (Z > Y)

Y = Z;

}

return Y;

}

a. maximum possible sum of elements in any sub-array of array E.

b. maximum element in any sub-array of array E.

c.sum of the maximum elements in all possible sub-arrays of array E

d. the sum of all the elements in the array E.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 38: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 29

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) Consider the following C program: (GATE 2018)

#include<stdio.h>

int main(){

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip = arr + 4;

printf("%dn", ip[1]);

return 0;

}

The number that will be displayed on execution of the program is _________ .

A) 6

B) 5

C) 4

D) Segmentation Error

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 39: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 30

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) Consider the following function implemented in C: (GATE 2018)

void printxy(int x, int y)

{

int *ptr;

x = 0;

ptr = &x;

y = *ptr;

*ptr = 1;

printf("%d,%d", x, y);

}

The output of the printxy(1,1) is

a. 0,0

b. 0,1

c. 1,0

d. 1,1

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 40: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 31

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1. Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the

contents of x and y. (GATE 2017)

int main()

{

int array[] = {3, 5, 1, 4, 6, 2};

int done = 0;

int i;

while (done == 0)

{

done = 1;

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

{

if (array[i] < array[i+1])

{

swap(&array[i], &array[i+1]);

done = 0;

}

}

for (i = 5; i >= 1; i--)

{

if (array[i] > array[i-1])

{

swap(&array[i], &array[i-1]);

done = 0;

}

}

}

printf("%d", array[3]);

}

The output of the program is _____. Note: This question appeared as Numerical Answer

Type.

a. 1

b. 2

c. 3

d. 4

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 41: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 32

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1. Consider the following program (GATE 2017)

#include<stdio.h>

#include<string.h>

int main()

{

char * c = "GATECSIT2017";

char *p = c;

printf("%d", (int)strlen(c+2[p]-6[p]-1));

return 0;

}

The Output of the following program is____

Note: This question appeared as Numerical Answer Type.

a. 1

b. 2

c. 4

d. 6

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 42: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 33

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1. Consider the following C code: (GATE 2016)

#include <stdio.h>

int * assignval (int *x, int val)

{

*x = val;

return x;

}

int main()

{

int *x = malloc(sizeof(int));

if (NULL == x) return;

x = assignval(x, 0);

if(x)

{

x = (int*) malloc(sizeof (int));

if (NULL == x) return;

x = assignval (x, 10);

}

printf("%dn", *x);

free(x);

}

The code suffers from which one of the following problems:

a. compiler error as the return of malloc is not typecast appropriately.

b. compiler error because the comparison should be made as x==NULL and

not as shown.

c. compiles successfully but execution may result in dangling pointer.

d. compiles successfully but execution may result in memory leak.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 43: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 34

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) Consider the following C program.(GATE 2016)

void f(int, short);

void main()

{

int i = 100;

short s = 12;

short *p = &s;

__________ ; // call to f()

}

Which one of the following expressions, when placed in the blank above, will NOT result in

a type checking error?

a. f(s, *s)

b. i = f(i,s)

c. f(i,*s)

d. f(i,*p)

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 44: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 35

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) Consider the following C program. (GATE 2016)

#include<stdio.h>

void mystery(int *ptra, int *ptrb)

{

int *temp;

temp = ptrb;

ptrb = ptra;

ptra = temp;

}

int main()

{

int a=2016, b=0, c=4, d=42;

mystery(&a, &b);

if (a < c)

mystery(&c, &a);

mystery(&a, &d);

printf("%dn", a);

}

The output of the program _____________ Note : This question was asked as Numerical

Answer Type.

a. 2016

b.0

c.4

d.8

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 45: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 36

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) The value printed by the following program is (GATE 2016)

void f(int* p, int m)

{

m = m + 5;

*p = *p + m;

return;

}

void main()

{

int i=5, j=10;

f(&i, j);

printf("%d", i+j);

}

a. 10

b. 20

c. 30

d. 40

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 46: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 37

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) Consider the following program: (GATE 2016)

int f(int *p, int n)

{

if (n <= 1) return 0;

else return max(f(p+1,n-1),p[0]-p[1]);

}

int main()

{

int a[] = {3,5,2,6,4};

printf("%d", f(a,5));

}

Run on IDE

Note: max(x,y) returns the maximum of x and y. The value printed by this program is

a. 2

b. 3

c. 4

d. 5

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 47: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 38

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1) The output of the following C program is __________. (GATE 2016)

void f1 (int a, int b)

{

int c;

c=a; a=b; b=c;

}

void f2 (int *a, int *b)

{

int c;

c=*a; *a=*b;*b=c;

}

int main()

{

int a=4, b=5, c=6;

f1(a, b);

f2(&b, &c);

printf (“%d”, c-a-b);

return 0;

}

a. -5

b. -4

c. 5

d. 3

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 48: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 39

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1. Consider the following function written in the C programming language. The output of the

above function on input “ABCD EFGH” is (GATE 2015)

void foo (char *a)

{

if (*a && *a != ` `)

{

foo(a+1);

putchar(*a);

}

}

a. ABCD EFGH

b. ABCD

c. HGFE DCBA

d. DCBA

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 49: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 40

GATE QUESTIONS

PROGRAMMING IN C

POINTERS

1. Consider the following program in C language:(GATE 2014)

#include <stdio.h>

main()

{

int i;

int *pi = &i;

scanf("%d", pi);

printf("%dn", i+5);

}

Which one of the following statements is TRUE?

a. Compilation fails.

b. Execution results in a run-time error.

c. On execution, the value printed is 5 more than the address of variable i.

d. On execution, the value printed is 5 more than the integer value entered.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 50: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 41

GATE QUESTIONS

PROGRAMMING IN C

STRUCTURE

1) What is the output(GATE 2018)

#include <stdio.h>

struct Ournode {

char x, y, z;

};

int main() {

struct Ournode p = {'1', '0', 'a' + 2};

struct Ournode *q = &p;

printf("%c, %c", *((char *)q + 1), *((char *)q + 2));

return 0;

}

The output of this program is:

a. 0, c

b. 0, a+2

c. '0', 'a+2'

d. '0', 'c'

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 51: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 42

GATE QUESTIONS

PROGRAMMING IN C

STRUCTURE

1) Consider the C struct defines below: (GATE 2017)

struct data {

int marks [100] ;

char grade;

int cnumber;

};

struct data student;

The base address of student is available in register R1. The field student.grade can be

accessed efficiently using

a. Post-increment addressing mode. (R1)+

b. Pre-decrement addressing mode, -(R1)

c. Register direct addressing mode, R1

d. Index addressing mode, X(R1), where X is an offset represented in 2’s

complement 16-bit representation.

Date : Hour : Mark : /10

Signature of the Student Mentor : Signature of Subject In – Charge:

Page 52: GATE QUESTION BOOKLET

GATE – PROGRAMMING IN C Page 43

*** ALL THE BEST ***